blob: 1146a25dc70175589f77a736af7ad93363b1b9aa [file] [log] [blame]
Guido van Rossum4acc25b2000-02-02 15:10:15 +00001"""Efficiently compare files, boolean outcome only (equal / not equal).
Guido van Rossumc6360141990-10-13 19:23:40 +00002
Guido van Rossum4acc25b2000-02-02 15:10:15 +00003Tricks (used in this order):
4 - Files with identical type, size & mtime are assumed to be clones
5 - Files with different type or size cannot be identical
6 - We keep a cache of outcomes of earlier comparisons
7 - We don't fork a process to run 'cmp' but read the files ourselves
8"""
Guido van Rossumc6360141990-10-13 19:23:40 +00009
Guido van Rossum25d7caf1992-03-31 19:04:48 +000010import os
Guido van Rossumc6360141990-10-13 19:23:40 +000011
12cache = {}
13
Guido van Rossum4acc25b2000-02-02 15:10:15 +000014def cmp(f1, f2, shallow=1):
15 """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))
Fred Drake132dce22000-12-12 23:11:42 +000019 if s1[0] != 8 or s2[0] != 8:
Guido van Rossum4acc25b2000-02-02 15:10:15 +000020 # Either is a not a plain file -- always report as different
21 return 0
22 if shallow and s1 == s2:
23 # type, size & mtime match -- report same
24 return 1
Fred Drake132dce22000-12-12 23:11:42 +000025 if s1[:2] != s2[:2]: # Types or sizes differ, don't bother
Guido van Rossum4acc25b2000-02-02 15:10:15 +000026 # types or sizes differ -- report different
27 return 0
28 # same type and size -- look in the cache
29 key = (f1, f2)
30 try:
31 cs1, cs2, outcome = cache[key]
32 # cache hit
33 if s1 == cs1 and s2 == cs2:
34 # cached signatures match
35 return outcome
36 # stale cached signature(s)
37 except KeyError:
38 # cache miss
39 pass
40 # really compare
41 outcome = do_cmp(f1, f2)
42 cache[key] = s1, s2, outcome
43 return outcome
Guido van Rossumc6360141990-10-13 19:23:40 +000044
Guido van Rossum4acc25b2000-02-02 15:10:15 +000045def sig(st):
46 """Return signature (i.e., type, size, mtime) from raw stat data
47 0-5: st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid
48 6-9: st_size, st_atime, st_mtime, st_ctime"""
49 type = st[0] / 4096
50 size = st[6]
51 mtime = st[8]
52 return type, size, mtime
Guido van Rossumc6360141990-10-13 19:23:40 +000053
Guido van Rossum4acc25b2000-02-02 15:10:15 +000054def do_cmp(f1, f2):
55 """Compare two files, really."""
56 bufsize = 8*1024 # Could be tuned
57 fp1 = open(f1, 'rb')
58 fp2 = open(f2, 'rb')
59 while 1:
60 b1 = fp1.read(bufsize)
61 b2 = fp2.read(bufsize)
Fred Drake132dce22000-12-12 23:11:42 +000062 if b1 != b2: return 0
Guido van Rossum4acc25b2000-02-02 15:10:15 +000063 if not b1: return 1