"Fossies" - the Fresh Open Source Software Archive

Member "fslint-2.46/fslint/supprt/md5sum_approx" (2 Feb 2017, 1662 Bytes) of package /linux/privat/fslint-2.46.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "md5sum_approx": 2.44_vs_2.46.

    1 #!/usr/bin/env python2
    2 
    3 # md5sum CHUNK_SIZE bytes of file
    4 
    5 CHUNK_SIZE=512 #4096 for full file
    6 
    7 # Note this takes 60% more CPU than md5sum on FC4 at least,
    8 # when checksumming the whole file.
    9 # On F7 I retested it takes around 20% more CPU.
   10 
   11 #TODO: see how kernel readahead affects this
   12 
   13 import os,sys,errno
   14 
   15 try:
   16     # md5 module is deprecated on python 2.6
   17     # so try the newer hashlib first
   18     import hashlib
   19     md5_new = hashlib.md5
   20 except ImportError:
   21     import md5
   22     md5_new = md5.new
   23 
   24 def md5sum(filename, CHUNK=CHUNK_SIZE):
   25     """takes filename, hand back Checksum of it"""
   26 
   27     try:
   28         fo = open(filename, 'r', CHUNK)
   29 
   30         sum = md5_new()
   31 
   32         # To sum the whole file do:
   33         #chunk = fo.read
   34         #while chunk:
   35         #    chunk = fo.read(CHUNK)
   36         #    sum.update(chunk) #update with nothing does not change sum
   37 
   38 
   39         # Note if seek past end of file then read() returns ''
   40         # Consequently the md5 will be the same as if one did:
   41         # md5sum /dev/null
   42         #fo.seek(1024)
   43         chunk=fo.read(CHUNK)
   44         sum.update(chunk)
   45 
   46         fo.close()
   47         del fo
   48 
   49         return sum.hexdigest()
   50     except (IOError, OSError), value:
   51         # One gets the following if you do
   52         # fo.seek(-CHUNK, 2) above and file is too small
   53         if value.errno == errno.EINVAL:
   54             return "d41d8cd98f00b204e9800998ecf8427e"
   55         else:
   56             raise
   57 
   58 def printsum(filename):
   59     try:
   60         sys.stdout.write("%s  %s\n" % (md5sum(filename),filename))
   61     except (IOError, OSError), value:
   62         sys.stderr.write("md5sum: %s: %s\n" % (filename, value.strerror))
   63 
   64 map(printsum, sys.argv[1:])