File tree 2 files changed +17
-8
lines changed 2 files changed +17
-8
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import hashlib
4
4
5
- def sha256_for_path (fp , blocksize = (4 * 1024 * 1024 )):
5
+ def sha256_for_file (fp , blocksize = (4 * 1024 * 1024 )):
6
6
hasher = hashlib .sha256 ()
7
7
buf = fp .read (blocksize )
8
- # while len(buf) > 0:
9
- assert len (buf ) < blocksize
10
- hasher .update (buf )
11
- # buf = fp.read(blocksize)
8
+ while len (buf ) > 0 :
9
+ hasher .update (buf )
10
+ buf = fp .read (blocksize )
12
11
return hasher .hexdigest ()
13
12
14
13
def get_dir_state (dirpath ):
15
14
ds = {}
16
15
for path in dirpath .iterdir ():
17
16
with open (path , 'rb' ) as fp :
18
- ds [path .name ] = { 'sha256' : sha256_for_path (fp ) }
17
+ ds [path .name ] = { 'sha256' : sha256_for_file (fp ) }
19
18
return ds
Original file line number Diff line number Diff line change 2
2
import tempfile
3
3
from pathlib import Path
4
4
5
-
6
-
5
+ def test_sha_for_file ():
6
+ # $ sha256sum COPYING
7
+ # 8ceb4b9ee5adedde47b31e975c1d90c73ad27b6b165a1dcd80c7c545eb65b903 COPYING
8
+ sha = '8ceb4b9ee5adedde47b31e975c1d90c73ad27b6b165a1dcd80c7c545eb65b903'
9
+ blocksize_4k = (4 * 1024 )
10
+ with open ('./COPYING' , 'rb' ) as fp :
11
+ class WrappedFp :
12
+ def read (self , blocksize ):
13
+ assert blocksize <= blocksize_4k
14
+ return fp .read (blocksize_4k );
15
+ h = meshfssync .sha256_for_file (WrappedFp (), blocksize_4k )
16
+ assert h == sha
7
17
8
18
def test_get_dir_state ():
9
19
# $ echo 'foo' > foo.txt
You can’t perform that action at this time.
0 commit comments