79540840

Date: 2025-03-28 08:09:34
Score: 0.5
Natty:
Report link

I decided to go with combination of 1. and 5., plus file size check suggested by @Osyotr. User can select between "quick" and "strict" mode, plus command to invalidate cache.

  1. Store cache files with filename original.filesize + '-' + encode(original.fullname), where encode escapes any non-alphanumeric characters to ensure valid filename. Storing this info in filename allows quick mode to determine cache validity without reading either file.
  2. In either mode, check if cache file with corresponding name exists, implicitly checking file size matches expected.
  3. If it does, check cache file has modified date newer than original file.
  4. In strict mode only, calculate hash of original and compare it to hash stored in first couple bytes of cache file.
  5. If all checks pass, use cache file contents.
  6. Delete all cache files matching filename but not size.

With the example program, here are some test cases and how they are handled in the two modes:

  1. echo a > test.txt - new file, no cache, both work
  2. echo b > test.txt - modifies last write time, both work
  3. echo abc > test2.txt && mv test2.txt test.txt - last write time unmodified but changes size, both work
  4. echo xyz > test2.txt && mv test2.txt test.txt - last write time and size unmodified, quick mode fails but strict mode works
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Osyotr
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Dominik Kaszewski