Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [patch] Avoid race in zf_mkdir
- X-seq: zsh-workers 47441
 
- From: Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx>
 
- To: Zsh hackers list <zsh-workers@xxxxxxx>
 
- Subject: Re: [patch] Avoid race in zf_mkdir
 
- Date: Fri, 9 Oct 2020 23:22:00 +0200
 
- Archived-at: <https://zsh.org/workers/47441>
 
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-workers/2020-10/CAN%3D4vMqJ8SXkz_EVqwL7-9gCQYg9GMczOztAtsErirXGEDRq8Q%40mail.gmail.com>
 
- Authentication-results: zsh.org;	iprev=pass (mail-io1-f46.google.com) smtp.remote-ip=209.85.166.46;	dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256;	dmarc=pass header.from=gmail.com;	arc=none
 
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed;        d=gmail.com; s=20161025;        h=mime-version:references:in-reply-to:from:date:message-id:subject:to;        bh=mhoUvhCHi2Vq0zPun4a1ZaezZDc1Paz67J09dA89OI0=;        b=PpkLSFmFgUmwCZ0XfzKGJqVQNY2TxHtULkRxmDs8w/huafu9Joa/DBJZATN9hD+BuG         c2azkXGnYckC0VVnGnCWo7ddmbbDBYfvICYbjSbRKDVxIHK2UHl31/N+KaRLjzgaOQad         YO5uB05eKdJyb8CNVE1Alh+m3z6aqul++p35COiNeFRPHJDjVlunNDPFL4gJ2Qp9S3i/         Jt+DPFpiTYmGcdt6ATHJjYwGTt/qQAtYNE2wTauV4j5yhXrDp3OizIpvAnTUctRI2BiH         oM41lC9YTTsSkX3LWkHo2UoNksoUUON1mawc5Qnkx42h/LDgmq5OBlioDcetTg/3BFbj         3gOA==
 
- In-reply-to: <20201009205357.GA6449@CptOrmolo.darkstar>
 
- List-archive: <http://www.zsh.org/sympa/arc/zsh-workers>
 
- List-help: <mailto:sympa@zsh.org?subject=help>
 
- List-id: <zsh-workers.zsh.org>
 
- List-owner: <mailto:zsh-workers-request@zsh.org>
 
- List-post: <mailto:zsh-workers@zsh.org>
 
- List-subscribe: <mailto:sympa@zsh.org?subject=subscribe%20zsh-workers>
 
- List-unsubscribe: <mailto:sympa@zsh.org?subject=unsubscribe%20zsh-workers>
 
- References: <20201009200737.GA78914@CptOrmolo.darkstar> <CAH+w=7anY9r+-MYki4cycVuaP2+d9L3W5u8k3sHyZx4R5kvafg@mail.gmail.com> <CAN=4vMoJrD+g4Go37yT6gF-Lx61vHm45GvmG1Fx6-+3yyb1hAQ@mail.gmail.com> <CAH+w=7ZRvW3Yq3XO6TwbK1hgSDNE8ynX0eQOU2dTuLcAELNUDg@mail.gmail.com> <20201009205357.GA6449@CptOrmolo.darkstar>
 
- Sender: zsh-workers-request@xxxxxxx
 
Perhaps something like this? This should provide the following
guarantees for zf_mkdir -p:
- If it succeeds, the directory must have existed at some point during
the execution of the function (either created by zf_mkdir itself or by
some other concurrent process).
- If it fails, there must have been a point in time during the
execution of the function where the target directory or one of its
parents didn't exist and it was impossible to create it.
`zf_mkdir -p foo` It should work as expected in the face of concurrent
`mkdir foo && rmdir foo` or `touch foo && rm foo`.
I confess that I haven't tested it.
Roman.
diff --git a/Src/Modules/files.c b/Src/Modules/files.c
index 6d20e38a8..a9ccccb8b 100644
--- a/Src/Modules/files.c
+++ b/Src/Modules/files.c
@@ -122,19 +122,28 @@ domkdir(char *nam, char *path, mode_t mode, int p)
 {
     int err;
     mode_t oumask;
+    struct stat st;
     char const *rpath = unmeta(path);
 
-    if(p) {
-	struct stat st;
-
-	if(!stat(rpath, &st) && S_ISDIR(st.st_mode))
+    while(1) {
+	oumask = umask(0);
+	err = mkdir(rpath, mode) ? errno : 0;
+	umask(oumask);
+	if (!err)
+	    return 0;
+	if(!p || err != EEXIST)
+	    break;
+	if(!stat(rpath, &st)) {
+	    if(errno == ENOENT)
+		continue;
+	    err = errno;
+	    break;
+	}
+	if(S_ISDIR(st.st_mode))
 	    return 0;
+	break;
     }
-    oumask = umask(0);
-    err = mkdir(rpath, mode) ? errno : 0;
-    umask(oumask);
-    if(!err)
-	return 0;
+
     zwarnnam(nam, "cannot make directory `%s': %e", path, err);
     return 1;
 }
Messages sorted by:
Reverse Date,
Date,
Thread,
Author