Skip to content

Commit

Permalink
Unlink existing symbolic and hard links (#145)
Browse files Browse the repository at this point in the history
The function writeNewFile() overwrites the file if it already exists.
Following the same behavior, modified writeNewSymbolicLink() and
writeNewHardLink() to do the same.
  • Loading branch information
jchionh authored and mholt committed Feb 8, 2019
1 parent 2b3c1e8 commit bfece90
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions archiver.go
Expand Up @@ -310,6 +310,15 @@ func writeNewSymbolicLink(fpath string, target string) error {
if err != nil {
return fmt.Errorf("%s: making directory for file: %v", fpath, err)
}

_, err = os.Lstat(fpath)
if err == nil {
err = os.Remove(fpath)
if err != nil {
return fmt.Errorf("%s: failed to unlink: %+v", fpath, err)
}
}

err = os.Symlink(target, fpath)
if err != nil {
return fmt.Errorf("%s: making symbolic link for: %v", fpath, err)
Expand All @@ -322,6 +331,15 @@ func writeNewHardLink(fpath string, target string) error {
if err != nil {
return fmt.Errorf("%s: making directory for file: %v", fpath, err)
}

_, err = os.Lstat(fpath)
if err == nil {
err = os.Remove(fpath)
if err != nil {
return fmt.Errorf("%s: failed to unlink: %+v", fpath, err)
}
}

err = os.Link(target, fpath)
if err != nil {
return fmt.Errorf("%s: making hard link for: %v", fpath, err)
Expand Down

1 comment on commit bfece90

@mholt
Copy link
Owner

@mholt mholt commented on bfece90 Feb 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose so; and I wonder if this should be configurable.

Please sign in to comment.