diff options
| author | Dan Engelbrecht <[email protected]> | 2022-05-10 10:08:31 +0200 |
|---|---|---|
| committer | Dan Engelbrecht <[email protected]> | 2022-05-10 10:13:16 +0200 |
| commit | 5d15fa59655c79a0c8ad1b4c5d44b657aa07c29e (patch) | |
| tree | db018ad336dc323f8dbebe6547938624c665c285 /zenserver/cache/structuredcachestore.cpp | |
| parent | happy path should be minimal work (diff) | |
| download | zen-5d15fa59655c79a0c8ad1b4c5d44b657aa07c29e.tar.xz zen-5d15fa59655c79a0c8ad1b4c5d44b657aa07c29e.zip | |
Make sure we clean up temp file in all scenarios
Diffstat (limited to 'zenserver/cache/structuredcachestore.cpp')
| -rw-r--r-- | zenserver/cache/structuredcachestore.cpp | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/zenserver/cache/structuredcachestore.cpp b/zenserver/cache/structuredcachestore.cpp index c3904d40a..ce55b24b6 100644 --- a/zenserver/cache/structuredcachestore.cpp +++ b/zenserver/cache/structuredcachestore.cpp @@ -1736,6 +1736,22 @@ ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, c throw std::system_error(Ec, fmt::format("Failed to open temporary file for put in '{}'", m_BucketDir)); } + bool CleanUpTempFile = false; + auto __ = MakeGuard([&] { + if (CleanUpTempFile) + { + std::error_code Ec; + std::filesystem::remove(DataFile.GetPath(), Ec); + if (Ec) + { + ZEN_WARN("Failed to clean up temporary file '{}' for put in '{}', reason '{}'", + DataFile.GetPath(), + m_BucketDir, + Ec.message()); + } + } + }); + DataFile.WriteAll(Value.Value, Ec); if (Ec) { @@ -1762,6 +1778,10 @@ ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, c DataFile.MoveTemporaryIntoPlace(FsPath, Ec); + // Once we have called MoveTemporaryIntoPlace automatic clean up the temp file + // will be disabled as the file handle has already been closed + CleanUpTempFile = Ec ? true : false; + if (Ec) { std::error_code ExistingEc; @@ -1849,14 +1869,6 @@ ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, c RetryCount--; } while (RetryCount > 0); - // Once we have called MoveTemporaryIntoPlace we no longer will automatically clean up the temp file - // as the file handle has already been closed - std::filesystem::remove(DataFile.GetPath(), Ec); - if (Ec) - { - ZEN_WARN("Failed to clean up temporary file '{}' for put in '{}', reason '{}'", DataFile.GetPath(), m_BucketDir, Ec.message()); - } - throw std::system_error(Ec, fmt::format("Failed to finalize file '{}' for put in '{}'", DataFilePath.ToUtf8(), m_BucketDir)); } |