35/40
Part 7 of 7 · Follow-upsLearn and do

How would you catch a duplicate as it's written?

Keep the index, digest to path, in a store that lasts, instead of rebuilding it each night. Hash each file once it's fully written, and look the digest up. A digest already in the index is a copy of the stored path. A new one goes in.

Linux tells you when a file open for writing is closed (inotify's IN_CLOSE_WRITE). On macOS, FSEvents only says that a file changed, so hash it once it stops changing.

Content-addressed storage goes all the way: it stores each piece of content once, under its digest.

In the editor, DupIndex is that index: by_digest maps each digest to the first path stored with it. Finish on_close(path, data), which runs when a file is closed after a write. Return the stored path when the data's SHA-256 digest is already there, and otherwise store the path and return None.