Skip to content

Commit

Permalink
Consider that files may have the exact same path
Browse files Browse the repository at this point in the history
  • Loading branch information
quackduck committed Feb 11, 2021
1 parent be6a0a5 commit 60c4347
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rem.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ func trashFile(path string) {
return
}
m := parseLogFile()
oldPath := path
i = 1
for ; existsInMap(m, path); i++ { // might be the same path as before
path = oldPath + " " + strconv.Itoa(i)
}
if i == 1 {
fmt.Println("A file of this exact path was deleted earlier. To avoid conflicts, this file will now be called " + color.YellowString(path))
}
m[path] = toMoveTo // logfile format is path where it came from ==> path in trash
setLogFile(m)
fmt.Println("Trashed " + color.YellowString(path) + "\nUndo using " + color.YellowString("rem --undo "+path))
Expand All @@ -237,6 +245,11 @@ func exists(path string) bool {
return !(os.IsNotExist(err))
}

func existsInMap(m map[string]string, elem string) bool {
_, alreadyExists := m[elem]
return alreadyExists
}

func ensureTrashDir() {
i, _ := os.Stat(trashDir)
if !exists(trashDir) {
Expand Down

0 comments on commit 60c4347

Please sign in to comment.