Skip to content

Avoid overwriting existing files when compressing - #1104

Merged
godlygeek merged 1 commit into
bloomberg:mainfrom
godlygeek:avoid_overwriting_files_when_compressing
Nov 25, 2026
Merged

godlygeek merged 1 commit into
bloomberg:mainfrom
godlygeek:avoid_overwriting_files_when_compressing

Conversation

@godlygeek

Copy link
Copy Markdown
Contributor

Previously when compressing a capture file, we would write to a temp file whose name was solely based on the name of the capture file, and if that file already existed we would overwrite it.

Although the file name did end with .tmp, we can avoid the risk of overwriting any file that the user might care about by ensuring that whatever file we write to doesn't already exist.

Since we can't set O_CREAT | O_EXCL when creating a file with std::ofstream, instead use mkdtemp to create a new, private, empty directory. Note that it must be a subdirectory of the directory our capture file is being written to, in order for rename() to remain atomic.

Previously when compressing a capture file, we would write to a temp
file whose name was solely based on the name of the capture file, and if
that file already existed we would overwrite it.

Although the file name did end with `.tmp`, we can avoid the risk of
overwriting any file that the user might care about by ensuring that
whatever file we write to doesn't already exist.

Since we can't set `O_CREAT | O_EXCL` when creating a file with
`std::ofstream`, instead use `mkdtemp` to create a new, private, empty
directory. Note that it must be a subdirectory of the directory our
capture file is being written to, in order for `rename()` to remain
atomic.

Signed-off-by: Matt Wozniski <mwozniski@bloomberg.net>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.27273% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.35%. Comparing base (4e4dd93) to head (edeb66c).

Files with missing lines Patch % Lines
src/memray/_memray/sink.cpp 77.27% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1104      +/-   ##
==========================================
- Coverage   92.40%   92.35%   -0.05%     
==========================================
  Files         103      103              
  Lines       13453    13470      +17     
  Branches      480      480              
==========================================
+ Hits        12431    12440       +9     
- Misses       1022     1030       +8     
Flag Coverage Δ
cpp 92.35% <77.27%> (-0.05%) ⬇️
python_and_cython 92.35% <77.27%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kiri11
kiri11 self-requested a review November 24, 2026 23:12
@godlygeek
godlygeek merged commit 597921f into bloomberg:main Nov 25, 2026
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants