Skip to content

Commit

Permalink
Sort files in ckmamedb when torrentzipping.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Aug 23, 2023
1 parent 5ee6647 commit 6726e99
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
1 change: 0 additions & 1 deletion regress/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ set(XFAIL_TESTS
file-no-crc-wrong-name-other-set.test
rom-from-extra-loose-directory.test
rom-from-extra-loose-toplevel.test
torrentzip-cache.test
unknown-rename-failed.test
)

Expand Down
52 changes: 43 additions & 9 deletions src/ArchiveZip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ bool ArchiveZip::ensure_zip() {

if (where == FILE_ROMSET && configuration.use_torrentzip) {
if (zip_get_archive_flag(za, ZIP_AFL_IS_TORRENTZIP, ZIP_FL_UNCHANGED) == 0) {
cache_changed = true;
modified = true;
}
if (zip_set_archive_flag(za, ZIP_AFL_WANT_TORRENTZIP, 1) < 0) {
output.error("can't torrentzip '%s'", name.c_str());
Expand Down Expand Up @@ -198,16 +198,50 @@ void ArchiveZip::commit_cleanup() {

ensure_zip();

for (uint64_t i = 0; i < files.size(); i++) {
struct zip_stat st;
if (zip_get_archive_flag(za, ZIP_AFL_IS_TORRENTZIP, 0) == 1) {
auto names = std::unordered_map<std::string, size_t>{};
auto sorted_files = std::vector<File>{};

if (zip_stat_index(za, i, 0, &st) < 0) {
output.set_error_archive(name);
output.archive_error("cannot stat file %" PRIu64 ": %s", i, zip_strerror(za));
continue;
}
auto index = size_t{0};
for (const auto& file: files) {
names[file.name] = index;
index += 1;
}

for (uint64_t i = 0; i < files.size(); i++) {
struct zip_stat st;

if (zip_stat_index(za, i, 0, &st) < 0) {
output.set_error_archive(name);
output.archive_error("cannot stat file %" PRIu64 ": %s", i, zip_strerror(za));
continue;
}

auto it = names.find(st.name);
if (it == names.end()) {
output.set_error_archive(name);
output.archive_error("unexpected name %s in archive", st.name);
continue;
}
sorted_files.push_back(files[names[st.name]]);

sorted_files[i].mtime = st.mtime;
}

files[i].mtime = st.mtime;
files = sorted_files;
}
else {
for (uint64_t i = 0; i < files.size(); i++) {
struct zip_stat st;

if (zip_stat_index(za, i, 0, &st) < 0) {
output.set_error_archive(name);
output.archive_error("cannot stat file %" PRIu64 ": %s", i, zip_strerror(za));
continue;
}

files[i].mtime = st.mtime;
}
}
}

Expand Down

0 comments on commit 6726e99

Please sign in to comment.