]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- added wrapper for mkstemp
authorArvin Schnell <aschnell@suse.de>
Thu, 24 Feb 2011 09:39:14 +0000 (10:39 +0100)
committerArvin Schnell <aschnell@suse.de>
Thu, 24 Feb 2011 09:39:14 +0000 (10:39 +0100)
snapper/AppUtil.cc
snapper/AppUtil.h
snapper/File.cc
tools/compare-dirs.cc

index 1a4f24c2b6c6fb469b6119028db4572b0ebb55cb..793fbbef9acb5b8b211d3d2c3da3e4d7687e7a35 100644 (file)
@@ -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";
 
 
index df0d00e6695b94ddcf23e72cb1d71b33245678e2..06a1f55357fd521cbc8c78eff732d5cc44c7ee84 100644 (file)
@@ -50,6 +50,8 @@ bool setStatMode(const string& Path_Cv, mode_t val );
 
     list<string> 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);
index 0e3b7b202fcc51c755610ca81dd93b7c9e792ca7..ee79592bc94a553773941a5103816b436a297fe3 100644 (file)
@@ -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;
     }
index 93482485dbd4ee71ba73f8e62db3475443be65e6..aa510a5b6e66375bea779c2ceb90930dd9aaf6ff 100644 (file)
@@ -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);
 }