static void _csync_exclude_add(CSYNC *ctx, const char *string) { if (ctx->excludes == NULL) { ctx->excludes = c_strlist_new(32); } if (ctx->excludes->count == ctx->excludes->size) { ctx->excludes = c_strlist_expand(ctx->excludes, 2 * ctx->excludes->size); } c_strlist_add(ctx->excludes, string); }
static int _csync_exclude_add(CSYNC *ctx, const char *string) { c_strlist_t *list; if (ctx->excludes == NULL) { ctx->excludes = c_strlist_new(32); if (ctx->excludes == NULL) { return -1; } } if (ctx->excludes->count == ctx->excludes->size) { list = c_strlist_expand(ctx->excludes, 2 * ctx->excludes->size); if (list == NULL) { return -1; } ctx->excludes = list; } return c_strlist_add(ctx->excludes, string); }
END_TEST START_TEST (check_c_strlist_expand) { size_t i = 0; c_strlist_t *strlist = NULL; strlist = c_strlist_new(42); fail_if(strlist == NULL, NULL); fail_unless(strlist->size == 42, NULL); fail_unless(strlist->count == 0, NULL); strlist = c_strlist_expand(strlist, 84); fail_if(strlist == NULL, NULL); fail_unless(strlist->size == 84, NULL); for (i = 0; i < strlist->size; i++) { fail_unless(c_strlist_add(strlist, (char *) "foobar") == 0, NULL); } c_strlist_destroy(strlist); }
END_TEST START_TEST (check_c_strlist_add) { size_t i = 0; c_strlist_t *strlist = NULL; strlist = c_strlist_new(42); fail_if(strlist == NULL, NULL); fail_unless(strlist->size == 42, NULL); fail_unless(strlist->count == 0, NULL); for (i = 0; i < strlist->size; i++) { fail_unless(c_strlist_add(strlist, (char *) "foobar") == 0, NULL); } fail_unless(strlist->count == 42, NULL); fail_unless(strcmp(strlist->vector[0], "foobar") == 0, NULL); fail_unless(strcmp(strlist->vector[41], "foobar") == 0, NULL); c_strlist_destroy(strlist); }
static CSYNC_EXCLUDE_TYPE _csync_excluded_common(c_strlist_t *excludes, const char *path, int filetype, bool check_leading_dirs) { size_t i = 0; const char *bname = NULL; size_t blen = 0; char *conflict = NULL; int rc = -1; CSYNC_EXCLUDE_TYPE match = CSYNC_NOT_EXCLUDED; CSYNC_EXCLUDE_TYPE type = CSYNC_NOT_EXCLUDED; /* split up the path */ bname = strrchr(path, '/'); if (bname) { bname += 1; // don't include the / } else { bname = path; } blen = strlen(bname); rc = csync_fnmatch("._sync_*.db*", bname, 0); if (rc == 0) { match = CSYNC_FILE_SILENTLY_EXCLUDED; goto out; } rc = csync_fnmatch(".sync_*.db*", bname, 0); if (rc == 0) { match = CSYNC_FILE_SILENTLY_EXCLUDED; goto out; } rc = csync_fnmatch(".csync_journal.db*", bname, 0); if (rc == 0) { match = CSYNC_FILE_SILENTLY_EXCLUDED; goto out; } // check the strlen and ignore the file if its name is longer than 254 chars. // whenever changing this also check createDownloadTmpFileName if (blen > 254) { match = CSYNC_FILE_EXCLUDE_LONG_FILENAME; goto out; } #ifdef _WIN32 // Windows cannot sync files ending in spaces (#2176). It also cannot // distinguish files ending in '.' from files without an ending, // as '.' is a separator that is not stored internally, so let's // not allow to sync those to avoid file loss/ambiguities (#416) if (blen > 1) { if (bname[blen-1]== ' ') { match = CSYNC_FILE_EXCLUDE_TRAILING_SPACE; goto out; } else if (bname[blen-1]== '.' ) { match = CSYNC_FILE_EXCLUDE_INVALID_CHAR; goto out; } } if (csync_is_windows_reserved_word(bname)) { match = CSYNC_FILE_EXCLUDE_INVALID_CHAR; goto out; } // Filter out characters not allowed in a filename on windows const char *p = NULL; for (p = path; *p; p++) { switch (*p) { case '\\': case ':': case '?': case '*': case '"': case '>': case '<': case '|': match = CSYNC_FILE_EXCLUDE_INVALID_CHAR; goto out; default: break; } } #endif rc = csync_fnmatch(".owncloudsync.log*", bname, 0); if (rc == 0) { match = CSYNC_FILE_SILENTLY_EXCLUDED; goto out; } /* Always ignore conflict files, not only via the exclude list */ rc = csync_fnmatch("*_conflict-*", bname, 0); if (rc == 0) { match = CSYNC_FILE_EXCLUDE_CONFLICT; goto out; } if (getenv("CSYNC_CONFLICT_FILE_USERNAME")) { rc = asprintf(&conflict, "*_conflict_%s-*", getenv("CSYNC_CONFLICT_FILE_USERNAME")); if (rc < 0) { goto out; } rc = csync_fnmatch(conflict, path, 0); if (rc == 0) { match = CSYNC_FILE_EXCLUDE_CONFLICT; SAFE_FREE(conflict); goto out; } SAFE_FREE(conflict); } if( ! excludes ) { goto out; } c_strlist_t *path_components = NULL; if (check_leading_dirs) { /* Build a list of path components to check. */ path_components = c_strlist_new(32); char *path_split = strdup(path); size_t len = strlen(path_split); for (i = len; ; --i) { // read backwards until a path separator is found if (i != 0 && path_split[i-1] != '/') { continue; } // check 'basename', i.e. for "/foo/bar/fi" we'd check 'fi', 'bar', 'foo' if (path_split[i] != 0) { c_strlist_add_grow(&path_components, path_split + i); } if (i == 0) { break; } // check 'dirname', i.e. for "/foo/bar/fi" we'd check '/foo/bar', '/foo' path_split[i-1] = '\0'; c_strlist_add_grow(&path_components, path_split); } SAFE_FREE(path_split); } /* Loop over all exclude patterns and evaluate the given path */ for (i = 0; match == CSYNC_NOT_EXCLUDED && i < excludes->count; i++) { bool match_dirs_only = false; char *pattern = excludes->vector[i]; type = CSYNC_FILE_EXCLUDE_LIST; if (!pattern[0]) { /* empty pattern */ continue; } /* Excludes starting with ']' means it can be cleanup */ if (pattern[0] == ']') { ++pattern; if (filetype == CSYNC_FTW_TYPE_FILE) { type = CSYNC_FILE_EXCLUDE_AND_REMOVE; } } /* Check if the pattern applies to pathes only. */ if (pattern[strlen(pattern)-1] == '/') { if (!check_leading_dirs && filetype == CSYNC_FTW_TYPE_FILE) { continue; } match_dirs_only = true; pattern[strlen(pattern)-1] = '\0'; /* Cut off the slash */ } /* check if the pattern contains a / and if, compare to the whole path */ if (strchr(pattern, '/')) { rc = csync_fnmatch(pattern, path, FNM_PATHNAME); if( rc == 0 ) { match = type; } /* if the pattern requires a dir, but path is not, its still not excluded. */ if (match_dirs_only && filetype != CSYNC_FTW_TYPE_DIR) { match = CSYNC_NOT_EXCLUDED; } } /* if still not excluded, check each component and leading directory of the path */ if (match == CSYNC_NOT_EXCLUDED && check_leading_dirs) { size_t j = 0; if (match_dirs_only && filetype == CSYNC_FTW_TYPE_FILE) { j = 1; // skip the first entry, which is bname } for (; j < path_components->count; ++j) { rc = csync_fnmatch(pattern, path_components->vector[j], 0); if (rc == 0) { match = type; break; } } } else if (match == CSYNC_NOT_EXCLUDED && !check_leading_dirs) { rc = csync_fnmatch(pattern, bname, 0); if (rc == 0) { match = type; } } if (match_dirs_only) { /* restore the '/' */ pattern[strlen(pattern)] = '/'; } } c_strlist_destroy(path_components); out: return match; }
/* query the statedb, caller must free the memory */ c_strlist_t *csync_statedb_query(sqlite3 *db, const char *statement) { int err = SQLITE_OK; int rc = SQLITE_OK; size_t i = 0; size_t busy_count = 0; size_t retry_count = 0; size_t column_count = 0; sqlite3_stmt *stmt; const char *tail = NULL; const char *field = NULL; c_strlist_t *result = NULL; int row = 0; do { /* compile SQL program into a virtual machine, reattempteing if busy */ do { if (busy_count) { /* sleep 100 msec */ usleep(100000); CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "sqlite3_prepare: BUSY counter: %zu", busy_count); } err = sqlite3_prepare(db, statement, -1, &stmt, &tail); } while (err == SQLITE_BUSY && busy_count ++ < 120); if (err != SQLITE_OK) { if (err == SQLITE_BUSY) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Gave up waiting for lock to clear"); } CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "sqlite3_compile error: %s - on query %s", sqlite3_errmsg(db), statement); break; } else { busy_count = 0; column_count = sqlite3_column_count(stmt); /* execute virtual machine by iterating over rows */ for(;;) { err = sqlite3_step(stmt); if (err == SQLITE_BUSY) { if (busy_count++ > 120) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Busy counter has reached its maximum. Aborting this sql statement"); break; } /* sleep 100 msec */ usleep(100000); CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "sqlite3_step: BUSY counter: %zu", busy_count); continue; } if (err == SQLITE_MISUSE) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite3_step: MISUSE!!"); } if (err == SQLITE_DONE) { if (result == NULL) { result = c_strlist_new(1); } break; } if (err == SQLITE_ERROR) { break; } row++; if( result ) { result = c_strlist_expand(result, row*column_count); } else { result = c_strlist_new(column_count); } if (result == NULL) { return NULL; } /* iterate over columns */ for (i = 0; i < column_count; i++) { field = (const char *) sqlite3_column_text(stmt, i); if (!field) field = ""; // CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "sqlite3_column_text: %s", field); if (c_strlist_add(result, field) < 0) { c_strlist_destroy(result); return NULL; } } } /* end infinite for loop */ /* deallocate vm resources */ rc = sqlite3_finalize(stmt); if (err != SQLITE_DONE && rc != SQLITE_SCHEMA) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite_step error: %s - on query: %s", sqlite3_errmsg(db), statement); if (result != NULL) { c_strlist_destroy(result); } return NULL; } if (rc == SQLITE_SCHEMA) { retry_count ++; CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "SQLITE_SCHEMA error occurred on query: %s", statement); if (retry_count < 10) { CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Retrying now."); } else { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "RETRY count has reached its maximum. Aborting statement: %s", statement); if (result != NULL) { c_strlist_destroy(result); } result = c_strlist_new(1); } } } } while (rc == SQLITE_SCHEMA && retry_count < 10); return result; }