#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <sys/stat.h>
-#include <sys/types.h>
+#include <unistd.h>
#include "log.h"
#include "util.h"
ssize_t ret = write(fd, ptr, to_write);
if (ret == -1) {
log_perror("write");
- if (close(fd) == -1) {
- log_perror("close");
- }
+ safe_close(fd);
return -1;
}
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.
- }
-
+ safe_close(fd); // Implicitly deletes the file.
return ok;
}
return true;
}
+
+int safe_close(int fd)
+{
+ int ret;
+ do {
+ ret = close(fd);
+ } while (ret == -1 && errno == EINTR);
+
+ if (ret == -1) {
+ log_perror("close()");
+ }
+
+ return ret;
+}