Prevent overwriting existing files by the capture utility.

This commit is contained in:
Bartosz Taudul 2020-09-20 20:04:43 +02:00
parent 1e34b22a82
commit 7da17680ae

View File

@ -9,6 +9,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h>
#include "../../common/TracyProtocol.hpp" #include "../../common/TracyProtocol.hpp"
#include "../../server/TracyFileWrite.hpp" #include "../../server/TracyFileWrite.hpp"
@ -27,7 +28,7 @@ void SigInt( int )
void Usage() void Usage()
{ {
printf( "Usage: capture -o output.tracy [-a address] [-p port]\n" ); printf( "Usage: capture -o output.tracy [-a address] [-p port] [-f]\n" );
exit( 1 ); exit( 1 );
} }
@ -41,12 +42,13 @@ int main( int argc, char** argv )
} }
#endif #endif
bool overwrite = false;
const char* address = "localhost"; const char* address = "localhost";
const char* output = nullptr; const char* output = nullptr;
int port = 8086; int port = 8086;
int c; int c;
while( ( c = getopt( argc, argv, "a:o:p:" ) ) != -1 ) while( ( c = getopt( argc, argv, "a:o:p:f" ) ) != -1 )
{ {
switch( c ) switch( c )
{ {
@ -59,6 +61,9 @@ int main( int argc, char** argv )
case 'p': case 'p':
port = atoi( optarg ); port = atoi( optarg );
break; break;
case 'f':
overwrite = true;
break;
default: default:
Usage(); Usage();
break; break;
@ -67,6 +72,13 @@ int main( int argc, char** argv )
if( !address || !output ) Usage(); if( !address || !output ) Usage();
struct stat st;
if( stat( output, &st ) == 0 && !overwrite )
{
printf( "Output file %s already exists! Use -f to force overwrite.\n", output );
return 4;
}
printf( "Connecting to %s:%i...", address, port ); printf( "Connecting to %s:%i...", address, port );
fflush( stdout ); fflush( stdout );
tracy::Worker worker( address, port ); tracy::Worker worker( address, port );