#include "koh_create.h" #include #include #include int koh_create_exclusive(const char *path, const void *data, size_t len) { int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0644); if (fd < 0) { return errno == EEXIST ? 0 : -1; } const char *bytes = (const char *)data; size_t written = 0; while (written < len) { ssize_t n = write(fd, bytes + written, len - written); if (n < 0) { if (errno == EINTR) { continue; } close(fd); unlink(path); return -1; } if (n == 0) { close(fd); unlink(path); return -1; } written += (size_t)n; } /* A failed close (full disk, deferred write error) means the bytes may not * be there; drop the name rather than leave a short file that looks * published. */ if (close(fd) != 0) { unlink(path); return -1; } return 1; }