From 2b7b705f7d986903b6e2190b559e7211e8e79f49 Mon Sep 17 00:00:00 2001 From: David Levine Date: Sun, 16 Sep 2012 13:54:23 -0500 Subject: [PATCH] With mhstore -clobber auto/suffix, when looking for a new filename, actually open(2) the file with O_CREAT | O_EXCL to avoid the race condition when figuring out the filename first and opening it later. --- uip/mhstoresbr.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/uip/mhstoresbr.c b/uip/mhstoresbr.c index f5e3347..bcb95ea 100644 --- a/uip/mhstoresbr.c +++ b/uip/mhstoresbr.c @@ -1129,7 +1129,7 @@ next_version (char *file, enum clobber_policy_t clobber_policy) { } for (version = 1; version < max_versions; ++version) { - struct stat st; + int fd; switch (clobber_policy) { case NMH_CLOBBER_AUTO: { @@ -1151,7 +1151,15 @@ next_version (char *file, enum clobber_policy_t clobber_policy) { return NULL; } - if (stat (buffer, &st) == NOTOK) { + /* Actually (try to) create the file here to avoid a race + condition on file naming + creation. This won't solve the + problem with old NFS that doesn't support O_EXCL, though. + Let the umask strip off permissions from 0666 as desired. + That's what fopen () would do if it was creating the file. */ + if ((fd = open (buffer, O_CREAT | O_EXCL, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | + S_IROTH | S_IWOTH)) >= 0) { + close (fd); break; } } -- 1.7.10.4