Whamcloud - gitweb
LU-6722 jbd: double minimum journal size for RHEL7
[tools/e2fsprogs.git] / lib / blkid / save.c
index 6802e9d..6f4499c 100644 (file)
@@ -10,6 +10,7 @@
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #endif
 #include "blkidP.h"
 
+#ifdef _WIN32
+#include "windows.h"
+#endif
+
 static int save_dev(blkid_dev dev, FILE *file)
 {
        struct list_head *p;
@@ -93,13 +98,19 @@ int blkid_flush_cache(blkid_cache cache)
        if (ret == 0 && S_ISREG(st.st_mode)) {
                tmp = malloc(strlen(filename) + 8);
                if (tmp) {
+                       mode_t save_umask = umask(022);
                        sprintf(tmp, "%s-XXXXXX", filename);
                        fd = mkstemp(tmp);
+                       umask(save_umask);
                        if (fd >= 0) {
                                file = fdopen(fd, "w");
                                opened = tmp;
                        }
+#ifndef _WIN32
                        fchmod(fd, 0644);
+#else
+                       chmod(tmp, 0644);
+#endif
                }
        }
 
@@ -133,7 +144,7 @@ int blkid_flush_cache(blkid_cache cache)
        fclose(file);
        if (opened != filename) {
                if (ret < 0) {
-                       unlink(opened);
+                       (void) unlink(opened);
                        DBG(DEBUG_SAVE,
                            printf("unlinked temp cache %s\n", opened));
                } else {
@@ -143,10 +154,19 @@ int blkid_flush_cache(blkid_cache cache)
                        if (backup) {
                                sprintf(backup, "%s.old", filename);
                                unlink(backup);
-                               link(filename, backup);
+#if defined(__GNUC__) && __GNUC__ >= 5
+/* explicit (void) cast is not enough with glibc and _FORTIFY_SOURCE */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-result"
+#endif
+                               (void) link(filename, backup);
+#if defined(__GNUC__) && __GNUC__ >= 5
+#pragma GCC diagnostic pop
+#endif
                                free(backup);
                        }
-                       rename(opened, filename);
+                       if (rename(opened, filename) < 0)
+                               (void) unlink(opened);
                        DBG(DEBUG_SAVE,
                            printf("moved temp cache %s\n", opened));
                }