UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2019 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef SYNC_H 30 #define SYNC_H 31 32 #include <curl/curl.h> 33 #include <libidav/webdav.h> 34 #include <cx/list.h> 35 #include <cx/hash_map.h> 36 37 #include "scfg.h" 38 #include "config.h" 39 #include "sopt.h" 40 #include "system.h" 41 42 #include "version.h" 43 44 #ifdef _WIN32 45 // TODO: thread includes 46 #else 47 #include <pthread.h> 48 #endif /* _WIN32 */ 49 50 #ifdef __cplusplus 51 extern "C" { 52 #endif 53 54 #define STDIN_BUF_SIZE 2048 55 56 #define VERSION_PATH_PROPERTY "version-collection" 57 58 typedef struct SyncFile { 59 SyncDirectory *dir; 60 char *path; 61 } SyncFile; 62 63 typedef struct MetadataHashes { 64 char *tags; 65 char *tags_remote; 66 char *xattr; 67 DavBool update_tags; 68 DavBool update_tags_remote; 69 DavBool update_xattr; 70 } MetadataHashes; 71 72 typedef struct MovedFile { 73 DavResource *resource; 74 LocalResource *content; 75 DavBool copy; 76 } MovedFile; 77 78 enum RemoteChangeType { 79 REMOTE_NO_CHANGE = 0, 80 REMOTE_CHANGE_MODIFIED, 81 REMOTE_CHANGE_NEW, 82 REMOTE_CHANGE_DELETED, 83 REMOTE_CHANGE_CONFLICT_LOCAL_MODIFIED, 84 REMOTE_CHANGE_METADATA, 85 REMOTE_CHANGE_MKDIR, 86 REMOTE_CHANGE_LINK 87 }; 88 typedef enum RemoteChangeType RemoteChangeType; 89 90 typedef struct RemoteChange { 91 DavResource *resource; 92 RemoteChangeType type; 93 } RemoteChange; 94 95 void print_usage(char *cmd); 96 97 #ifndef _WIN32 98 pthread_t start_sighandler(pthread_mutex_t *mutex) ; 99 void stop_sighandler(pthread_mutex_t *mutex, pthread_t tid); 100 #else 101 // TODO: just dummies at the moment 102 int start_sighandler(int* mutex); 103 int stop_sighandler(int* mutex, int tid); 104 #endif 105 106 void res2map(DavResource *root, CxMap *map); 107 108 int cmd_pull(CmdArgs *args, DavBool incoming); 109 int cmd_push(CmdArgs *args, DavBool outgoing, DavBool archive); 110 int cmd_restore(CmdArgs *args); 111 112 void log_printf(const char *fmt, ...); 113 114 void print_outgoing( 115 CmdArgs *args, 116 CxList *ls_new, 117 CxList *ls_modified, 118 CxList *ls_conflict, 119 CxList *ls_update, 120 CxList *ls_delete, 121 CxList *ls_move, 122 CxList *ls_copy, 123 CxList *ls_mkcol); 124 125 RemoteChangeType resource_get_remote_change( 126 CmdArgs *a, 127 DavResource *res, 128 SyncDirectory *dir, 129 SyncDatabase *db); 130 131 void sync_set_metadata_from_stat(LocalResource *local, SYS_STAT *s); 132 int sync_move_resource( 133 CmdArgs *a, 134 SyncDirectory *dir, 135 DavResource *res, 136 LocalResource *content, 137 DavBool copy, 138 SyncDatabase *db, 139 int *counter); 140 int sync_get_resource( 141 CmdArgs *a, 142 SyncDirectory *dir, 143 const char *path, 144 DavResource *res, 145 SyncDatabase *db, 146 DavBool update_local, 147 int *counter); 148 int sync_get_collection( 149 CmdArgs *a, 150 SyncDirectory *dir, 151 DavResource *res, 152 SyncDatabase *db); 153 int sync_remove_local_resource(SyncDirectory *dir, LocalResource *res); 154 int sync_remove_local_directory(SyncDirectory *dir, LocalResource *res); 155 void rename_conflict_file(SyncDirectory *dir, SyncDatabase *db, char *path, DavBool copy); 156 char* create_tmp_download_path(char *path); 157 void move_to_trash(SyncDirectory *dir, char *path); 158 159 CxList* local_scan(SyncDirectory *dir, SyncDatabase *db); 160 CxList* read_changes(SyncDirectory *dir, SyncDatabase *db); 161 LocalResource* local_resource_new(SyncDirectory *dir, SyncDatabase *db, char *path); 162 char* local_resource_path(LocalResource *res); 163 int local_resource_is_changed( 164 SyncDirectory *dir, 165 SyncDatabase *db, 166 LocalResource *res, 167 CxMap *svrres, 168 DavBool restore_deleted, 169 DavBool restore_modified); 170 int remote_resource_is_changed( 171 DavSession *sn, 172 SyncDirectory *dir, 173 SyncDatabase *db, 174 DavResource *remote, 175 LocalResource *res, 176 DavBool *equal); 177 int local_resource_load_metadata(SyncDirectory *dir, LocalResource *res); 178 179 void local_resource_set_etag(LocalResource *local, const char *etag); 180 181 char* resource_local_path(DavResource *res); 182 183 size_t resource_get_blocksize(SyncDirectory *dir, LocalResource *local, DavResource *res, off_t filesize); 184 185 int resource_pathlen_cmp(LocalResource *res1, LocalResource *res2, void *n); 186 int resource_path_cmp(LocalResource *res1, LocalResource *res2, void *n); 187 188 DavResource *versioning_simple_find(DavResource *res, const char *version); 189 DavResource *versioning_deltav_find(DavResource *res, const char *version); 190 191 int sync_set_status(DavResource *res, char *status); 192 int sync_remove_status(DavResource *res); 193 CxBuffer* sync_get_file_tag_data(SyncDirectory *dir, LocalResource *res); 194 CxList* sync_get_file_tags(SyncDirectory *dir, LocalResource *res, DavBool *changed, char **newhash); 195 int sync_tags_equal(CxList *tags1, CxList *tags2); 196 int sync_store_metadata(SyncDirectory *dir, const char *path, LocalResource *local, DavResource *res); 197 int sync_store_xattr(SyncDirectory *dir, const char *path, XAttributes *xattr); 198 int sync_store_tags(SyncDirectory *dir, const char *path, LocalResource *local, DavResource *res); 199 int sync_store_tags_local(SyncDirectory *dir, LocalResource *local, const char *path, CxList *tags); 200 int sync_put_resource( 201 SyncDirectory *dir, 202 DavResource *res, 203 LocalResource *local, 204 int *counter); 205 int sync_mkdir(SyncDirectory *dir, DavResource *res, LocalResource *local); 206 int sync_move_remote_resource( 207 SyncDirectory *dir, 208 SyncDatabase *db, 209 DavResource *origin, 210 LocalResource *local, 211 DavBool copy, 212 int *counter); 213 int sync_delete_remote_resource(SyncDirectory *dir, DavSession *sn, LocalResource *res, int *counter, CxList *cols); 214 MetadataHashes sync_set_metadata_properties( 215 SyncDirectory *dir, 216 DavSession *sn, 217 DavResource *res, 218 LocalResource *local, 219 DavBool force); 220 int sync_update_metadata( 221 SyncDirectory *dir, 222 DavSession *sn, 223 DavResource *res, 224 LocalResource *local); 225 226 void update_parts(LocalResource *local, CxList *updates, uint64_t numparts); 227 228 void remove_deleted_conflicts(SyncDirectory *dir, SyncDatabase *db); 229 230 int cmd_list_conflicts(CmdArgs *args); 231 int cmd_resolve_conflicts(CmdArgs *args); 232 int cmd_delete_conflicts(CmdArgs *args); 233 234 int cmd_list_versions(CmdArgs *args); 235 236 int cmd_trash_info(CmdArgs *args); 237 int cmd_empty_trash(CmdArgs *args); 238 239 int cmd_add_tag(CmdArgs *args); 240 int cmd_remove_tag(CmdArgs *args); 241 int cmd_set_tags(CmdArgs *args); 242 int cmd_list_tags(CmdArgs *args); 243 int cmd_tagop(CmdArgs *args, int cmd); 244 245 /* 246 * gets the syncdir and resource path for a given file path 247 * 248 * returns 0 or error code: 249 * 1: file not found 250 * 2: file permission error 251 * 3: stat error 252 * 4: file is not in any syncdir 253 * 5: file is in multiple syncdirs 254 * 6: syncdir not found 255 */ 256 int sync_get_file(CmdArgs *args, const char *path, SyncFile *f, DavBool dostat); 257 void sync_print_get_file_err(const char *path, int err); 258 259 int isfileindir(SyncDirectory *dir, const char *path, SyncFile *f); 260 261 int cmd_add_directory(CmdArgs *args); 262 int cmd_list_dirs(); 263 int cmd_check_repositories(CmdArgs *args); 264 265 char* create_locktoken_file(const char *syncdirname, const char *locktoken); 266 267 /* 268 * if encryption is enabled, the crypto-hash is returned as hex string 269 * otherwise the content-hash is returned 270 */ 271 char* sync_get_content_hash(DavResource *res); 272 273 /* 274 * if encryption is enabled, sets the crypto-hash property 275 * otherwise it sets the content-hash property 276 * 277 * hashdata must be raw sha256 data and 32 bytes long 278 */ 279 void sync_set_content_hash(DavResource *res, const unsigned char *hashdata); 280 281 #ifdef __cplusplus 282 } 283 #endif 284 285 #endif /* SYNC_H */ 286 287