Fix up the filesystem summary logger

This commit is contained in:
2025-11-20 15:37:58 +01:00
parent 610ba20276
commit 88ef93e9af
3 changed files with 48 additions and 27 deletions

View File

@@ -177,19 +177,28 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
return
}
linkKind := opSymlink
if instruction.Hard {
linkKind = opHardlink
}
reportErr := func(err error) {
filesystem.RecordLinkAttempt(linkKind, instruction.Source, instruction.Target, err, filesystem.IsDryRun())
status <- err
}
if !FileExists(instruction.Source) {
status <- fmt.Errorf("instruction source %s does not exist", FormatSourcePath(instruction.Source))
reportErr(fmt.Errorf("instruction source %s does not exist", FormatSourcePath(instruction.Source)))
return
}
if instruction.Files {
info, err := os.Stat(instruction.Source)
if err != nil {
status <- fmt.Errorf("could not stat source %s; err: %v", FormatSourcePath(instruction.Source), err)
reportErr(fmt.Errorf("could not stat source %s; err: %v", FormatSourcePath(instruction.Source), err))
return
}
if info.IsDir() {
status <- fmt.Errorf("source %s is a directory but files=true", FormatSourcePath(instruction.Source))
reportErr(fmt.Errorf("source %s is a directory but files=true", FormatSourcePath(instruction.Source)))
return
}
}
@@ -207,31 +216,31 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
if FileExists(instruction.Target) {
if instruction.Files {
if info, err := os.Stat(instruction.Target); err == nil && info.IsDir() {
status <- fmt.Errorf("target %s is a directory but files=true", FormatTargetPath(instruction.Target))
reportErr(fmt.Errorf("target %s is a directory but files=true", FormatTargetPath(instruction.Target)))
return
}
}
if instruction.Force {
isSymlink, err := IsSymlink(instruction.Target)
if err != nil {
status <- fmt.Errorf("could not determine whether %s is a sym link or not, stopping; err: %v",
FormatTargetPath(instruction.Target), err)
reportErr(fmt.Errorf("could not determine whether %s is a sym link or not, stopping; err: %v",
FormatTargetPath(instruction.Target), err))
return
}
if instruction.Hard {
info, err := os.Stat(instruction.Target)
if err != nil {
status <- fmt.Errorf("could not stat %s, stopping; err: %v",
FormatTargetPath(instruction.Target), err)
reportErr(fmt.Errorf("could not stat %s, stopping; err: %v",
FormatTargetPath(instruction.Target), err))
return
}
if info.Mode().IsRegular() && info.Name() == filepath.Base(instruction.Source) {
LogTarget("Overwriting existing file %s", instruction.Target)
err := filesystem.Remove(instruction.Target)
if err != nil {
status <- fmt.Errorf("could not remove existing file %s; err: %v",
FormatTargetPath(instruction.Target), err)
reportErr(fmt.Errorf("could not remove existing file %s; err: %v",
FormatTargetPath(instruction.Target), err))
return
}
}
@@ -241,27 +250,27 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
LogTarget("Removing symlink at %s", instruction.Target)
err = filesystem.Remove(instruction.Target)
if err != nil {
status <- fmt.Errorf("failed deleting %s due to %v",
FormatTargetPath(instruction.Target), err)
reportErr(fmt.Errorf("failed deleting %s due to %v",
FormatTargetPath(instruction.Target), err))
return
}
} else {
if !instruction.Delete {
status <- fmt.Errorf("refusing to delte actual (non symlink) file %s",
FormatTargetPath(instruction.Target))
reportErr(fmt.Errorf("refusing to delte actual (non symlink) file %s",
FormatTargetPath(instruction.Target)))
return
}
LogImportant("Deleting (!!!) %s", instruction.Target)
err = filesystem.RemoveAll(instruction.Target)
if err != nil {
status <- fmt.Errorf("failed deleting %s due to %v",
FormatTargetPath(instruction.Target), err)
reportErr(fmt.Errorf("failed deleting %s due to %v",
FormatTargetPath(instruction.Target), err))
return
}
}
} else {
status <- fmt.Errorf("target %s exists - handle manually or set the 'forced' flag (3rd field)",
FormatTargetPath(instruction.Target))
reportErr(fmt.Errorf("target %s exists - handle manually or set the 'forced' flag (3rd field)",
FormatTargetPath(instruction.Target)))
return
}
}
@@ -270,8 +279,8 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
err = filesystem.MkdirAll(targetDir, 0755)
if err != nil {
status <- fmt.Errorf("failed creating directory %s due to %v",
FormatTargetPath(targetDir), err)
reportErr(fmt.Errorf("failed creating directory %s due to %v",
FormatTargetPath(targetDir), err))
return
}
}