forced setting of HAVE_UTIMENSAT=1
[rrq/fuse_xattrs.git] / binary_storage.c
index 139f3a04c3504973528824ae49e6335f595519c9..c782d8672123924ac72d155614c716f8c8faacb2 100644 (file)
 
 #include "binary_storage.h"
 #include "utils.h"
-#include "const.h"
+#include "fuse_xattrs_config.h"
 
-#include <attr/xattr.h>
+#include <sys/xattr.h>
+
+#ifndef ENOATTR
+    #define ENOATTR ENODATA
+#endif
 
 struct on_memory_attr {
     u_int16_t name_size;
@@ -43,8 +47,12 @@ void __print_on_memory_attr(struct on_memory_attr *attr)
 
 void __free_on_memory_attr(struct on_memory_attr *attr)
 {
-    free(attr->name);
-    free(attr->value);
+    if(attr->name != NULL)
+        free(attr->name);
+
+    if(attr->value != NULL)
+        free(attr->value);
+
     free(attr);
 }
 
@@ -131,6 +139,8 @@ struct on_memory_attr *__read_on_memory_attr(size_t *offset, char *buffer, size_
 {
     debug_print("offset=%zu\n", *offset);
     struct on_memory_attr *attr = malloc(sizeof(struct on_memory_attr));
+    attr->name = NULL;
+    attr->value = NULL;
 
     ////////////////////////////////
     // Read name size
@@ -191,7 +201,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 sanitized_value=%s value_size=%zu\n", name, sanitized_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);
 #endif
 
@@ -207,8 +217,11 @@ int __write_to_file(FILE *file, const char *name, const char *value, const size_
     if (fwrite(&value_size, sizeof(size_t), 1, file) != 1) {
         return -1;
     }
-    if (fwrite(value, value_size, 1, file) != 1) {
-        return -1;
+    // write value content only if we have something to write.
+    if (value_size > 0) {
+        if (fwrite(value, value_size, 1, file) != 1) {
+            return -1;
+        }
     }
 
     return 0;
@@ -221,7 +234,7 @@ int __write_to_file(FILE *file, const char *name, const char *value, const size_
  * @param value - attribute value. size < XATTR_SIZE_MAX
  * @param size
  * @param flags - XATTR_CREATE and/or XATTR_REPLACE
- * @return On success, zero is returned.  On failure, -errno is returnted.
+ * @return On success, zero is returned.  On failure, -errno is returned.
  */
 int binary_storage_write_key(const char *path, const char *name, const char *value, size_t size, int flags)
 {
@@ -236,7 +249,7 @@ int binary_storage_write_key(const char *path, const char *name, const char *val
 
     if (buffer == NULL && buffer_size == -ENOENT && flags & XATTR_REPLACE) {
         error_print("No xattr. (flag XATTR_REPLACE)");
-        return -ENODATA;
+        return -ENOATTR;
     }
 
     if (buffer == NULL && buffer_size != -ENOENT) {
@@ -406,7 +419,7 @@ int binary_storage_list_keys(const char *path, char *list, size_t size)
 
     if (size == 0 && res > XATTR_LIST_MAX) {
         // FIXME: we should return the size or an error ?
-        return -E2BIG;
+        return -ENOSPC;
     }
 
     return (int)res;
@@ -465,4 +478,4 @@ int binary_storage_remove_key(const char *path, const char *name)
     fclose(file);
     free(buffer);
     return res;
-}
\ No newline at end of file
+}