"""Efficiently compare files, boolean outcome only (equal / not equal).
Tricks (used in this order):
- Use the statcache module to avoid statting files more than once
- Files with identical type, size & mtime are assumed to be clones
- Files with different type or size cannot be identical
- We keep a cache of outcomes of earlier comparisons
- We don't fork a process to run 'cmp' but read the files ourselves
def cmp(f1
, f2
, shallow
=1):
"""Compare two files, use the cache if possible.
May raise os.error if a stat or open of either fails.
Return 1 for identical files, 0 for different.
Raise exceptions if either file could not be statted, read, etc."""
s1
, s2
= sig(statcache
.stat(f1
)), sig(statcache
.stat(f2
))
if not S_ISREG(s1
[0]) or not S_ISREG(s2
[0]):
# Either is a not a plain file -- always report as different
# type, size & mtime match -- report same
if s1
[:2] != s2
[:2]: # Types or sizes differ, don't bother
# types or sizes differ -- report different
# same type and size -- look in the cache
cs1
, cs2
, outcome
= cache
[key
]
if s1
== cs1
and s2
== cs2
:
# cached signatures match
# stale cached signature(s)
cache
[key
] = s1
, s2
, outcome
"""Return signature (i.e., type, size, mtime) from raw stat data."""
return S_IFMT(st
[ST_MODE
]), st
[ST_SIZE
], st
[ST_MTIME
]
"""Compare two files, really."""
#print ' cmp', f1, f2 # XXX remove when debugged
bufsize
= 8*1024 # Could be tuned