From: Arvin Schnell Date: Thu, 24 Feb 2011 09:39:14 +0000 (+0100) Subject: - added wrapper for mkstemp X-Git-Tag: v0.1.3~458 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=673661a0f1bf0d862ae946d8f735dca894e16885;p=thirdparty%2Fsnapper.git - added wrapper for mkstemp --- diff --git a/snapper/AppUtil.cc b/snapper/AppUtil.cc index 1a4f24c2..793fbbef 100644 --- a/snapper/AppUtil.cc +++ b/snapper/AppUtil.cc @@ -128,6 +128,22 @@ checkNormalFile(const string& Path_Cv) } + FILE* + mkstemp(string& path) + { + char* tmp = strdup(path.c_str()); + + int fd = ::mkstemp(tmp); + if (fd == -1) + return NULL; + + path = tmp; + free(tmp); + + return fdopen(fd, "w"); + } + + static const blocxx::String component = "libsnapper"; diff --git a/snapper/AppUtil.h b/snapper/AppUtil.h index df0d00e6..06a1f553 100644 --- a/snapper/AppUtil.h +++ b/snapper/AppUtil.h @@ -50,6 +50,8 @@ bool setStatMode(const string& Path_Cv, mode_t val ); list glob(const string& path, int flags); + FILE* mkstemp(string& path); + int clonefile(const string& dest, const string& src, mode_t mode); int readlink(const string& path, string& buf); diff --git a/snapper/File.cc b/snapper/File.cc index 0e3b7b20..ee79592b 100644 --- a/snapper/File.cc +++ b/snapper/File.cc @@ -192,13 +192,9 @@ namespace snapper string output = snapper->snapshotsDir() + "/" + decString(num2) + "/filelist-" + decString(num1) + ".txt"; - char* tmp_name = (char*) malloc(output.length() + 12); - strcpy(tmp_name, output.c_str()); - strcat(tmp_name, ".tmp-XXXXXX"); + string tmp_name = output + ".tmp-XXXXXX"; - int fd = mkstemp(tmp_name); - - FILE* file = fdopen(fd, "w"); + FILE* file = mkstemp(tmp_name); for (const_iterator it = entries.begin(); it != entries.end(); ++it) { @@ -212,9 +208,7 @@ namespace snapper fclose(file); - rename(tmp_name, output.c_str()); - - free(tmp_name); + rename(tmp_name.c_str(), output.c_str()); return true; } diff --git a/tools/compare-dirs.cc b/tools/compare-dirs.cc index 93482485..aa510a5b 100644 --- a/tools/compare-dirs.cc +++ b/tools/compare-dirs.cc @@ -10,7 +10,7 @@ using namespace snapper; using namespace std; -char* tmp_name = NULL; +string tmp_name; FILE* file = NULL; @@ -21,8 +21,8 @@ terminate(int) if (file != NULL) fclose(file); - if (tmp_name != NULL) - unlink(tmp_name); + if (!tmp_name.empty()) + unlink(tmp_name.c_str()); exit(EXIT_FAILURE); } @@ -62,21 +62,15 @@ main(int argc, char** argv) sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); - tmp_name = (char*) malloc(output.length() + 12); - strcpy(tmp_name, output.c_str()); - strcat(tmp_name, ".tmp-XXXXXX"); + tmp_name = output + ".tmp-XXXXXX"; - int fd = mkstemp(tmp_name); - - file = fdopen(fd, "w"); + file = mkstemp(tmp_name); cmpDirs(path1, path2, write_line); fclose(file); - rename(tmp_name, output.c_str()); - - free(tmp_name); + rename(tmp_name.c_str(), output.c_str()); exit(EXIT_SUCCESS); }