From 9d14bf7e1b9e691428703acdbcebedb6ed2880e4 Mon Sep 17 00:00:00 2001 From: Ralph Ronnquist Date: Fri, 12 Nov 2021 14:27:12 +1100 Subject: [PATCH] ensure paret directories for sidecars --- binary_storage.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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); -- 2.39.2