From e397e5ba0239e145ed0e5b01d0052a464b6af8d6 Mon Sep 17 00:00:00 2001 From: Carlo Marcelo Arenas Belon Date: Sun, 5 Feb 2012 22:24:17 -0800 Subject: [PATCH] BUG4621: create ancestors of destination when mkdir fails because the ancestors of a destination directory are missing, fallback to create all ancestors to try to recover from such failure. this allows a destination directory which is missing its parent to be used, by letting rsync create those directories as it does for the last leaf. Signed-off-by: Carlo Marcelo Arenas Belon --- syscall.c | 25 ++++++++++++++++++++++++- 1 files changed, 24 insertions(+), 1 deletions(-) diff --git a/syscall.c b/syscall.c index c85f73e..9bd1ec0 100644 --- a/syscall.c +++ b/syscall.c @@ -219,10 +219,33 @@ void trim_trailing_slashes(char *name) int do_mkdir(char *fname, mode_t mode) { + int ret; if (dry_run) return 0; RETURN_ERROR_IF_RO_OR_LO; trim_trailing_slashes(fname); - return mkdir(fname, mode); + ret = mkdir(fname, mode); + if (ret < 0 && errno == ENOENT) { + char *pname, *p, *np; + + pname = strdup(fname); + if (*pname == '/') + p = pname + 1; + else + p = pname; + + do { + np = strchr(p, '/'); + if (np) { + *np = 0; + mkdir(pname, mode); + *np = '/'; + p = np + 1; + } + } while (np); + ret = mkdir(pname, mode); + free(pname); + } + return ret; } /* like mkstemp but forces permissions */ -- 1.7.8.4