Skip to content

Commit

Permalink
chore: update create according to code review
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijie-yang committed Dec 2, 2024
1 parent 27121e0 commit ae03a43
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
18 changes: 5 additions & 13 deletions internal/fsutil/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,19 @@ type CreateOptions struct {
OverrideMode bool
}

// The following constants are used to distinguish different link types,
// such as hard links and symlinks.
const (
TypeSymlink = 1 << iota
TypeHardLink
)

type Entry struct {
Path string
Mode fs.FileMode
SHA256 string
Size int
Link string
LinkType int
HardLink bool
}

// Create creates a filesystem entry according to the provided options and returns
// the information about the created entry.
//
// Create returns errors from the os package.
// Create can return errors from the os package.
func Create(options *CreateOptions) (*Entry, error) {
rp := &readerProxy{inner: options.Data, h: sha256.New()}
// Use the proxy instead of the raw Reader.
Expand All @@ -55,7 +48,7 @@ func Create(options *CreateOptions) (*Entry, error) {

var err error
var hash string
var linkType int
var linkType bool
if o.MakeParents {
if err := os.MkdirAll(filepath.Dir(o.Path), 0755); err != nil {
return nil, err
Expand All @@ -68,7 +61,7 @@ func Create(options *CreateOptions) (*Entry, error) {
// Creating the hard link does not involve reading the file.
// Therefore, its size and hash is not calculated here.
err = createHardLink(o)
linkType = TypeHardLink
linkType = true
} else {
err = createFile(o)
hash = hex.EncodeToString(rp.h.Sum(nil))
Expand All @@ -77,7 +70,6 @@ func Create(options *CreateOptions) (*Entry, error) {
err = createDir(o)
case fs.ModeSymlink:
err = createSymlink(o)
linkType = TypeSymlink
default:
err = fmt.Errorf("unsupported file type: %s", o.Path)
}
Expand Down Expand Up @@ -105,7 +97,7 @@ func Create(options *CreateOptions) (*Entry, error) {
SHA256: hash,
Size: rp.size,
Link: o.Link,
LinkType: linkType,
HardLink: linkType,
}
return entry, nil
}
Expand Down
8 changes: 7 additions & 1 deletion internal/fsutil/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ var createTests = []createTest{{
},
error: `link /[^ ]*/file /[^ ]*/hardlink: file exists`,
}, {
summary: "The mode of a dir can be overridden",
options: fsutil.CreateOptions{
Path: "foo",
Mode: fs.ModeDir | 0775,
Expand All @@ -175,6 +176,7 @@ var createTests = []createTest{{
"/foo/": "dir 0775",
},
}, {
summary: "The mode of a file can be overridden",
options: fsutil.CreateOptions{
Path: "foo",
Mode: 0775,
Expand All @@ -190,6 +192,7 @@ var createTests = []createTest{{
"/foo": "file 0775 85738f8f",
},
}, {
summary: "The mode of a symlink cannot be overridden",
options: fsutil.CreateOptions{
Path: "foo",
Link: "./bar",
Expand All @@ -203,6 +206,7 @@ var createTests = []createTest{{
"/foo": "symlink ./bar",
},
}, {
summary: "The mode of a symlink target is not overridden by the symlink",
options: fsutil.CreateOptions{
Path: "foo",
Link: "./bar",
Expand All @@ -221,6 +225,7 @@ var createTests = []createTest{{
"/bar": "file 0666 3a6eb079",
},
}, {
summary: "The target of an existing symlink can be overridden",
options: fsutil.CreateOptions{
Path: "bar",
// Existing link with different target.
Expand All @@ -235,6 +240,7 @@ var createTests = []createTest{{
"/bar": "symlink other",
},
}, {
summary: "Same symlink can be overwritten",
options: fsutil.CreateOptions{
Path: "bar",
// Existing link with same target.
Expand Down Expand Up @@ -281,7 +287,7 @@ func (s *S) TestCreate(c *C) {

// [fsutil.Create] does not return information about parent directories
// created implicitly. We only check for the requested path.
if entry.LinkType == fsutil.TypeHardLink {
if entry.HardLink {
// We should test hard link entries differently to ensure that it
// produces a hard link indeed.
pathInfo, err := os.Lstat(entry.Path)
Expand Down
2 changes: 1 addition & 1 deletion internal/manifest/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (r *Report) Add(slice *setup.Slice, fsEntry *fsutil.Entry) error {
// Copy the fsEntry.Link to avoid overwriting it when an fsEntry maps to
// multiple slices.
link := fsEntry.Link
if fsEntry.LinkType == fsutil.TypeHardLink {
if fsEntry.HardLink {
relLinkPath, _ := r.sanitizeAbsPath(fsEntry.Link, false)
entry, ok := r.Entries[relLinkPath]
if !ok {
Expand Down
8 changes: 4 additions & 4 deletions internal/manifest/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ var sampleHardLinkReg = fsutil.Entry{
Path: "/base/example-hard-link-reg",
Mode: sampleFile.Mode,
Link: "/base/example-file",
LinkType: fsutil.TypeHardLink,
HardLink: true,
}

var sampleHardLinkSym = fsutil.Entry{
Path: "/base/example-hard-link-sym",
Mode: fs.ModeSymlink | sampleFile.Mode,
Link: "/base/example-link",
LinkType: fsutil.TypeHardLink,
HardLink: true,
}

var sampleFileMutated = fsutil.Entry{
Expand Down Expand Up @@ -350,7 +350,7 @@ var reportTests = []struct {
Path: "/base/another-example-hard-link-reg",
Mode: sampleFile.Mode,
Link: "/base/another-example-file",
LinkType: fsutil.TypeHardLink,
HardLink: true,
},
slice: otherSlice,
}},
Expand Down Expand Up @@ -399,7 +399,7 @@ var reportTests = []struct {
Path: "/base/another-hard-link-reg",
Mode: sampleFile.Mode,
Link: "/base/example-file",
LinkType: fsutil.TypeHardLink,
HardLink: true,
},
slice: otherSlice,
}},
Expand Down
2 changes: 1 addition & 1 deletion internal/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func Run(options *RunOptions) error {
data := pathData{
mutable: mutable,
until: until,
hardLink: entry.LinkType == fsutil.TypeHardLink,
hardLink: entry.HardLink,
}
addKnownPath(knownPaths, relPath, data)
}
Expand Down

0 comments on commit ae03a43

Please sign in to comment.