-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
284 lines (243 loc) · 8.11 KB
/
handler.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
import os
from os import path
from Crypto.Hash import SHA256
from multiprocessing import Pool
import cipher.crypt as crypt
import sync
import json
import math
import secrets
import time
loc = json.load(open("config/loc.json", "r"))
block_size = 8388608
copy_count = 1
class File:
def __init__(self, filename = None, sha256 = None, last_modified = 0):
self.filename = filename
self.last_modified = last_modified
if self.filename:
if os.path.isfile(filename):
self.sha256 = get_sha256(path)
else:
self.sha256 = sha256
class Block:
def __init__(self, name, frompath):
self.name = name
self.frompath = frompath
def get_sha256(path):
if os.path.isfile(path):
tmp_hash = SHA256.new()
tmp_hash.update(open(path, "rb").read())
return tmp_hash.digest()
else:
return None
"""
To do: update(upload) file list
"""
def gen_file_list():
print("Generating file list...")
list_file = open("files/filelist.blk", "w")
lines = [str(int(time.time()))]
lines += [i.name for i in os.scandir("files/") if path.isfile(i) and i.name.split(".")[-1] != "blk"]
list_file.write("\n".join(lines))
print("Generated files/filelist.blk")
def gen_block_list(file):
print(f"Generating block list for {file.filename}")
timestamp = int(os.stat(path.join("files", file.filename)).st_ctime)
if file.sha256:
sha256 = file.sha256
else:
sha256 = get_sha256(os.path.join("files", file.filename))
lines = []
lines.append(str(timestamp))
lines.append(str(sha256))
block_file = open(f"files/{file.filename}.blk", "w")
block_count = math.ceil(float(path.getsize(path.join("files", file.filename))) / block_size)
for i in range(block_count):
frompath = []
for j in range(copy_count):
randserver = secrets.choice(loc["servers"])
if not randserver in frompath:
frompath.append(randserver)
lines.append(" ".join(frompath))
block_file.write("\n".join(lines))
print(f"block list successfully generated")
def read_file_list():
try:
get_file_list()
except Exception:
pass
# if
files = []
list_file = open("files/filelist.blk", "r")
lines = list_file.readlines()
# the first line is timestamp
for line in lines[1:]:
files.append(File(line.strip()))
return files
def read_block_list(file):
get_block_list(file)
list_file = open(f"files/{file.filename}.blk", 'r')
lines = list_file.readlines()
try:
file.last_modified = int(lines[0])
file.sha256 = lines[1]
# to do process paths with spaces
blocks = []
for i, j in zip(lines[2:], filename_gen(file.filename)):
blocks.append(Block(j, i.strip().split(" ")))
except IndexError:
print(f"Error processing file {filename}")
return blocks
def get_file_list():
print("Fetching file list...")
for server in loc["servers"]:
if sync.copy(sync.CopyTask("filelist.blk", server, loc["here"])):
timelocal = int(open("files/filelist.blk.old", "r").readline())
timeext = int(open("files/filelist.blk", "r").readline())
if timelocal < timeext:
os.remove("files/filelist.blk.old")
print("Successfully updated file list")
else:
os.replace("files/filelist.blk.old", "files/filelist.blk")
print("The file list is up to date")
# if the file is ok
print("Error downloading file list")
"""
TO-DO:
check time
"""
def get_block_list(file):
for server in loc["servers"]:
if sync.copy(sync.CopyTask(f"{file.filename}.blk", server, loc["here"])):
pass
def update():
files = get_file_list()
for file in files:
if not exist(file.filename):
download(file)
continue
localfile = File(filename = file.filename)
if localfile.sha256 != file.sha256:
if localfile.last_modified >= file.last_modified:
upload(localfile)
else:
download(file)
def download_block(block):
for i in block.frompath:
if sync.copy(sync.CopyTask(block.name, block.frompath, loc["here"])):
break
print(f"Downloading from {block.frompath} failed...")
print(f"Error cannot download {block.name}")
def upload_block(block):
for i in block.frompath:
if not sync.copy(sync.CopyTask(block.name, loc["here"], i)):
print(f"Uploading to {i} failed...\
Remove the entry from the block list afterwards")
else:
print(f"Successfully uploaded to {i}/{block.name}")
def download(file):
blocks = read_block_list(file)
with Pool(sync.config["MAX_PROCESSES"]) as p:
p.map(download_block, blocks)
def upload(file):
blocks = read_block_list(file)
with Pool(sync.config["MAX_PROCESSES"]) as p:
p.map(upload_block, blocks)
def filename_gen(filename, pdir = ""):
n = 0
while True:
yield path.join(pdir, filename + "_{}".format(n))
n = n + 1
"""
input filename
generate filename_0, filename_1 ...
"""
def split(filepath,partsize=block_size):
filedir,name=os.path.split(filepath)
stream=open(filepath,'rb');
for partname in filename_gen(name, pdir=loc["here"]):
part_stream=open(partname,'wb')
read_size=block_size
read_part_total=0
read_once_length=0
#write for part of the block
while(read_part_total < partsize):
read_content=stream.read(read_size)
read_once_length=len(read_content)
if(read_once_length > 0):
part_stream.write(read_content)
else:
break
read_part_total += read_once_length
part_stream.close()
if(read_once_length==0):
break
print('Done')
"""
input file name, cluster name
gernerate {filename} from {clustername_0, clustername_1, ...}
"""
def merge(filename, filepath, partsize=block_size):
fileobj = open(filename,'wb')
once_read_size = block_size
try:
for filepart in filepath:
partstream=open(filepart,'rb')
read_content_total=0
while(read_content_total<partsize):
read_once=0
read_content=partstream.read(once_read_size)
read_once=len(read_content)
if(read_once>0):
fileobj.write(read_content)
else:
break
read_content_total+=read_once
partstream.close()
except FileNotFoundError:
fileobj.close()
"""
def get_file_list():
dir = './files'
myfile = open('./files/filelist.blk','w')
list = os.listdir(dir)
for line in list:
filepath = os.path.join(dir,line)
myfile.write(''+line +'\n')
myfile.close()
def block_list_addr(filename):
#get the block list addr of given file
filelist=open('./filelist','r')
i=0;
while True:
line1=filelist.readline().strip('\n')
line2=filelist.readline().strip('\n')
if len(line2)==0:
print('No such file!\n')
break;
if filename==line1:
addr=int(line2);#assume that block list addr is a number
break;
i=i+1
filelist.close();
return addr
def block_list_map(file):
#give the file name, find the addr of blocklist of the file, and give a list of block map
#e.g. [[1,2,3],[2,3],[1,2]]
addr=block_list_addr(file)
#assume that bl of a file is file+'_bl'
#Download is waiting for implemented...
Download(file+'_bl',addr)
bl=open(file+'_bl','r')
bl.readline();
bl.readline();
line=bl.readline();
blocknum=int([x for x in line.split(' ')][1])
blockmap=[]
for i in range(blocknum):
line=bl.readline().strip('\n')
blockmap.append([int(x) for x in line.split(' ')])#assume that block list addr is a number
bl.close()
return blockmap
"""