summaryrefslogtreecommitdiff
path: root/fs/expose/dokan/libdokan.py
blob: 9d33be44838d7f9a848586bc97108b991e69b030 (plain)
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
#  Copyright (c) 2009-2010, Cloud Matrix Pty. Ltd.
#  Copyright (c) 2016-2016, Adrien J. <liryna.stark@gmail.com>.
#  All rights reserved; available under the terms of the MIT License.
"""

  fs.expose.dokan.libdokan:  low-level ctypes interface to Dokan

"""

from ctypes import *

try:
    DokanMain = windll.Dokan1.DokanMain
    DokanVersion = windll.Dokan1.DokanVersion
except AttributeError:
    raise ImportError("Dokan DLL not found")


from ctypes.wintypes import *
ULONG64 = c_ulonglong
PULONGLONG = POINTER(c_ulonglong)
PVOID = c_void_p
PULONG = POINTER(c_ulong)
UCHAR = c_ubyte
LPDWORD = POINTER(c_ulong)
LONGLONG = c_longlong
NTSTATUS = c_long
USHORT = c_ushort
WCHAR = c_wchar


DokanVersion.restype = ULONG
DokanVersion.argtypes = ()
DOKAN_MINIMUM_COMPATIBLE_VERSION = 100  # this is release 1.0.0
if DokanVersion() < DOKAN_MINIMUM_COMPATIBLE_VERSION:
    raise ImportError("Dokan DLL is too old")


MAX_PATH = 260

class SECURITY_DESCRIPTOR(Structure): pass

PSECURITY_DESCRIPTOR = POINTER(SECURITY_DESCRIPTOR)
PPSECURITY_DESCRIPTOR = POINTER(PSECURITY_DESCRIPTOR)

SECURITY_INFORMATION = DWORD
PSECURITY_INFORMATION = POINTER(SECURITY_INFORMATION)

class FILETIME(Structure):
    _fields_ = [
        ("dwLowDateTime", DWORD),
        ("dwHighDateTime", DWORD),
    ]


class WIN32_FIND_DATAW(Structure):
    _fields_ = [
        ("dwFileAttributes", DWORD),
        ("ftCreationTime", FILETIME),
        ("ftLastAccessTime", FILETIME),
        ("ftLastWriteTime", FILETIME),
        ("nFileSizeHigh", DWORD),
        ("nFileSizeLow", DWORD),
        ("dwReserved0", DWORD),
        ("dwReserved1", DWORD),
        ("cFileName", WCHAR * MAX_PATH),
        ("cAlternateFileName", WCHAR * 14),
    ]


class BY_HANDLE_FILE_INFORMATION(Structure):
    _fields_ = [
        ('dwFileAttributes', DWORD),
        ('ftCreationTime', FILETIME),
        ('ftLastAccessTime', FILETIME),
        ('ftLastWriteTime', FILETIME),
        ('dwVolumeSerialNumber', DWORD),
        ('nFileSizeHigh', DWORD),
        ('nFileSizeLow', DWORD),
        ('nNumberOfLinks', DWORD),
        ('nFileIndexHigh', DWORD),
        ('nFileIndexLow', DWORD),
    ]


class DOKAN_OPTIONS(Structure):
    _fields_ = [
        ("Version", USHORT),
        ("ThreadCount", USHORT),
        ("Options", ULONG),
        ("GlobalContext", ULONG64),
        ("MountPoint", LPCWSTR),
        ("UNCName", LPCWSTR),
        ("Timeout", ULONG),
        ("AllocationUnitSize", ULONG),
        ("SectorSize", ULONG),
    ]


class DOKAN_FILE_INFO(Structure):
    _fields_ = [
        ("Context", ULONG64),
        ("DokanContext", ULONG64),
        ("DokanOptions", POINTER(DOKAN_OPTIONS)),
        ("ProcessId", ULONG),
        ("IsDirectory", UCHAR),
        ("DeleteOnClose", UCHAR),
        ("PagingIO", UCHAR),
        ("SyncronousIo", UCHAR),
        ("Nocache", UCHAR),
        ("WriteToEndOfFile", UCHAR),
    ]


PDOKAN_FILE_INFO = POINTER(DOKAN_FILE_INFO)
PFillFindData = WINFUNCTYPE(c_int, POINTER(WIN32_FIND_DATAW), PDOKAN_FILE_INFO)


class DOKAN_OPERATIONS(Structure):
    _fields_ = [
        ("ZwCreateFile", WINFUNCTYPE(NTSTATUS,
        LPCWSTR,      # FileName
        PVOID,        # SecurityContext, see
                      # https://msdn.microsoft.com/en-us/library/windows/hardware/ff550613(v=vs.85).aspx
        DWORD,        # DesiredAccess
        ULONG,        # FileAttributes
        ULONG,        # ShareAccess
        ULONG,        # CreateDisposition
        ULONG,        # CreateOptions
        PDOKAN_FILE_INFO)),
        ("Cleanup", WINFUNCTYPE(None,
        LPCWSTR,      # FileName
        PDOKAN_FILE_INFO)),
        ("CloseFile", WINFUNCTYPE(None,
        LPCWSTR,      # FileName
        PDOKAN_FILE_INFO)),
        ("ReadFile", WINFUNCTYPE(NTSTATUS,
        LPCWSTR,  # FileName
        LPVOID,   # Buffer
        DWORD,    # NumberOfBytesToRead
        LPDWORD,  # NumberOfBytesRead
        LONGLONG, # Offset
        PDOKAN_FILE_INFO)),
        ("WriteFile", WINFUNCTYPE(NTSTATUS,
        LPCWSTR,  # FileName
        LPCVOID,  # Buffer
        DWORD,    # NumberOfBytesToWrite
        LPDWORD,  # NumberOfBytesWritten
        LONGLONG, # Offset
        PDOKAN_FILE_INFO)),
        ("FlushFileBuffers", WINFUNCTYPE(NTSTATUS,
        LPCWSTR,  # FileName
        PDOKAN_FILE_INFO)),
        ("GetFileInformation", WINFUNCTYPE(NTSTATUS,
        LPCWSTR,  # FileName
        POINTER(BY_HANDLE_FILE_INFORMATION), # Buffer
        PDOKAN_FILE_INFO)),
        ("FindFiles", WINFUNCTYPE(NTSTATUS,
        LPCWSTR,  # PathName
        PFillFindData, # call this function with PWIN32_FIND_DATAW
        PDOKAN_FILE_INFO)),
        ("FindFilesWithPattern", WINFUNCTYPE(NTSTATUS,
        LPCWSTR,  # PathName
        LPCWSTR,  # SearchPattern
        PFillFindData,  #call this function with PWIN32_FIND_DATAW
        PDOKAN_FILE_INFO)),
        ("SetFileAttributes", WINFUNCTYPE(NTSTATUS,
        LPCWSTR, # FileName
        DWORD,   # FileAttributes
        PDOKAN_FILE_INFO)),
        ("SetFileTime", WINFUNCTYPE(NTSTATUS,
        LPCWSTR, # FileName
        POINTER(FILETIME), # CreationTime
        POINTER(FILETIME), # LastAccessTime
        POINTER(FILETIME), # LastWriteTime
        PDOKAN_FILE_INFO)),
        ("DeleteFile", WINFUNCTYPE(NTSTATUS,
        LPCWSTR, # FileName
        PDOKAN_FILE_INFO)),
        ("DeleteDirectory", WINFUNCTYPE(NTSTATUS,
        LPCWSTR, # FileName
        PDOKAN_FILE_INFO)),
        ("MoveFile", WINFUNCTYPE(NTSTATUS,
        LPCWSTR, # ExistingFileName
        LPCWSTR, # NewFileName
        BOOL,    # ReplaceExisiting
        PDOKAN_FILE_INFO)),
        ("SetEndOfFile", WINFUNCTYPE(NTSTATUS,
        LPCWSTR,  # FileName
        LONGLONG, # Length
        PDOKAN_FILE_INFO)),
        ("SetAllocationSize", WINFUNCTYPE(NTSTATUS,
        LPCWSTR,  # FileName
        LONGLONG, # Length
        PDOKAN_FILE_INFO)),
        ("LockFile", WINFUNCTYPE(NTSTATUS,
        LPCWSTR,  # FileName
        LONGLONG, # ByteOffset
        LONGLONG, # Length
        PDOKAN_FILE_INFO)),
        ("UnlockFile", WINFUNCTYPE(NTSTATUS,
        LPCWSTR,  # FileName
        LONGLONG, # ByteOffset
        LONGLONG, # Length
        PDOKAN_FILE_INFO)),
        ("GetDiskFreeSpace", WINFUNCTYPE(NTSTATUS,
        PULONGLONG, # FreeBytesAvailable
        PULONGLONG, # TotalNumberOfBytes
        PULONGLONG, # TotalNumberOfFreeBytes
        PDOKAN_FILE_INFO)),
        ("GetVolumeInformation", WINFUNCTYPE(NTSTATUS,
        PVOID,  # VolumeNameBuffer
        DWORD,   # VolumeNameSize in num of chars
        LPDWORD, # VolumeSerialNumber
        LPDWORD, # MaximumComponentLength in num of chars
        LPDWORD, # FileSystemFlags
        PVOID,  # FileSystemNameBuffer
        DWORD,   # FileSystemNameSize in num of chars
        PDOKAN_FILE_INFO)),
        ("Mounted", WINFUNCTYPE(NTSTATUS,
        PDOKAN_FILE_INFO)),
        ("Unmounted", WINFUNCTYPE(NTSTATUS,
        DOKAN_FILE_INFO)),
        ("GetFileSecurity", WINFUNCTYPE(NTSTATUS,
        LPCWSTR, # FileName
        PULONG,   # A pointer to SECURITY_INFORMATION value being requested
        PVOID,   # A pointer to SECURITY_DESCRIPTOR buffer to be filled
        ULONG,   # Length of Security descriptor buffer
        PULONG,  # Length Needed
        PDOKAN_FILE_INFO)),
        ("SetFileSecurity", WINFUNCTYPE(NTSTATUS,
        LPCWSTR, # FileName
        PVOID,   # A pointer to SECURITY_INFORMATION value being
        PVOID,   # A pointer to SECURITY_DESCRIPTOR buffer
        ULONG,   # Length of Security descriptor buffer
        PDOKAN_FILE_INFO)),
        ("FindStreams", WINFUNCTYPE(NTSTATUS,
        LPCWSTR, # FileName
        PVOID,   # call this function with PWIN32_FIND_STREAM_DATA
        PDOKAN_FILE_INFO))
    ]


DokanMain.restype = c_int
DokanMain.argtypes = (
    POINTER(DOKAN_OPTIONS),
    POINTER(DOKAN_OPERATIONS),
)

DokanRemoveMountPoint = windll.Dokan1.DokanRemoveMountPoint
DokanRemoveMountPoint.restype = BOOL
DokanRemoveMountPoint.argtypes = (
    LPCWSTR,
)

DokanIsNameInExpression = windll.Dokan1.DokanIsNameInExpression
DokanIsNameInExpression.restype = BOOL
DokanIsNameInExpression.argtypes = (
    LPCWSTR,  # pattern
    LPCWSTR,  # name
    BOOL,     # ignore case
)

DokanDriverVersion = windll.Dokan1.DokanDriverVersion
DokanDriverVersion.restype = ULONG
DokanDriverVersion.argtypes = (
)

DokanResetTimeout = windll.Dokan1.DokanResetTimeout
DokanResetTimeout.restype = BOOL
DokanResetTimeout.argtypes = (
    ULONG,  #timeout
    PDOKAN_FILE_INFO,  # file info pointer
)

GetFileSecurity = windll.advapi32.GetFileSecurityW
GetFileSecurity.restype = BOOL
GetFileSecurity.argtypes = (
    LPWSTR,                     # _In_ LPCTSTR lpFileName,
    SECURITY_INFORMATION,       # _In_ SECURITY_INFORMATION RequestedInformation,
    PSECURITY_DESCRIPTOR,       # _Out_opt_ PSECURITY_DESCRIPTOR pSecurityDescriptor,
    DWORD,                      # _In_ DWORD nLength,
    LPDWORD,                    # _Out_ LPDWORD lpnLengthNeeded
)