From: Ralph Ronnquist Date: Fri, 12 Nov 2021 03:27:12 +0000 (+1100) Subject: ensure paret directories for sidecars X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;ds=inline;h=9d14bf7e1b9e691428703acdbcebedb6ed2880e4;hp=e70621ea5b523cf48ba0b04629fed9355f6e536a;p=rrq%2Ffuse_xattrs.git ensure paret directories for sidecars --- diff --git a/binary_storage.c b/binary_storage.c index 0f02891..3e41441 100644 --- a/binary_storage.c +++ b/binary_storage.c @@ -12,6 +12,9 @@ #include #include #include +#include +#include +#include #include "binary_storage.h" #include "utils.h" @@ -228,6 +231,25 @@ int __write_to_file(FILE *file, const char *name, const char *value, const size_ return 0; } +static int ensure_dirs(char *path) { + int n = strlen( path ); + char *p = stralloc( n+1 ); + memcpy( p, path, n+1 ); + char *e = p + 1; + for ( ;; ) { + while ( *e && *e != '/' ) e++; + if ( *e ) { + *e = 0; + if ( access( p, F_OK ) && mkdir( p, 0777 ) ) { + return 1; + } + *(e++) = '/'; + } else { + return 0; + } + } +} + /** * * @param path - path to file. @@ -259,6 +281,11 @@ int binary_storage_write_key(const char *path, const char *name, const char *val int status; char *sidecar_path = get_sidecar_path(path); + + if ( ensure_dirs( sidecar_path ) ) { + return -ENOATTR; + } + FILE *file = fopen(sidecar_path, "w"); strfree(sidecar_path);