Skip to content

Commit

Permalink
Create all directories before creating a new file (#511)
Browse files Browse the repository at this point in the history
Summary:
From the golang 1.23 os/file.go
```
// Flags to OpenFile wrapping those of the underlying system. Not all
// flags may be implemented on a given system.
const (
	// Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
	O_RDONLY int = syscall.O_RDONLY // open the file read-only.
	O_WRONLY int = syscall.O_WRONLY // open the file write-only.
	O_RDWR   int = syscall.O_RDWR   // open the file read-write.
	// The remaining values may be or'ed in to control behavior.
	O_APPEND int = syscall.O_APPEND // append data to the file when writing.
	O_CREATE int = syscall.O_CREAT  // create a new file if none exists.
	O_EXCL   int = syscall.O_EXCL   // used with O_CREATE, file must not exist.
	O_SYNC   int = syscall.O_SYNC   // open for synchronous I/O.
	O_TRUNC  int = syscall.O_TRUNC  // truncate regular writable file when opened.
)
```

Which makes me think that we might want to call MkdirAll function before creating a file

Pull Request resolved: #511

Resolves #506

Differential Revision: D61658174

fbshipit-source-id: b0352f44335f19773854170df787292676cf5a8e
  • Loading branch information
inesusvet authored and facebook-github-bot committed Aug 27, 2024
1 parent 8934004 commit 854023e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/blocks/createfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package blocks
import (
"fmt"
"os"
"path/filepath"

"github.com/facebookincubator/ttpforge/pkg/fileutils"
"github.com/facebookincubator/ttpforge/pkg/logging"
Expand Down Expand Up @@ -94,6 +95,17 @@ func (s *CreateFileStep) Execute(_ TTPExecutionContext) (*ActResult, error) {
mode = 0666
}

dir := filepath.Dir(pathToCreate)
exists, err = afero.Exists(fsys, dir)
if err != nil {
return nil, err
}
if !exists {
if err = fsys.MkdirAll(dir, os.ModePerm); err != nil {
return nil, err
}
}

// actually write the file
f, err := fsys.OpenFile(pathToCreate, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(mode))
if err != nil {
Expand Down

0 comments on commit 854023e

Please sign in to comment.