use larger string heap
[rrq/fuse_xattrs.git] / binary_storage.c
index 0a4321e7d69f61bec66a4b44c1c51157e4bdafb4..e8eaba4564fba93768181f6a4c98d3e322715e34 100644 (file)
 #include <string.h>
 #include <errno.h>
 #include <assert.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
 #include "binary_storage.h"
 #include "utils.h"
 #include "fuse_xattrs_config.h"
 #include "stringmem.h"
+#include "xattrs_config.h"
 
 #include <sys/xattr.h>
 
@@ -118,7 +122,7 @@ char *__read_file_sidecar(const char *path, int *buffer_size)
     debug_print("path=%s sidecar_path=%s\n", path, sidecar_path);
 
     char *buffer = __read_file(sidecar_path, buffer_size);
-    free (sidecar_path);
+    strfree (sidecar_path);
     return buffer;
 }
 
@@ -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);