13 #define __O_TMPFILE 020000000
14 #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
19 int make_tempfile(const std::string &contents)
21 char filename[] = "/tmp/cubemap.XXXXXX";
22 int fd = open(filename, O_RDWR | O_TMPFILE, 0600);
24 fd = mkstemp(filename);
26 log_perror("mkstemp");
30 if (unlink(filename) == -1) {
32 // Can still continue.
36 const char *ptr = contents.data();
37 size_t to_write = contents.size();
38 while (to_write > 0) {
39 ssize_t ret = write(fd, ptr, to_write);
53 bool read_tempfile_and_close(int fd, std::string *contents)
55 bool ok = read_tempfile(fd, contents);
56 safe_close(fd); // Implicitly deletes the file.
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!");
94 int safe_close(int fd)
99 } while (ret == -1 && errno == EINTR);
102 log_perror("close()");