X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=util.cpp;h=41c13c6f2a3ed743a5345b3d0d41c37aefb6e5bc;hp=c8e26d26aa65e5f36a6fa0b64692e767e176c006;hb=3ce04a3c410c5836394417b19e70f2a95bc8a5e7;hpb=7ddd07eb95bb3216fad47aa6d3eb76a8b2648579 diff --git a/util.cpp b/util.cpp index c8e26d2..41c13c6 100644 --- a/util.cpp +++ b/util.cpp @@ -1,8 +1,10 @@ +#include +#include #include #include #include -#include +#include "log.h" #include "util.h" using namespace std; @@ -40,27 +42,41 @@ 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) { + perror("lseek"); + ok = false; + goto done; + } + + contents->resize(len); + if (lseek(fd, 0, SEEK_SET) == -1) { perror("lseek"); - return false; + 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; + 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); @@ -70,5 +86,5 @@ bool read_tempfile(int fd, std::string *contents) // Can still continue. } - return true; + return ok; }