-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_linux.go
266 lines (228 loc) · 6.39 KB
/
utils_linux.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"bufio"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"time"
)
// ~/.local/share/Trash/info/
type Info struct {
path string
deletionDate time.Time
}
func parseLine(line string, delimiter string) []string {
pl := strings.Split(line, delimiter)
return pl
}
func decodeLine(pl []string) string {
decodedFilePath, err := url.QueryUnescape(pl[1])
if err != nil {
fmt.Errorf("Failure to decode: %s", err)
return ""
}
return decodedFilePath
}
func printDisplayName(line string, label string) {
fmt.Printf("%s\t: %s\n", label, line)
}
func PrintTrashBoxItems() (ret error) {
user, err := user.Current()
if err != nil {
fmt.Errorf("Failure to get user's home directory: %s", err)
return err
}
// Contents of a trash directory
// https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
trashBase := strings.Replace("~/.local/share/Trash", "~", user.HomeDir, 1)
// Generate fullPath from .~/.local/share/Trash/files/
allFiles, err := ioutil.ReadDir(trashBase + "/files/")
if err != nil {
fmt.Errorf("Failure to get files in ~/.local/share/Trash : %s", err)
return err
}
for _, file := range allFiles {
infoFilePath := trashBase + "/info/" + file.Name() + ".trashinfo"
filesFilePath := trashBase + "/files/" + file.Name()
iFile, err := os.Open(infoFilePath)
if err != nil {
fmt.Printf("Failure to open info file: %s\n", err)
continue
}
defer iFile.Close()
fFile, err := os.Open(filesFilePath)
if err != nil {
fmt.Printf("Failure to open files file: %s\n", err)
continue
}
defer fFile.Close()
var decodedFilePath string
var deletedDate string
// Read one line at a time, as the order of 'Path' and 'DeletionDate' may be different
scanner := bufio.NewScanner(iFile)
for scanner.Scan() {
line := scanner.Text()
if pl := parseLine(line, "Path="); len(pl) > 1 {
decodedFilePath, _ = url.QueryUnescape(pl[1])
} else if pl := parseLine(line, "DeletionDate="); len(pl) > 1 {
deletedDate = pl[1]
} else {
// "[Trash Info]"
continue
}
}
fi, err := fFile.Stat()
if err != nil {
fmt.Printf("Failure to open file: %s\n", err)
}
fmt.Println()
printDisplayName(filepath.Base(decodedFilePath), "FileName")
printDisplayName(decodedFilePath, "Location")
printDisplayName(deletedDate, "DeletedDate")
printDisplayName(strconv.FormatInt(fi.Size(), 10), "Size\t")
}
return nil
}
func convertTrashInfo(i Info) string {
return fmt.Sprintf("[Trash Info]\nPath=%s\nDeletionDate=%s\n", i.path, i.deletionDate.UTC().Format(time.RFC3339))
}
func MoveToTrashBox(path string) (err error) {
filename := filepath.Base(path)
abs, err := filepath.Abs(path)
if err != nil {
return err
}
info := Info{abs, time.Now()}
user, err := user.Current()
if err != nil {
return err
}
trashBase := strings.Replace("~/.local/share/Trash", "~", user.HomeDir, 1)
err = os.WriteFile(trashBase+"/info/"+filename+".trashinfo", []byte(convertTrashInfo(info)), os.ModePerm)
if err != nil {
return err
}
// May not be able to move files or directories between different partitions
// Occur "Invalid cross-device link error"
// https://stackoverflow.com/questions/42392600/oserror-errno-18-invalid-cross-device-link
err = os.Rename(path, trashBase+"/files/"+filename)
if err != nil {
return err
}
return nil
}
type fileInfo struct {
originalPath string
trashboxFilesPath string
trashboxInfoPath string
dateDelete string
size int64
}
func addMatchedFileList(fl []fileInfo, infoFilePath string, filesFilePath string) []fileInfo {
iFile, err := os.Open(infoFilePath)
if err != nil {
fmt.Printf("Failure to open file: %s", err)
return fl
}
defer iFile.Close()
fFile, err := os.Open(filesFilePath)
if err != nil {
fmt.Printf("Failure to open file: %s", err)
return fl
}
defer fFile.Close()
var decodedFilePath string
var deletedDate string
// Read one line at a time, as the order of 'Path' and 'DeletionDate' may be different
scanner := bufio.NewScanner(iFile)
for scanner.Scan() {
line := scanner.Text()
if pl := parseLine(line, "Path="); len(pl) > 1 {
decodedFilePath, _ = url.QueryUnescape(pl[1])
} else if pl := parseLine(line, "DeletionDate="); len(pl) > 1 {
deletedDate = pl[1]
} else {
// "[Trash Info]"
continue
}
}
fs, err := fFile.Stat()
if err != nil {
fmt.Printf("Failure to open file: %s", err)
}
// assign value to fileInfo
var fi fileInfo
fi.originalPath = decodedFilePath
fi.trashboxFilesPath = filesFilePath
fi.trashboxInfoPath = infoFilePath
fi.dateDelete = deletedDate
fi.size = fs.Size()
return append(fl, fi)
}
func unDelete(id uint, fl []fileInfo, outputPath string) (err error) {
if uint(len(fl)) <= id {
return fmt.Errorf("Index out of range")
}
var path string
if len(outputPath) == 0 {
// assign original location to 'path'
path = fl[id].originalPath
} else {
path, _ = filepath.Abs(outputPath)
}
fmt.Printf("Restore %s → %s\n", fl[id].trashboxFilesPath, path)
err = os.Rename(fl[id].trashboxFilesPath, path)
if err != nil {
return err
}
err = os.Remove(fl[id].trashboxInfoPath)
if err != nil {
return err
}
return nil
}
func RestoreItem(filename string, outputPath string) (ret error) {
user, err := user.Current()
if err != nil {
return err
}
// Contents of a trash directory
// https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html
trashBase := strings.Replace("~/.local/share/Trash", "~", user.HomeDir, 1)
// Generate fullPath from .~/.local/share/Trash/files/
allFiles, err := os.ReadDir(trashBase + "/files/")
if err != nil {
return err
}
var fl []fileInfo
for _, file := range allFiles {
if !strings.Contains(file.Name(), filename) {
continue
}
infoFilePath := trashBase + "/info/" + file.Name() + ".trashinfo"
filesFilePath := trashBase + "/files/" + file.Name()
fl = addMatchedFileList(fl, infoFilePath, filesFilePath)
}
var id int
if len(fl) > 1 {
fmt.Println("ID\t DateDeleted\t\t FileSize\t Path")
for i, v := range fl {
fmt.Printf("%d\t %s\t %d\t\t %s\t\n", i, v.dateDelete, v.size, v.originalPath)
}
fmt.Printf("Which one do you want to restore[ID] ? > ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
id, _ = strconv.Atoi(scanner.Text())
unDelete(uint(id), fl, outputPath)
} else if len(fl) == 1 {
unDelete(uint(0), fl, outputPath)
} else {
return fmt.Errorf("No such file or director")
}
return nil
}