-
Notifications
You must be signed in to change notification settings - Fork 1
/
iblocksync.py
1174 lines (901 loc) · 38.9 KB
/
iblocksync.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse, collections, errno, hashlib, itertools, json, logging, os, re, shlex, stat, struct, subprocess, sys, threading, time, zlib
__version__ = "1.0.1"
_SIGNAL_ACK = b'\x06'
_SIGNAL_NAK = b'\x15'
_SIGNAL_EOT = b'\x04'
_byteSuffix = [
('B', 'B', 'B'),
('KiB', 'K', 'kB'),
('MiB', 'M', 'MB'),
('GiB', 'G', 'GB'),
('TiB', 'T', 'TB'),
('PiB', 'P', 'PB'),
('EiB', 'E', 'EB'),
]
class ReadableBlockImage(object):
_io = None
_filePath = None
_fileSize = None
_uuid = None
_blockSize = None
_blockCount = None
_currentBlock = None
_currentBlockHash = None
_currentBlockIndex = -1
_hashAlgorithm = None
def __init__(self, filePath, blockSize, hashAlgorithm="sha1"):
self._filePath = filePath
self._blockSize = blockSize
self._hashAlgorithm = hashAlgorithm
@property
def filePath(self):
return self._filePath
@property
def fileSize(self):
if self._fileSize is None:
currentOffset = self._file.tell()
self._file.seek(0, os.SEEK_END)
self._fileSize = self._file.tell()
self._file.seek(currentOffset, os.SEEK_SET)
return self._fileSize
@property
def uuid(self):
if self._uuid is None:
try:
cmd = [ "/sbin/blkid", "-o", "value", "-s", "UUID", self._filePath ]
self._uuid = subprocess.check_output(cmd).decode("ascii")
if self._uuid[-1:] == "\n":
self._uuid = self._uuid[:-1]
except Exception:
self._uuid = ""
return self._uuid
@property
def blockSize(self):
return self._blockSize
@property
def blockCount(self):
if self._blockCount is None:
self._blockCount = -(- self.fileSize // self._blockSize)
return self._blockCount
@property
def hashAlgorithm(self):
return self._hashAlgorithm
@property
def closed(self):
return self._io is None or self._io.closed
@property
def _file(self):
if not self._io:
self.open()
return self._io
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
return self.close()
def __iter__(self):
return self
def __next__(self):
try:
return self.next()
except EOFError:
raise StopIteration
def open(self):
self.close()
try:
self._io = open(self._filePath, "rb")
except:
self.close()
raise
def close(self):
if self._io:
self._io.close()
self._io = None
self._fileSize = None
self._uuid = None
self._blockCount = None
self._currentBlock = None
self._currentBlockHash = None
self._currentBlockIndex = -1
def _sync(self):
if self._currentBlockIndex >= 0:
currentOffset = min((self._currentBlockIndex + 1) * self._blockSize, self.fileSize)
self._file.seek(currentOffset, os.SEEK_SET)
else:
self._file.seek(0, os.SEEK_SET)
def next(self):
if self._currentBlockIndex + 1 >= self.blockCount:
raise EOFError
self._sync()
self._currentBlock = self._file.read(self._blockSize)
self._currentBlockHash = None
self._currentBlockIndex += 1
if len(self._currentBlock) == 0:
raise RuntimeError("Unexpectedly reached end of file")
return self.read()
def read(self):
if self._currentBlock is None:
if self._currentBlockIndex == -1:
return None, None
self.rewind()
return self.next()
if self._currentBlockHash is None and len(self._currentBlock) > 0:
self._currentBlockHash = _hash(self._hashAlgorithm, self._currentBlock)
return self._currentBlock, self._currentBlockHash
def rewind(self, blocks=1):
if not blocks >= 1:
raise ValueError("Block count must be >= 1")
if self._currentBlockIndex == -1:
raise RuntimeError("You can't rewind at the start of the file")
self._currentBlock = None
self._currentBlockHash = None
self._currentBlockIndex = max(self._currentBlockIndex - blocks, -1)
def reset(self):
self._currentBlock = None
self._currentBlockHash = None
self._currentBlockIndex = -1
def skip(self, blocks=1):
if not blocks >= 1:
raise ValueError("Block count must be >= 1")
self._currentBlock = None
self._currentBlockHash = None
if self._currentBlockIndex + blocks < self.blockCount:
self._currentBlockIndex = self._currentBlockIndex + blocks
else:
self._currentBlockIndex = self.blockCount - 1
raise EOFError
def tell(self):
return self._currentBlockIndex
class WritableBlockImage(ReadableBlockImage):
@property
def _blockDevice(self):
mode = os.stat(self._filePath).st_mode
return stat.S_ISBLK(mode)
def open(self):
self.close()
try:
self._io = open(self._filePath, "r+b")
except IOError as error:
if error.errno == errno.ENOENT:
self._io = open(self._filePath, "w+b")
else:
raise
except:
self.close()
raise
def close(self):
if self._io:
self.flush()
return super(WritableBlockImage, self).close()
def write(self, bytes):
if self._currentBlockIndex == -1:
raise RuntimeError("No block to overwrite")
self._currentBlockIndex -= 1
self._sync()
bytesLength = len(bytes)
bytesBlockCount = -(- bytesLength // self._blockSize)
if bytesLength == 0:
self._fileSize = self._file.tell()
self._blockCount = self._currentBlockIndex + 1
self._currentBlock = None
self._currentBlockHash = None
return
self._file.write(bytes)
lastBlockSize = bytesLength % self._blockSize
if lastBlockSize == 0:
lastBlockSize = self._blockSize
self._fileSize = max(self.fileSize, (self._currentBlockIndex + 1) * self._blockSize + bytesLength)
self._blockCount = max(self.blockCount, self._currentBlockIndex + 1 + bytesBlockCount)
else:
self._fileSize = (self._currentBlockIndex + 1) * self._blockSize + bytesLength
self._blockCount = self._currentBlockIndex + 1 + bytesBlockCount
self._currentBlock = bytes[-lastBlockSize:]
self._currentBlockHash = None
self._currentBlockIndex += bytesBlockCount
def seek(self, blocks=1):
if not blocks >= 1:
raise ValueError("Block count must be >= 1")
if self._currentBlockIndex + blocks >= self.blockCount:
if self.fileSize % self._blockSize > 0:
raise RuntimeError("You must not seek over an incomplete block")
self._fileSize = max(self.fileSize, (self._currentBlockIndex + 1 + blocks) * self._blockSize)
self._blockCount = max(self.blockCount, self._currentBlockIndex + 1 + blocks)
self._currentBlock = None
self._currentBlockHash = None
self._currentBlockIndex += blocks
def truncate(self):
self._sync()
self._fileSize = self._file.tell()
self._blockCount = self._currentBlockIndex + 1
if not self._blockDevice:
self._file.truncate()
else:
self._file.seek(0, os.SEEK_END)
blockDeviceSize = self._file.tell()
subsequentData = blockDeviceSize - self._fileSize
if subsequentData > 0:
self._file.seek(self._fileSize, os.SEEK_SET)
self._file.write(b'\0' * subsequentData)
def flush(self):
self._sync()
self._file.flush()
os.fsync(self._file.fileno())
class Remote(object):
_image = None
_remoteImageInfo = None
@property
def image(self):
if not self._image:
raise RuntimeException("{!r} wasn't initialized yet".format(self.__class__.__name__))
return self._image
@property
def _imageInfo(self):
return {
"blockSize": self.image.blockSize,
"hashAlgorithm": self.image.hashAlgorithm,
"filePath": self.image.filePath,
"fileSize": self.image.fileSize,
"uuid": self.image.uuid
}
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
return self.close()
def open(self):
self.image.open()
def close(self):
if self._image:
self._image.close()
def checkIdentifier(self):
expectedIdentifier = "iblocksync {}\n".format(__version__)
identifier = sys.stdin.buffer.readline().decode("utf-8")
if identifier != expectedIdentifier:
raise ValueError("Expecting identifier {!r}, got {!r}".format(expectedIdentifier, identifier))
def sendError(self, message, code=None):
errorDict = { "error": message }
if code is not None:
errorDict["errno"] = code
sys.stdout.buffer.write((json.dumps(errorDict) + "\n").encode("utf-8"))
sys.stdout.buffer.flush()
def sendImageInfo(self):
sys.stdout.buffer.write((json.dumps(self._imageInfo) + "\n").encode("utf-8"))
sys.stdout.buffer.flush()
def readImageInfo(self):
jsonString = sys.stdin.buffer.readline()
if len(jsonString) == 0:
raise ValueError("Failed to read image info")
self._remoteImageInfo = json.loads(jsonString.decode("utf-8"))
if self._remoteImageInfo["blockSize"] != self.image.blockSize:
raise ValueError("Expecting a block size of {:,} bytes, got {:,} bytes".format(
self._blockSize, self._imageInfo["blockSize"]
))
elif self._remoteImageInfo["hashAlgorithm"] != self.image.hashAlgorithm:
raise ValueError("Expecting hash algorithm {}, got {}".format(
self._hashAlgorithm, self._imageInfo["hashAlgorithm"]
))
def _imageDiff(self, sourceImageInfo, targetImageInfo):
sourceBlockCount = -(- sourceImageInfo["fileSize"] // sourceImageInfo["blockSize"])
targetBlockCount = -(- targetImageInfo["fileSize"] // targetImageInfo["blockSize"])
commonBlockCount = min(sourceBlockCount, targetBlockCount)
self._sendHashes(commonBlockCount)
blocks = self._readBlockUpdates(commonBlockCount)
blocksFiller = itertools.repeat(True, sourceBlockCount - commonBlockCount)
return itertools.chain(blocks, blocksFiller)
def _sendHashes(self, blockCount):
for _, blockHash in itertools.islice(self.image, blockCount):
sys.stdout.buffer.write(blockHash)
sys.stdout.buffer.flush()
self.image.reset()
def _readBlockUpdates(self, blockCount):
blocks = []
for blockIndex in range(blockCount):
blocks.append(self._readSignal())
return blocks
def _readSignal(self):
signal = sys.stdin.buffer.read(1)
if signal == _SIGNAL_ACK:
return True
elif signal == _SIGNAL_NAK:
return False
else:
raise ValueError("Expecting signal {!r} or {!r}, got {!r}".format(_SIGNAL_ACK, _SIGNAL_NAK, signal))
def _sendEndOfTransmission(self):
sys.stdout.buffer.write(_SIGNAL_EOT)
sys.stdout.buffer.flush()
def run(self):
raise NotImplementedError()
class ServeRemote(Remote):
def __init__(self, imagePath, blockSize, hashAlgorithm):
self._image = ReadableBlockImage(imagePath, blockSize, hashAlgorithm)
def run(self):
blocks = self._imageDiff(self._imageInfo, self._remoteImageInfo)
for updateBlock in blocks:
if updateBlock:
blockBytes, _ = self.image.next()
sys.stdout.buffer.write(blockBytes)
else:
self.image.skip()
sys.stdout.buffer.flush()
self._sendEndOfTransmission()
class ReceiveRemote(Remote):
def __init__(self, imagePath, blockSize, hashAlgorithm):
self._image = WritableBlockImage(imagePath, blockSize, hashAlgorithm)
def run(self):
blocks = self._imageDiff(self._remoteImageInfo, self._imageInfo)
for blockIndex, updateBlock in enumerate(blocks):
self.image.seek()
if updateBlock:
blockBytes = self._readBlock(blockIndex)
self.image.write(blockBytes)
self._sendTruncationInfo(self._imageInfo["fileSize"] > self._remoteImageInfo["fileSize"])
self.image.truncate()
self.image.flush()
self._sendEndOfTransmission()
def _readBlock(self, blockIndex):
blockSize = self._getBlockSize(blockIndex)
block = sys.stdin.buffer.read(blockSize)
if len(block) != blockSize:
raise ValueError("Receiving invalid data of block #{}, expecting {:,} bytes, got {:,} bytes".format(
blockIndex, blockSize, len(block)
))
return block
def _getBlockSize(self, blockIndex):
blockCount = -(- self._remoteImageInfo["fileSize"] // self.image.blockSize)
if blockIndex + 1 < blockCount:
return self.image.blockSize
else:
return self._remoteImageInfo["fileSize"] % self.image.blockSize or self.image.blockSize
def _sendTruncationInfo(self, truncate):
sys.stdout.buffer.write(_SIGNAL_ACK if truncate else _SIGNAL_NAK)
sys.stdout.buffer.flush()
class Local(threading.Thread):
_executable = None
_cmd = None
_pipe = None
_imagePath = None
_imageInfo = None
_blockSize = None
_hashAlgorithm = None
_hashSize = None
_hashList = None
_commonBlockCount = None
_additionalBlockCount = None
_differentBlocks = None
_sync = None
_syncBarrier = threading.Barrier(2)
_finished = False
_logStats = None
def __init__(self, imagePath, blockSize, hashAlgorithm, logStats=None):
self._blockSize = blockSize
self._hashAlgorithm = hashAlgorithm
self._hashSize = _hashSize(hashAlgorithm)
self._cmd, self._imagePath = self._parseCommand(imagePath)
self._pipe = subprocess.Popen(
self._cmd,
bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
close_fds=True
)
if self._pipe.poll() is not None:
print("Execution failed with {}".format(self._pipe.returncode))
sys.exit(1)
self._hashList = []
self._logStats = logStats
super(Local, self).__init__()
@property
def exec(self):
return self._executable
@property
def rsh(self):
return [ "ssh" ]
@property
def cmd(self):
return self._cmd
@property
def pipe(self):
return self._pipe
@property
def imagePath(self):
return self._imagePath
@property
def imageInfo(self):
return self._imageInfo
@property
def hashList(self):
return self._hashList
@property
def commonBlockCount(self):
return self._commonBlockCount
@property
def differentBlocks(self):
return self._differentBlocks
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
return self._close()
def _close(self):
if self._pipe and self._pipe.poll() is None:
self._pipe.kill()
def _parseCommand(self, imagePath):
proto = None
connection = None
if imagePath[:7] == "file://":
proto = "file"
imagePath = imagePath[7:]
elif imagePath[:6] == "ssh://":
match = re.match(r"ssh://((?:[^@:/]+@)?[^:/]+(?::\d+)?)/", imagePath)
if match is not None:
proto = "ssh"
connection = match.group(1)
pathIndex = 6 + len(connection) + 1
imagePath = imagePath[pathIndex:]
imagePath = "/" + imagePath if imagePath[:1] != "." else imagePath
else:
match = re.match(r"((?:[^@:/]+@)?[^:/]+):", imagePath)
if match is not None:
proto = "scp"
connection = match.group(1)
pathIndex = len(connection) + 1
imagePath = imagePath[pathIndex:]
cmd = None
if proto is None or proto == "file":
cmd = []
elif proto == "ssh" or proto == "scp":
cmd = self.rsh + [ connection, "--" ]
else:
raise ValueError("Invalid protocol {!r}".format(proto))
cmd += [ self.exec, "--block", str(self._blockSize), "--algo", self._hashAlgorithm, imagePath ]
return cmd, imagePath
def sendIdentifier(self):
self._pipe.stdin.write("iblocksync {}\n".format(__version__).encode("utf-8"))
self._pipe.stdin.flush()
def readImageInfo(self):
jsonString = self._pipe.stdout.readline()
if len(jsonString) == 0:
raise RemoteProcessError(self._executable, "Initialization failed")
jsonDict = json.loads(jsonString.decode("utf-8"))
if 'error' in jsonDict:
errno = jsonDict["errno"] if "errno" in jsonDict else None
raise RemoteProcessError(self._executable, jsonDict["error"], errno)
self._imageInfo = jsonDict
if self._imageInfo["blockSize"] != self._blockSize:
raise ValueError("Expecting a block size of {:,} bytes, got {:,} bytes".format(
self._blockSize, self._imageInfo["blockSize"]
))
elif self._imageInfo["hashAlgorithm"] != self._hashAlgorithm:
raise ValueError("Expecting hash algorithm {}, got {}".format(
self._hashAlgorithm, self._imageInfo["hashAlgorithm"]
))
def sendImageInfo(self, imageInfo):
self._pipe.stdin.write((json.dumps(imageInfo) + "\n").encode("utf-8"))
self._pipe.stdin.flush()
def prepare(self, sync, sourceBlockCount, targetBlockCount):
self._sync = sync
self._commonBlockCount = min(sourceBlockCount, targetBlockCount)
self._additionalBlockCount = sourceBlockCount - self._commonBlockCount
self._differentBlocks = []
def run(self):
try:
self._hashList = []
for blockIndex in range(self._commonBlockCount):
if self._logStats:
self._logStats.hitBlockHash(self._executable, blockIndex)
hash = self._readHash(blockIndex)
self._hashList.append(hash)
self._syncBarrier.wait()
for blockIndex in range(self._commonBlockCount):
if self._logStats:
self._logStats.hitBlock(self._executable, blockIndex, self._hashList[blockIndex], self._sync.hashList[blockIndex])
updateBlock = self._hashList[blockIndex] != self._sync.hashList[blockIndex]
self._pipe.stdin.write(_SIGNAL_ACK if updateBlock else _SIGNAL_NAK)
if updateBlock:
self._differentBlocks.append(blockIndex)
self._differentBlocks.extend(range(
self._commonBlockCount,
self._commonBlockCount + self._additionalBlockCount
))
self._pipe.stdin.flush()
self._finished = True
except Exception:
self._syncBarrier.abort()
raise
def finished(self):
return self._finished
def readEndOfTransmission(self):
signal = self._pipe.stdout.read(1)
if signal != _SIGNAL_EOT:
raise ValueError("Expecting EOT signal ({!r}), got {!r}".format(_SIGNAL_EOT, signal))
def _readHash(self, blockIndex):
hash = self._pipe.stdout.read(self._hashSize)
hashSize = len(hash)
if hashSize != self._hashSize:
raise ValueError("Receiving invalid hash of block #{}, expecting {:,} bytes, got {:,} bytes".format(
blockIndex, self._hashSize, hashSize
))
return hash
def wait(self):
self._pipe.wait()
if self._pipe.returncode > 0:
raise RemoteProcessError(
self._executable,
"Proccess suddenly died with status {}".format(self._pipe.returncode)
)
class ServeLocal(Local):
_executable = "iblocksync-serve"
@property
def exec(self):
return os.getenv("IBLOCKSYNC_SOURCE_EXEC", super(ServeLocal, self).exec)
@property
def rsh(self):
if "IBLOCKSYNC_SOURCE_RSH" in os.environ:
return shlex.split(os.environ["IBLOCKSYNC_SOURCE_RSH"])
return super(ServeLocal, self).rsh
def readBlock(self, blockIndex):
blockSize = self._getBlockSize(blockIndex)
block = self._pipe.stdout.read(blockSize)
if len(block) != blockSize:
raise ValueError("Receiving invalid data of block #{}, expecting {:,} bytes, got {:,} bytes".format(
blockIndex, blockSize, len(block)
))
return block
def _getBlockSize(self, blockIndex):
blockCount = -(- self._imageInfo["fileSize"] // self._blockSize)
if blockIndex + 1 < blockCount:
return self._blockSize
else:
return self._imageInfo["fileSize"] % self._blockSize or self._blockSize
class ReceiveLocal(Local):
_executable = "iblocksync-receive"
@property
def exec(self):
return os.getenv("IBLOCKSYNC_TARGET_EXEC", super(ReceiveLocal, self).exec)
@property
def rsh(self):
if "IBLOCKSYNC_TARGET_RSH" in os.environ:
return shlex.split(os.environ["IBLOCKSYNC_TARGET_RSH"])
return super(ReceiveLocal, self).rsh
def sendBlock(self, blockIndex, block):
self._pipe.stdin.write(block)
self._pipe.stdin.flush()
def readTruncationInfo(self):
signal = self._pipe.stdout.read(1)
if signal == _SIGNAL_ACK:
return True
elif signal == _SIGNAL_NAK:
return False
else:
raise ValueError("Expecting signal {!r} or {!r}, got {!r}".format(_SIGNAL_ACK, _SIGNAL_NAK, signal))
class LogStats(threading.Thread):
_STATE_IDLE = 0
_STATE_SYNCING = 1
_STATE_UPDATING = 2
_STATE_EXCEPTION = 3
_STATE_STOP = 4
_logger = None
_progress = None
_progressLength = 0
_server = None
_receiver = None
_blocks = None
_updatedBlocks = None
_avgSyncTime = None
_avgUpdateTime = None
_threadState = _STATE_IDLE
_threadEvent = threading.Event()
def __init__(self, verbosity, progress, loggerName="iblocksync"):
logHandler = logging.StreamHandler(stream=sys.stderr)
logHandler.setFormatter(logging.Formatter("%(asctime)s.%(msecs)03d: %(levelname)s: %(message)s", "%Y-%m-%d %H:%M:%S"))
self._logger = logging.getLogger(loggerName)
self._logger.addHandler(logHandler)
self._logger.setLevel(max(logging.DEBUG, logging.WARNING - verbosity * 10))
self._progress = progress if progress != "none" else None
self._blocks = { "source": [], "target": [] }
self._updatedBlocks = collections.OrderedDict()
super(LogStats, self).__init__()
def __enter__(self):
self.start()
return self
def __exit__(self, exc_type, exc_value, traceback):
return self.stop()
@property
def logger(self):
return self._logger
def run(self):
try:
while True:
self._threadEvent.wait()
if self._threadState == LogStats._STATE_STOP:
break
elif self._threadState == LogStats._STATE_IDLE:
self._threadEvent.clear()
elif self._threadState == LogStats._STATE_SYNCING:
self._progressSync()
time.sleep(0.5)
elif self._threadState == LogStats._STATE_UPDATING:
self._progressUpdate()
time.sleep(0.5)
else:
raise ValueError("Invalid state {!r}".format(self._threadState))
except:
self._threadState = LogStats._STATE_EXCEPTION
raise
def _set(self, state):
self._threadState = state
self._threadEvent.set()
def stop(self):
self._set(LogStats._STATE_STOP)
def init(self, source, target, blockSize, hashAlgorithm):
self._logger.info("Syncing from {!r} to {!r}".format(source, target))
self._logger.info("Using a block size of {:,} bytes ({}) and {} hashes".format(
blockSize,
_formatBytes(blockSize),
hashAlgorithm
))
def hitServer(self, server):
self._server = server
self._logger.info("Source: {!r}, {}{:,} bytes".format(
self._server.imageInfo["filePath"],
"UUID {!r}, ".format(self._server.imageInfo["uuid"]) if self._server.imageInfo["uuid"] != "" else "",
self._server.imageInfo["fileSize"]
))
def hitReceiver(self, receiver):
self._receiver = receiver
self._logger.info("Target: {!r}, {}{:,} bytes".format(
self._receiver.imageInfo["filePath"],
"UUID {!r}, ".format(self._receiver.imageInfo["uuid"]) if self._receiver.imageInfo["uuid"] != "" else "",
self._receiver.imageInfo["fileSize"]
))
if self._server.imageInfo["fileSize"] > 0 and self._receiver.imageInfo["fileSize"] > 0:
if self._server.imageInfo["fileSize"] != self._receiver.imageInfo["fileSize"]:
self._logger.warning("Source and target image file sizes don't match")
if self._server.imageInfo["uuid"] and self._receiver.imageInfo["uuid"]:
if self._server.imageInfo["uuid"] != self._receiver.imageInfo["uuid"]:
self._logger.warning("Source and target UUID don't match")
def startSync(self):
message = "No blocks to sync"
if self._server.commonBlockCount > 0:
self._set(LogStats._STATE_SYNCING)
message = "Start syncing {:,} of {:,} blocks...".format(
self._server.commonBlockCount,
-(- self._server.imageInfo["fileSize"] // self._server.imageInfo["blockSize"]),
)
self._logger.info(message)
if self._progress is not None and self._logger.getEffectiveLevel() > logging.INFO:
print(message)
def hitBlockHash(self, remote, blockIndex):
assert remote in ( "iblocksync-serve", "iblocksync-receive" )
remote = "source" if remote == "iblocksync-serve" else "target"
assert blockIndex == len(self._blocks[remote])
self._blocks[remote].append(time.time())
if self._progress == "dot" and self._logger.getEffectiveLevel() > logging.DEBUG:
sys.stdout.write(".")
sys.stdout.flush()
self._logger.debug("Receiving hash of {} block #{}...".format(remote, blockIndex))
def hitBlock(self, remote, blockIndex, sourceHash, targetHash):
self._logger.debug("Comparing hash of {} block #{} ({!r} vs. {!r})...".format(
"source" if remote == "iblocksync-serve" else "target", blockIndex, sourceHash, targetHash
))
def finishSync(self):
if self._server.commonBlockCount > 0:
self._progressSync()
self._finishProgress()
serverAverage, serverStartTime, serverFinishTime = self._avgBlocks(self._blocks["source"], None, None)
serverMessage = "Processed {:,} source blocks in {:,} seconds ({:.2f} blocks/s)".format(
len(self._blocks["source"]),
round(serverFinishTime - serverStartTime),
serverAverage
)
receiverAverage, receiverStartTime, receiverFinishTime = self._avgBlocks(self._blocks["target"], None, None)
receiverMessage = "Processed {:,} target blocks in {:,} seconds ({:.2f} blocks/s)".format(
len(self._blocks["target"]),
round(receiverFinishTime - receiverStartTime),
receiverAverage
)
self._logger.info(serverMessage)
self._logger.info(receiverMessage)
if self._progress is not None and self._logger.getEffectiveLevel() > logging.INFO:
print(serverMessage)
print(receiverMessage)
self._logger.info("Finished syncing blocks")
def startUpdate(self):
message = "No blocks to transmit"
if len(self._server.differentBlocks) > 0:
self._set(LogStats._STATE_UPDATING)
message = "Start transmitting {:,} of {:,} blocks ({})...".format(
len(self._server.differentBlocks),
-(- self._server.imageInfo["fileSize"] // self._server.imageInfo["blockSize"]),
_formatBytes(len(self._server.differentBlocks) * self._server.imageInfo["blockSize"])
)
self._logger.info(message)
if self._progress is not None and self._logger.getEffectiveLevel() > logging.INFO:
print(message)
def startBlockUpdate(self, blockIndex):
self._updatedBlocks[blockIndex] = [ time.time(), None ]
self._logger.debug("Sending block #{}...".format(blockIndex))
def endBlockUpdate(self, blockIndex):
self._updatedBlocks[blockIndex][1] = time.time()
if self._progress == "dot" and self._logger.getEffectiveLevel() > logging.DEBUG:
sys.stdout.write(".")
sys.stdout.flush()
self._logger.debug("Block #{} sent".format(blockIndex))
def truncateImage(self):
self._logger.info("Truncating target image...")
def finishUpdate(self):
if len(self._server.differentBlocks) > 0:
self._progressUpdate()
self._finishProgress()
average, startTime, finishTime = self._avgUpdates(self._server.imageInfo["blockSize"], None, None)
message = "Transmitted {:,} blocks ({}) in {:,} seconds ({}/s)".format(
len(self._updatedBlocks),
_formatBytes(len(self._updatedBlocks) * self._server.imageInfo["blockSize"]),
round(finishTime - startTime),
_formatBytes(average)
)
self._logger.info(message)
if self._progress is not None and self._logger.getEffectiveLevel() > logging.INFO:
print(message)
self._logger.info("Finished transmitting blocks")
def _printProgress(self, progressLine):
self._progressLength = max(self._progressLength, len(progressLine))
sys.stdout.write(progressLine.ljust(self._progressLength) + "\r")
sys.stdout.flush()
def _finishProgress(self):
self._set(LogStats._STATE_IDLE)
self._progressLength = 0
if self._progress is not None and self._logger.getEffectiveLevel() > logging.DEBUG:
sys.stdout.write("\n")
sys.stdout.flush()
def _progressSync(self):
if self._progress == "full" and self._logger.getEffectiveLevel() > logging.DEBUG:
self._printProgress("{:,} / {:,} of {:,} blocks processed; {:.2f} blocks/s".format(
len(self._blocks["source"]),
len(self._blocks["target"]),
self._server.commonBlockCount,
self._avgBlocks(itertools.chain(self._blocks["source"], self._blocks["target"]))[0]
))
def _progressUpdate(self):
if self._progress == "full" and self._logger.getEffectiveLevel() > logging.DEBUG:
self._printProgress("{:,} of {:,} blocks transmitted; {}/s".format(
len(self._updatedBlocks),
len(self._server.differentBlocks),
_formatBytes(self._avgUpdates(self._server.imageInfo["blockSize"])[0])
))