Check (attributes) file when using --update flag#177
Conversation
ebd0d0b to
eb30dad
Compare
3c3859e to
9b8db3c
Compare
|
It seems to me that MD5 is the most reliable metric but also the most expensive. CRC32 seems a little less reliable but a bit faster than MD5. Timestamps seem reliable for the matching case, i.e. if the timestamps and the file sizes match, it ought to be a strong sign of equality, but as #174 shows, timestamps that don't match don't need to mean that the files are not equal. I'm thinking that we should thus start by comparing file sizes + timestamps. If the timestamps don't match, we look at MD5 if present, then resort to CRC32. So I'm proposing something like this: flowchart TD
A[Start] --> FS{File size equals}
FS -->|No| U[Update file]
FS -->|Yes| TE{Timestamps exist and equals} -->|Yes| DU
TE -->|No| MX
DU[Don't update file]
MX{MD5 exists} -->|Yes| FB
MX -->|No| CX
FB{MD5 equals} -->|Yes| DU
FB -->|No| U
CX{CRC32 exists} -->|Yes| CE
CX -->|No| U
CE{CRC32 equals} -->|Yes| DU
CE -->|No| U
I'll push a new commit with that implementation. |
|
I made a benchmark (code). The baseline is when we don't pass in
500 files · 512 KB each · 100 runs So this implementation offers a clear speedup compared to dropping the |
The
--updateflag for theaddsubcommand intends to only add the files if the local file is deemed "updated" compared to the file in the archive. A file is currently considered "updated" when the file sizes aren't equal.However, as issue #174 points out, files can be updated yet still have the same file size. This is not uncommon - some of the files that mods for StarCraft I and WarCraft II change most often always have fixed sizes.
While not mandatory, some MPQ archives contains an
(attributes)file, which can contain MD5 sums, CRC32 checksums and timestamps of the files inside the archives. I believe that starcraft1, warcraft2, diablo1, diablo2 and lordsofmagic does not contain(attributes)but that newer games do.This PR will check the file sizes, and if they are the same it proceeds to check against the values of
(attributes)if present.It compares the MD5 if present, if not it checks the CRC32 if present, if not it checks the timestamps if present.It compares the timestamps if present, if not or if not matching, it checks the MD5 if present, if not it checks the CRC32 if present. So if file sizes and timestamps, MD5 or CRC32 are matching, the file is not considered "updated"; if file sizes, MD5 or CRC32 are not matching, the file is considered "updated" (but timestamps are allowed to be different).If
(attributes)is not present in the archive (which again, it would not be for starcraft1, warcraft2, diablo1, diablo2 and lordsofmagic), passing in--updatewill basically do nothing with this PR.The code for CRC32 and MD5 was written by AI and is by its nature quite messy to read. We need to ask ourselves if this is complexity we want in mpqcli. I myself am somewhat leaning towards answering No to that question - and I'd then say that the
--updateflag should be removed altogether. In my opinion, it is not unreasonable to delegate the decision of whether to add a file to the archive to the user itself.