create sync state parent directories recursively

this may matter with SyncState being for example ~/.local/state/isync/.
This commit is contained in:
Oswald Buddenhagen 2022-05-05 19:00:24 +02:00
parent 1ba0cd7b96
commit 8bb679ea06
4 changed files with 20 additions and 17 deletions

View File

@ -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 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) \ #define DEFINE_ARRAY_TYPE(T) \
typedef struct { \ typedef struct { \
T *data; \ T *data; \

View File

@ -563,21 +563,6 @@ maildir_clear_tmp( char *buf, int bufsz, int bl )
return DRV_OK; 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 static int
maildir_validate( const char *box, int create, maildir_store_t *ctx ) 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) if (!create)
return DRV_BOX_BAD; 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 ); sys_error( "Maildir error: cannot create mailbox '%s'", buf );
ctx->conf->failed = FAIL_FINAL; ctx->conf->failed = FAIL_FINAL;
maildir_invoke_bad_callback( ctx ); maildir_invoke_bad_callback( ctx );

View File

@ -58,7 +58,7 @@ prepare_state( sync_vars_t *svars )
// Note that this may be shorter than the configuration value, // Note that this may be shorter than the configuration value,
// as that may contain a filename prefix. // as that may contain a filename prefix.
*s = 0; *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 ); sys_error( "Error: cannot create SyncState directory '%s'", svars->dname );
return 0; return 0;
} }

View File

@ -8,6 +8,7 @@
#include "common.h" #include "common.h"
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <ctype.h> #include <ctype.h>
#include <pwd.h> #include <pwd.h>
@ -539,6 +540,21 @@ map_name( const char *arg, char **result, uint reserve, const char *in, const ch
return 0; 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 static int
compare_uints( const void *l, const void *r ) compare_uints( const void *l, const void *r )
{ {