Skip to content

Commit

Permalink
fix RelativizeLink()
Browse files Browse the repository at this point in the history
  • Loading branch information
rdesaintleger committed Oct 18, 2024
1 parent 85f7c54 commit 009c677
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions pkg/utils/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil }
func RelativizeLink(fs types.FS, path string) error {
// Check if the file is a symbolic link
fileInfo, err := fs.Lstat(path)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return fmt.Errorf("error reading file info: %v", err)
}
Expand All @@ -262,29 +265,31 @@ func RelativizeLink(fs types.FS, path string) error {
// retrieve parent directory
parent := filepath.Dir(path)

// if link target is absolute, relativize it
if filepath.IsAbs(target) {
// if link target is absolute, relativize it
target, err = filepath.Rel(parent, target)
// vfs implementation readlink return raw path for absolute targets
raw, err := fs.RawPath(parent)
if err != nil {
return fmt.Errorf("unable to compute raw parent path: %v", err)
}

target, err = filepath.Rel(raw, target)
if err != nil {
return fmt.Errorf("unable to compute relative path: %v", err)
}
}

// remove symlink (this is also used to avoid infinite recursive calls)
err = fs.Remove(path)

if err != nil {
return fmt.Errorf("unable to remove link: %v", err)
}

// apply relative target against parent directory
absolute := filepath.Join(parent, target)

// recursively relativize target (if applicable)
rerr := RelativizeLink(fs, absolute)

// and create the symlink as relative
// finally create back the symlink as relative
err = fs.Symlink(target, path)

if err != nil {
Expand Down

0 comments on commit 009c677

Please sign in to comment.