X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=util.cpp;fp=util.cpp;h=b79c6c3dccd5e0a58ca2f25a09b28c6d223bdd64;hp=a5d6378fd6817ac5921f7009e7a1b9166794dd43;hb=14aa32dce539ac645ba10e8c488fb5a435ffa314;hpb=e4637f55fbd4671089b6700fdd17c3110587436f diff --git a/util.cpp b/util.cpp index a5d6378..b79c6c3 100644 --- a/util.cpp +++ b/util.cpp @@ -41,27 +41,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; + fprintf(stderr, "Unexpected EOF!\n"); + 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); @@ -71,5 +85,5 @@ bool read_tempfile(int fd, std::string *contents) // Can still continue. } - return true; + return ok; }