Skip to content

Commit

Permalink
implement checksum in terms of reducefileobj.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewguy9 committed Oct 2, 2023
1 parent 5ee7655 commit 483c0ac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
13 changes: 4 additions & 9 deletions farmfs/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from os.path import splitext
from fnmatch import fnmatchcase
from functools import total_ordering
from farmfs.util import ingest, safetype, uncurry, first, second, ffilter, copyfileobj
from farmfs.util import ingest, safetype, uncurry, first, second, ffilter, copyfileobj, reducefileobj
from future.utils import python_2_unicode_compatible
from safeoutput import open as safeopen
from safeoutput import _sameDir as sameDir
Expand Down Expand Up @@ -349,15 +349,10 @@ def checksum(self):
If self points to a missing file or a broken symlink, raises FileDoesNotExist.
If self points to a directory or a symlink facing directory, raises IsADirectory.
"""
hasher = md5()
with self.open('rb') as fd:
buf = fd.read(_BLOCKSIZE)
while len(buf) > 0:
# TODO Could cancel work here.
hasher.update(buf)
buf = fd.read(_BLOCKSIZE)
digest = safetype(hasher.hexdigest())
return digest
hash = reducefileobj(lambda hasher, buf: hasher.update(buf) or hasher, fd, md5(), _BLOCKSIZE)
digest = safetype(hash.hexdigest())
return digest

def __cmp__(self, other):
return (self > other) - (self < other)
Expand Down
11 changes: 11 additions & 0 deletions farmfs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,14 @@ def copyfileobj(fsrc, fdst, length=16 * 1024):
break
fdst.write(buf)

def reducefileobj(function, fsrc, initial=None, length=16 * 1024):
if initial is None:
acc = fsrc.read(length)
else:
acc = initial
while 1:
buf = fsrc.read(length)
if not buf:
break
acc = function(acc, buf)
return acc

0 comments on commit 483c0ac

Please sign in to comment.