rework sanitized_source_directory using realpath
[rrq/fuse_xattrs.git] / binary_storage.c
index c782d8672123924ac72d155614c716f8c8faacb2..0f0289119baa719a38fff12eea2a920878cb0727 100644 (file)
@@ -16,6 +16,7 @@
 #include "binary_storage.h"
 #include "utils.h"
 #include "fuse_xattrs_config.h"
+#include "stringmem.h"
 
 #include <sys/xattr.h>
 
@@ -41,17 +42,17 @@ void __print_on_memory_attr(struct on_memory_attr *attr)
     debug_print("value size: %zu\n", attr->value_size);
     debug_print("sanitized_value: '%s'\n", sanitized_value);
     debug_print("--------------\n");
-    free(sanitized_value);
+    strfree(sanitized_value);
 #endif
 }
 
 void __free_on_memory_attr(struct on_memory_attr *attr)
 {
     if(attr->name != NULL)
-        free(attr->name);
+        strfree(attr->name);
 
     if(attr->value != NULL)
-        free(attr->value);
+        strfree(attr->value);
 
     free(attr);
 }
@@ -117,7 +118,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;
 }
 
@@ -202,7 +203,7 @@ int __write_to_file(FILE *file, const char *name, const char *value, const size_
 #ifdef DEBUG
     char *sanitized_value = sanitize_value(value, value_size);
     debug_print("name='%s' name_size=%hu sanitized_value='%s' value_size=%zu\n", name, name_size, sanitized_value, value_size);
-    free(sanitized_value);
+    strfree(sanitized_value);
 #endif
 
     // write name
@@ -241,7 +242,7 @@ int binary_storage_write_key(const char *path, const char *name, const char *val
 #ifdef DEBUG
     char *sanitized_value = sanitize_value(value, size);
     debug_print("path=%s name=%s sanitized_value=%s size=%zu flags=%d\n", path, name, sanitized_value, size, flags);
-    free(sanitized_value);
+    strfree(sanitized_value);
 #endif
 
     int buffer_size;
@@ -259,14 +260,14 @@ int binary_storage_write_key(const char *path, const char *name, const char *val
     int status;
     char *sidecar_path = get_sidecar_path(path);
     FILE *file = fopen(sidecar_path, "w");
-    free(sidecar_path);
+    strfree(sidecar_path);
 
     if (buffer == NULL) {
         debug_print("new file, writing directly...\n");
         status = __write_to_file(file, name, value, size);
         assert(status == 0);
         fclose(file);
-        free(buffer);
+        strfree(buffer);
         return 0;
     }
     assert(buffer_size >= 0);
@@ -314,7 +315,7 @@ int binary_storage_write_key(const char *path, const char *name, const char *val
     }
 
     fclose(file);
-    free(buffer);
+    strfree(buffer);
     return res;
 }
 
@@ -342,7 +343,7 @@ int binary_storage_read_key(const char *path, const char *name, char *value, siz
     {
         struct on_memory_attr *attr = __read_on_memory_attr(&offset, buffer, _buffer_size);
         if (attr == NULL) {
-            free(buffer);
+            strfree(buffer);
             return -EILSEQ;
         }
 
@@ -358,13 +359,13 @@ int binary_storage_read_key(const char *path, const char *name, char *value, siz
                 error_print("error, attr->value_size=%zu > size=%zu\n", attr->value_size, size);
                 res = -ERANGE;
             }
-            free(buffer);
+            strfree(buffer);
             __free_on_memory_attr(attr);
             return res;
         }
         __free_on_memory_attr(attr);
     }
-    free(buffer);
+    strfree(buffer);
 
     return -ENOATTR;
 }
@@ -395,7 +396,7 @@ int binary_storage_list_keys(const char *path, char *list, size_t size)
     {
         struct on_memory_attr *attr = __read_on_memory_attr(&offset, buffer, _buffer_size);
         if (attr == NULL) {
-            free(buffer);
+            strfree(buffer);
             return -EILSEQ;
         }
 
@@ -404,7 +405,7 @@ int binary_storage_list_keys(const char *path, char *list, size_t size)
                 error_print("Not enough memory allocated. allocated=%zu required=%ld\n",
                             size, attr->name_size + res);
                 __free_on_memory_attr(attr);
-                free(buffer);
+                strfree(buffer);
                 return -ERANGE;
             } else {
                 memcpy(list + res, attr->name, attr->name_size);
@@ -415,7 +416,7 @@ int binary_storage_list_keys(const char *path, char *list, size_t size)
         }
         __free_on_memory_attr(attr);
     }
-    free(buffer);
+    strfree(buffer);
 
     if (size == 0 && res > XATTR_LIST_MAX) {
         // FIXME: we should return the size or an error ?
@@ -439,7 +440,7 @@ int binary_storage_remove_key(const char *path, const char *name)
 
     char *sidecar_path = get_sidecar_path(path);
     FILE *file = fopen(sidecar_path, "w");
-    free(sidecar_path);
+    strfree(sidecar_path);
 
     size_t offset = 0;
     size_t name_len = strlen(name) + 1; // null byte \0
@@ -476,6 +477,6 @@ int binary_storage_remove_key(const char *path, const char *name)
     }
 
     fclose(file);
-    free(buffer);
+    strfree(buffer);
     return res;
 }