X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=util.cpp;h=8159e3821ccb1071efa8801ae8a63b90b65d2db4;hp=a5d6378fd6817ac5921f7009e7a1b9166794dd43;hb=a313e4c7a3c0c453d4dffd317fc143f1f4a7f8ba;hpb=488f28bf7070f44469a006ed4a9d4c423788d175 diff --git a/util.cpp b/util.cpp index a5d6378..8159e38 100644 --- a/util.cpp +++ b/util.cpp @@ -4,6 +4,7 @@ #include #include +#include "log.h" #include "util.h" using namespace std; @@ -13,12 +14,12 @@ int make_tempfile(const std::string &contents) char filename[] = "/tmp/cubemap.XXXXXX"; int fd = mkstemp(filename); if (fd == -1) { - perror("mkstemp"); + log_perror("mkstemp"); return -1; } if (unlink(filename) == -1) { - perror("unlink"); + log_perror("unlink"); // Can still continue; } @@ -27,7 +28,7 @@ int make_tempfile(const std::string &contents) while (to_write > 0) { ssize_t ret = write(fd, ptr, to_write); if (ret == -1) { - perror("write"); + log_perror("write"); close(fd); return -1; } @@ -41,35 +42,49 @@ int make_tempfile(const std::string &contents) bool read_tempfile(int fd, std::string *contents) { + bool ok = true; + ssize_t ret, has_read; + + off_t len = lseek(fd, 0, SEEK_END); + if (len == -1) { + log_perror("lseek"); + ok = false; + goto done; + } + + contents->resize(len); + if (lseek(fd, 0, SEEK_SET) == -1) { - perror("lseek"); - return false; + log_perror("lseek"); + ok = false; + goto done; } - char buf[4096]; - for ( ;; ) { - ssize_t ret = read(fd, buf, sizeof(buf)); + has_read = 0; + while (has_read < len) { + ret = read(fd, &((*contents)[has_read]), len - has_read); if (ret == -1) { - perror("read"); - return false; + log_perror("read"); + ok = false; + goto done; } if (ret == 0) { - // EOF. - break; + log(ERROR, "Unexpected EOF!"); + ok = false; + goto done; } - - contents->append(string(buf, buf + ret)); + has_read += ret; } - int ret; +done: do { ret = close(fd); // Implicitly deletes the files. } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("close"); + log_perror("close"); // Can still continue. } - return true; + return ok; }