12 int make_tempfile(const std::string &contents)
14 char filename[] = "/tmp/cubemap.XXXXXX";
15 int fd = mkstemp(filename);
17 log_perror("mkstemp");
21 if (unlink(filename) == -1) {
23 // Can still continue;
26 const char *ptr = contents.data();
27 size_t to_write = contents.size();
28 while (to_write > 0) {
29 ssize_t ret = write(fd, ptr, to_write);
43 bool read_tempfile_and_close(int fd, std::string *contents)
45 bool ok = read_tempfile(fd, contents);
49 ret = close(fd); // Implicitly deletes the file.
50 } while (ret == -1 && errno == EINTR);
54 // Can still continue.
60 bool read_tempfile(int fd, std::string *contents)
62 ssize_t ret, has_read;
64 off_t len = lseek(fd, 0, SEEK_END);
70 contents->resize(len);
72 if (lseek(fd, 0, SEEK_SET) == -1) {
78 while (has_read < len) {
79 ret = read(fd, &((*contents)[has_read]), len - has_read);
85 log(ERROR, "Unexpected EOF!");