aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/cache_cmd.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zen/cmds/cache_cmd.cpp')
-rw-r--r--src/zen/cmds/cache_cmd.cpp26
1 files changed, 21 insertions, 5 deletions
diff --git a/src/zen/cmds/cache_cmd.cpp b/src/zen/cmds/cache_cmd.cpp
index b6f272ee7..c03284462 100644
--- a/src/zen/cmds/cache_cmd.cpp
+++ b/src/zen/cmds/cache_cmd.cpp
@@ -704,6 +704,11 @@ CacheGetSubCmd::Run(const ZenCliOptions& /*GlobalOptions*/)
}
}
+ if (m_OutputPath.empty() && !m_AsText)
+ {
+ throw OptionParseException("'--output-path' is required (or pass '--as-text' to print to stdout)", m_SubOptions.help());
+ }
+
if (!m_OutputPath.empty())
{
if (IsDir(m_OutputPath))
@@ -715,10 +720,6 @@ CacheGetSubCmd::Run(const ZenCliOptions& /*GlobalOptions*/)
CreateDirectories(m_OutputPath.parent_path());
}
}
- if (m_OutputPath.empty())
- {
- m_OutputPath = (m_AttachmentHash.empty() ? m_ValueKey : m_AttachmentHash);
- }
std::string Url = fmt::format("/z$/{}/{}/{}", m_Namespace, m_Bucket, ValueId);
if (AttachmentHash != IoHash::Zero)
@@ -727,6 +728,11 @@ CacheGetSubCmd::Run(const ZenCliOptions& /*GlobalOptions*/)
}
if (HttpClient::Response Result = Http.Download(Url, std::filesystem::temp_directory_path()); Result)
{
+ // `Http.Download` parks the payload in the system temp dir and returns
+ // a buffer that already has delete-on-close set, so every exit path
+ // (exception, fallback WriteFile, `--as-text` console print) reaps it.
+ // A successful MoveToFile below clears the flag so the payload's
+ // handle-close doesn't delete the caller's output afterwards.
auto TryDecompress = [](const IoBuffer& Buffer) -> IoBuffer {
IoHash RawHash;
uint64_t RawSize;
@@ -753,7 +759,17 @@ CacheGetSubCmd::Run(const ZenCliOptions& /*GlobalOptions*/)
}
else
{
- if (!MoveToFile(m_OutputPath, ChunkData))
+ if (MoveToFile(m_OutputPath, ChunkData))
+ {
+ // The file was renamed into place; clearing DeleteOnClose prevents
+ // the move'd-out file at m_OutputPath from being deleted when the
+ // payload's handle closes. When m_Decompress is false ChunkData
+ // shares a core with ResponsePayload so either clear suffices;
+ // when decompressed ChunkData is in-memory and MoveToFile would
+ // have failed, so we don't reach this branch.
+ Result.ResponsePayload.SetDeleteOnClose(false);
+ }
+ else
{
WriteFile(m_OutputPath, ChunkData);
}