]> git.sesse.net Git - cubemap/blobdiff - util.cpp
Fix broken umask (we had the bits inverted).
[cubemap] / util.cpp
index b8862ed48c79719b632dd1575a6cd1cc6e057b39..25a8da4a1e8893ca2407615d418aeab7fffe81d5 100644 (file)
--- a/util.cpp
+++ b/util.cpp
@@ -2,6 +2,7 @@
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/stat.h>
 #include <unistd.h>
 
 #include "log.h"
@@ -12,7 +13,9 @@ using namespace std;
 int make_tempfile(const std::string &contents)
 {
        char filename[] = "/tmp/cubemap.XXXXXX";
+       mode_t old_umask = umask(077);
        int fd = mkstemp(filename);
+       umask(old_umask);
        if (fd == -1) {
                log_perror("mkstemp");
                return -1;
@@ -29,7 +32,7 @@ int make_tempfile(const std::string &contents)
                ssize_t ret = write(fd, ptr, to_write);
                if (ret == -1) {
                        log_perror("write");
-                       close(fd);
+                       safe_close(fd);
                        return -1;
                }
 
@@ -43,17 +46,7 @@ int make_tempfile(const std::string &contents)
 bool read_tempfile_and_close(int fd, std::string *contents)
 {
        bool ok = read_tempfile(fd, contents);
-
-       int ret;
-       do {
-               ret = close(fd);  // Implicitly deletes the file.
-       } while (ret == -1 && errno == EINTR);
-       
-       if (ret == -1) {
-               log_perror("close");
-               // Can still continue.
-       }
-
+       safe_close(fd);  // Implicitly deletes the file.
        return ok;
 }
 
@@ -90,3 +83,17 @@ bool read_tempfile(int fd, std::string *contents)
 
        return true;
 }
+
+int safe_close(int fd)
+{
+       int ret;
+       do {
+               ret = close(fd);
+       } while (ret == -1 && errno == EINTR);
+
+       if (ret == -1) {
+               log_perror("close()");
+       }
+
+       return ret;
+}