Commit b96f42e8 authored by Konstantin Khlebnikov's avatar Konstantin Khlebnikov

ioping: add support for O_TMPFILE

Starting from linux 3.11 kernel can create unlinked temporary files atomically.
Signed-off-by: default avatarKonstantin Khlebnikov <koct9i@gmail.com>
parent e1083021
...@@ -753,16 +753,16 @@ static void aio_setup(void) ...@@ -753,16 +753,16 @@ static void aio_setup(void)
#ifdef __MINGW32__ #ifdef __MINGW32__
int create_temp(char *path, char *template) int create_temp(char *path, char *name)
{ {
int length = strlen(path) + strlen(template) + 2; int length = strlen(path) + strlen(name) + 9;
char *temp = malloc(length); char *temp = malloc(length);
HANDLE h; HANDLE h;
DWORD attr; DWORD attr;
if (!temp) if (!temp)
err(2, NULL); err(2, NULL);
snprintf(temp, length, "%s\\%s", path, template); snprintf(temp, length, "%s\\%s.XXXXXX", path, name);
mktemp(temp); mktemp(temp);
attr = FILE_ATTRIBUTE_HIDDEN | FILE_FLAG_DELETE_ON_CLOSE; attr = FILE_ATTRIBUTE_HIDDEN | FILE_FLAG_DELETE_ON_CLOSE;
...@@ -803,26 +803,39 @@ void set_signal(void) ...@@ -803,26 +803,39 @@ void set_signal(void)
#else /* __MINGW32__ */ #else /* __MINGW32__ */
int create_temp(char *path, char *template) int create_temp(char *path, char *name)
{ {
int length = strlen(path) + strlen(template) + 2; int length = strlen(path) + strlen(name) + 9;
char *temp = malloc(length); char *temp = malloc(length);
int fd; int fd;
if (!temp) if (!temp)
err(2, NULL); err(2, NULL);
snprintf(temp, length, "%s/%s", path, template);
snprintf(temp, length, "%s/%s", path, name);
#ifdef O_TMPFILE
fd = open(temp, O_RDWR|O_TMPFILE, 0600);
if (fd >= 0)
goto done;
#endif
strcat(temp, ".XXXXXX");
fd = mkstemp(temp); fd = mkstemp(temp);
if (fd < 0) if (fd < 0)
err(2, "failed to create temporary file at \"%s\"", path); goto out;
if (unlink(temp)) if (unlink(temp))
err(2, "unlink \"%s\" failed", temp); err(2, "unlink \"%s\" failed", temp);
free(temp);
# ifdef HAVE_DIRECT_IO #ifdef O_TMPFILE
done:
#endif
#ifdef HAVE_DIRECT_IO
if (direct && fcntl(fd, F_SETFL, O_DIRECT)) if (direct && fcntl(fd, F_SETFL, O_DIRECT))
errx(2, "fcntl failed, please retry without -D"); errx(2, "fcntl failed, please retry without -D");
# endif #endif
out:
free(temp);
return fd; return fd;
} }
...@@ -943,7 +956,9 @@ int main (int argc, char **argv) ...@@ -943,7 +956,9 @@ int main (int argc, char **argv)
memset(buf, '*', size); memset(buf, '*', size);
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
fd = create_temp(path, "ioping.XXXXXX"); fd = create_temp(path, "ioping.tmp");
if (fd < 0)
err(2, "failed to create temporary file at \"%s\"", path);
for (woffset = 0 ; woffset + size <= wsize ; woffset += size) { for (woffset = 0 ; woffset + size <= wsize ; woffset += size) {
if (pwrite(fd, buf, size, offset + woffset) != size) if (pwrite(fd, buf, size, offset + woffset) != size)
err(2, "write failed"); err(2, "write failed");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment