X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=util.cpp;h=1b76962a2b1e5061209da30acfaff172a8d4e981;hp=d6d39f29ada05dc4437886d61178d190df189d02;hb=562cf44b96b5c0260d1a1ab18b2dd2408b6d1fc8;hpb=71fc5575037bead8b6e927a1fffd199e4fc4514b diff --git a/util.cpp b/util.cpp index d6d39f2..1b76962 100644 --- a/util.cpp +++ b/util.cpp @@ -2,9 +2,8 @@ #include #include #include -#include #include -#include +#include #include "log.h" #include "util.h" @@ -33,9 +32,7 @@ int make_tempfile(const std::string &contents) ssize_t ret = write(fd, ptr, to_write); if (ret == -1) { log_perror("write"); - if (close(fd) == -1) { - log_perror("close"); - } + safe_close(fd); return -1; } @@ -49,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; } @@ -96,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; +}