-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_windows.go
688 lines (598 loc) · 18 KB
/
utils_windows.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
package main
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"unicode/utf16"
"unsafe"
"golang.org/x/sys/windows"
)
// https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shfileoperationw
var (
modshell32 = syscall.NewLazyDLL("Shell32.dll")
procSHFileOperationW = modshell32.NewProc("SHFileOperationW")
procSHGetDesktopFolder = modshell32.NewProc("SHGetDesktopFolder")
procSHGetSpecialFolderLocation = modshell32.NewProc("SHGetSpecialFolderLocation")
procSHGetDisplayName = modshell32.NewProc("SHGetDisplayName")
procSHGetDataFromIDList = modshell32.NewProc("SHGetDataFromIDListW")
modole32 = syscall.NewLazyDLL("ole32.dll")
procCoInitialize = modole32.NewProc("CoInitialize")
procCoUninitialize = modole32.NewProc("CoUninitialize")
procCoTaskMemFree = modole32.NewProc("CoTaskMemFree")
moduser32 = syscall.NewLazyDLL("User32.dll")
procCreatePopupMenu = moduser32.NewProc("CreatePopupMenu")
procGetMenuItemCount = moduser32.NewProc("GetMenuItemCount")
)
// https://pinvoke.net/default.aspx/Enums/FileFuncFlags.html
const (
FO_MOVE = 0x1
FO_COPY = 0x2
FO_DELETE = 0x3
FO_RENAME = 0x4
)
// https://learn.microsoft.com/ja-jp/windows/win32/api/winuser/nf-winuser-showwindow
const (
SW_HIDE = 0x00
SW_SHOWNORMAL = 0x01
SW_NORMAL = 0x01
SW_SHOWMINIMIZED = 0x02
SW_SHOWMAXIMIZED = 0x03
SW_MAXIMIZE = 0x03
SW_SHOWNOACTIVATE = 0x04
SW_SHOW = 0x05
SW_MINIMIZE = 0x06
SW_SHOWMINNOACTIVE = 0x07
SW_SHOWNA = 0x08
SW_RESTORE = 0x09
SW_SHOWDEFAULT = 0x10
SW_FORCEMINIMIZE = 0x11
SW_MAX = 0x11
)
// https://www.pinvoke.net/default.aspx/Enums/FILEOP_FLAGS.html
// https://groups.google.com/g/microsoft.public.vb.winapi/c/htqEx2zQjGo
const (
FOF_MULTIDESTFILES = 0x1
FOF_CONFIRMMOUSE = 0x2
FOF_SILENT = 0x4
FOF_RENAMEONCOLLISION = 0x8
FOF_NOCONFIRMATION = 0x10
FOF_WANTMAPPINGHANDLE = 0x20
FOF_ALLOWUNDO = 0x40
FOF_FILESONLY = 0x80
FOF_SIMPLEPROGRESS = 0x100
FOF_NOCONFIRMMKDIR = 0x200
FOF_NOERRORUI = 0x400
FOF_NOCOPYSECURITYATTRIBS = 0x800
FOF_NORECURSION = 0x1000
FOF_NO_CONNECTED_ELEMENTS = 0x2000
FOF_WANTNUKEWARNING = 0x4000
FOF_NORECURSEREPARSE = 0x8000
FOF_NO_UI = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR
)
const (
FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100
FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000
FORMAT_MESSAGE_FROM_HMODULE = 0x00000800
FORMAT_MESSAGE_FROM_STRING = 0x00000400
FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
)
// https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shfileopstructa
type SHFILEOPSTRUCT struct {
Hwnd uintptr
Func uint32
From *uint16
To *uint16
Flags uint16
AnyOperationsAborted int32
NameMappings *byte
ProgressTitle *uint16
}
type STRRET struct {
UType uint32
STRRET_Anonymous
}
type STRRET_Anonymous struct {
Data [32]uint64
}
type FILE_ATTRIBUTE uint32
const (
FILE_ATTRIBUTE_INVALID FILE_ATTRIBUTE = 0xffff_ffff // -1
FILE_ATTRIBUTE_READONLY FILE_ATTRIBUTE = 0x0000_0001
FILE_ATTRIBUTE_HIDDEN FILE_ATTRIBUTE = 0x0000_0002
FILE_ATTRIBUTE_SYSTEM FILE_ATTRIBUTE = 0x0000_0004
FILE_ATTRIBUTE_DIRECTORY FILE_ATTRIBUTE = 0x0000_0010
FILE_ATTRIBUTE_ARCHIVE FILE_ATTRIBUTE = 0x0000_0020
FILE_ATTRIBUTE_DEVICE FILE_ATTRIBUTE = 0x0000_0040
FILE_ATTRIBUTE_NORMAL FILE_ATTRIBUTE = 0x0000_0080
FILE_ATTRIBUTE_TEMPORARY FILE_ATTRIBUTE = 0x0000_0100
FILE_ATTRIBUTE_SPARSE_FILE FILE_ATTRIBUTE = 0x0000_0200
FILE_ATTRIBUTE_REPARSE_POINT FILE_ATTRIBUTE = 0x0000_0400
FILE_ATTRIBUTE_COMPRESSED FILE_ATTRIBUTE = 0x0000_0800
FILE_ATTRIBUTE_OFFLINE FILE_ATTRIBUTE = 0x0000_1000
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED FILE_ATTRIBUTE = 0x0000_2000
FILE_ATTRIBUTE_ENCRYPTED FILE_ATTRIBUTE = 0x0000_4000
FILE_ATTRIBUTE_INTEGRITY_STREAM FILE_ATTRIBUTE = 0x0000_8000
FILE_ATTRIBUTE_VIRTUAL FILE_ATTRIBUTE = 0x0001_0000
FILE_ATTRIBUTE_NO_SCRUB_DATA FILE_ATTRIBUTE = 0x0002_0000
FILE_ATTRIBUTE_EA FILE_ATTRIBUTE = 0x0004_0000
FILE_ATTRIBUTE_PINNED FILE_ATTRIBUTE = 0x0008_0000
FILE_ATTRIBUTE_UNPINNED FILE_ATTRIBUTE = 0x0010_0000
FILE_ATTRIBUTE_RECALL_ON_OPEN FILE_ATTRIBUTE = 0x0004_0000
FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS FILE_ATTRIBUTE = 0x0040_0000
)
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
type FILETIME struct {
dwLowDateTime uint32
dwHighDateTime uint32
}
// https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-win32_find_dataw
type WIN32_FIND_DATA struct {
DwFileAttributes FILE_ATTRIBUTE
FtCreationTime FILETIME
FtLastAccessTime FILETIME
FtLastWriteTime FILETIME
NFileSizeHigh uint32
NFileSizeLow uint32
dwReserved0 uint32
dwReserved1 uint32
cFileName [260]uint16 // MAX_PATH
cCAlternateFileName [14]uint16
DwFileType uint32
DwCreatorType uint32
WFinderFlags uint16
}
const (
CSIDL_BITBUCKET = 0xa
SHGDN_NORMAL = 0x0000
SHGDN_INFOLDER = 0x1
SHGDN_FOREDITING = 0x1000
SHGDN_FORADDRESSBAR = 0x4000
SHGDN_FORPARSING = 0x8000
)
type IShellFolderVtbl struct {
QueryInterface uintptr
AddRef uintptr
Release uintptr
ParseDisplayName uintptr
EnumObjects uintptr
BindToObject uintptr
BindToStorage uintptr
CompareIDs uintptr
CreateViewObject uintptr
GetAttributesOf uintptr
GetUIObjectOf uintptr
GetDisplayNameOf uintptr
SetNameOf uintptr
}
type IShellFolder struct {
lpVtbl *IShellFolderVtbl
}
type IEnumIDListVtbl struct {
QueryInterface uintptr
AddRef uintptr
Release uintptr
Next uintptr
Skip uintptr
Reset uintptr
Clone uintptr
}
type IEnumIDList struct {
lpVtbl *IEnumIDListVtbl
}
const (
SHCONTF_FOLDERS = 0x0020
SHCONTF_NONFOLDERS = 0x0040
)
type IID_IShellFolder struct {
Data1 uint32
Data2 uint16
Data3 uint16
Data4 [8]byte
}
// https://docs.microsoft.com/en-us/windows/win32/api/shtypes/ns-shtypes-itemidlist
type ITEMIDLIST struct {
ID SHITEMID
}
// https://docs.microsoft.com/en-us/windows/win32/api/shtypes/ns-shtypes-shitemid
type SHITEMID struct {
CB uint16
ABID [1]byte
}
func CoTaskMemFree(pv uintptr) {
modole32.NewProc("CoTaskMemFree").Call(pv)
}
func (this *STRRET) pOleStr() **uint16 {
return (**uint16)(unsafe.Pointer(&this.Data[0]))
}
func (this *STRRET) uOffset() *uint32 {
return (*uint32)(unsafe.Pointer(&this.Data[0]))
}
func (this *STRRET) cStr() *[260]byte {
return (*[260]byte)(unsafe.Pointer(&this.Data[0]))
}
func (v *IShellFolder) Release() int32 {
ret, _, _ := syscall.Syscall(
v.lpVtbl.Release,
1,
uintptr(unsafe.Pointer(v)),
0,
0)
return int32(ret)
}
func (v *IEnumIDList) Release() int32 {
ret, _, _ := syscall.Syscall(
v.lpVtbl.Release,
1,
uintptr(unsafe.Pointer(v)),
0,
0)
return int32(ret)
}
func (v *IShellFolder) BindToObject(pidl uintptr, pbc uintptr, riid *syscall.GUID, ppv **IShellFolder) uintptr {
ret, _, _ := syscall.Syscall6(
v.lpVtbl.BindToObject,
5,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(pidl)),
uintptr(pbc),
uintptr(unsafe.Pointer(riid)),
uintptr(unsafe.Pointer(ppv)),
0)
return ret
}
// https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellfolder-enumobjects
func (v *IShellFolder) EnumObjects(hwnd uintptr, grfFlags int, ppenumIDlist **IEnumIDList) uintptr {
ret, _, _ := syscall.Syscall6(
v.lpVtbl.EnumObjects,
4,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(hwnd)),
uintptr(grfFlags),
uintptr(unsafe.Pointer(ppenumIDlist)),
0,
0)
return ret
}
func (v *IEnumIDList) Next(celt int, rgelt **ITEMIDLIST, pceltFetched *uint32) uintptr {
ret, _, _ := syscall.Syscall6(
v.lpVtbl.Next,
4,
uintptr(unsafe.Pointer(v)),
uintptr(celt),
uintptr(unsafe.Pointer(rgelt)),
uintptr(unsafe.Pointer(pceltFetched)),
0,
0)
return ret
}
func (v *IShellFolder) GetDisplayNameOf(pidl *ITEMIDLIST, uFlags uint32, pName *STRRET) uintptr {
ret, _, _ := syscall.Syscall6(
v.lpVtbl.GetDisplayNameOf,
4,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(pidl)),
uintptr(uFlags),
uintptr(unsafe.Pointer(pName)),
0,
0)
return ret
}
func _SHGetSpecialFolderLocation(
hwnd uintptr,
csidl int,
ppidl uintptr,
) (r1 uintptr, err error) {
r1, _, err = procSHGetSpecialFolderLocation.Call(hwnd, uintptr(csidl), ppidl)
return
}
func _SHGetDesktopFolder(
ppshf uintptr,
) (r1 uintptr, err error) {
r1, _, err = procSHGetDesktopFolder.Call(ppshf)
return
}
func _CoTaskMemFree(
pv uintptr,
) (r1 uintptr, err error) {
r1, _, err = procCoTaskMemFree.Call(pv)
return
}
func _CoUninitialize() (r1 uintptr, err error) {
r1, _, err = procCoUninitialize.Call()
return
}
func _CoInitialize(
pvReserved uintptr,
) (r1 uintptr, err error) {
r1, _, err = procCoInitialize.Call(pvReserved)
return
}
// https://stackoverflow.com/questions/39961171/golang-winapi-call-with-struct-parameter
// utf16PtrToString is like UTF16ToString, but takes *uint16
// as a parameter instead of []uint16.
// max is how many times p can be advanced looking for the null terminator.
// If max is hit, the string is truncated at that point.
func utf16PtrToString(p *uint16, max int) string {
if p == nil {
return ""
}
// Find NUL terminator.
end := unsafe.Pointer(p)
n := 0
for *(*uint16)(end) != 0 && n < max {
end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p))
n++
}
s := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(p))[:n:n]
return string(utf16.Decode(s))
}
func CStringToString(cs *uint16) (s string) {
if cs != nil {
us := make([]uint16, 0, 256)
for p := uintptr(unsafe.Pointer(cs)); ; p += 2 {
u := *(*uint16)(unsafe.Pointer(p))
if u == 0 {
return string(utf16.Decode(us))
}
us = append(us, u)
}
}
return ""
}
func getDateDelete(rbInternalFormat []byte) time.Time {
var dd int64
binary.Read(bytes.NewReader(rbInternalFormat[16:]), binary.LittleEndian, &dd)
// Convert NT time to Unix epoch
// Unix: 1/1/1970 00:00, Windows NT: 1/1/1601 00:00
return time.Unix((dd/10000000)-11644473600, (dd%10000000)*100)
}
func getFileSize(rbInternalFormat []byte) int64 {
var fsize int64
binary.Read(bytes.NewReader(rbInternalFormat[8:16]), binary.LittleEndian, &fsize)
return fsize
}
func PrintDisplayName(psf *IShellFolder, pidl *ITEMIDLIST, uFlags uint32, label string) {
var pName STRRET
ret := psf.GetDisplayNameOf(pidl, uFlags, &pName)
if ret != 0 {
fmt.Println("Failed to get item name.")
return
}
if strings.Contains(label, "DateDelete") || strings.Contains(label, "Size") {
recycleDir := filepath.Dir(CStringToString(*pName.pOleStr()))
ipath := strings.Replace(filepath.Base(CStringToString(*pName.pOleStr())), "$R", "$I", 1)
// Version 2 (Introduced somewhere in a Windows 10 release)
// https://github.com/danielmarschall/recyclebinunit/blob/master/FORMAT.md#version-2-introduced-somewhere-in-a-windows-10-release
buf := make([]byte, 24)
f, _ := os.Open(recycleDir + "\\" + ipath)
f.Read(buf)
if strings.Contains(label, "DateDelete") {
fmt.Printf("%s\t: %v\n", label, getDateDelete(buf).Local())
} else if strings.Contains(label, "Size") {
fmt.Printf("%s\t: %v\n", label, getFileSize(buf))
}
} else {
fmt.Printf("%s\t: %v\n", label, CStringToString(*pName.pOleStr()))
}
}
func GetRecycleBinShellFolder(pRecycleBinFolder **IShellFolder) (ret uintptr, err error) {
var pDesktopFolder *IShellFolder
ret, err = _SHGetDesktopFolder(uintptr(unsafe.Pointer(&pDesktopFolder)))
if ret != 0 {
return ret, err
}
defer pDesktopFolder.Release()
var pRecycleBinIDL uintptr
ret, err = _SHGetSpecialFolderLocation(uintptr(0), CSIDL_BITBUCKET, uintptr(unsafe.Pointer(&pRecycleBinIDL)))
if ret != 0 {
return ret, err
}
var IID_IShellFolder = syscall.GUID{0x000214E6, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
ret = pDesktopFolder.BindToObject(pRecycleBinIDL, uintptr(0), &IID_IShellFolder, &(*pRecycleBinFolder))
return ret, err
}
func PrintTrashBoxItems() error {
ret, _ := _CoInitialize(uintptr(0))
if ret != 0 {
// Call FormatMessage API to display correct errors.
return _FormatMessage(ret)
}
var pRecycleBinFolder *IShellFolder
ret, _ = GetRecycleBinShellFolder(&pRecycleBinFolder)
if ret != 0 {
return _FormatMessage(ret)
}
defer pRecycleBinFolder.Release()
var pEnum *IEnumIDList
ret = pRecycleBinFolder.EnumObjects(0, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS, &pEnum)
if ret != 0 {
fmt.Println("Failed to enumerate Recycle Bin items.")
return _FormatMessage(ret)
}
defer pEnum.Release()
var pItemIDL *ITEMIDLIST
for {
ret = pEnum.Next(1, &pItemIDL, nil)
if ret != 0 {
break
}
fmt.Println()
PrintDisplayName(pRecycleBinFolder, pItemIDL, SHGDN_INFOLDER, "InFolder")
PrintDisplayName(pRecycleBinFolder, pItemIDL, SHGDN_NORMAL, "Normal\t")
PrintDisplayName(pRecycleBinFolder, pItemIDL, SHGDN_FORPARSING, "ForParsing")
PrintDisplayName(pRecycleBinFolder, pItemIDL, SHGDN_FORPARSING, "DateDeleted")
PrintDisplayName(pRecycleBinFolder, pItemIDL, SHGDN_FORPARSING, "Size\t")
CoTaskMemFree(uintptr(unsafe.Pointer(pItemIDL)))
}
_CoUninitialize()
return nil
}
func unDelete(id uint, fl []fileInfo, outputPath string) 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].path
} else {
path, _ = filepath.Abs(outputPath)
}
fmt.Printf("Restore %s → %s\n", fl[id].path, path)
r := os.Rename(fl[id].forParsing, path)
if r != nil {
return r
}
recycleDir := filepath.Dir(fl[id].forParsing)
ipath := strings.Replace(filepath.Base(fl[id].forParsing), "$R", "$I", 1)
os.Remove(recycleDir + "\\" + ipath)
return nil
}
func isMatchFilename(psf *IShellFolder, pidl *ITEMIDLIST, file string) bool {
var pName STRRET
ret := psf.GetDisplayNameOf(pidl, SHGDN_NORMAL, &pName)
if ret != 0 {
fmt.Println("Failed to get item name.")
return false
}
if strings.Contains(filepath.Base(CStringToString(*pName.pOleStr())), file) {
return true
}
return false
}
type fileInfo struct {
path string
forParsing string
dateDelete time.Time
size int64
}
func addMatchedFileList(fl []fileInfo, psf *IShellFolder, pidl *ITEMIDLIST) []fileInfo {
var fi fileInfo
var pName STRRET
// For path
ret := psf.GetDisplayNameOf(pidl, SHGDN_NORMAL, &pName)
if ret != 0 {
fmt.Println("Failed to get item name.")
}
normalPath := CStringToString(*pName.pOleStr())
// For dateDelete, size
ret = psf.GetDisplayNameOf(pidl, SHGDN_FORPARSING, &pName)
if ret != 0 {
fmt.Println("Failed to get item name.")
}
parsingPath := CStringToString(*pName.pOleStr())
recycleDir := filepath.Dir(CStringToString(*pName.pOleStr()))
ipath := strings.Replace(filepath.Base(CStringToString(*pName.pOleStr())), "$R", "$I", 1)
buf := make([]byte, 24)
f, _ := os.Open(recycleDir + "\\" + ipath)
f.Read(buf)
// assign value to fileInfo
fi.path = normalPath
fi.forParsing = parsingPath
fi.dateDelete = getDateDelete(buf).Local()
fi.size = getFileSize(buf)
return append(fl, fi)
}
func RestoreItem(file string, outputPath string) error {
ret, _ := _CoInitialize(uintptr(0))
if ret != 0 {
// Call FormatMessage API to display correct errors.
return _FormatMessage(ret)
}
var pRecycleBinFolder *IShellFolder
ret, _ = GetRecycleBinShellFolder(&pRecycleBinFolder)
if ret != 0 {
return _FormatMessage(ret)
}
defer pRecycleBinFolder.Release()
var pEnum *IEnumIDList
ret = pRecycleBinFolder.EnumObjects(0, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS, &pEnum)
if ret != 0 {
fmt.Println("Failed to enumerate Recycle Bin items.")
return _FormatMessage(ret)
}
defer pEnum.Release()
var pItemIDL *ITEMIDLIST
var fl []fileInfo
for {
ret = pEnum.Next(1, &pItemIDL, nil)
if ret != 0 {
break
}
var pName STRRET
ret := pRecycleBinFolder.GetDisplayNameOf(pItemIDL, SHGDN_NORMAL, &pName)
if ret != 0 {
fmt.Println("Failed to get item name.")
return _FormatMessage(ret)
}
if isMatchFilename(pRecycleBinFolder, pItemIDL, file) {
fl = addMatchedFileList(fl, pRecycleBinFolder, pItemIDL)
// unDelete(pRecycleBinFolder, pItemIDL, SHGDN_FORPARSING, path)
}
CoTaskMemFree(uintptr(unsafe.Pointer(pItemIDL)))
}
_CoUninitialize()
var id int
if len(fl) > 1 {
fmt.Println("ID\t DateDeleted\t\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.path)
}
fmt.Printf("Which one do you want to restore[ID] ? > ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
id, _ = strconv.Atoi(scanner.Text())
} else if len(fl) == 1 {
id = 0
} else {
return fmt.Errorf("No such file or directorye")
}
r := unDelete(uint(id), fl, outputPath)
if r != nil {
return r
}
return nil
}
func _FormatMessage(errno uintptr) (err error) {
buf := make([]uint16, 0xff)
windows.FormatMessage(
windows.FORMAT_MESSAGE_FROM_SYSTEM,
uintptr(0),
uint32(errno),
0,
buf,
nil,
)
return errors.New(windows.UTF16ToString(buf))
}
func _SHFileOperation(
shFileOp *SHFILEOPSTRUCT,
) (r1 uintptr, err error) {
r1, _, err = procSHFileOperationW.Call(
uintptr(unsafe.Pointer(shFileOp)),
)
return
}
func MoveToTrashBox(path string) (err error) {
var fileOp SHFILEOPSTRUCT
fileOp.Hwnd = uintptr(0)
fileOp.Func = FO_DELETE
fileOp.From = windows.StringToUTF16Ptr(path)
fileOp.Flags = FOF_SILENT | FOF_ALLOWUNDO | FOF_NOCONFIRMATION
// Return error is always "The operation completed successfully."
ret, _ := _SHFileOperation(&fileOp)
if ret != 0 {
// Call FormatMessage API to display correct errors.
return _FormatMessage(ret)
}
return nil
}