This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrashinfo_unix.go
75 lines (62 loc) · 1.75 KB
/
trashinfo_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2018 Nikola Trubitsyn. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// +build !windows
package recyclebin
import (
"bufio"
"errors"
"net/url"
"strings"
)
type TrashInfo struct {
TrashInfoMtime int64
Path string
DeletionDate string
}
func readTrashInfo(trashInfoPath string) (TrashInfo, error) {
file, err := fs.Open(trashInfoPath)
if err != nil {
return TrashInfo{}, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Scan()
header := scanner.Text()
scanner.Scan()
pathPair := scanner.Text()
scanner.Scan()
deletionDatePair := scanner.Text()
if string(header) != "[Trash Info]" {
return TrashInfo{}, errors.New(".trashinfo file is not valid")
}
path := strings.Split(string(pathPair), "=")[1]
pathUnescaped, err := url.PathUnescape(path)
if err != nil {
return TrashInfo{}, err
}
deletionDate := strings.Split(string(deletionDatePair), "=")[1]
info, err := fs.Stat(trashInfoPath)
trashInfoMtime := info.ModTime().Unix()
return TrashInfo{trashInfoMtime, pathUnescaped, deletionDate}, nil
}
func writeTrashInfo(trashPath string, filepath string, deletionDate, trashedFilename string) error {
f, err := fs.Create(buildTrashInfoPath(trashPath, trashedFilename))
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString("[Trash Info]\n"); err != nil {
return err
}
if _, err = f.WriteString("Path=" + url.PathEscape(filepath) + "\n"); err != nil {
return err
}
if _, err = f.WriteString("DeletionDate=" + deletionDate + "\n"); err != nil {
return err
}
return nil
}
func buildTrashInfoPath(trashPath string, filename string) string {
return trashPath + "/info/" + filename + ".trashinfo"
}