-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
171 lines (148 loc) · 4.5 KB
/
file.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
package main
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"time"
"github.com/abdullah2993/encfile"
"github.com/keybase/kbfs/dokan"
"github.com/keybase/kbfs/dokan/winacl"
)
type cryptfile struct {
path string
fs *cryptfs
fullpath string
isdir bool
f *encfile.EncryptedFile
}
var currentUserSID, _ = winacl.CurrentProcessUserSid()
var currentGroupSID, _ = winacl.CurrentProcessPrimaryGroupSid()
var _ dokan.File = (*cryptfile)(nil)
func newCryptFile(fs *cryptfs, fp string, f *encfile.EncryptedFile) *cryptfile {
c := &cryptfile{
path: fp,
fs: fs,
fullpath: filepath.Join(fs.root, fp),
isdir: f == nil,
f: f,
}
return c
}
func (f *cryptfile) ReadFile(ctx context.Context, fi *dokan.FileInfo, bs []byte, offset int64) (int, error) {
if !f.isdir {
return f.f.ReadAt(bs, offset)
}
panic("dir read")
}
func (f *cryptfile) WriteFile(ctx context.Context, fi *dokan.FileInfo, bs []byte, offset int64) (int, error) {
if !f.isdir {
return f.f.WriteAt(bs, offset)
}
panic("dir write")
}
func (f *cryptfile) FlushFileBuffers(ctx context.Context, fi *dokan.FileInfo) error {
if !f.isdir {
return f.f.Sync()
}
panic("dir flush")
}
func (f *cryptfile) GetFileInformation(ctx context.Context, fi *dokan.FileInfo) (*dokan.Stat, error) {
fistat, err := os.Stat(f.fullpath)
if err != nil {
return nil, err
}
stat := fileInfoToStat(fistat)
return &stat, err
}
func (f *cryptfile) FindFiles(ctx context.Context, fi *dokan.FileInfo, pattern string, fillStatCallback func(*dokan.NamedStat) error) error {
if f.isdir {
files, err := ioutil.ReadDir(f.fullpath)
if err != nil {
return dokan.ErrAccessDenied
}
for _, fv := range files {
fillStatCallback(&dokan.NamedStat{
Name: f.fs.decFilePath(fv.Name()),
Stat: fileInfoToStat(fv),
})
}
return nil
}
return dokan.ErrNotADirectory
}
func (f *cryptfile) SetFileTime(ctx context.Context, fi *dokan.FileInfo, creation time.Time, lastAccess time.Time, lastWrite time.Time) error {
return os.Chtimes(f.fullpath, lastAccess, lastWrite)
}
func (f *cryptfile) SetFileAttributes(ctx context.Context, fi *dokan.FileInfo, fileAttributes dokan.FileAttribute) error {
return syscall.SetFileAttributes(syscall.StringToUTF16Ptr(f.fullpath), uint32(fileAttributes))
}
func (f *cryptfile) SetEndOfFile(ctx context.Context, fi *dokan.FileInfo, length int64) error {
if !f.isdir {
// return f.f.Truncate(length)
//SetEndOfFile has a syscall but I think it works just like Truncate
return nil
}
panic("dir truncate")
}
func (f *cryptfile) SetAllocationSize(ctx context.Context, fi *dokan.FileInfo, length int64) error {
if !f.isdir {
// return f.f.Truncate(length)
//encfile needs support for Truncate
return nil
}
return nil
}
func (f *cryptfile) LockFile(ctx context.Context, fi *dokan.FileInfo, offset int64, length int64) error {
panic("not implemented")
//no idea about it can use flock or just a mutex????
}
func (f *cryptfile) UnlockFile(ctx context.Context, fi *dokan.FileInfo, offset int64, length int64) error {
panic("not implemented")
}
func (f *cryptfile) GetFileSecurity(ctx context.Context, fi *dokan.FileInfo, si winacl.SecurityInformation, sd *winacl.SecurityDescriptor) error {
if si&winacl.OwnerSecurityInformation != 0 && currentUserSID != nil {
sd.SetOwner(currentUserSID)
}
if si&winacl.GroupSecurityInformation != 0 && currentGroupSID != nil {
sd.SetGroup(currentGroupSID)
}
if si&winacl.DACLSecurityInformation != 0 {
var acl winacl.ACL
acl.AddAllowAccess(0x001F01FF, currentUserSID)
sd.SetDacl(&acl)
}
return nil
}
func (f *cryptfile) SetFileSecurity(ctx context.Context, fi *dokan.FileInfo, si winacl.SecurityInformation, sd *winacl.SecurityDescriptor) error {
if si&winacl.OwnerSecurityInformation != 0 && currentUserSID != nil {
sd.SetOwner(currentUserSID)
}
if si&winacl.GroupSecurityInformation != 0 && currentGroupSID != nil {
sd.SetGroup(currentGroupSID)
}
if si&winacl.DACLSecurityInformation != 0 {
var acl winacl.ACL
acl.AddAllowAccess(0x001F01FF, currentUserSID)
sd.SetDacl(&acl)
}
return nil
}
func (f *cryptfile) CanDeleteFile(ctx context.Context, fi *dokan.FileInfo) error {
return nil
}
func (f *cryptfile) CanDeleteDirectory(ctx context.Context, fi *dokan.FileInfo) error {
return nil
}
func (f *cryptfile) Cleanup(ctx context.Context, fi *dokan.FileInfo) {
f.CloseFile(ctx, fi)
if fi.IsDeleteOnClose() {
os.RemoveAll(f.fullpath)
}
}
func (f *cryptfile) CloseFile(ctx context.Context, fi *dokan.FileInfo) {
if !f.isdir {
f.f.Close()
}
}