From 41ffc55614eba03412e90ff2d91f80e5553a1e68 Mon Sep 17 00:00:00 2001 From: claudio Date: Mon, 17 May 2021 11:52:10 +0000 Subject: [PATCH] Sync code with the original from mkdir(1). OK benno@ --- usr.bin/rsync/mkpath.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/usr.bin/rsync/mkpath.c b/usr.bin/rsync/mkpath.c index c8c7bd4477e..1625508ac6b 100644 --- a/usr.bin/rsync/mkpath.c +++ b/usr.bin/rsync/mkpath.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mkpath.c,v 1.4 2019/05/08 21:30:11 benno Exp $ */ +/* $OpenBSD: mkpath.c,v 1.5 2021/05/17 11:52:10 claudio Exp $ */ /* * Copyright (c) 1983, 1992, 1993 * The Regents of the University of California. All rights reserved. @@ -46,29 +46,35 @@ mkpath(char *path) { struct stat sb; char *slash; - int done = 0; + int done; slash = path; - while (!done) { + for (;;) { slash += strspn(slash, "/"); slash += strcspn(slash, "/"); done = (*slash == '\0'); *slash = '\0'; - if (stat(path, &sb)) { - if (errno != ENOENT || (mkdir(path, 0777) && - errno != EEXIST)) { - ERR("%s: stat", path); + if (mkdir(path, 0777) != 0) { + int mkdir_errno = errno; + + if (stat(path, &sb) == -1) { + /* Not there; use mkdir()s errno */ + errno = mkdir_errno; + return (-1); + } + if (!S_ISDIR(sb.st_mode)) { + /* Is there, but isn't a directory */ + errno = ENOTDIR; return (-1); } - } else if (!S_ISDIR(sb.st_mode)) { - errno = ENOTDIR; - ERR("%s: stat", path); - return (-1); } + if (done) + break; + *slash = '/'; } -- 2.20.1