971 abort = 1; |
971 abort = 1; |
972 } |
972 } |
973 } |
973 } |
974 |
974 |
975 if(dir->tagconfig && local_res->tags_updated && !abort) { |
975 if(dir->tagconfig && local_res->tags_updated && !abort) { |
976 // get tags from tagstore (xattr or something else) |
976 sync_update_tags(dir, sn, res, local_res); |
977 // and store it in resource property |
|
978 UcxList *tags = sync_get_file_tags(dir, local_res, NULL); |
|
979 DavXmlNode *prop = create_xml_taglist(tags); |
|
980 if(prop) { |
|
981 dav_set_property_ns(res, DAV_NS, "tags", prop); |
|
982 printf("update: %s\n", local_res->path); |
|
983 if(dav_store(res)) { |
|
984 print_resource_error(sn, local_res->path); |
|
985 ret = -1; |
|
986 sync_error++; |
|
987 } else { |
|
988 sync_success++; |
|
989 local_res->tags_updated = 0; |
|
990 } |
|
991 } |
|
992 } |
977 } |
993 } else { |
978 } else { |
994 // dav_exists() failed |
979 // dav_exists() failed |
995 print_resource_error(sn, local_res->path); |
980 print_resource_error(sn, local_res->path); |
996 ret = -1; |
981 ret = -1; |
1246 if(db_res) { |
1231 if(db_res) { |
1247 res->tags_updated = db_res->tags_updated; |
1232 res->tags_updated = db_res->tags_updated; |
1248 if(db_res->etag) { |
1233 if(db_res->etag) { |
1249 res->etag = strdup(db_res->etag); |
1234 res->etag = strdup(db_res->etag); |
1250 } |
1235 } |
|
1236 if(db_res->tags_hash) { |
|
1237 res->tags_hash = strdup(db_res->tags_hash); |
|
1238 } |
1251 |
1239 |
1252 if(dir->tagconfig && dir->tagconfig->detect_changes && !res->tags_updated) { |
1240 if(dir->tagconfig && dir->tagconfig->detect_changes && !res->tags_updated) { |
1253 UcxBuffer *tags = sync_get_file_tag_data(dir, res); |
1241 UcxBuffer *tags = sync_get_file_tag_data(dir, res); |
1254 if(tags) { |
1242 if(tags) { |
1255 if(db_res->tags_hash) { |
1243 if(db_res->tags_hash) { |
1536 } |
1526 } |
1537 |
1527 |
1538 dav_set_content(res, in, (dav_read_func)fread); |
1528 dav_set_content(res, in, (dav_read_func)fread); |
1539 |
1529 |
1540 if(dir->tagconfig) { |
1530 if(dir->tagconfig) { |
1541 UcxList *tags = sync_get_file_tags(dir, local, NULL); // TODO: check remote tags |
1531 UcxList *tags = sync_get_file_tags(dir, local, NULL); |
1542 DavXmlNode *prop = create_xml_taglist(tags); |
1532 DavXmlNode *prop = create_xml_taglist(tags); |
1543 if(prop) { |
1533 if(prop) { |
1544 dav_set_property_ns(res, DAV_NS, "tags", prop); |
1534 dav_set_property_ns(res, DAV_NS, "tags", prop); |
1545 } |
1535 } |
1546 } |
1536 } |
1669 |
1659 |
1670 return ret; |
1660 return ret; |
1671 } |
1661 } |
1672 |
1662 |
1673 int sync_update_tags(SyncDirectory *dir, DavSession *sn, DavResource *res, LocalResource *local) { |
1663 int sync_update_tags(SyncDirectory *dir, DavSession *sn, DavResource *res, LocalResource *local) { |
1674 if(!dir->tagconfig) { |
1664 if(!dir->tagconfig || !local->tags_updated) { |
1675 return 0; |
1665 return 0; |
1676 } |
1666 } |
1677 |
1667 |
1678 // get local tags |
1668 // get local tags |
1679 DavBool changed = FALSE; |
1669 UcxList *tags = sync_get_file_tags(dir, local, NULL); |
1680 UcxList *tags = sync_get_file_tags(dir, local, &changed); |
1670 |
1681 if(!tags || !changed) { |
1671 DavXmlNode *prop = create_xml_taglist(tags); |
1682 return 0; |
1672 if(prop) { |
1683 } |
1673 dav_set_property_ns(res, DAV_NS, "tags", prop); |
1684 |
1674 } else { |
1685 // local tags changed |
1675 dav_remove_property_ns(res, DAV_NS, "tags"); |
1686 DavBool update_tags = TRUE; |
1676 } |
1687 if(dir->tagconfig->conflict != TAG_NO_CONFLICT) { |
1677 |
1688 // get tags from the server |
1678 printf("update: %s\n", local->path); |
1689 DavXmlNode *tagsprop = dav_get_property_ns(res, DAV_NS, "tags"); |
1679 if(dav_store(res)) { |
1690 UcxList *remote_tags = NULL; |
1680 print_resource_error(sn, local->path); |
1691 if(tagsprop) { |
1681 } else { |
1692 remote_tags = parse_dav_xml_taglist(tagsprop); |
1682 UcxBuffer *tag_data = local->cached_tags; |
1693 } |
1683 if(local->tags_hash) { |
1694 |
1684 free(local->tags_hash); |
1695 if(remote_tags) { |
1685 local->tags_hash = NULL; |
1696 // compare the local tags with server tags |
1686 } |
1697 DavBool tags_equal = TRUE; |
1687 if(tag_data) { |
1698 UcxList *local_t = tags; |
1688 char *hash = dav_create_hash(tag_data->space, tag_data->size); |
1699 UcxList *remote_t = remote_tags; |
1689 local->tags_hash = hash; |
1700 while(local_t) { |
1690 } |
1701 if(!remote_t) { |
1691 local->tags_updated = FALSE; |
1702 tags_equal = FALSE; |
|
1703 break; |
|
1704 } |
|
1705 |
|
1706 DavTag *lt = local_t->data; |
|
1707 DavTag *rt = remote_t->data; |
|
1708 if(strcmp(lt->name, rt->name)) { |
|
1709 tags_equal = FALSE; |
|
1710 break; |
|
1711 } |
|
1712 |
|
1713 local_t = local_t->next; |
|
1714 remote_t = remote_t->next; |
|
1715 } |
|
1716 |
|
1717 if(!tags_equal) { |
|
1718 switch(dir->tagconfig->conflict) { |
|
1719 case TAG_KEEP_LOCAL: break; |
|
1720 case TAG_KEEP_REMOTE: |
|
1721 case TAG_MERGE: { |
|
1722 printf("skip update: %s\n", local->path); |
|
1723 update_tags = FALSE; |
|
1724 break; |
|
1725 } |
|
1726 } |
|
1727 } |
|
1728 |
|
1729 // TODO: free remote_tags |
|
1730 } |
|
1731 } |
|
1732 |
|
1733 if(update_tags) { |
|
1734 DavXmlNode *prop = create_xml_taglist(tags); |
|
1735 if(prop) { |
|
1736 dav_set_property_ns(res, DAV_NS, "tags", prop); |
|
1737 if(dav_store(res)) { |
|
1738 print_resource_error(sn, local->path); |
|
1739 } |
|
1740 } |
|
1741 } |
1692 } |
1742 |
1693 |
1743 // TODO: free stuff |
1694 // TODO: free stuff |
1744 return 0; |
1695 return 0; |
1745 } |
1696 } |