Skip to content

Commit

Permalink
Malicious filenames cause no damage, ensure parents of trashDir exist…
Browse files Browse the repository at this point in the history
…, thank kind reddit user u/skeeto
  • Loading branch information
quackduck committed Feb 14, 2021
1 parent f58a8bf commit b22566c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,9 @@ Options:
-h/--help print this help message
-v/--version print Rem version
```

## Thanks

Thanks to [u/skeeto](https://www.reddit.com/user/skeeto/) for helping me with race conditions and design [here](https://www.reddit.com/r/golang/comments/lixr6k/rem_the_trash_cli_that_makes_it_ridiculously_easy/gn7z86z?utm_source=share&utm_medium=web2x&context=3)


9 changes: 6 additions & 3 deletions rem.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ func parseLogFile() map[string]string {
lines := make(map[string]string)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines[strings.Split(scanner.Text(), logSeparator)[0]] = strings.Split(scanner.Text(), logSeparator)[1]
line := scanner.Text()
lastLogSeparator := strings.LastIndex(line, logSeparator)
from := line[:lastLogSeparator] // up to last logSeparator
pathInTrash := line[lastLogSeparator+1:] // after last logSeparator
lines[from] = pathInTrash
}
if scanner.Err() != nil {
handleErr(err)
Expand Down Expand Up @@ -203,7 +207,6 @@ func trashFile(path string) {
switch i {
case 0:
toMoveTo = trashDir + "/" + filepath.Base(path) + " Deleted at " + time.Now().Format(time.Stamp)
fmt.Println("Much deleting.")
case 1: // seconds are the same
toMoveTo = trashDir + "/" + filepath.Base(path) + " Deleted at " + time.Now().Format(time.StampMilli)
fmt.Println("No way. This is super unlikely. Please contact my creator at [email protected] or on github @quackduck and tell him what you were doing.")
Expand Down Expand Up @@ -256,7 +259,7 @@ func existsInMap(m map[string]string, elem string) bool {
func ensureTrashDir() {
i, _ := os.Stat(trashDir)
if !exists(trashDir) {
err := os.Mkdir(trashDir, os.ModePerm)
err := os.MkdirAll(trashDir, os.ModePerm)
if err != nil {
handleErr(err)
return
Expand Down

0 comments on commit b22566c

Please sign in to comment.