3 # Efficiently compare files, boolean outcome only (equal / not equal).
5 # Tricks (used in this order):
6 # - Files with identical type, size & mtime are assumed to be clones
7 # - Files with different type or size cannot be identical
8 # - We keep a cache of outcomes of earlier comparisons
9 # - We don't fork a process to run 'cmp' but read the files ourselves
15 def cmp(f1
, f2
): # Compare two files, use the cache if possible.
16 # Return 1 for identical files, 0 for different.
17 # Raise exceptions if either file could not be statted, read, etc.
18 s1
, s2
= sig(os
.stat(f1
)), sig(os
.stat(f2
))
19 if s1
[0] <> 8 or s2
[0] <> 8:
20 # Either is a not a plain file -- always report as different
23 # type, size & mtime match -- report same
25 if s1
[:2] <> s2
[:2]: # Types or sizes differ, don't bother
26 # types or sizes differ -- report different
28 # same type and size -- look in the cache
31 cs1
, cs2
, outcome
= cache
[key
]
33 if s1
== cs1
and s2
== cs2
:
34 # cached signatures match
36 # stale cached signature(s)
41 outcome
= do_cmp(f1
, f2
)
42 cache
[key
] = s1
, s2
, outcome
45 def sig(st
): # Return signature (i.e., type, size, mtime) from raw stat data
46 # 0-5: st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid
47 # 6-9: st_size, st_atime, st_mtime, st_ctime
51 return type, size
, mtime
53 def do_cmp(f1
, f2
): # Compare two files, really
54 bufsize
= 8*1024 # Could be tuned
58 b1
= fp1
.read(bufsize
)
59 b2
= fp2
.read(bufsize
)