static void check_c_rbtree_insert_duplicate(void **state) { c_rbtree_t *tree = *state; test_t *testdata; int rc; testdata = malloc(sizeof(test_t)); assert_non_null(testdata); testdata->key = 42; rc = c_rbtree_insert(tree, (void *) testdata); assert_int_equal(rc, 0); /* add again */ testdata = malloc(sizeof(test_t)); assert_non_null(testdata); testdata->key = 42; /* check for duplicate */ rc = c_rbtree_insert(tree, (void *) testdata); assert_int_equal(rc, 1); free(testdata); }
int main(void) { int ntest = 0, exitval = 0; struct c_rbtree *r; /* Create hashtable. */ testresult("create rbtree", r = c_rbtree_new(c_stringequals)); /* Insert data. */ testresult("insert rbtree 1", c_rbtree_insert(r, strdup("charly"), strdup("bacon"))); testresult("insert rbtree 2", c_rbtree_insert(r, strdup("charlene"), strdup("apple"))); /* Fetch data. */ test_expect_str("lookup rbtree 1", c_rbtree_lookup(r, "charly"), "bacon"); test_expect_str("lookup rbtree 2", c_rbtree_lookup(r, "charlene"), "apple"); /* Replace it. */ testresult("replace rbtree existing", c_rbtree_replace(r, strdup("charly"), strdup("fries"))); /* Fetch it again. */ test_expect_str("lookup rbtree 3", c_rbtree_lookup(r, "charly"), "fries"); test_expect_str("lookup rbtree 4", c_rbtree_lookup(r, "charlene"), "apple"); /* Insert new data with replace. */ testresult("replace rbtree new", c_rbtree_replace(r, strdup("elephant"), strdup("peanuts"))); /* Fetch that too. */ test_expect_str("lookup rbtree 5", c_rbtree_lookup(r, "elephant"), "peanuts"); /* Remove it all. - not implemented yet */ /* testresult("clear rbtree", c_rbtree_remove_all(r)); */ /* Destroy rbtree. - not implemented yet */ /* c_rbtree_destroy(r); */ /* testresult("destroy rbtree", 1); */ exit(exitval ? EXIT_FAILURE : EXIT_SUCCESS); }
static void check_c_rbtree_insert_delete(void **state) { c_rbtree_t *tree = NULL; c_rbnode_t *node = NULL; test_t *testdata = NULL; int rc; (void) state; /* unused */ rc = c_rbtree_create(&tree, key_cmp, data_cmp); assert_int_equal(rc, 0); testdata = malloc(sizeof(test_t)); testdata->key = 42; rc = c_rbtree_insert(tree, (void *) testdata); assert_int_equal(rc, 0); node = c_rbtree_head(tree); assert_non_null(node); testdata = c_rbtree_node_data(node); SAFE_FREE(testdata); rc = c_rbtree_node_delete(node); assert_int_equal(rc, 0); c_rbtree_free(tree); }
int csync_statedb_get_below_path( CSYNC *ctx, const char *path ) { int rc; sqlite3_stmt *stmt = NULL; int64_t cnt = 0; char *likepath; int asp; if( !path ) { return -1; } if( !ctx ) { return -1; } rc = sqlite3_prepare_v2(ctx->statedb.db, BELOW_PATH_QUERY, -1, &stmt, NULL); if( rc != SQLITE_OK ) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "WRN: Unable to create stmt for hash query."); return -1; } if (stmt == NULL) { return -1; } asp = asprintf( &likepath, "%s/%%%%", path); if (asp < 0) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "asprintf failed!"); return -1; } sqlite3_bind_text(stmt, 1, likepath, -1, SQLITE_STATIC); cnt = 0; do { csync_file_stat_t *st = NULL; rc = _csync_file_stat_from_metadata_table( &st, stmt); if( st ) { /* store into result list. */ if (c_rbtree_insert(ctx->remote.tree, (void *) st) < 0) { SAFE_FREE(st); ctx->status_code = CSYNC_STATUS_TREE_ERROR; break; } cnt++; } } while( rc == SQLITE_ROW ); if( rc != SQLITE_DONE ) { ctx->status_code = CSYNC_STATUS_TREE_ERROR; } else { CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "%ld entries read below path %s from db.", cnt, path); } sqlite3_finalize(stmt); SAFE_FREE(likepath); return 0; }
static void check_c_rbtree_insert_random(void **state) { c_rbtree_t *tree = *state; int i = 0, rc; for (i = 0; i < 100; i++) { test_t *testdata = NULL; testdata = malloc(sizeof(test_t)); assert_non_null(testdata); testdata->key = i; rc = c_rbtree_insert(tree, testdata); assert_int_equal(rc, 0); } rc = c_rbtree_check_sanity(tree); assert_int_equal(rc, 0); }
static void setup_complete_tree(void **state) { c_rbtree_t *tree = NULL; int i = 0; int rc; rc = c_rbtree_create(&tree, key_cmp, data_cmp); assert_int_equal(rc, 0); for (i = 0; i < 100; i++) { test_t *testdata = NULL; testdata = c_malloc(sizeof(test_t)); assert_non_null(testdata); testdata->key = i; rc = c_rbtree_insert(tree, (void *) testdata); assert_int_equal(rc, 0); } *state = tree; }
static int _csync_detect_update(CSYNC *ctx, const char *file, const csync_vio_file_stat_t *fs, const int type) { uint64_t h = 0; size_t len = 0; size_t size = 0; const char *path = NULL; csync_file_stat_t *st = NULL; csync_file_stat_t *tmp = NULL; CSYNC_EXCLUDE_TYPE excluded; if ((file == NULL) || (fs == NULL)) { errno = EINVAL; ctx->status_code = CSYNC_STATUS_PARAM_ERROR; return -1; } path = file; switch (ctx->current) { case LOCAL_REPLICA: if (strlen(path) <= strlen(ctx->local.uri)) { ctx->status_code = CSYNC_STATUS_PARAM_ERROR; return -1; } path += strlen(ctx->local.uri) + 1; break; case REMOTE_REPLICA: if (strlen(path) <= strlen(ctx->remote.uri)) { ctx->status_code = CSYNC_STATUS_PARAM_ERROR; return -1; } path += strlen(ctx->remote.uri) + 1; break; default: path = NULL; ctx->status_code = CSYNC_STATUS_PARAM_ERROR; return -1; } len = strlen(path); /* Check if file is excluded */ excluded = csync_excluded(ctx, path,type); if (excluded != CSYNC_NOT_EXCLUDED) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "%s excluded (%d)", path, excluded); if (excluded == CSYNC_FILE_EXCLUDE_AND_REMOVE) { switch (ctx->current) { case LOCAL_REPLICA: ctx->local.ignored_cleanup = c_list_append(ctx->local.ignored_cleanup, c_strdup(path)); break; case REMOTE_REPLICA: ctx->remote.ignored_cleanup = c_list_append(ctx->remote.ignored_cleanup, c_strdup(path)); break; default: break; } return 0; } if (excluded == CSYNC_FILE_SILENTLY_EXCLUDED) { return 0; } } h = _hash_of_file(ctx, file ); if( h == 0 ) { return -1; } size = sizeof(csync_file_stat_t) + len + 1; st = c_malloc(size); if (st == NULL) { ctx->status_code = CSYNC_STATUS_MEMORY_ERROR; return -1; } /* Set instruction by default to none */ st->instruction = CSYNC_INSTRUCTION_NONE; st->etag = NULL; st->child_modified = 0; /* check hardlink count */ if (type == CSYNC_FTW_TYPE_FILE ) { if( fs->nlink > 1) { st->instruction = CSYNC_INSTRUCTION_IGNORE; goto out; } if (fs->mtime == 0) { tmp = csync_statedb_get_stat_by_hash(ctx, h); CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s - mtime is zero!", path); if (tmp == NULL) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s - not found in db, IGNORE!", path); st->instruction = CSYNC_INSTRUCTION_IGNORE; } else { SAFE_FREE(st); st = tmp; st->instruction = CSYNC_INSTRUCTION_NONE; CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s - tmp non zero, mtime %lu", path, st->modtime ); tmp = NULL; } goto fastout; /* Skip copying of the etag. That's an important difference to upstream * without etags. */ } } /* Ignore non statable files and other strange cases. */ if (type == CSYNC_FTW_TYPE_SKIP) { st->instruction = CSYNC_INSTRUCTION_NONE; goto out; } if (excluded > CSYNC_NOT_EXCLUDED || type == CSYNC_FTW_TYPE_SLINK) { if( type == CSYNC_FTW_TYPE_SLINK ) { st->error_status = CSYNC_STATUS_INDIVIDUAL_IS_SYMLINK; /* Symbolic links are ignored. */ } st->instruction = CSYNC_INSTRUCTION_IGNORE; goto out; } /* Update detection: Check if a database entry exists. * If not, the file is either new or has been renamed. To see if it is * renamed, the db gets queried by the inode of the file as that one * does not change on rename. */ if (csync_get_statedb_exists(ctx)) { tmp = csync_statedb_get_stat_by_hash(ctx, h); if(tmp && tmp->phash == h ) { /* there is an entry in the database */ /* we have an update! */ CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Database entry found, compare: %" PRId64 " <-> %" PRId64 ", etag: %s <-> %s, inode: %" PRId64 " <-> %" PRId64, ((int64_t) fs->mtime), ((int64_t) tmp->modtime), fs->etag, tmp->etag, (uint64_t) fs->inode, (uint64_t) tmp->inode); if( !fs->etag) { st->instruction = CSYNC_INSTRUCTION_EVAL; goto out; } if((ctx->current == REMOTE_REPLICA && !c_streq(fs->etag, tmp->etag )) || (ctx->current == LOCAL_REPLICA && (fs->mtime != tmp->modtime #if 0 || fs->inode != tmp->inode #endif ))) { /* Comparison of the local inode is disabled because people reported problems * on windows with flacky inode values, see github bug #779 * * The inode needs to be observed because: * $> echo a > a.txt ; echo b > b.txt * both files have the same mtime * sync them. * $> rm a.txt && mv b.txt a.txt * makes b.txt appearing as a.txt yet a sync is not performed because * both have the same modtime as mv does not change that. */ st->instruction = CSYNC_INSTRUCTION_EVAL; goto out; } if (type == CSYNC_FTW_TYPE_DIR && ctx->current == REMOTE_REPLICA && c_streq(fs->file_id, tmp->file_id) && !ctx->read_from_db_disabled) { /* If both etag and file id are equal for a directory, read all contents from * the database. * The comparison of file id ensure that we fetch all the file id when upgrading from * owncloud 5 to owncloud 6. */ CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Reading from database: %s", path); ctx->remote.read_from_db = true; } if (!c_streq(fs->file_id, tmp->file_id) && ctx->current == REMOTE_REPLICA) { /* file id has changed. Which means we need to update the DB. * (upgrade from owncloud 5 to owncloud 6 for instence) */ st->should_update_etag = true; } st->instruction = CSYNC_INSTRUCTION_NONE; } else { enum csync_vio_file_type_e tmp_vio_type = CSYNC_VIO_FILE_TYPE_UNKNOWN; /* check if it's a file and has been renamed */ if (ctx->current == LOCAL_REPLICA) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Checking for rename based on inode # %" PRId64 "", (uint64_t) fs->inode); tmp = csync_statedb_get_stat_by_inode(ctx, fs->inode); /* translate the file type between the two stat types csync has. */ if( tmp && tmp->type == 0 ) { tmp_vio_type = CSYNC_VIO_FILE_TYPE_REGULAR; } else if( tmp && tmp->type == 2 ) { tmp_vio_type = CSYNC_VIO_FILE_TYPE_DIRECTORY; } else { tmp_vio_type = CSYNC_VIO_FILE_TYPE_UNKNOWN; } if (tmp && tmp->inode == fs->inode && tmp_vio_type == fs->type && (tmp->modtime == fs->mtime || fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY) #ifdef NO_RENAME_EXTENSION && _csync_sameextension(tmp->path, path) #endif ) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "pot rename detected based on inode # %" PRId64 "", (uint64_t) fs->inode); /* inode found so the file has been renamed */ st->instruction = CSYNC_INSTRUCTION_EVAL_RENAME; if (fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY) { csync_rename_record(ctx, tmp->path, path); } goto out; } else { /* file not found in statedb */ st->instruction = CSYNC_INSTRUCTION_NEW; goto out; } } else { /* Remote Replica Rename check */ tmp = csync_statedb_get_stat_by_file_id(ctx, fs->file_id); if(tmp ) { /* tmp existing at all */ if ((tmp->type == CSYNC_FTW_TYPE_DIR && fs->type != CSYNC_VIO_FILE_TYPE_DIRECTORY) || (tmp->type == CSYNC_FTW_TYPE_FILE && fs->type != CSYNC_VIO_FILE_TYPE_REGULAR)) { CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "WARN: file types different is not!"); st->instruction = CSYNC_INSTRUCTION_NEW; goto out; } st->instruction = CSYNC_INSTRUCTION_EVAL_RENAME; if (fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY) { csync_rename_record(ctx, tmp->path, path); } else { if( !c_streq(tmp->etag, fs->etag) ) { /* CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "ETags are different!"); */ /* File with different etag, don't do a rename, but download the file again */ st->instruction = CSYNC_INSTRUCTION_NEW; } } goto out; } else { /* file not found in statedb */ st->instruction = CSYNC_INSTRUCTION_NEW; goto out; } } } } else { st->instruction = CSYNC_INSTRUCTION_NEW; } out: /* Set the ignored error string. */ if (st->instruction == CSYNC_INSTRUCTION_IGNORE) { if (excluded == CSYNC_FILE_EXCLUDE_LIST) { st->error_status = CSYNC_STATUS_INDIVIDUAL_IGNORE_LIST; /* File listed on ignore list. */ } else if (excluded == CSYNC_FILE_EXCLUDE_INVALID_CHAR) { st->error_status = CSYNC_STATUS_INDIVIDUAL_IS_INVALID_CHARS; /* File contains invalid characters. */ } } if (st->instruction != CSYNC_INSTRUCTION_NONE && st->instruction != CSYNC_INSTRUCTION_IGNORE && type != CSYNC_FTW_TYPE_DIR) { st->child_modified = 1; } ctx->current_fs = st; csync_file_stat_free(tmp); st->inode = fs->inode; st->mode = fs->mode; st->size = fs->size; st->modtime = fs->mtime; st->nlink = fs->nlink; st->type = type; st->etag = NULL; if( fs->etag ) { SAFE_FREE(st->etag); st->etag = c_strdup(fs->etag); } csync_vio_set_file_id(st->file_id, fs->file_id); if (fs->fields & CSYNC_VIO_FILE_STAT_FIELDS_DIRECTDOWNLOADURL) { SAFE_FREE(st->directDownloadUrl); st->directDownloadUrl = c_strdup(fs->directDownloadUrl); } if (fs->fields & CSYNC_VIO_FILE_STAT_FIELDS_DIRECTDOWNLOADCOOKIES) { SAFE_FREE(st->directDownloadCookies); st->directDownloadCookies = c_strdup(fs->directDownloadCookies); } fastout: /* target if the file information is read from database into st */ st->phash = h; st->pathlen = len; memcpy(st->path, (len ? path : ""), len + 1); switch (ctx->current) { case LOCAL_REPLICA: if (c_rbtree_insert(ctx->local.tree, (void *) st) < 0) { SAFE_FREE(st); ctx->status_code = CSYNC_STATUS_TREE_ERROR; return -1; } break; case REMOTE_REPLICA: if (c_rbtree_insert(ctx->remote.tree, (void *) st) < 0) { SAFE_FREE(st); ctx->status_code = CSYNC_STATUS_TREE_ERROR; return -1; } break; default: break; } CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "file: %s, instruction: %s <<=", st->path, csync_instruction_str(st->instruction)); return 0; }
int csync_statedb_get_below_path( CSYNC *ctx, const char *path ) { int rc; sqlite3_stmt *stmt = NULL; int64_t cnt = 0; if( !path ) { return -1; } if( !ctx || ctx->db_is_empty ) { return -1; } /* Select the entries for anything that starts with (path+'/') * In other words, anything that is between path+'/' and path+'0', * (because '0' follows '/' in ascii) */ const char *below_path_query = "SELECT phash, pathlen, path, inode, uid, gid, mode, modtime, type, md5, fileid, remotePerm, filesize, ignoredChildrenRemote FROM metadata WHERE path > (?||'/') AND path < (?||'0')"; SQLITE_BUSY_HANDLED(sqlite3_prepare_v2(ctx->statedb.db, below_path_query, -1, &stmt, NULL)); ctx->statedb.lastReturnValue = rc; if( rc != SQLITE_OK ) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "WRN: Unable to create stmt for below path query."); return -1; } if (stmt == NULL) { return -1; } sqlite3_bind_text(stmt, 1, path, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 2, path, -1, SQLITE_STATIC); cnt = 0; ctx->statedb.lastReturnValue = rc; do { csync_file_stat_t *st = NULL; rc = _csync_file_stat_from_metadata_table( &st, stmt); if( st ) { /* Check for exclusion from the tree. * Note that this is only a safety net in case the ignore list changes * without a full remote discovery being triggered. */ CSYNC_EXCLUDE_TYPE excluded = csync_excluded_traversal(ctx->excludes, st->path, st->type); if (excluded != CSYNC_NOT_EXCLUDED) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "%s excluded (%d)", st->path, excluded); if (excluded == CSYNC_FILE_EXCLUDE_AND_REMOVE || excluded == CSYNC_FILE_SILENTLY_EXCLUDED) { SAFE_FREE(st); continue; } st->instruction = CSYNC_INSTRUCTION_IGNORE; } /* store into result list. */ if (c_rbtree_insert(ctx->remote.tree, (void *) st) < 0) { SAFE_FREE(st); ctx->status_code = CSYNC_STATUS_TREE_ERROR; break; } cnt++; } } while( rc == SQLITE_ROW ); ctx->statedb.lastReturnValue = rc; if( rc != SQLITE_DONE ) { ctx->status_code = CSYNC_STATUS_TREE_ERROR; } else { CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "%" PRId64 " entries read below path %s from db.", cnt, path); } sqlite3_finalize(stmt); return 0; }
static int _csync_detect_update(CSYNC *ctx, const char *file, const csync_vio_file_stat_t *fs, const int type) { uint64_t h = 0; size_t len = 0; size_t size = 0; const char *path = NULL; csync_file_stat_t *st = NULL; csync_file_stat_t *tmp = NULL; CSYNC_EXCLUDE_TYPE excluded; if ((file == NULL) || (fs == NULL)) { errno = EINVAL; ctx->status_code = CSYNC_STATUS_PARAM_ERROR; return -1; } path = file; switch (ctx->current) { case LOCAL_REPLICA: if (strlen(path) <= strlen(ctx->local.uri)) { ctx->status_code = CSYNC_STATUS_PARAM_ERROR; return -1; } path += strlen(ctx->local.uri) + 1; break; case REMOTE_REPLICA: if (strlen(path) <= strlen(ctx->remote.uri)) { ctx->status_code = CSYNC_STATUS_PARAM_ERROR; return -1; } path += strlen(ctx->remote.uri) + 1; break; default: path = NULL; ctx->status_code = CSYNC_STATUS_PARAM_ERROR; return -1; } len = strlen(path); if (type == CSYNC_FTW_TYPE_SKIP) { excluded =CSYNC_FILE_EXCLUDE_STAT_FAILED; } else { /* Check if file is excluded */ excluded = csync_excluded_traversal(ctx->excludes, path, type); } if( excluded == CSYNC_NOT_EXCLUDED ) { /* Even if it is not excluded by a pattern, maybe it is to be ignored * because it's a hidden file that should not be synced. * This code should probably be in csync_exclude, but it does not have the fs parameter. * Keep it here for now */ if (ctx->ignore_hidden_files && (fs->flags & CSYNC_VIO_FILE_FLAGS_HIDDEN)) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file excluded because it is a hidden file: %s", path); excluded = CSYNC_FILE_EXCLUDE_HIDDEN; } } else { /* File is ignored because it's matched by a user- or system exclude pattern. */ CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "%s excluded (%d)", path, excluded); if (excluded == CSYNC_FILE_EXCLUDE_AND_REMOVE) { return 1; } if (excluded == CSYNC_FILE_SILENTLY_EXCLUDED) { return 1; } } if (ctx->current == REMOTE_REPLICA && ctx->callbacks.checkSelectiveSyncBlackListHook) { if (ctx->callbacks.checkSelectiveSyncBlackListHook(ctx->callbacks.update_callback_userdata, path)) { return 1; } } h = _hash_of_file(ctx, file ); if( h == 0 ) { return -1; } size = sizeof(csync_file_stat_t) + len + 1; st = c_malloc(size); /* Set instruction by default to none */ st->instruction = CSYNC_INSTRUCTION_NONE; st->etag = NULL; st->child_modified = 0; st->has_ignored_files = 0; if (type == CSYNC_FTW_TYPE_FILE ) { if (fs->mtime == 0) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s - mtime is zero!", path); } } if (excluded > CSYNC_NOT_EXCLUDED || type == CSYNC_FTW_TYPE_SLINK) { st->instruction = CSYNC_INSTRUCTION_IGNORE; if (ctx->current_fs) { ctx->current_fs->has_ignored_files = true; } goto out; } /* Update detection: Check if a database entry exists. * If not, the file is either new or has been renamed. To see if it is * renamed, the db gets queried by the inode of the file as that one * does not change on rename. */ if (csync_get_statedb_exists(ctx)) { tmp = csync_statedb_get_stat_by_hash(ctx, h); if(_last_db_return_error(ctx)) { SAFE_FREE(st); SAFE_FREE(tmp); ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL; return -1; } if(tmp && tmp->phash == h ) { /* there is an entry in the database */ /* we have an update! */ CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Database entry found, compare: %" PRId64 " <-> %" PRId64 ", etag: %s <-> %s, inode: %" PRId64 " <-> %" PRId64 ", size: %" PRId64 " <-> %" PRId64 ", perms: %s <-> %s, ignore: %d", ((int64_t) fs->mtime), ((int64_t) tmp->modtime), fs->etag, tmp->etag, (uint64_t) fs->inode, (uint64_t) tmp->inode, (uint64_t) fs->size, (uint64_t) tmp->size, fs->remotePerm, tmp->remotePerm, tmp->has_ignored_files ); if (ctx->current == REMOTE_REPLICA && !c_streq(fs->etag, tmp->etag)) { st->instruction = CSYNC_INSTRUCTION_EVAL; // Preserve the EVAL flag later on if the type has changed. if (_csync_filetype_different(tmp, fs)) { st->child_modified = 1; } goto out; } if (ctx->current == LOCAL_REPLICA && (!_csync_mtime_equal(fs->mtime, tmp->modtime) // zero size in statedb can happen during migration || (tmp->size != 0 && fs->size != tmp->size))) { // Checksum comparison at this stage is only enabled for .eml files, // check #4754 #4755 bool isEmlFile = csync_fnmatch("*.eml", file, FNM_CASEFOLD) == 0; if (isEmlFile && fs->size == tmp->size && tmp->checksumTypeId) { if (ctx->callbacks.checksum_hook) { st->checksum = ctx->callbacks.checksum_hook( file, tmp->checksumTypeId, ctx->callbacks.checksum_userdata); } bool checksumIdentical = false; if (st->checksum) { st->checksumTypeId = tmp->checksumTypeId; checksumIdentical = strncmp(st->checksum, tmp->checksum, 1000) == 0; } if (checksumIdentical) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "NOTE: Checksums are identical, file did not actually change: %s", path); st->instruction = CSYNC_INSTRUCTION_NONE; st->should_update_metadata = true; goto out; } } // Preserve the EVAL flag later on if the type has changed. if (_csync_filetype_different(tmp, fs)) { st->child_modified = 1; } st->instruction = CSYNC_INSTRUCTION_EVAL; goto out; } bool metadata_differ = (ctx->current == REMOTE_REPLICA && (!c_streq(fs->file_id, tmp->file_id) || !c_streq(fs->remotePerm, tmp->remotePerm))) || (ctx->current == LOCAL_REPLICA && fs->inode != tmp->inode); if (type == CSYNC_FTW_TYPE_DIR && ctx->current == REMOTE_REPLICA && !metadata_differ && ctx->read_remote_from_db) { /* If both etag and file id are equal for a directory, read all contents from * the database. * The metadata comparison ensure that we fetch all the file id or permission when * upgrading owncloud */ CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Reading from database: %s", path); ctx->remote.read_from_db = true; } if (metadata_differ) { /* file id or permissions has changed. Which means we need to update them in the DB. */ CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Need to update metadata for: %s", path); st->should_update_metadata = true; } /* If it was remembered in the db that the remote dir has ignored files, store * that so that the reconciler can make advantage of. */ if( ctx->current == REMOTE_REPLICA ) { st->has_ignored_files = tmp->has_ignored_files; } st->instruction = CSYNC_INSTRUCTION_NONE; } else { enum csync_vio_file_type_e tmp_vio_type = CSYNC_VIO_FILE_TYPE_UNKNOWN; /* tmp might point to malloc mem, so free it here before reusing tmp */ SAFE_FREE(tmp); /* check if it's a file and has been renamed */ if (ctx->current == LOCAL_REPLICA) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Checking for rename based on inode # %" PRId64 "", (uint64_t) fs->inode); tmp = csync_statedb_get_stat_by_inode(ctx, fs->inode); if(_last_db_return_error(ctx)) { SAFE_FREE(st); ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL; return -1; } /* translate the file type between the two stat types csync has. */ if( tmp && tmp->type == CSYNC_FTW_TYPE_FILE ) { tmp_vio_type = CSYNC_VIO_FILE_TYPE_REGULAR; } else if( tmp && tmp->type == CSYNC_FTW_TYPE_DIR) { tmp_vio_type = CSYNC_VIO_FILE_TYPE_DIRECTORY; } else if( tmp && tmp->type == CSYNC_FTW_TYPE_SLINK ) { tmp_vio_type = CSYNC_VIO_FILE_TYPE_SYMBOLIC_LINK; } else { tmp_vio_type = CSYNC_VIO_FILE_TYPE_UNKNOWN; } // Default to NEW unless we're sure it's a rename. st->instruction = CSYNC_INSTRUCTION_NEW; bool isRename = tmp && tmp->inode == fs->inode && tmp_vio_type == fs->type && (tmp->modtime == fs->mtime || fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY) #ifdef NO_RENAME_EXTENSION && _csync_sameextension(tmp->path, path) #endif ; // Verify the checksum where possible if (isRename && tmp->checksumTypeId && ctx->callbacks.checksum_hook && fs->type == CSYNC_VIO_FILE_TYPE_REGULAR) { st->checksum = ctx->callbacks.checksum_hook( file, tmp->checksumTypeId, ctx->callbacks.checksum_userdata); if (st->checksum) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "checking checksum of potential rename %s %s <-> %s", path, st->checksum, tmp->checksum); st->checksumTypeId = tmp->checksumTypeId; isRename = strncmp(st->checksum, tmp->checksum, 1000) == 0; } } if (isRename) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "pot rename detected based on inode # %" PRId64 "", (uint64_t) fs->inode); /* inode found so the file has been renamed */ st->instruction = CSYNC_INSTRUCTION_EVAL_RENAME; if (fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY) { csync_rename_record(ctx, tmp->path, path); } } goto out; } else { /* Remote Replica Rename check */ tmp = csync_statedb_get_stat_by_file_id(ctx, fs->file_id); if(_last_db_return_error(ctx)) { SAFE_FREE(st); ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL; return -1; } if(tmp ) { /* tmp existing at all */ if ( _csync_filetype_different(tmp, fs)) { CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "WARN: file types different is not!"); st->instruction = CSYNC_INSTRUCTION_NEW; goto out; } CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "remote rename detected based on fileid %s %s", tmp->path, file); st->instruction = CSYNC_INSTRUCTION_EVAL_RENAME; if (fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY) { csync_rename_record(ctx, tmp->path, path); } else { if( !c_streq(tmp->etag, fs->etag) ) { /* CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "ETags are different!"); */ /* File with different etag, don't do a rename, but download the file again */ st->instruction = CSYNC_INSTRUCTION_NEW; } } goto out; } else { /* file not found in statedb */ st->instruction = CSYNC_INSTRUCTION_NEW; if (fs->type == CSYNC_VIO_FILE_TYPE_DIRECTORY && ctx->current == REMOTE_REPLICA && ctx->callbacks.checkSelectiveSyncNewFolderHook) { if (ctx->callbacks.checkSelectiveSyncNewFolderHook(ctx->callbacks.update_callback_userdata, path)) { SAFE_FREE(st); return 1; } } goto out; } } } } else { CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Unable to open statedb" ); SAFE_FREE(st); ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL; return -1; } out: /* Set the ignored error string. */ if (st->instruction == CSYNC_INSTRUCTION_IGNORE) { if( type == CSYNC_FTW_TYPE_SLINK ) { st->error_status = CSYNC_STATUS_INDIVIDUAL_IS_SYMLINK; /* Symbolic links are ignored. */ } else { if (excluded == CSYNC_FILE_EXCLUDE_LIST) { st->error_status = CSYNC_STATUS_INDIVIDUAL_IGNORE_LIST; /* File listed on ignore list. */ } else if (excluded == CSYNC_FILE_EXCLUDE_INVALID_CHAR) { st->error_status = CSYNC_STATUS_INDIVIDUAL_IS_INVALID_CHARS; /* File contains invalid characters. */ } else if (excluded == CSYNC_FILE_EXCLUDE_LONG_FILENAME) { st->error_status = CSYNC_STATUS_INDIVIDUAL_EXCLUDE_LONG_FILENAME; /* File name is too long. */ } else if (excluded == CSYNC_FILE_EXCLUDE_HIDDEN ) { st->error_status = CSYNC_STATUS_INDIVIDUAL_EXCLUDE_HIDDEN; } else if (excluded == CSYNC_FILE_EXCLUDE_STAT_FAILED) { st->error_status = CSYNC_STATUS_INDIVIDUAL_STAT_FAILED; } } } if (st->instruction != CSYNC_INSTRUCTION_NONE && st->instruction != CSYNC_INSTRUCTION_IGNORE && type != CSYNC_FTW_TYPE_DIR) { st->child_modified = 1; } ctx->current_fs = st; csync_file_stat_free(tmp); st->inode = fs->inode; st->mode = fs->mode; st->size = fs->size; st->modtime = fs->mtime; st->type = type; st->etag = NULL; if( fs->etag ) { SAFE_FREE(st->etag); st->etag = c_strdup(fs->etag); } csync_vio_set_file_id(st->file_id, fs->file_id); if (fs->fields & CSYNC_VIO_FILE_STAT_FIELDS_DIRECTDOWNLOADURL) { SAFE_FREE(st->directDownloadUrl); st->directDownloadUrl = c_strdup(fs->directDownloadUrl); } if (fs->fields & CSYNC_VIO_FILE_STAT_FIELDS_DIRECTDOWNLOADCOOKIES) { SAFE_FREE(st->directDownloadCookies); st->directDownloadCookies = c_strdup(fs->directDownloadCookies); } if (fs->fields & CSYNC_VIO_FILE_STAT_FIELDS_PERM) { strncpy(st->remotePerm, fs->remotePerm, REMOTE_PERM_BUF_SIZE); } st->phash = h; st->pathlen = len; memcpy(st->path, (len ? path : ""), len + 1); switch (ctx->current) { case LOCAL_REPLICA: if (c_rbtree_insert(ctx->local.tree, (void *) st) < 0) { SAFE_FREE(st); ctx->status_code = CSYNC_STATUS_TREE_ERROR; return -1; } break; case REMOTE_REPLICA: if (c_rbtree_insert(ctx->remote.tree, (void *) st) < 0) { SAFE_FREE(st); ctx->status_code = CSYNC_STATUS_TREE_ERROR; return -1; } break; default: break; } CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "file: %s, instruction: %s <<=", st->path, csync_instruction_str(st->instruction)); return 0; }
static int _csync_detect_update(CSYNC *ctx, const char *file, const csync_vio_file_stat_t *fs, const int type) { uint64_t h = 0; size_t len = 0; size_t size = 0; const char *path = NULL; csync_file_stat_t *st = NULL; csync_file_stat_t *tmp = NULL; if ((file == NULL) || (fs == NULL)) { errno = EINVAL; return -1; } path = file; switch (ctx->current) { case LOCAL_REPLICA: if (strlen(path) <= strlen(ctx->local.uri)) { return -1; } path += strlen(ctx->local.uri) + 1; break; case REMOTE_REPLCIA: if (strlen(path) <= strlen(ctx->remote.uri)) { return -1; } path += strlen(ctx->remote.uri) + 1; break; default: path = NULL; return -1; break; } len = strlen(path); h = c_jhash64((uint8_t *) path, len, 0); size = sizeof(csync_file_stat_t) + len + 1; st = c_malloc(size); if (st == NULL) { return -1; } CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "file: %s - hash %llu, st size: %zu", path, (long long unsigned int) h, size); /* Set instruction by default to none */ st->instruction = CSYNC_INSTRUCTION_NONE; /* check hardlink count */ if (type == CSYNC_FTW_TYPE_FILE && fs->nlink > 1) { st->instruction = CSYNC_INSTRUCTION_IGNORE; goto out; } /* Update detection */ if (csync_get_statedb_exists(ctx)) { tmp = csync_statedb_get_stat_by_hash(ctx, h); if (tmp && tmp->phash == h) { /* we have an update! */ if (fs->mtime > tmp->modtime) { st->instruction = CSYNC_INSTRUCTION_EVAL; goto out; } st->instruction = CSYNC_INSTRUCTION_NONE; } else { /* check if the file has been renamed */ if (ctx->current == LOCAL_REPLICA) { tmp = csync_statedb_get_stat_by_inode(ctx, fs->inode); if (tmp && tmp->inode == fs->inode) { /* inode found so the file has been renamed */ st->instruction = CSYNC_INSTRUCTION_RENAME; goto out; } else { /* file not found in statedb */ st->instruction = CSYNC_INSTRUCTION_NEW; goto out; } } /* remote and file not found in statedb */ st->instruction = CSYNC_INSTRUCTION_NEW; } } else { st->instruction = CSYNC_INSTRUCTION_NEW; } out: SAFE_FREE(tmp); st->inode = fs->inode; st->mode = fs->mode; st->size = fs->size; st->modtime = fs->mtime; st->uid = fs->uid; st->gid = fs->gid; st->nlink = fs->nlink; st->type = type; st->phash = h; st->pathlen = len; memcpy(st->path, (len ? path : ""), len + 1); switch (ctx->current) { case LOCAL_REPLICA: if (c_rbtree_insert(ctx->local.tree, (void *) st) < 0) { SAFE_FREE(st); return -1; } break; case REMOTE_REPLCIA: if (c_rbtree_insert(ctx->remote.tree, (void *) st) < 0) { SAFE_FREE(st); return -1; } break; default: break; } CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "file: %s, instruction: %s", st->path, csync_instruction_str(st->instruction)); return 0; }
static void propfind_results_recursive(void *userdata, const ne_uri *uri, const ne_prop_result_set *set) { struct resource *newres = 0; const char *clength, *modtime, *file_id = NULL; const char *resourcetype = NULL; const char *md5sum = NULL; const ne_status *status = NULL; char *path = ne_path_unescape( uri->path ); char *parentPath; char *propfindRootUri = (char*) userdata; propfind_recursive_element_t *element = NULL; propfind_recursive_element_t *pElement = NULL; int depth = 0; (void) status; (void) propfindRootUri; if (!propfind_recursive_cache) { c_rbtree_create(&propfind_recursive_cache, _key_cmp, _data_cmp); } /* Fill the resource structure with the data about the file */ newres = c_malloc(sizeof(struct resource)); ZERO_STRUCTP(newres); newres->uri = path; /* no need to strdup because ne_path_unescape already allocates */ newres->name = c_basename( path ); modtime = ne_propset_value( set, &ls_props[0] ); clength = ne_propset_value( set, &ls_props[1] ); resourcetype = ne_propset_value( set, &ls_props[2] ); md5sum = ne_propset_value( set, &ls_props[3] ); file_id = ne_propset_value( set, &ls_props[4] ); newres->type = resr_normal; if( resourcetype && strncmp( resourcetype, "<DAV:collection>", 16 ) == 0) { newres->type = resr_collection; propfind_recursive_cache_folder_count++; } else { /* DEBUG_WEBDAV("propfind_results_recursive %s [%d]", newres->uri, newres->type); */ propfind_recursive_cache_file_count++; } if (modtime) { newres->modtime = oc_httpdate_parse(modtime); } /* DEBUG_WEBDAV("Parsing Modtime: %s -> %llu", modtime, (unsigned long long) newres->modtime ); */ newres->size = 0; if (clength) { newres->size = atoll(clength); /* DEBUG_WEBDAV("Parsed File size for %s from %s: %lld", newres->name, clength, (long long)newres->size ); */ } if( md5sum ) { newres->md5 = csync_normalize_etag(md5sum); } csync_vio_set_file_id(newres->file_id, file_id); /* DEBUG_WEBDAV("propfind_results_recursive %s [%s] %s", newres->uri, newres->type == resr_collection ? "collection" : "file", newres->md5); */ /* Create new item in rb tree */ if (newres->type == resr_collection) { DEBUG_WEBDAV("propfind_results_recursive %s is a folder", newres->uri); /* Check if in rb tree */ element = c_rbtree_node_data(c_rbtree_find(propfind_recursive_cache,uri->path)); /* If not, create a new item and insert it */ if (!element) { element = c_malloc(sizeof(propfind_recursive_element_t)); element->self = resource_dup(newres); element->children = NULL; element->parent = NULL; c_rbtree_insert(propfind_recursive_cache, element); /* DEBUG_WEBDAV("results_recursive Added collection %s", newres->uri); */ } } /* Check for parent in tree. If exists: Insert it into the children elements there */ parentPath = ne_path_parent(uri->path); if (parentPath) { propfind_recursive_element_t *parentElement = NULL; parentElement = c_rbtree_node_data(c_rbtree_find(propfind_recursive_cache,parentPath)); free(parentPath); if (parentElement) { newres->next = parentElement->children; parentElement->children = newres; /* If the current result is a collection we also need to set its parent */ if (element) element->parent = parentElement; pElement = element; while (pElement) { depth++; pElement = pElement->parent; } if (depth > propfind_recursive_cache_depth) { DEBUG_WEBDAV("propfind_results_recursive %s new maximum tree depth %d", newres->uri, depth); propfind_recursive_cache_depth = depth; } /* DEBUG_WEBDAV("results_recursive Added child %s to collection %s", newres->uri, element->self->uri); */ } else { /* DEBUG_WEBDAV("results_recursive No parent %s found for child %s", parentPath, newres->uri); */ resource_free(newres); newres = NULL; } } }
static void propfind_results_recursive_callback(void *userdata, const ne_uri *uri, const ne_prop_result_set *set) { struct resource *newres = 0; const ne_status *status = NULL; char *path = ne_path_unescape( uri->path ); char *parentPath; propfind_recursive_element_t *element = NULL; propfind_recursive_element_t *pElement = NULL; int depth = 0; csync_owncloud_ctx_t *ctx = (csync_owncloud_ctx_t*) userdata; (void) status; if (!ctx->propfind_recursive_cache) { c_rbtree_create(&ctx->propfind_recursive_cache, _key_cmp, _data_cmp); } /* Fill the resource structure with the data about the file */ newres = c_malloc(sizeof(struct resource)); newres->uri = path; /* no need to strdup because ne_path_unescape already allocates */ newres->name = c_basename( path ); fill_webdav_properties_into_resource(newres, set); if( newres->type == resr_collection) { ctx->propfind_recursive_cache_folder_count++; } else { ctx->propfind_recursive_cache_file_count++; } /* Create new item in rb tree */ if (newres->type == resr_collection) { DEBUG_WEBDAV("propfind_results_recursive %s is a folder", newres->uri); /* Check if in rb tree */ element = c_rbtree_node_data(c_rbtree_find(ctx->propfind_recursive_cache,uri->path)); /* If not, create a new item and insert it */ if (!element) { element = c_malloc(sizeof(propfind_recursive_element_t)); element->self = resource_dup(newres); element->self->next = 0; element->children = NULL; element->parent = NULL; c_rbtree_insert(ctx->propfind_recursive_cache, element); /* DEBUG_WEBDAV("results_recursive Added collection %s", newres->uri); */ } } /* Check for parent in tree. If exists: Insert it into the children elements there */ parentPath = ne_path_parent(uri->path); if (parentPath) { propfind_recursive_element_t *parentElement = NULL; parentElement = c_rbtree_node_data(c_rbtree_find(ctx->propfind_recursive_cache,parentPath)); free(parentPath); if (parentElement) { newres->next = parentElement->children; parentElement->children = newres; /* If the current result is a collection we also need to set its parent */ if (element) element->parent = parentElement; pElement = element; while (pElement) { depth++; pElement = pElement->parent; } if (depth > ctx->propfind_recursive_cache_depth) { DEBUG_WEBDAV("propfind_results_recursive %s new maximum tree depth %d", newres->uri, depth); ctx->propfind_recursive_cache_depth = depth; } /* DEBUG_WEBDAV("results_recursive Added child %s to collection %s", newres->uri, element->self->uri); */ return; } } resource_free(newres); newres = NULL; }
int csync_statedb_get_below_path( CSYNC *ctx, const char *path ) { int rc; sqlite3_stmt *stmt = NULL; int64_t cnt = 0; char *likepath; int asp; int min_path_len; if( !path ) { return -1; } if( !ctx || ctx->db_is_empty ) { return -1; } SQLITE_BUSY_HANDLED(sqlite3_prepare_v2(ctx->statedb.db, BELOW_PATH_QUERY, -1, &stmt, NULL)); ctx->statedb.lastReturnValue = rc; if( rc != SQLITE_OK ) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "WRN: Unable to create stmt for below path query."); return -1; } if (stmt == NULL) { return -1; } asp = asprintf( &likepath, "%s/%%%%", path); if (asp < 0) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "asprintf failed!"); return -1; } min_path_len = strlen(path); sqlite3_bind_int(stmt, 1, min_path_len); sqlite3_bind_text(stmt, 2, likepath, -1, SQLITE_STATIC); cnt = 0; ctx->statedb.lastReturnValue = rc; do { csync_file_stat_t *st = NULL; rc = _csync_file_stat_from_metadata_table( &st, stmt); if( st ) { /* Check for exclusion from the tree. * Note that this is only a safety net in case the ignore list changes * without a full remote discovery being triggered. */ CSYNC_EXCLUDE_TYPE excluded = csync_excluded(ctx, st->path, st->type); if (excluded != CSYNC_NOT_EXCLUDED) { CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "%s excluded (%d)", st->path, excluded); if (excluded == CSYNC_FILE_EXCLUDE_AND_REMOVE || excluded == CSYNC_FILE_SILENTLY_EXCLUDED) { SAFE_FREE(st); continue; } st->instruction = CSYNC_INSTRUCTION_IGNORE; } /* store into result list. */ if (c_rbtree_insert(ctx->remote.tree, (void *) st) < 0) { SAFE_FREE(st); ctx->status_code = CSYNC_STATUS_TREE_ERROR; break; } cnt++; } } while( rc == SQLITE_ROW ); ctx->statedb.lastReturnValue = rc; if( rc != SQLITE_DONE ) { ctx->status_code = CSYNC_STATUS_TREE_ERROR; } else { CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "%" PRId64 " entries read below path %s from db.", cnt, path); } sqlite3_finalize(stmt); SAFE_FREE(likepath); return 0; }