-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirror.py
351 lines (281 loc) · 12.4 KB
/
mirror.py
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
import os
import subprocess
from ctypes import *
from ctypes.wintypes import *
configFilePath = os.path.join(os.path.dirname(__file__), 'conf', 'config.cfg')
kernel32 = WinDLL('kernel32')
LPDWORD = POINTER(DWORD)
UCHAR = c_ubyte
GetFileAttributesW = kernel32.GetFileAttributesW
GetFileAttributesW.restype = DWORD
GetFileAttributesW.argtypes = (LPCWSTR,)
FILE_ATTRIBUTE_HIDDEN = 2
INVALID_FILE_ATTRIBUTES = 0xFFFFFFFF
FILE_ATTRIBUTE_REPARSE_POINT = 0x00400
CreateFileW = kernel32.CreateFileW
CreateFileW.restype = HANDLE
CreateFileW.argtypes = (LPCWSTR, DWORD, DWORD, LPVOID, DWORD, DWORD, HANDLE)
CloseHandle = kernel32.CloseHandle
CloseHandle.restype = BOOL
CloseHandle.argtypes = (HANDLE,)
INVALID_HANDLE_VALUE = HANDLE(-1).value
OPEN_EXISTING = 3
FILE_FLAG_BACKUP_SEMANTICS = 0x02000000
FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000
DeviceIoControl = kernel32.DeviceIoControl
DeviceIoControl.restype = BOOL
DeviceIoControl.argtypes = (HANDLE, DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPVOID)
FSCTL_GET_REPARSE_POINT = 0x000900A8
IO_REPARSE_TAG_MOUNT_POINT = 0xA0000003
IO_REPARSE_TAG_SYMLINK = 0xA000000C
MAXIMUM_REPARSE_DATA_BUFFER_SIZE = 0x4000
sortedRootsCache = []
sortedDirData = []
class GENERIC_REPARSE_BUFFER(Structure):
_fields_ = (('DataBuffer', UCHAR * 1),)
class SYMBOLIC_LINK_REPARSE_BUFFER(Structure):
_fields_ = (('SubstituteNameOffset', USHORT),
('SubstituteNameLength', USHORT),
('PrintNameOffset', USHORT),
('PrintNameLength', USHORT),
('Flags', ULONG),
('PathBuffer', WCHAR * 1))
@property
def PrintName(self):
arrayt = WCHAR * (self.PrintNameLength // 2)
offset = type(self).PathBuffer.offset + self.PrintNameOffset
return arrayt.from_address(addressof(self) + offset).value
class MOUNT_POINT_REPARSE_BUFFER(Structure):
_fields_ = (('SubstituteNameOffset', USHORT),
('SubstituteNameLength', USHORT),
('PrintNameOffset', USHORT),
('PrintNameLength', USHORT),
('PathBuffer', WCHAR * 1))
@property
def PrintName(self):
arrayt = WCHAR * (self.PrintNameLength // 2)
offset = type(self).PathBuffer.offset + self.PrintNameOffset
return arrayt.from_address(addressof(self) + offset).value
class REPARSE_DATA_BUFFER(Structure):
class REPARSE_BUFFER(Union):
_fields_ = (('SymbolicLinkReparseBuffer', SYMBOLIC_LINK_REPARSE_BUFFER),
('MountPointReparseBuffer', MOUNT_POINT_REPARSE_BUFFER),
('GenericReparseBuffer', GENERIC_REPARSE_BUFFER))
_fields_ = (('ReparseTag', ULONG),
('ReparseDataLength', USHORT),
('Reserved', USHORT),
('ReparseBuffer', REPARSE_BUFFER))
_anonymous_ = ('ReparseBuffer',)
def get_sorted_root_data(root):
if root == sampleDir:
return [targetDir, 0]
if sortedRootsCache:
for rootVals in sortedRootsCache:
if root == rootVals[0]:
return [rootVals[1], rootVals[2]]
if root[0:len(sampleDir)] == sampleDir:
return [targetDir + root[len(sampleDir):len(root)], 0]
return [root, 0]
def is_to_be_walked(path):
if path == sampleDir:
return True
for dirToWalk in dirsToWalk:
pathToWalk = os.path.join(sampleDir, dirToWalk)
if pathToWalk == path[0:len(pathToWalk)]:
return True
return False
def is_hidden(path):
attribs = GetFileAttributesW(path)
hidden = attribs & FILE_ATTRIBUTE_HIDDEN
return hidden
def is_custom_dir(path):
fullPath = os.path.join(path, '')
dir = os.path.split(os.path.dirname(fullPath))[1]
for customDir in customDirs:
if dir == customDir:
return True
return False
def is_final_dir(path):
for root, dirs, files in os.walk(path):
if dirs and dirs != ['More']:
return False
else:
return True
def is_junction(path):
result = GetFileAttributesW(path)
if result == INVALID_FILE_ATTRIBUTES:
raise WinError()
return bool(result & FILE_ATTRIBUTE_REPARSE_POINT)
def read_junction(path):
reparse_point_handle = CreateFileW(path, 0, 0, None, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, None)
if reparse_point_handle == INVALID_HANDLE_VALUE:
raise WinError()
target_buffer = c_buffer(MAXIMUM_REPARSE_DATA_BUFFER_SIZE)
n_bytes_returned = DWORD()
io_result = DeviceIoControl(reparse_point_handle, FSCTL_GET_REPARSE_POINT, None, 0, target_buffer, len(target_buffer), byref(n_bytes_returned), None)
CloseHandle(reparse_point_handle)
if not io_result:
raise WinError()
rdb = REPARSE_DATA_BUFFER.from_buffer(target_buffer)
if rdb.ReparseTag == IO_REPARSE_TAG_SYMLINK:
return rdb.SymbolicLinkReparseBuffer.PrintName
elif rdb.ReparseTag == IO_REPARSE_TAG_MOUNT_POINT:
return rdb.MountPointReparseBuffer.PrintName
raise ValueError("not a link")
def parse_dir(origRoot, origDirs):
if not is_final_dir(origRoot):
sortedRootData = get_sorted_root_data(origRoot)
sortedDirs = []
for origDir in origDirs:
origPath = os.path.join(origRoot, origDir)
if is_to_be_walked(origPath):
if sortedRootData[1] == 1 or is_hidden(origPath):
sortedRootsCache.append([origPath, '', 1])
else:
if sortedRootData[1] == 2:
sortedRootsCache.append([origPath, '', 2])
else:
print('Reading path "' + origPath + '"')
sortedDirs.append([origPath, origDir])
if sortedDirs:
# start of actual sorting jazz
sortedByGroups = [[]]
groupParam = 'SortGroup='
for sortedVals in sortedDirs:
fileName = os.path.join(origRoot, sortedVals[1] + '.nfo')
groupNum = False;
if os.path.isfile(fileName):
file = open(fileName, 'r')
lines = file.readlines()
for line in lines:
if line[0:len(groupParam)] == groupParam:
groupNumTmp = line[len(groupParam):len(line)]
groupNum = groupNumTmp.strip()
break
if groupNum and groupNum.isnumeric():
if int(groupNum) >= len(sortedByGroups):
for x in range(len(sortedByGroups), int(groupNum) + 1):
sortedByGroups.insert(x, [])
subArr = sortedByGroups[int(groupNum)]
subArr.append(sortedVals)
sortedByGroups[int(groupNum)] = subArr
continue
subArr = sortedByGroups[0]
subArr.append(sortedVals)
sortedByGroups[0] = subArr
sortedDirs = []
y = len(sortedByGroups) - 1
while y >= 0:
subArr = sortedByGroups[y]
if (len(subArr) > 0):
for subArrVals in subArr:
sortedDirs.append(subArrVals)
y -= 1
# end of sorting jazz
sortIndex = 1
for sortedVals in sortedDirs:
sortedDigits = '{0:0=2d}'.format(sortIndex)
sortedDir = '[' + sortedDigits + '] ' + sortedVals[1]
sortedPath = os.path.join(sortedRootData[0], sortedDir)
if is_junction(sortedVals[0]):
sortedRootsCache.append([sortedVals[0], sortedPath, 2])
junctionTarget = read_junction(sortedVals[0])
if is_final_dir(sortedVals[0]):
sortedDirData.append([sortedRootData[0], sortedDir, junctionTarget])
else:
sortedDirData.append([sortedRootData[0], sortedDir, junctionTarget, True])
else:
if is_custom_dir(sortedVals[0]):
sortedRootsCache.append([sortedVals[0], sortedPath, 2])
sortedDirData.append([sortedRootData[0], sortedDir, sortedVals[0]])
else:
sortedRootsCache.append([sortedVals[0], sortedPath, 0])
if is_final_dir(sortedVals[0]):
sortedDirData.append([sortedRootData[0], sortedDir, sortedVals[0]])
else:
sortedDirData.append([sortedRootData[0], sortedDir])
sortIndex += 1
def value_found(line, valParam):
if line[0:len(valParam)] == valParam:
value = line[len(valParam):len(line)]
if value.startswith('"') and value.endswith('"'):
return value[1:len(value) - 1]
if value.startswith('[') and value.endswith(']'):
splitValues = value[1:len(value) - 1].split(',')
result = []
for splitValue in splitValues:
splitTmp = splitValue.strip()
if splitTmp.startswith('"') and splitTmp.endswith('"'):
result.append(splitTmp[1:len(splitTmp) - 1])
return result
return False
sampleDir = False
targetDir = False
dirsToWalk = False
customDirs = False
configData = False
# read config data
if os.path.isfile(configFilePath):
file = open(configFilePath, 'r')
lines = file.readlines()
for line in lines:
strippedLine = line.strip()
if value_found(strippedLine, 'SourceFolder='):
sampleDir = value_found(strippedLine, 'SourceFolder=')
if not os.path.isdir(sampleDir):
print('Source directory not found at "' + sampleDir + '".')
sampleDir = False
if value_found(strippedLine, 'SubFolders='):
dirsToWalk = value_found(strippedLine, 'SubFolders=')
if value_found(strippedLine, 'MixedFolders='):
customDirs = value_found(strippedLine, 'MixedFolders=')
if value_found(strippedLine, 'PathToTarget='):
pathToTarget = value_found(strippedLine, 'PathToTarget=')
targetDir = os.path.join(os.path.dirname(configFilePath), pathToTarget)
if not os.path.isdir(targetDir):
print('Target directory not found at "' + targetDir + '".')
targetDir = False
if sampleDir and dirsToWalk and customDirs and targetDir:
configData = True
else:
print('Config file missing at "' + configFilePath + '".')
if configData:
# collect latest structure data
parse_dir(sampleDir, dirsToWalk)
for dirToWalk in dirsToWalk:
for walkRoot, walkDirs, walkFiles in os.walk(os.path.join(sampleDir, dirToWalk)):
parse_dir(walkRoot, walkDirs)
for dirData in sortedDirData:
if len(dirData) >= 4:
sortedRootData = get_sorted_root_data(dirData[2])
dirData[2] = sortedRootData[0]
# clear existing mirror structure
existingDirs = os.listdir(targetDir)
for existingDir in existingDirs:
dirBase = existingDir.split()[0]
if dirBase.startswith('[') and dirBase.endswith(']'):
if str(dirBase[1:len(dirBase) - 1].isnumeric()):
target = os.path.join(targetDir, existingDir)
command = 'rmdir /q /s "' + target + '"'
with open(os.devnull, 'wb') as devnull:
subprocess.check_call(command, stdout=devnull, stderr=subprocess.STDOUT, shell=True)
print('Delete directory "' + existingDir + '" at "' + targetDir + '"')
# write sorted mirror structure
for dirData in sortedDirData:
if len(dirData) >= 3:
source = os.path.join(dirData[0], dirData[1])
target = dirData[2]
command = 'mklink /j "' + source + '" "' + target + '"'
with open(os.devnull, 'wb') as devnull:
subprocess.check_call(command, stdout=devnull, stderr=subprocess.STDOUT, shell=True)
print('Adding junction "' + dirData[1] + '" at "' + dirData[0] + '" pointing to "' + target + '"')
else:
try:
os.mkdir(os.path.join(dirData[0], dirData[1]))
except OSError:
print('Failed adding directory "' + dirData[1] + '" at "' + dirData[0] + '"')
else:
print('Adding directory "' + dirData[1] + '" at "' + dirData[0] + '"')
print('Done.')
else:
print('Config data missing or incorrect.')