X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=util.cpp;h=b8862ed48c79719b632dd1575a6cd1cc6e057b39;hp=8159e3821ccb1071efa8801ae8a63b90b65d2db4;hb=5cd46e39b4063d94f6dc559ae350beeb8406a8f9;hpb=4856c49f1b63753ce86ad759ee649a1117628a8e diff --git a/util.cpp b/util.cpp index 8159e38..b8862ed 100644 --- a/util.cpp +++ b/util.cpp @@ -40,24 +40,38 @@ int make_tempfile(const std::string &contents) return fd; } +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. + } + + return ok; +} + 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; + return false; } contents->resize(len); if (lseek(fd, 0, SEEK_SET) == -1) { log_perror("lseek"); - ok = false; - goto done; + return false; } has_read = 0; @@ -65,26 +79,14 @@ bool read_tempfile(int fd, std::string *contents) ret = read(fd, &((*contents)[has_read]), len - has_read); if (ret == -1) { log_perror("read"); - ok = false; - goto done; + return false; } if (ret == 0) { log(ERROR, "Unexpected EOF!"); - ok = false; - goto done; + return false; } has_read += ret; } -done: - do { - ret = close(fd); // Implicitly deletes the files. - } while (ret == -1 && errno == EINTR); - - if (ret == -1) { - log_perror("close"); - // Can still continue. - } - - return ok; + return true; }