90 points · by SXX · 10y ago · 44 comments
- Files are indexed by inode and device, files with the same inode + device are considered equal. (My main use case for this was to bundle up JS files.)
- Files are then indexed by size; only files with the same size are compared.
- During comparison, the files are read at block sizes increasing in powers of two, starting with 2k. The blocks are hashed and compared, and if they do not match the comparison is stopped early (often without having to read the full file). If all the hashes are equal, then the files are considered to be equal.
- Hashes are only computed when needed and cached in memory. Since the hash block size increases in powers of two, only a few dozen hashes are needed even for large files (reducing memory usage compared to a fixed hash block size).
To find out how many files of each size you have:
find ~ -type f -printf '%s\n' | sort | uniq -c | sort -n
The core of the duplicate detection is the shell script here:
https://github.com/pixelb/fslint/blob/ffcd3b85/fslint/findup...
(meanwhile there's stuff in the wild using things like Java hashCode() to determine identities, and the birthday point on that is only around 75k IIRC)
If you crunch your way through files (and lets assume they're random from the hash functions point of view) for 10 years, you have to calculate the hash of 60.000.000.000 files / second - to have 50% chance of finding a collision. If the average filesize is 1 KB, that is ~56TB data every single second.
Md5 is not bad as long as you're not expecting malicious input, or gigantic datasets.
Makes me wonder now. Might've been a rogue rsync at the time or something, then. /shrug.
https://s3-eu-west-1.amazonaws.com/md5collisions/a.php https://s3-eu-west-1.amazonaws.com/md5collisions/b.php
Obviously a contrived example, but given the above and other weaknesses md5 should not be considered safe for verifying file contents (at least from a security perspective).
... but accidental collisions? That's still hugely more likely to be "you're misremembering" or "you misread" than a real collision.
But I do believe you thought it did, if that's any consolation ;)
... but I would loved to be proved wrong.
A 64 bit checksum + 64 bit filesize or a 128 bit checksum?
https://github.com/rburchell/fastdup
:-)
Size is a fast initial differentiator because scanning for file size only needs to read metadata, but how good does it differentiate?
# all files
find ~ -type f | wc -l
1098045
# files with a unique size
find ~ -type f -printf '%s\n'|sort|uniq -c|grep ' 1 '|wc -l
48437
# most prevalent sizes (prevalence vs size)
find ~ -type f -printf '%s\n'|sort |uniq -c |sort -nr|head
36972 0
12707 24
9480 22
8036 39
5025 12
4804 26
3879 6
3232 76
2946 38
2855 20
In my home dir, less than 5% of files have a unique size.http://perlmonks.org/?node_id=1067570
discussion at http://mail.pm.org/pipermail/dfw-pm/2013-December/thread.htm...
=> http://dfw.pm.org/winners.html
it is really not easy. I used as only one bloom hashes, which explains why mine was fastest and smallest, but unfortunately not correct.
one way to mitigate that might be take a _few_ random, say, 1k chunks of file content, and hash those. as long as you have a map of (offset, hash) de-duping should be just fine.
the only thing is, i don't really know, what an upper bound on 'few samples' would be like. asymptotically, it should approach file-size/chunk-size, but ideally, it should be way less than that.
any ideas on how to determine a minimal but complete set ?
Consider the case of a log file and its backup made one short log message earlier.
If you want to gamble on using only a partial hash, I would hash the last few kb of each file, as that (at least based on this 30-second 'analysis' of the problem domain) likely would work better in practice. Theoretically, it is as vulnerable, though.
Edit: if you have a VM image on your disk, and that small append happens to a log file in the image, that small change can happen anywhere. It also can be even smaller; someone might run 'touch' on an already existing file. Theoretically, that could be a one-byte change.
1. take a sha1 hash of the last 4KB of each file.
2. for any equal hashes, compare the whole file.
With this method you should be able to skip reading many large files in their entirety.
As an optimization, you might want to check for files with holes in them ("sparse" files). Not sure how to do that, though.