Skip to content

Commit

Permalink
Make zip manipulation thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 committed Sep 28, 2022
1 parent 3a81f96 commit 4208268
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/cpp/helpers/ArchiveIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ class ArchiveIOSpecificException : public std::runtime_error {
std::to_string(expectedStatus) + " expected)") {}
};
#include <filesystem>
#include <shared_mutex>
class ArchiveIO {
private:
std::filesystem::path archivePath_;

protected:
mutable std::shared_mutex mutex_;

virtual void Create() = 0;

public:
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/helpers/ArchiveReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ int32_t ArchiveReader::ExtractFile(
int32_t ArchiveReader::ExtractFile(
const std::filesystem::path& fileToExtractPath,
const std::filesystem::path& destination) {
std::unique_lock lock(mutex_);
int32_t err = MZ_OK;
LocateEntry(fileToExtractPath);
OpenEntry(fileToExtractPath);
Expand Down Expand Up @@ -74,6 +75,7 @@ void ArchiveReader::OpenEntry(const std::filesystem::path& fileToExtractPath) {
}
std::istringstream ArchiveReader::ExtractFileInStringStream(
const std::filesystem::path& FileToExtractPath) {
std::unique_lock lock(mutex_);
LocateEntry(FileToExtractPath);
OpenEntry(FileToExtractPath);
int32_t len = mz_zip_reader_entry_save_buffer_length(internalPointer_);
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/helpers/ArchiveWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ int32_t ArchiveWriter::Close() { return mz_zip_writer_close(internalPointer_); }
void ArchiveWriter::Delete() { mz_zip_writer_delete(&internalPointer_); }

int32_t ArchiveWriter::AddFileInArchive(const FileBuffer& FileBufferToAdd) {
std::unique_lock lock(mutex_);
fileInfo_.filename = FileBufferToAdd.fname.c_str();
fileInfo_.creation_date = std::time(0);
int32_t err = mz_zip_writer_entry_open(internalPointer_, &fileInfo_);
Expand Down Expand Up @@ -75,6 +76,7 @@ int32_t ArchiveWriter::AddFileInArchive(const FileBuffer& FileBufferToAdd) {
}
int32_t ArchiveWriter::AddFileInArchive(
const std::filesystem::path& FileToAdd) {
std::unique_lock lock(mutex_);
auto err =
mz_zip_writer_add_file(internalPointer_, FileToAdd.string().c_str(),
FileToAdd.filename().string().c_str());
Expand Down
6 changes: 3 additions & 3 deletions src/cpp/lpnamer/problem_modifier/LinkProblemsGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ void LinkProblemsGenerator::treatloop(const std::filesystem::path &root,
auto writer = ArchiveWriter(tmpArchivePath);
writer.Open();
auto mpsList = readMPSList(mps_file_name);
std::for_each(std::execution::seq, mpsList.begin(), mpsList.end(), [&](const auto& mps) {
treat(root, mps, couplings, reader, writer);
});
std::for_each(
std::execution::par, mpsList.begin(), mpsList.end(),
[&](const auto &mps) { treat(root, mps, couplings, reader, writer); });
reader.Close();
reader.Delete();
writer.Close();
Expand Down

0 comments on commit 4208268

Please sign in to comment.