From 8bb679ea06e38d34f8db08db7ed2e840a64cc16d Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Thu, 5 May 2022 19:00:24 +0200 Subject: [PATCH] create sync state parent directories recursively this may matter with SyncState being for example ~/.local/state/isync/. --- src/common.h | 2 ++ src/drv_maildir.c | 17 +---------------- src/sync_state.c | 2 +- src/util.c | 16 ++++++++++++++++ 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/common.h b/src/common.h index b5f3c74..8f0ded5 100644 --- a/src/common.h +++ b/src/common.h @@ -186,6 +186,8 @@ void ATTR_NORETURN oom( void ); int map_name( const char *arg, char **result, uint reserve, const char *in, const char *out ); +int mkdir_p( char *path, int len ); + #define DEFINE_ARRAY_TYPE(T) \ typedef struct { \ T *data; \ diff --git a/src/drv_maildir.c b/src/drv_maildir.c index c4dea26..6c1daa4 100644 --- a/src/drv_maildir.c +++ b/src/drv_maildir.c @@ -563,21 +563,6 @@ maildir_clear_tmp( char *buf, int bufsz, int bl ) return DRV_OK; } -static int -make_box_dir( char *buf, int bl ) -{ - char *p; - - if (!mkdir( buf, 0700 ) || errno == EEXIST) - return 0; - p = memrchr( buf, '/', (size_t)bl - 1 ); - *p = 0; - if (make_box_dir( buf, (int)(p - buf) )) - return -1; - *p = '/'; - return mkdir( buf, 0700 ); -} - static int maildir_validate( const char *box, int create, maildir_store_t *ctx ) { @@ -593,7 +578,7 @@ maildir_validate( const char *box, int create, maildir_store_t *ctx ) } if (!create) return DRV_BOX_BAD; - if (make_box_dir( buf, bl )) { + if (mkdir_p( buf, bl - 1 )) { sys_error( "Maildir error: cannot create mailbox '%s'", buf ); ctx->conf->failed = FAIL_FINAL; maildir_invoke_bad_callback( ctx ); diff --git a/src/sync_state.c b/src/sync_state.c index c6761e9..0b0cfd6 100644 --- a/src/sync_state.c +++ b/src/sync_state.c @@ -58,7 +58,7 @@ prepare_state( sync_vars_t *svars ) // Note that this may be shorter than the configuration value, // as that may contain a filename prefix. *s = 0; - if (mkdir( svars->dname, 0700 ) && errno != EEXIST) { + if (mkdir_p( svars->dname, s - svars->dname )) { sys_error( "Error: cannot create SyncState directory '%s'", svars->dname ); return 0; } diff --git a/src/util.c b/src/util.c index 9e3c846..df150ee 100644 --- a/src/util.c +++ b/src/util.c @@ -8,6 +8,7 @@ #include "common.h" #include +#include #include #include #include @@ -539,6 +540,21 @@ map_name( const char *arg, char **result, uint reserve, const char *in, const ch return 0; } +int +mkdir_p( char *path, int len ) +{ + if (!mkdir( path, 0700 ) || errno == EEXIST) + return 0; + char *p = memrchr( path, '/', (size_t)len ); + *p = 0; + if (mkdir_p( path, (int)(p - path) )) { + *p = '/'; + return -1; + } + *p = '/'; + return mkdir( path, 0700 ); +} + static int compare_uints( const void *l, const void *r ) {