Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit bfece90

Browse files
jchionhmholt
authored andcommitted
Unlink existing symbolic and hard links (#145)
The function writeNewFile() overwrites the file if it already exists. Following the same behavior, modified writeNewSymbolicLink() and writeNewHardLink() to do the same.
1 parent 2b3c1e8 commit bfece90

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

archiver.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,15 @@ func writeNewSymbolicLink(fpath string, target string) error {
310310
if err != nil {
311311
return fmt.Errorf("%s: making directory for file: %v", fpath, err)
312312
}
313+
314+
_, err = os.Lstat(fpath)
315+
if err == nil {
316+
err = os.Remove(fpath)
317+
if err != nil {
318+
return fmt.Errorf("%s: failed to unlink: %+v", fpath, err)
319+
}
320+
}
321+
313322
err = os.Symlink(target, fpath)
314323
if err != nil {
315324
return fmt.Errorf("%s: making symbolic link for: %v", fpath, err)
@@ -322,6 +331,15 @@ func writeNewHardLink(fpath string, target string) error {
322331
if err != nil {
323332
return fmt.Errorf("%s: making directory for file: %v", fpath, err)
324333
}
334+
335+
_, err = os.Lstat(fpath)
336+
if err == nil {
337+
err = os.Remove(fpath)
338+
if err != nil {
339+
return fmt.Errorf("%s: failed to unlink: %+v", fpath, err)
340+
}
341+
}
342+
325343
err = os.Link(target, fpath)
326344
if err != nil {
327345
return fmt.Errorf("%s: making hard link for: %v", fpath, err)

0 commit comments

Comments
 (0)