Skip to content

Commit

Permalink
Prevent concurrent access during close and delete of archive (#917)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 authored Sep 4, 2024
1 parent aa5619f commit 7b8a284
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/cpp/helpers/ArchiveWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <time.h>

#include <iostream>
#include <mutex>
#include <sstream>

#include "LogUtils.h"
Expand All @@ -26,12 +27,13 @@ void ArchiveWriter::InitFileInfo() {
}
int32_t ArchiveWriter::Open() {
// disk-spanning is disabled, meaning that only one file is created
std::unique_lock lock(mutex_);
const auto err = mz_zip_writer_open_file(
pmz_zip_writer_instance_, ArchivePath().string().c_str(),
0 /* disk-spanning disabled */, 1 /* append */);
if (err != MZ_OK) {
Close();
Delete();
CloseUnsafe();
DeleteUnsafe();
std::stringstream errMsg;
errMsg << "Open Archive: " << ArchivePath().string() << std::endl;
throw ArchiveIOGeneralException(err, errMsg.str(), LOGLOCATION);
Expand Down Expand Up @@ -125,6 +127,33 @@ int32_t ArchiveWriter::CloseInternal() {
return MZ_OK;
}

/**
* @brief Close the archive
* Not thread safe
* Meant to be used with DeleteUnsafe and both of them guarded by a single mutex
* to prevent concurrency issues between Close() and Delete()
*
* @return int32_t
*/
int32_t ArchiveWriter::CloseUnsafe() {
if (pmz_zip_writer_instance_) {
return mz_zip_writer_close(pmz_zip_writer_instance_);
}
return MZ_OK;
}

/**
* @brief Delete the archive
* Not thread safe
* Meant to be used with CloseUnsafe and both of them guarded by a single mutex
* to prevent concurrency issues between Close() and Delete()
*
* @return int32_t
*/
void ArchiveWriter::DeleteUnsafe() {
return mz_zip_writer_delete(&pmz_zip_writer_instance_);
}

void ArchiveWriter::DeleteInternal() {
std::unique_lock lock(mutex_);
return mz_zip_writer_delete(&pmz_zip_writer_instance_);
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/helpers/ArchiveWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class ArchiveWriter : public ArchiveIO {

int32_t Close() override;
void Delete() override;
int32_t CloseUnsafe();
void DeleteUnsafe();

int Open() override;
void InitFileInfo();
Expand Down

0 comments on commit 7b8a284

Please sign in to comment.