Permalink
Browse files

Merge pull request #67 from astiob/rename

Make file renames atomic
  • Loading branch information...
fhanau committed Oct 12, 2018
2 parents 25ef2cd + 01f11bf commit 9c8c6263fa734c00b954aad112a7995c3a429ecd
Showing with 14 additions and 4 deletions.
  1. +14 −4 src/main.cpp
@@ -17,6 +17,10 @@
#include <id3/tag.h>
#endif

#ifdef _WIN32
#include <Windows.h>
#endif

static size_t processedfiles;
static size_t bytes;
static long long savings;
@@ -73,6 +77,14 @@ static void Usage() {
);
}

static void RenameAndReplace(const char * Infile, const char * Outfile){
#ifdef _WIN32
MoveFileExA(Infile, Outfile, MOVEFILE_REPLACE_EXISTING);
#else
rename(Infile, Outfile);
#endif
}

static void ECT_ReportSavings(){
if (processedfiles){
printf("Processed %zu file%s\n", processedfiles, processedfiles > 1 ? "s":"");
@@ -139,8 +151,7 @@ static int ECTGzip(const char * Infile, const unsigned Mode, unsigned char multi
}
ZopfliGzip(((std::string)Infile).append(".ungz").c_str(), 0, Mode, multithreading, ZIP);
if (filesize(((std::string)Infile).append(".ungz.gz").c_str()) < filesize(Infile)){
unlink(Infile);
rename(((std::string)Infile).append(".ungz.gz").c_str(), Infile);
RenameAndReplace(((std::string)Infile).append(".ungz.gz").c_str(), Infile);
}
else {
unlink(((std::string)Infile).append(".ungz.gz").c_str());
@@ -220,8 +231,7 @@ static unsigned char OptimizePNG(const char * Infile, const ECTOptions& Options)
unlink(((std::string)Infile).append(".bak").c_str());
}
else {
unlink(Infile);
rename(((std::string)Infile).append(".bak").c_str(), Infile);
RenameAndReplace(((std::string)Infile).append(".bak").c_str(), Infile);
}
}

0 comments on commit 9c8c626

Please sign in to comment.