X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=binary_storage.c;h=e8eaba4564fba93768181f6a4c98d3e322715e34;hb=HEAD;hp=0f0289119baa719a38fff12eea2a920878cb0727;hpb=e69c1b55d384c0931aca7d58a88a402053ae6c18;p=rrq%2Ffuse_xattrs.git diff --git a/binary_storage.c b/binary_storage.c index 0f02891..e8eaba4 100644 --- a/binary_storage.c +++ b/binary_storage.c @@ -12,11 +12,15 @@ #include #include #include +#include +#include +#include #include "binary_storage.h" #include "utils.h" #include "fuse_xattrs_config.h" #include "stringmem.h" +#include "xattrs_config.h" #include @@ -228,6 +232,36 @@ int __write_to_file(FILE *file, const char *name, const char *value, const size_ return 0; } +// Ensure existence of tail path from either source_dir or sidecar_dir +static int ensure_dirs(char *path) { + int n = strlen( path ); + char *p = stralloc( n+1 ); + memcpy( p, path, n+1 ); + char *e = p + 1; + if ( strncmp( path, xattrs_config.source_dir, + xattrs_config.source_dir_size ) == 0 ) { + e += xattrs_config.source_dir_size; + } else if ( strncmp( path, xattrs_config.sidecar_dir, + xattrs_config.sidecar_dir_size ) == 0 ) { + e += xattrs_config.sidecar_dir_size; + } + if ( e - p >= n ) { + return 0; + } + 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 +293,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);