dav/sync.c

changeset 367
4a6a59f89f9f
parent 366
5228b912c925
child 368
11797f33bc24
--- a/dav/sync.c	Fri Feb 02 16:46:04 2018 +0100
+++ b/dav/sync.c	Fri Feb 02 18:57:21 2018 +0100
@@ -42,6 +42,7 @@
 
 #include <libidav/webdav.h>
 #include <libidav/utils.h>
+#include <libidav/crypto.h>
 
 #include "config.h"
 #include "scfg.h"
@@ -1247,6 +1248,21 @@
             res->etag = strdup(db_res->etag);
         }
         
+        if(dir->tagconfig && dir->tagconfig->scan && !res->tags_updated) {
+            UcxBuffer *tags = sync_get_file_tag_data(dir, res);
+            if(tags) {
+                if(db_res->tags_hash) {
+                    char *hash = dav_create_hash(tags->space, tags->size);
+                    if(strcmp(hash, db_res->tags_hash)) {
+                        res->tags_updated = 1;
+                    }
+                    free(hash);
+                } else {
+                    res->tags_updated = 1;
+                }
+            }
+        }
+        
         if(db_res->last_modified == res->last_modified && db_res->size == res->size) {
             return 0;
         }
@@ -1350,6 +1366,28 @@
     return ret;
 }
 
+UcxBuffer* sync_get_file_tag_data(SyncDirectory *dir, LocalResource *res) {
+    if(!dir->tagconfig) {
+        return NULL;
+    }
+    UcxBuffer *buf = NULL;
+    if(dir->tagconfig->store == TAG_STORE_XATTR) {
+        ssize_t tag_length = 0;
+        char *local_path = util_concat_path(dir->path, res->path);
+        char* tag_data = xattr_get(
+                local_path,
+                dir->tagconfig->local_format == TAG_FORMAT_MACOS ? MACOS_TAG_XATTR : "tags",
+                &tag_length);
+        free(local_path);
+        
+        if(tag_length > 0) {
+            buf = ucx_buffer_new(tag_data, (size_t)tag_length, UCX_BUFFER_AUTOFREE);
+            buf->size = (size_t)tag_length;
+        }
+    }
+    return buf;
+}
+
 UcxList* sync_get_file_tags(SyncDirectory *dir, LocalResource *res) {
     UcxList *tags = NULL;
     
@@ -1357,27 +1395,32 @@
         return NULL;
     }
     if(dir->tagconfig->store == TAG_STORE_XATTR) {
-        ssize_t tag_length = 0;
-        char *local_path = util_concat_path(dir->path, res->path);
-        char* tag_data = xattr_get(local_path, "tags", &tag_length);
-        free(local_path);
+        UcxBuffer *tag_buf = res->cached_tags ?
+                res->cached_tags :
+                sync_get_file_tag_data(dir, res);
         
-        if(tag_length > 0) {
+        if(tag_buf) {
+            res->tags_hash = dav_create_hash(tag_buf->space, tag_buf->size);
+            
             switch(dir->tagconfig->local_format) {
                 default: break;
                 case TAG_FORMAT_TEXT: {
-                    tags = parse_text_taglist(tag_data, tag_length);
+                    tags = parse_text_taglist(tag_buf->space, tag_buf->size);
                     break;
                 }
                 case TAG_FORMAT_CSV: {
-                    tags = parse_csv_taglist(tag_data, tag_length);
+                    tags = parse_csv_taglist(tag_buf->space, tag_buf->size);
                     break;
                 }
                 case TAG_FORMAT_MACOS: {
-                    tags = parse_macos_taglist(tag_data, tag_length);
+                    tags = parse_macos_taglist(tag_buf->space, tag_buf->size);
                     break;
                 }
             }
+            if(!res->cached_tags) {
+                // we created the buffer therefore we destroy it
+                ucx_buffer_free(tag_buf);
+            }
         }
     }
     

mercurial