13 #define __O_TMPFILE 020000000
14 #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
19 int make_tempfile(const string &contents)
21 int fd = open("/tmp", O_RDWR | O_TMPFILE, 0600);
23 char filename[] = "/tmp/cubemap.XXXXXX";
24 mode_t old_umask = umask(077);
25 fd = mkstemp(filename);
27 log_perror("mkstemp");
32 if (unlink(filename) == -1) {
34 // Can still continue.
38 const char *ptr = contents.data();
39 size_t to_write = contents.size();
40 while (to_write > 0) {
41 ssize_t ret = write(fd, ptr, to_write);
55 bool read_tempfile_and_close(int fd, string *contents)
57 bool ok = read_tempfile(fd, contents);
58 safe_close(fd); // Implicitly deletes the file.
62 bool read_tempfile(int fd, string *contents)
64 ssize_t ret, has_read;
66 off_t len = lseek(fd, 0, SEEK_END);
72 contents->resize(len);
74 if (lseek(fd, 0, SEEK_SET) == -1) {
80 while (has_read < len) {
81 ret = read(fd, &((*contents)[has_read]), len - has_read);
87 log(ERROR, "Unexpected EOF!");
96 int safe_close(int fd)
101 } while (ret == -1 && errno == EINTR);
104 log_perror("close()");