From ebccc49d345b0aa468fc6af4f1b334b07b8a2488 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 16 Aug 2024 09:43:07 +0200 Subject: [PATCH] Make forced hard links overwrite target if it has the same name as source Or something like that... Maybe... --- instruction.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/instruction.go b/instruction.go index cf69d61..8fefbf3 100644 --- a/instruction.go +++ b/instruction.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "path" "regexp" "strconv" "strings" @@ -110,6 +111,22 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) { return } + if instruction.Hard { + info, err := os.Stat(instruction.Target) + if err != nil { + status <- fmt.Errorf("could not stat %s%s%s, stopping; err: %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor) + return + } + if info.Mode().IsRegular() && info.Name() == path.Base(instruction.Source) { + log.Printf("Overwriting existing file %s%s%s", TargetColor, instruction.Target, DefaultColor) + err := os.Remove(instruction.Target) + if err != nil { + status <- fmt.Errorf("could not remove existing file %s%s%s; err: %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor) + return + } + } + } + if isSymlink { log.Printf("Removing symlink at %s%s%s", TargetColor, instruction.Target, DefaultColor) err = os.Remove(instruction.Target)