Replace the output file safely in tracy-capture.

The old pre-flight truncated the output at startup with -f, so an
interrupted capture lost the trace, and the probe fopen followed a
symlink. The existing file is now moved to FILE~ for the duration of
the capture and restored if the trace cannot be written.
This commit is contained in:
Bartosz Taudul
2026-09-02 00:41:07 +02:00
parent cf3407bbe3
commit ebf5ebd9bd
5 changed files with 135 additions and 10 deletions

View File

@@ -11,7 +11,6 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "../../server/TracyFileWrite.hpp"
#include "../../server/TracyPrint.hpp"
@@ -19,6 +18,7 @@
#include "../../server/TracyWorker.hpp"
#include "../../public/common/TracyVersion.hpp"
#include "GitRef.hpp"
#include "CaptureFileBackup.hpp"
#include "CaptureOutput.hpp"
@@ -96,21 +96,18 @@ int main( int argc, char** argv )
if( !address || !output ) Usage();
struct stat st;
if( stat( output, &st ) == 0 && !overwrite )
const char* prepError = nullptr;
const auto prep = tracy::PrepareOutputFile( output, overwrite, &prepError );
if( prep == tracy::OutputPrep::Exists )
{
printf( "Output file %s already exists! Use -f to force overwrite.\n", output );
return 4;
}
FILE* test = fopen( output, "wb" );
if( !test )
if( prep == tracy::OutputPrep::Unusable )
{
printf( "Cannot open output file %s for writing!\n", output );
printf( "Cannot use output file: %s!\n", prepError );
return 5;
}
fclose( test );
unlink( output );
printf( "Connecting to %s:%i...", address, port );
fflush( stdout );
@@ -169,10 +166,12 @@ int main( int argc, char** argv )
f->Finish();
const auto stats = f->GetCompressionStatistics();
printf( "Trace size %s (%.2f%% ratio)\n", tracy::MemSizeToString( stats.second ), 100.f * stats.second / stats.first );
tracy::DiscardOutputBackup();
}
else
{
AnsiPrintf( ANSI_RED ANSI_BOLD, " failed!\n");
tracy::RestoreOutputBackup();
}
return 0;