Skip to content

Commit

Permalink
feat: use LinkType to identify hard links for report.Add
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijie-yang committed Nov 28, 2024
1 parent 4c7313a commit 310216f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
32 changes: 22 additions & 10 deletions internal/fsutil/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ 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
Path string
Mode fs.FileMode
SHA256 string
Size int
Link string
LinkType int
}

// Create creates a filesystem entry according to the provided options and returns
Expand All @@ -45,6 +53,7 @@ func Create(options *CreateOptions) (*Entry, error) {

var err error
var hash string
var linkType int
if o.MakeParents {
if err := os.MkdirAll(filepath.Dir(o.Path), 0755); err != nil {
return nil, err
Expand All @@ -57,6 +66,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
} else {
err = createFile(o)
hash = hex.EncodeToString(rp.h.Sum(nil))
Expand All @@ -65,6 +75,7 @@ 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 All @@ -87,11 +98,12 @@ func Create(options *CreateOptions) (*Entry, error) {
}

entry := &Entry{
Path: o.Path,
Mode: mode,
SHA256: hash,
Size: rp.size,
Link: o.Link,
Path: o.Path,
Mode: mode,
SHA256: hash,
Size: rp.size,
Link: o.Link,
LinkType: linkType,
}
return entry, nil
}
Expand Down
12 changes: 2 additions & 10 deletions internal/manifest/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *Report) Add(slice *setup.Slice, fsEntry *fsutil.Entry) error {
}

hardLinkId := NON_HARD_LINK
if r.entryIsHardLink(fsEntry.Link) {
if fsEntry.LinkType == fsutil.TypeHardLink {
hardLinkId = r.getHardLinkId(fsEntry)
}

Expand Down Expand Up @@ -110,6 +110,7 @@ func (r *Report) getHardLinkId(fsEntry *fsutil.Entry) uint64 {
fsEntry.Link = ""
} else {
// The hard link links to a symlink
fmt.Println("FSENTRY:", fsEntry.Path, fsEntry.Link, "ENTRY:", entry.Path, entry.Link)
fsEntry.Link = entry.Link
}
}
Expand Down Expand Up @@ -151,12 +152,3 @@ func (r *Report) sanitizeAbsPath(path string, isDir bool) (relPath string, err e
}
return relPath, nil
}

// entryIsHardLink determines if a link is a hard link by checking if the link
// has a prefix of the root dir, since hard links are created with absolute
// paths prefixing the report's root dir, while symlinks are created either with
// relative paths or absolute paths that do not have the report's root dir as a
// prefix.
func (r *Report) entryIsHardLink(link string) bool {
return link != "" && strings.HasPrefix(link, r.Root)
}

0 comments on commit 310216f

Please sign in to comment.