-
Notifications
You must be signed in to change notification settings - Fork 4
/
extract.py
46 lines (38 loc) · 1.07 KB
/
extract.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
#!/bin/python
import sys
import json
import re
import struct
import os
input = sys.stdin
def getHashSize(physicalSize, metadata):
size = physicalSize
if ('meta' in metadata):
size = min(size, metadata['meta']['size'])
return size
def getPath(metadata):
name = metadata['name']['name']
return metadata['path'] + name, name
def exportFile(path, data):
dir = os.path.dirname(path)
if (False == os.path.exists(dir)):
os.makedirs(dir)
with open(path, 'w') as f:
f.write(data)
search = re.compile(sys.argv[1])
line = input.readline()
filesRead = 0
while line:
try:
metadata = json.loads(line)
path, name = getPath(metadata)
sizeData = input.read(8)
size = struct.unpack('<Q', sizeData)[0]
data = input.read(size)
size = getHashSize(size, metadata)
if (None != search.search(path) and 5 == metadata['name']['type'] and not (name == '.' or name == '..')):
exportFile(path, data[:size])
line = input.readline()
except struct.error as e:
print("%s with len(sizeData) == %s on %s" % (e, str(len(sizeData)), path))
raise