]> git.sesse.net Git - cubemap/blobdiff - util.cpp
Support UDP packets larger than 4 kB.
[cubemap] / util.cpp
index 45bf5b52453309b1ff178d63ed1d64603e76b9de..1b76962a2b1e5061209da30acfaff172a8d4e981 100644 (file)
--- a/util.cpp
+++ b/util.cpp
@@ -2,9 +2,8 @@
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <sys/stat.h>
-#include <sys/types.h>
+#include <unistd.h>
 
 #include "log.h"
 #include "util.h"
@@ -33,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;
                }
 
@@ -47,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;
 }
 
@@ -94,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;
+}