Skip to content

Commit

Permalink
fsutil: Explicit parent directory creation (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
woky authored Aug 29, 2023
1 parent 5e8cdac commit f235273
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
14 changes: 8 additions & 6 deletions internal/deb/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ func extractData(dataReader io.Reader, options *ExtractOptions) error {
// the metadata, since the extracted content itself will also create
// any missing directories unaccounted for in the options.
err := fsutil.Create(&fsutil.CreateOptions{
Path: filepath.Join(options.TargetDir, sourcePath),
Mode: tarHeader.FileInfo().Mode(),
Path: filepath.Join(options.TargetDir, sourcePath),
Mode: tarHeader.FileInfo().Mode(),
MakeParents: true,
})
if err != nil {
return err
Expand Down Expand Up @@ -227,10 +228,11 @@ func extractData(dataReader io.Reader, options *ExtractOptions) error {
tarHeader.Mode = int64(extractInfo.Mode)
}
err := fsutil.Create(&fsutil.CreateOptions{
Path: targetPath,
Mode: tarHeader.FileInfo().Mode(),
Data: pathReader,
Link: tarHeader.Linkname,
Path: targetPath,
Mode: tarHeader.FileInfo().Mode(),
Data: pathReader,
Link: tarHeader.Linkname,
MakeParents: true,
})
if err != nil {
return err
Expand Down
23 changes: 10 additions & 13 deletions internal/fsutil/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ type CreateOptions struct {
Mode fs.FileMode
Data io.Reader
Link string
// If MakeParents is true, missing parent directories of Path are
// created with permissions 0755.
MakeParents bool
}

// Creates a filesystem entry according to the provided options.
func Create(o *CreateOptions) error {
var err error
if o.MakeParents {
if err := os.MkdirAll(filepath.Dir(o.Path), 0755); err != nil {
return err
}
}
switch o.Mode & fs.ModeType {
case 0:
err = createFile(o)
Expand All @@ -32,11 +41,7 @@ func Create(o *CreateOptions) error {

func createDir(o *CreateOptions) error {
debugf("Creating directory: %s (mode %#o)", o.Path, o.Mode)
err := os.MkdirAll(filepath.Dir(o.Path), 0755)
if err != nil {
return err
}
err = os.Mkdir(o.Path, o.Mode)
err := os.Mkdir(o.Path, o.Mode)
if os.IsExist(err) {
err = os.Chmod(o.Path, o.Mode)
}
Expand All @@ -45,10 +50,6 @@ func createDir(o *CreateOptions) error {

func createFile(o *CreateOptions) error {
debugf("Writing file: %s (mode %#o)", o.Path, o.Mode)
err := os.MkdirAll(filepath.Dir(o.Path), 0755)
if err != nil && !os.IsExist(err) {
return err
}
file, err := os.OpenFile(o.Path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, o.Mode)
if err != nil {
return err
Expand All @@ -63,10 +64,6 @@ func createFile(o *CreateOptions) error {

func createSymlink(o *CreateOptions) error {
debugf("Creating symlink: %s => %s", o.Path, o.Link)
err := os.MkdirAll(filepath.Dir(o.Path), 0755)
if err != nil && !os.IsExist(err) {
return err
}
fileinfo, err := os.Lstat(o.Path)
if err == nil {
if (fileinfo.Mode() & os.ModeSymlink) != 0 {
Expand Down
25 changes: 17 additions & 8 deletions internal/fsutil/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,31 @@ type createTest struct {

var createTests = []createTest{{
options: fsutil.CreateOptions{
Path: "foo/bar",
Data: bytes.NewBufferString("data1"),
Mode: 0444,
Path: "foo/bar",
Data: bytes.NewBufferString("data1"),
Mode: 0444,
MakeParents: true,
},
result: map[string]string{
"/foo/": "dir 0755",
"/foo/bar": "file 0444 5b41362b",
},
}, {
options: fsutil.CreateOptions{
Path: "foo/bar",
Link: "../baz",
Mode: fs.ModeSymlink,
Path: "foo/bar",
Link: "../baz",
Mode: fs.ModeSymlink,
MakeParents: true,
},
result: map[string]string{
"/foo/": "dir 0755",
"/foo/bar": "symlink ../baz",
},
}, {
options: fsutil.CreateOptions{
Path: "foo/bar",
Mode: fs.ModeDir | 0444,
Path: "foo/bar",
Mode: fs.ModeDir | 0444,
MakeParents: true,
},
result: map[string]string{
"/foo/": "dir 0755",
Expand All @@ -55,6 +58,12 @@ var createTests = []createTest{{
result: map[string]string{
"/tmp/": "dir 01775",
},
}, {
options: fsutil.CreateOptions{
Path: "foo/bar",
Mode: fs.ModeDir | 0775,
},
error: `.*: no such file or directory`,
}}

func (s *S) TestCreate(c *C) {
Expand Down
9 changes: 5 additions & 4 deletions internal/setup/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ func extractTar(dataReader io.Reader, targetDir string) error {
//debugf("Extracting header: %#v", tarHeader)

err = fsutil.Create(&fsutil.CreateOptions{
Path: filepath.Join(targetDir, sourcePath),
Mode: tarHeader.FileInfo().Mode(),
Data: tarReader,
Link: tarHeader.Linkname,
Path: filepath.Join(targetDir, sourcePath),
Mode: tarHeader.FileInfo().Mode(),
Data: tarReader,
Link: tarHeader.Linkname,
MakeParents: true,
})
if err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions internal/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,11 @@ func Run(options *RunOptions) error {
}

err := fsutil.Create(&fsutil.CreateOptions{
Path: targetPath,
Mode: tarHeader.FileInfo().Mode(),
Data: fileContent,
Link: linkTarget,
Path: targetPath,
Mode: tarHeader.FileInfo().Mode(),
Data: fileContent,
Link: linkTarget,
MakeParents: true,
})
if err != nil {
return err
Expand Down

0 comments on commit f235273

Please sign in to comment.