14 int make_tempfile(const std::string &contents)
16 char filename[] = "/tmp/cubemap.XXXXXX";
17 mode_t old_umask = umask(0600);
18 int fd = mkstemp(filename);
21 log_perror("mkstemp");
25 if (unlink(filename) == -1) {
27 // Can still continue;
30 const char *ptr = contents.data();
31 size_t to_write = contents.size();
32 while (to_write > 0) {
33 ssize_t ret = write(fd, ptr, to_write);
47 bool read_tempfile_and_close(int fd, std::string *contents)
49 bool ok = read_tempfile(fd, contents);
53 ret = close(fd); // Implicitly deletes the file.
54 } while (ret == -1 && errno == EINTR);
58 // Can still continue.
64 bool read_tempfile(int fd, std::string *contents)
66 ssize_t ret, has_read;
68 off_t len = lseek(fd, 0, SEEK_END);
74 contents->resize(len);
76 if (lseek(fd, 0, SEEK_SET) == -1) {
82 while (has_read < len) {
83 ret = read(fd, &((*contents)[has_read]), len - has_read);
89 log(ERROR, "Unexpected EOF!");