forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgo.go
796 lines (731 loc) · 21.9 KB
/
cgo.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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
// Copyright 2015-2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// +build windows
package dokan
/*
#include "bridge.h"
*/
import "C"
import (
"errors"
"fmt"
"io"
"reflect"
"runtime"
"syscall"
"time"
"unicode/utf16"
"unsafe"
"github.com/keybase/kbfs/dokan/winacl"
"golang.org/x/net/context"
"golang.org/x/sys/windows"
)
// Wrap SID for users.
type SID syscall.SID
const (
kbfsLibdokanDebug = MountFlag(C.kbfsLibdokanDebug)
kbfsLibdokanStderr = MountFlag(C.kbfsLibdokanStderr)
kbfsLibdokanRemovable = MountFlag(C.kbfsLibdokanRemovable)
kbfsLibdokanMountManager = MountFlag(C.kbfsLibdokanMountManager)
kbfsLibdokanCurrentSession = MountFlag(C.kbfsLibdokanCurrentSession)
kbfsLibdokanUseFindFilesWithPattern = MountFlag(C.kbfsLibdokanUseFindFilesWithPattern)
)
// loadDokanDLL can be called to init the system with custom Dokan location,
// e.g. LoadDokanDLL(`C:\mypath\dokan1.dll`).
func loadDokanDLL(fullpath string) error {
if fullpath == "" {
return nil
}
dw := syscall.Errno(C.kbfsLibdokanLoadLibrary((*C.WCHAR)(stringToUtf16Ptr(fullpath))))
if dw != 0 {
return dw
}
return nil
}
const ntstatusOk = C.NTSTATUS(0)
func checkFileDirectoryFile(err error, isDir bool, createOptions uint32) {
if createOptions&createOptions&FileDirectoryFile != 0 && createOptions&FileNonDirectoryFile != 0 {
debugf("checkFileDirectoryFile both FileDirectoryFile FileNonDirectoryFile set")
}
if err == nil {
if (!isDir && createOptions&FileDirectoryFile != 0) ||
(isDir && createOptions&FileNonDirectoryFile != 0) {
debugf("checkFileDirectoryFile INCONSISTENCY %v %08X", isDir, createOptions)
}
} else if err == ErrNotADirectory {
if createOptions&FileDirectoryFile == 0 {
debugf("checkFileDirectoryFile ErrNotADirectory but no createOptions&FileDirectoryFile")
}
} else if err == ErrFileIsADirectory {
if createOptions&FileNonDirectoryFile == 0 {
debugf("checkFileDirectoryFile ErrFileIsADirectory but no createOptions&FileNonDirectoryFile")
}
}
}
//export kbfsLibdokanCreateFile
func kbfsLibdokanCreateFile(
fname C.LPCWSTR,
psec C.PDOKAN_IO_SECURITY_CONTEXT,
DesiredAccess C.ACCESS_MASK,
FileAttributes C.ULONG,
ShareAccess C.ULONG,
cCreateDisposition C.ULONG,
CreateOptions C.ULONG,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
var cd = CreateData{
DesiredAccess: uint32(DesiredAccess),
FileAttributes: FileAttribute(FileAttributes),
ShareAccess: uint32(ShareAccess),
CreateDisposition: CreateDisposition(cCreateDisposition),
CreateOptions: uint32(CreateOptions),
}
debugf("CreateFile '%v' %#v pid: %v\n",
d16{fname}, cd, pfi.ProcessId)
fs := getfs(pfi)
ctx, cancel := fs.WithContext(globalContext())
if cancel != nil {
defer cancel()
}
fi, isDir, err := fs.CreateFile(ctx, makeFI(fname, pfi), &cd)
if isDebug {
checkFileDirectoryFile(err, isDir, uint32(CreateOptions))
}
if isDir {
pfi.IsDirectory = 1
}
return fiStore(pfi, fi, err)
}
func globalContext() context.Context {
return context.Background()
}
func getContext(pfi C.PDOKAN_FILE_INFO) (context.Context, context.CancelFunc) {
return getfs(pfi).WithContext(globalContext())
}
//export kbfsLibdokanCleanup
func kbfsLibdokanCleanup(fname C.LPCWSTR, pfi C.PDOKAN_FILE_INFO) {
debugf("Cleanup '%v' %v\n", d16{fname}, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
getfi(pfi).Cleanup(ctx, makeFI(fname, pfi))
}
//export kbfsLibdokanCloseFile
func kbfsLibdokanCloseFile(fname C.LPCWSTR, pfi C.PDOKAN_FILE_INFO) {
debugf("CloseFile '%v' %v\n", d16{fname}, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
getfi(pfi).CloseFile(ctx, makeFI(fname, pfi))
fiTableFreeFile(uint32(pfi.DokanOptions.GlobalContext), uint32(pfi.Context))
pfi.Context = 0
}
//export kbfsLibdokanReadFile
func kbfsLibdokanReadFile(
fname C.LPCWSTR,
Buffer C.LPVOID,
NumberOfBytesToRead C.DWORD,
NumberOfBytesRead C.LPDWORD,
Offset C.LONGLONG,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("ReadFile '%v' %d bytes @ %d %v", d16{fname}, NumberOfBytesToRead, Offset, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
n, err := getfi(pfi).ReadFile(
ctx,
makeFI(fname, pfi),
bufToSlice(unsafe.Pointer(Buffer), uint32(NumberOfBytesToRead)),
int64(Offset))
*NumberOfBytesRead = C.DWORD(n)
// EOF is success with Windows...
if err == io.EOF {
err = nil
}
debug("->", n, err)
return errToNT(err)
}
//export kbfsLibdokanWriteFile
func kbfsLibdokanWriteFile(
fname C.LPCWSTR,
Buffer C.LPCVOID,
NumberOfBytesToWrite C.DWORD,
NumberOfBytesWritten C.LPDWORD,
Offset C.LONGLONG,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("WriteFile '%v' %d bytes @ %d %v", d16{fname}, NumberOfBytesToWrite, Offset, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
n, err := getfi(pfi).WriteFile(
ctx,
makeFI(fname, pfi),
bufToSlice(unsafe.Pointer(Buffer), uint32(NumberOfBytesToWrite)),
int64(Offset))
*NumberOfBytesWritten = C.DWORD(n)
debug("->", n, err)
return errToNT(err)
}
//export kbfsLibdokanFlushFileBuffers
func kbfsLibdokanFlushFileBuffers(
fname C.LPCWSTR,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("FlushFileBuffers '%v' %v", d16{fname}, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
err := getfi(pfi).FlushFileBuffers(ctx, makeFI(fname, pfi))
return errToNT(err)
}
func u32zeroToOne(u uint32) uint32 {
if u == 0 {
return 1
}
return u
}
//export kbfsLibdokanGetFileInformation
func kbfsLibdokanGetFileInformation(
fname C.LPCWSTR,
sbuf C.LPBY_HANDLE_FILE_INFORMATION,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("GetFileInformation '%v' %v", d16{fname}, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
st, err := getfi(pfi).GetFileInformation(ctx, makeFI(fname, pfi))
debug("->", st, err)
if st != nil {
sbuf.dwFileAttributes = C.DWORD(st.FileAttributes)
sbuf.ftCreationTime = packTime(st.Creation)
sbuf.ftLastAccessTime = packTime(st.LastAccess)
sbuf.ftLastWriteTime = packTime(st.LastWrite)
sbuf.dwVolumeSerialNumber = C.DWORD(st.VolumeSerialNumber)
sbuf.nFileSizeHigh = C.DWORD(st.FileSize >> 32)
sbuf.nFileSizeLow = C.DWORD(st.FileSize)
sbuf.nNumberOfLinks = C.DWORD(u32zeroToOne(st.NumberOfLinks))
sbuf.nFileIndexHigh = C.DWORD(st.FileIndex >> 32)
sbuf.nFileIndexLow = C.DWORD(st.FileIndex)
}
return errToNT(err)
}
var errFindNoSpace = errors.New("Find out of space")
//export kbfsLibdokanFindFiles
func kbfsLibdokanFindFiles(
PathName C.LPCWSTR,
FindData C.PFillFindData, // call this function with PWIN32_FIND_DATAW
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("FindFiles '%v' %v", d16{PathName}, *pfi)
return kbfsLibdokanFindFilesImpl(PathName, "", FindData, pfi)
}
//export kbfsLibdokanFindFilesWithPattern
func kbfsLibdokanFindFilesWithPattern(
PathName C.LPCWSTR,
SearchPattern C.LPCWSTR,
FindData C.PFillFindData, // call this function with PWIN32_FIND_DATAW
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
pattern := lpcwstrToString(SearchPattern)
debugf("FindFilesWithPattern '%v' %v %q", d16{PathName}, *pfi, pattern)
return kbfsLibdokanFindFilesImpl(PathName, pattern, FindData, pfi)
}
func kbfsLibdokanFindFilesImpl(
PathName C.LPCWSTR,
pattern string,
FindData C.PFillFindData,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("FindFiles '%v' %v", d16{PathName}, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
var fdata C.WIN32_FIND_DATAW
fun := func(ns *NamedStat) error {
fdata.dwFileAttributes = C.DWORD(ns.FileAttributes)
fdata.ftCreationTime = packTime(ns.Creation)
fdata.ftLastAccessTime = packTime(ns.LastAccess)
fdata.ftLastWriteTime = packTime(ns.LastWrite)
fdata.nFileSizeHigh = C.DWORD(ns.FileSize >> 32)
fdata.nFileSizeLow = C.DWORD(ns.FileSize)
fdata.dwReserved0 = C.DWORD(ns.ReparsePointTag)
stringToUtf16Buffer(ns.Name,
C.LPWSTR(unsafe.Pointer(&fdata.cFileName)),
C.DWORD(C.MAX_PATH))
if ns.ShortName != "" {
stringToUtf16Buffer(ns.ShortName,
C.LPWSTR(unsafe.Pointer(&fdata.cFileName)),
C.DWORD(14))
}
v := C.kbfsLibdokanFill_find(FindData, &fdata, pfi)
if v != 0 {
return errFindNoSpace
}
return nil
}
err := getfi(pfi).FindFiles(ctx, makeFI(PathName, pfi), pattern, fun)
return errToNT(err)
}
//export kbfsLibdokanSetFileAttributes
func kbfsLibdokanSetFileAttributes(
fname C.LPCWSTR,
fileAttributes C.DWORD,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("SetFileAttributes '%v' %X %v", d16{fname}, fileAttributes, pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
err := getfi(pfi).SetFileAttributes(ctx, makeFI(fname, pfi), FileAttribute(fileAttributes))
return errToNT(err)
}
//export kbfsLibdokanSetFileTime
func kbfsLibdokanSetFileTime(
fname C.LPCWSTR,
creation *C.FILETIME,
lastAccess *C.FILETIME,
lastWrite *C.FILETIME,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("SetFileTime '%v' %v", d16{fname}, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
var t0, t1, t2 time.Time
if creation != nil {
t0 = unpackTime(*creation)
}
if lastAccess != nil {
t1 = unpackTime(*lastAccess)
}
if lastWrite != nil {
t2 = unpackTime(*lastWrite)
}
err := getfi(pfi).SetFileTime(ctx, makeFI(fname, pfi), t0, t1, t2)
return errToNT(err)
}
//export kbfsLibdokanDeleteFile
func kbfsLibdokanDeleteFile(
fname C.LPCWSTR,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("DeleteFile '%v' %v", d16{fname}, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
err := getfi(pfi).CanDeleteFile(ctx, makeFI(fname, pfi))
return errToNT(err)
}
//export kbfsLibdokanDeleteDirectory
func kbfsLibdokanDeleteDirectory(
fname C.LPCWSTR,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("DeleteDirectory '%v' %v", d16{fname}, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
err := getfi(pfi).CanDeleteDirectory(ctx, makeFI(fname, pfi))
return errToNT(err)
}
//export kbfsLibdokanMoveFile
func kbfsLibdokanMoveFile(
oldFName C.LPCWSTR,
newFName C.LPCWSTR,
replaceExisiting C.BOOL,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
newPath := lpcwstrToString(newFName)
debugf("MoveFile '%v' %v target %v", d16{oldFName}, *pfi, newPath)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
err := getfs(pfi).MoveFile(ctx, makeFI(oldFName, pfi), newPath, bool(replaceExisiting != 0))
return errToNT(err)
}
//export kbfsLibdokanSetEndOfFile
func kbfsLibdokanSetEndOfFile(
fname C.LPCWSTR,
length C.LONGLONG,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("SetEndOfFile '%v' %d %v", d16{fname}, length, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
err := getfi(pfi).SetEndOfFile(ctx, makeFI(fname, pfi), int64(length))
return errToNT(err)
}
//export kbfsLibdokanSetAllocationSize
func kbfsLibdokanSetAllocationSize(
fname C.LPCWSTR,
length C.LONGLONG,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("SetAllocationSize '%v' %d %v", d16{fname}, length, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
err := getfi(pfi).SetAllocationSize(ctx, makeFI(fname, pfi), int64(length))
return errToNT(err)
}
//export kbfsLibdokanLockFile
func kbfsLibdokanLockFile(
fname C.LPCWSTR,
offset C.LONGLONG,
length C.LONGLONG,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("LockFile '%v' %v", d16{fname}, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
err := getfi(pfi).LockFile(ctx, makeFI(fname, pfi), int64(offset), int64(length))
return errToNT(err)
}
//export kbfsLibdokanUnlockFile
func kbfsLibdokanUnlockFile(
fname C.LPCWSTR,
offset C.LONGLONG,
length C.LONGLONG,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debugf("UnlockFile '%v' %v", d16{fname}, *pfi)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
err := getfi(pfi).UnlockFile(ctx, makeFI(fname, pfi), int64(offset), int64(length))
return errToNT(err)
}
//export kbfsLibdokanGetDiskFreeSpace
func kbfsLibdokanGetDiskFreeSpace(
FreeBytesAvailable *C.ULONGLONG,
TotalNumberOfBytes *C.ULONGLONG,
TotalNumberOfFreeBytes *C.ULONGLONG,
FileInfo C.PDOKAN_FILE_INFO) C.NTSTATUS {
debug("GetDiskFreeSpace", *FileInfo)
fs := getfs(FileInfo)
ctx, cancel := fs.WithContext(globalContext())
if cancel != nil {
defer cancel()
}
space, err := fs.GetDiskFreeSpace(ctx)
debug("->", space, err)
if err != nil {
return errToNT(err)
}
if FreeBytesAvailable != nil {
*FreeBytesAvailable = C.ULONGLONG(space.FreeBytesAvailable)
}
if TotalNumberOfBytes != nil {
*TotalNumberOfBytes = C.ULONGLONG(space.TotalNumberOfBytes)
}
if TotalNumberOfFreeBytes != nil {
*TotalNumberOfFreeBytes = C.ULONGLONG(space.TotalNumberOfFreeBytes)
}
return ntstatusOk
}
//export kbfsLibdokanGetVolumeInformation
func kbfsLibdokanGetVolumeInformation(
VolumeNameBuffer C.LPWSTR,
VolumeNameSize C.DWORD, // in num of chars
VolumeSerialNumber C.LPDWORD,
MaximumComponentLength C.LPDWORD, // in num of chars
FileSystemFlags C.LPDWORD,
FileSystemNameBuffer C.LPWSTR,
FileSystemNameSize C.DWORD, // in num of chars
FileInfo C.PDOKAN_FILE_INFO) C.NTSTATUS {
debug("GetVolumeInformation", VolumeNameSize, MaximumComponentLength, FileSystemNameSize, *FileInfo)
fs := getfs(FileInfo)
ctx, cancel := fs.WithContext(globalContext())
if cancel != nil {
defer cancel()
}
vi, err := fs.GetVolumeInformation(ctx)
debug("->", vi, err)
if err != nil {
return errToNT(err)
}
if VolumeNameBuffer != nil {
stringToUtf16Buffer(vi.VolumeName, VolumeNameBuffer, VolumeNameSize)
}
if VolumeSerialNumber != nil {
*VolumeSerialNumber = C.DWORD(vi.VolumeSerialNumber)
}
if MaximumComponentLength != nil {
*MaximumComponentLength = C.DWORD(vi.MaximumComponentLength)
}
if FileSystemFlags != nil {
*FileSystemFlags = C.DWORD(vi.FileSystemFlags)
}
if FileSystemNameBuffer != nil {
stringToUtf16Buffer(vi.FileSystemName, FileSystemNameBuffer, FileSystemNameSize)
}
return ntstatusOk
}
//export kbfsLibdokanMounted
func kbfsLibdokanMounted(pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debug("Mounted")
// Signal that the filesystem is mounted and can be used.
fsTableGetErrChan(uint32(pfi.DokanOptions.GlobalContext)) <- nil
// Dokan wants a NTSTATUS here, but is discarding it.
return ntstatusOk
}
//export kbfsLibdokanGetFileSecurity
func kbfsLibdokanGetFileSecurity(
fname C.LPCWSTR,
//A pointer to SECURITY_INFORMATION value being requested
input C.PSECURITY_INFORMATION,
// A pointer to SECURITY_DESCRIPTOR buffer to be filled
output C.PSECURITY_DESCRIPTOR,
outlen C.ULONG, // length of Security descriptor buffer
LengthNeeded *C.ULONG,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
var si winacl.SecurityInformation
if input != nil {
si = winacl.SecurityInformation(*input)
}
debug("GetFileSecurity", si)
ctx, cancel := getContext(pfi)
if cancel != nil {
defer cancel()
}
buf := bufToSlice(unsafe.Pointer(output), uint32(outlen))
sd := winacl.NewSecurityDescriptorWithBuffer(
buf)
err := getfi(pfi).GetFileSecurity(ctx, makeFI(fname, pfi), si, sd)
if err != nil {
return errToNT(err)
}
if LengthNeeded != nil {
*LengthNeeded = C.ULONG(sd.Size())
}
if sd.HasOverflowed() {
debug("Too small buffer", outlen, "would have needed", sd.Size())
return errToNT(StatusBufferOverflow)
}
debugf("%X", buf)
debug("-> ok,", sd.Size(), "bytes")
return ntstatusOk
}
//export kbfsLibdokanSetFileSecurity
func kbfsLibdokanSetFileSecurity(
fname C.LPCWSTR,
SecurityInformation C.PSECURITY_INFORMATION,
SecurityDescriptor C.PSECURITY_DESCRIPTOR,
SecurityDescriptorLength C.ULONG,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
debug("SetFileSecurity TODO")
return ntstatusOk
}
/* FIXME add support for multiple streams per file?
//export kbfsLibdokanFindStreams
func kbfsLibdokanFindStreams (
fname C.LPCWSTR,
// call this function with PWIN32_FIND_STREAM_DATA
FindStreamData uintptr,
pfi C.PDOKAN_FILE_INFO) C.NTSTATUS {
}
*/
// FileInfo contains information about a file including the path.
type FileInfo struct {
ptr C.PDOKAN_FILE_INFO
rawPath C.LPCWSTR
}
func makeFI(fname C.LPCWSTR, pfi C.PDOKAN_FILE_INFO) *FileInfo {
return &FileInfo{pfi, fname}
}
func packTime(t time.Time) C.FILETIME {
ft := syscall.NsecToFiletime(t.UnixNano())
return C.FILETIME{dwLowDateTime: C.DWORD(ft.LowDateTime), dwHighDateTime: C.DWORD(ft.HighDateTime)}
}
func unpackTime(c C.FILETIME) time.Time {
ft := syscall.Filetime{LowDateTime: uint32(c.dwLowDateTime), HighDateTime: uint32(c.dwHighDateTime)}
// This is valid, see docs and code for package time.
return time.Unix(0, ft.Nanoseconds())
}
func getfs(fi C.PDOKAN_FILE_INFO) FileSystem {
return fsTableGet(uint32(fi.DokanOptions.GlobalContext))
}
func getfi(fi C.PDOKAN_FILE_INFO) File {
file := fiTableGetFile(uint32(fi.Context))
if file == nil {
file = &errorFile{getfs(fi)}
}
return file
}
func fiStore(pfi C.PDOKAN_FILE_INFO, fi File, err error) C.NTSTATUS {
debug("->", fi, err)
if fi != nil {
pfi.Context = C.ULONG64(fiTableStoreFile(uint32(pfi.DokanOptions.GlobalContext), fi))
}
return errToNT(err)
}
func errToNT(err error) C.NTSTATUS {
// NTSTATUS constants are defined as unsigned but the type is signed
// and the values overflow on purpose. This is horrible.
var code uint32
if err != nil {
debug("ERROR:", err)
n, ok := err.(NtStatus)
if ok {
code = uint32(n)
} else {
code = uint32(ErrAccessDenied)
}
}
return C.NTSTATUS(code)
}
type dokanCtx struct {
ptr *C.struct_kbfsLibdokanCtx
slot uint32
}
func allocCtx(slot uint32) *dokanCtx {
return &dokanCtx{C.kbfsLibdokanAllocCtx(C.ULONG64(slot)), slot}
}
func (ctx *dokanCtx) Run(path string, flags MountFlag) error {
ctx.ptr.dokan_options.Options = C.ULONG(flags)
if isDebug {
ctx.ptr.dokan_options.Options |= C.kbfsLibdokanDebug | C.kbfsLibdokanStderr
}
C.kbfsLibdokanSet_path(ctx.ptr, stringToUtf16Ptr(path))
ec := C.kbfsLibdokanRun(ctx.ptr)
if ec != 0 {
return fmt.Errorf("Dokan failed: code=%d %q", ec, dokanErrString(int32(ec)))
}
return nil
}
func dokanErrString(code int32) string {
switch code {
case C.kbfsLibDokan_ERROR:
return "General error"
case C.kbfsLibDokan_DRIVE_LETTER_ERROR:
return "Drive letter error"
case C.kbfsLibDokan_DRIVER_INSTALL_ERROR:
return "Driver install error"
case C.kbfsLibDokan_START_ERROR:
return "Start error"
case C.kbfsLibDokan_MOUNT_ERROR:
return "Mount error"
case C.kbfsLibDokan_MOUNT_POINT_ERROR:
return "Mount point error"
case C.kbfsLibDokan_VERSION_ERROR:
return "Version error"
case C.kbfsLibDokan_DLL_LOAD_ERROR:
return "Error loading Dokan DLL"
default:
return "UNKNOWN"
}
}
func (ctx *dokanCtx) Free() {
debug("dokanCtx.Free")
C.kbfsLibdokanFree(ctx.ptr)
fsTableFree(ctx.slot)
}
// getRequestorToken returns the syscall.Token associated with
// the requestor of this file system operation. Remember to
// call Close on the Token.
func (fi *FileInfo) getRequestorToken() (syscall.Token, error) {
hdl := syscall.Handle(C.kbfsLibdokan_OpenRequestorToken(fi.ptr))
var err error
if hdl == syscall.InvalidHandle {
// Tokens are value types, so returning nil is impossible,
// returning an InvalidHandle is the best way.
err = errors.New("Invalid handle from DokanOpenRequestorHandle")
}
return syscall.Token(hdl), err
}
// isRequestorUserSidEqualTo returns true if the sid passed as
// the argument is equal to the sid of the user associated with
// the filesystem request.
func (fi *FileInfo) isRequestorUserSidEqualTo(sid *winacl.SID) bool {
tok, err := fi.getRequestorToken()
if err != nil {
debug("IsRequestorUserSidEqualTo:", err)
return false
}
defer tok.Close()
tokUser, err := tok.GetTokenUser()
if err != nil {
debug("IsRequestorUserSidEqualTo: GetTokenUser:", err)
return false
}
res, _, _ := syscall.Syscall(procEqualSid.Addr(), 2,
uintptr(unsafe.Pointer(sid)),
uintptr(unsafe.Pointer(tokUser.User.Sid)),
0)
if isDebug {
u1, _ := (*syscall.SID)(sid).String()
u2, _ := tokUser.User.Sid.String()
debugf("IsRequestorUserSidEqualTo: EqualSID(%q,%q) => %v (expecting non-zero)\n", u1, u2, res)
}
runtime.KeepAlive(sid)
runtime.KeepAlive(tokUser.User.Sid)
return res != 0
}
var (
modadvapi32 = windows.NewLazySystemDLL("advapi32.dll")
procEqualSid = modadvapi32.NewProc("EqualSid")
)
// unmount a drive mounted by dokan.
func unmount(path string) error {
debug("Unmount: Calling Dokan.Unmount")
res := C.kbfsLibdokan_RemoveMountPoint((*C.WCHAR)(stringToUtf16Ptr(path)))
if res == C.FALSE {
debug("Unmount: Failed!")
return errors.New("DokanRemoveMountPoint failed!")
}
debug("Unmount: Success!")
return nil
}
// lpcwstrToString converts a nul-terminated Windows wide string to a Go string,
func lpcwstrToString(ptr C.LPCWSTR) string {
if ptr == nil {
return ""
}
var len = 0
for tmp := ptr; *tmp != 0; tmp = (C.LPCWSTR)(unsafe.Pointer((uintptr(unsafe.Pointer(tmp)) + 2))) {
len++
}
raw := ptrUcs2Slice(ptr, len)
return string(utf16.Decode(raw))
}
// stringToUtf16Buffer pokes a string into a Windows wide string buffer.
// On overflow does not poke anything and returns false.
func stringToUtf16Buffer(s string, ptr C.LPWSTR, blenUcs2 C.DWORD) bool {
if ptr == nil || blenUcs2 == 0 {
return false
}
src := utf16.Encode([]rune(s))
tgt := ptrUcs2Slice(C.LPCWSTR(unsafe.Pointer(ptr)), int(blenUcs2))
if len(src)+1 >= len(tgt) {
tgt[0] = 0
return false
}
copy(tgt, src)
tgt[len(src)] = 0
return true
}
// stringToUtf16Ptr return a pointer to the string as utf16 with zero
// termination.
func stringToUtf16Ptr(s string) unsafe.Pointer {
tmp := utf16.Encode([]rune(s + "\000"))
return unsafe.Pointer(&tmp[0])
}
// ptrUcs2Slice takes a C Windows wide string and length in UCS2
// and returns it aliased as a uint16 slice.
func ptrUcs2Slice(ptr C.LPCWSTR, lenUcs2 int) []uint16 {
return *(*[]uint16)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(ptr)),
Len: lenUcs2,
Cap: lenUcs2}))
}
// d16 wraps C wide string pointers to a struct with nice printing
// with zero cost when not debugging and pretty prints when debugging.
type d16 struct{ ptr C.LPCWSTR }
func (s d16) String() string {
return lpcwstrToString(s.ptr)
}