void psync_decrease_local_folder_taskcnt(psync_folderid_t lfolderid){ psync_sql_res *res; res=psync_sql_prep_statement("UPDATE localfolder SET taskcnt=taskcnt+1 WHERE id=?"); psync_sql_bind_uint(res, 1, lfolderid); psync_sql_run_free(res); assertw(psync_sql_affected_rows()==1); }
static void create_task5(psync_uint_t type, psync_syncid_t syncid, uint64_t entryid){ psync_sql_res *res; res=psync_sql_prep_statement("INSERT INTO task (type, syncid, itemid, localitemid) VALUES (?, ?, ?, 0)"); psync_sql_bind_uint(res, 1, type); psync_sql_bind_uint(res, 2, syncid); psync_sql_bind_uint(res, 3, entryid); psync_sql_run_free(res); }
static void create_task4(psync_uint_t type, uint64_t entryid, const char *name){ psync_sql_res *res; res=psync_sql_prep_statement("INSERT INTO task (type, syncid, itemid, localitemid, name) VALUES (?, 0, ?, 0, ?)"); psync_sql_bind_uint(res, 1, type); psync_sql_bind_uint(res, 2, entryid); psync_sql_bind_string(res, 3, name); psync_sql_run_free(res); }
static void psync_sync_newsyncedfolder(psync_syncid_t syncid){ psync_sql_res *res; psync_uint_row row; uint64_t folderid; psync_synctype_t synctype; psync_sql_start_transaction(); res=psync_sql_query("SELECT folderid, synctype FROM syncfolder WHERE id=? AND flags=0"); psync_sql_bind_uint(res, 1, syncid); row=psync_sql_fetch_rowint(res); if (unlikely_log(!row)){ psync_sql_free_result(res); psync_sql_rollback_transaction(); return; } folderid=row[0]; synctype=row[1]; psync_sql_free_result(res); if (synctype&PSYNC_DOWNLOAD_ONLY){ psync_add_folder_for_downloadsync(syncid, synctype, folderid, 0); } else { res=psync_sql_prep_statement("REPLACE INTO syncedfolder (syncid, folderid, localfolderid, synctype) VALUES (?, ?, 0, ?)"); psync_sql_bind_uint(res, 1, syncid); psync_sql_bind_uint(res, 2, folderid); psync_sql_bind_uint(res, 3, synctype); psync_sql_run_free(res); } res=psync_sql_prep_statement("UPDATE syncfolder SET flags=1 WHERE flags=0 AND id=?"); psync_sql_bind_uint(res, 1, syncid); psync_sql_run_free(res); if (likely_log(psync_sql_affected_rows())){ if (!psync_sql_commit_transaction()){ if (synctype&PSYNC_UPLOAD_ONLY) psync_wake_localscan(); if (synctype&PSYNC_DOWNLOAD_ONLY){ psync_status_recalc_to_download(); psync_send_status_update(); psync_wake_download(); } psync_localnotify_add_sync(syncid); } } else psync_sql_rollback_transaction(); }
void cache_account_teams() { psync_userid_t *teamids = 0; void *params = 0; psync_sql_res *q; psync_sql_lock(); q=psync_sql_prep_statement("DELETE FROM baccountteam "); psync_sql_run_free(q); do_psync_account_teams(teamids, 0, &insert_cache_team, params); psync_sql_unlock(); }
void cache_account_emails() { psync_userid_t *userids = 0; void *params = 0; psync_sql_res *q; psync_sql_lock(); q=psync_sql_prep_statement("DELETE FROM baccountemail "); psync_sql_run_free(q); do_psync_account_users(userids, 0, &insert_cache_email, params); psync_sql_unlock(); }
void psync_task_rename_remote_folder(psync_syncid_t oldsyncid, psync_syncid_t newsyncid, psync_fileid_t localfileid, psync_folderid_t newlocalparentfolderid, const char *newname){ psync_sql_res *res; res=psync_sql_prep_statement("INSERT INTO task (type, syncid, newsyncid, localitemid, newitemid, name, itemid) VALUES (?, ?, ?, ?, ?, ?, 0)"); psync_sql_bind_uint(res, 1, PSYNC_RENAME_REMOTE_FOLDER); psync_sql_bind_uint(res, 2, oldsyncid); psync_sql_bind_uint(res, 3, newsyncid); psync_sql_bind_uint(res, 4, localfileid); psync_sql_bind_uint(res, 5, newlocalparentfolderid); psync_sql_bind_string(res, 6, newname); psync_sql_run_free(res); }
void psync_task_rename_local_file(psync_syncid_t oldsyncid, psync_syncid_t newsyncid, psync_fileid_t fileid, psync_folderid_t oldlocalfolderid, psync_folderid_t newlocalfolderid, const char *newname){ psync_sql_res *res; res=psync_sql_prep_statement("INSERT INTO task (type, syncid, newsyncid, itemid, localitemid, newitemid, name) VALUES (?, ?, ?, ?, ?, ?, ?)"); psync_sql_bind_uint(res, 1, PSYNC_RENAME_LOCAL_FILE); psync_sql_bind_uint(res, 2, oldsyncid); psync_sql_bind_uint(res, 3, newsyncid); psync_sql_bind_uint(res, 4, fileid); psync_sql_bind_uint(res, 5, oldlocalfolderid); psync_sql_bind_uint(res, 6, newlocalfolderid); psync_sql_bind_string(res, 7, newname); psync_sql_run_free(res); }
static void insert_cache_team(int i, const binresult *team, void *_this) { const char *nameret = 0; nameret = psync_find_result(team, "name", PARAM_STR)->str; uint64_t teamid = 0; psync_sql_res *q; teamid = psync_find_result(team, "id", PARAM_NUM)->num; //debug(D_NOTICE, "Team name %s team id %lld\n", nameret,(long long)teamid); q=psync_sql_prep_statement("REPLACE INTO baccountteam (id, name) VALUES (?, ?)"); psync_sql_bind_uint(q, 1, teamid); psync_sql_bind_lstring(q, 2, nameret, strlen(nameret)); psync_sql_run_free(q); }
void cache_ba_my_teams() { psync_socket *sock; binresult *bres; int i; const binresult *users; const binresult *user; const binresult *teams; psync_sql_res *q; psync_sql_lock(); q=psync_sql_prep_statement("DELETE FROM myteams "); psync_sql_run_free(q); binparam params[] = { P_STR("auth", psync_my_auth), P_STR("timeformat", "timestamp"), P_STR("userids", "me"), P_STR("showteams", "1"), P_STR("showeveryone", "1") }; sock = psync_apipool_get(); bres = send_command(sock, "account_users", params); if (likely(bres)) psync_apipool_release(sock); else { psync_apipool_release_bad(sock); debug(D_WARNING, "Send command returned invalid result.\n"); return; } users = psync_find_result(bres, "users", PARAM_ARRAY); if (!users->length) { psync_free(bres); debug(D_WARNING, "Account_users returned empty result!\n"); return; } else { user = users->array[0]; teams = psync_find_result(user, "teams", PARAM_ARRAY); if (teams->length) { for (i = 0; i < teams->length; ++i) cache_my_team(teams->array[i]); } } psync_free(bres); psync_sql_unlock(); }
static void cache_my_team(const binresult *team1) { const char *nameret = 0; const binresult *team; uint64_t teamid = 0; psync_sql_res *q; team = psync_find_result(team1, "team", PARAM_HASH); nameret = psync_find_result(team, "name", PARAM_STR)->str; teamid = psync_find_result(team, "id", PARAM_NUM)->num; //debug(D_NOTICE, "My Team name %s team id %lld\n", nameret,(long long)teamid); q=psync_sql_prep_statement("INSERT INTO myteams (id, name) VALUES (?, ?)"); psync_sql_bind_uint(q, 1, teamid); psync_sql_bind_lstring(q, 2, nameret, strlen(nameret)); psync_sql_run_free(q); }
static void insert_cache_email(int i, const binresult *user, void *_this) { const char *char_field = 0; char_field = psync_find_result(user, "email", PARAM_STR)->str; uint64_t id = 0; psync_sql_res *q; int active = 0; int frozen = 0; active = psync_find_result(user, "active", PARAM_BOOL)->num; frozen = psync_find_result(user, "frozen", PARAM_BOOL)->num; id = psync_find_result(user, "id", PARAM_NUM)->num; if (id && (active || frozen)) { q=psync_sql_prep_statement("REPLACE INTO baccountemail (id, mail, firstname, lastname) VALUES (?, ?, ?, ?)"); psync_sql_bind_uint(q, 1, id); psync_sql_bind_lstring(q, 2, char_field, strlen(char_field)); char_field = psync_find_result(user, "firstname", PARAM_STR)->str; psync_sql_bind_lstring(q, 3, char_field, strlen(char_field)); char_field = psync_find_result(user, "lastname", PARAM_STR)->str; psync_sql_bind_lstring(q, 4, char_field, strlen(char_field)); psync_sql_run_free(q); } }
void get_ba_team_name(uint64_t teamid, char** name /*OUT*/, size_t *length /*OUT*/) { psync_sql_res *res; psync_variant_row row; const char *cstr; res=psync_sql_query("SELECT name FROM baccountteam WHERE id=?"); psync_sql_bind_uint(res, 1, teamid); if ((row=psync_sql_fetch_row(res))){ cstr=psync_get_lstring(row[0], length); *name=(char *)psync_malloc(*length); memcpy(*name, cstr, *length); } else { psync_sql_res *q; psync_userid_t teamids[] = {teamid}; team_visitor_params params = {name, length}; do_psync_account_teams(teamids, 1, ©_team, ¶ms); q=psync_sql_prep_statement("INSERT INTO baccountteam (id, name) VALUES (?, ?)"); psync_sql_bind_uint(q, 1, teamid); psync_sql_bind_lstring(q, 2, *name, *length); psync_sql_run_free(q); } psync_sql_free_result(res); }
void psync_add_folder_for_downloadsync(psync_syncid_t syncid, psync_synctype_t synctype, psync_folderid_t folderid, psync_folderid_t lfoiderid){ psync_sql_res *res; psync_variant_row row; const char *name; psync_folderid_t cfolderid, clfolderid; res=psync_sql_prep_statement("REPLACE INTO syncedfolder (syncid, folderid, localfolderid, synctype) VALUES (?, ?, ?, ?)"); psync_sql_bind_uint(res, 1, syncid); psync_sql_bind_uint(res, 2, folderid); psync_sql_bind_uint(res, 3, lfoiderid); psync_sql_bind_uint(res, 4, synctype); psync_sql_run_free(res); psync_add_folder_to_downloadlist(folderid); res=psync_sql_query("SELECT id, permissions, name FROM folder WHERE parentfolderid=?"); psync_sql_bind_uint(res, 1, folderid); while ((row=psync_sql_fetch_row(res))){ if (psync_get_number(row[1])&PSYNC_PERM_READ){ name=psync_get_string(row[2]); if (psync_is_name_to_ignore(name)) continue; cfolderid=psync_get_number(row[0]); clfolderid=psync_create_local_folder_in_db(syncid, cfolderid, lfoiderid, name); psync_task_create_local_folder(syncid, cfolderid, clfolderid); psync_add_folder_for_downloadsync(syncid, synctype, cfolderid, clfolderid/*, path*/); } } psync_sql_free_result(res); res=psync_sql_query("SELECT id, name FROM file WHERE parentfolderid=?"); psync_sql_bind_uint(res, 1, folderid); while ((row=psync_sql_fetch_row(res))){ name=psync_get_string(row[1]); if (psync_is_name_to_ignore(name)) continue; psync_task_download_file_silent(syncid, psync_get_number(row[0]), lfoiderid, name); } psync_sql_free_result(res); }
static void delete_delayed_sync(uint64_t id){ psync_sql_res *res; res=psync_sql_prep_statement("DELETE FROM syncfolderdelayed WHERE id=?"); psync_sql_bind_uint(res, 1, id); psync_sql_run_free(res); }
psync_folderid_t psync_create_local_folder_in_db(psync_syncid_t syncid, psync_folderid_t folderid, psync_folderid_t localparentfolderid, const char *name){ psync_sql_res *res; psync_uint_row row; psync_folderid_t lfolderid, dbfolderid; const char *ptr; char *vname; debug(D_NOTICE, "creating local folder in db as %lu/%s for folderid %lu", (unsigned long)localparentfolderid, name, (unsigned long)folderid); res=psync_sql_query("SELECT id FROM localfolder WHERE syncid=? AND folderid=?"); psync_sql_bind_uint(res, 1, syncid); psync_sql_bind_uint(res, 2, folderid); row=psync_sql_fetch_rowint(res); if (row) lfolderid=row[0]; else lfolderid=0; psync_sql_free_result(res); if (lfolderid) return lfolderid; vname=NULL; if (name) for (ptr=name; *ptr; ptr++) if (psync_invalid_filename_chars[(unsigned char)*ptr]){ if (!vname) vname=psync_strdup(name); vname[ptr-name]='_'; } if (vname) name=vname; res=psync_sql_prep_statement("INSERT OR IGNORE INTO localfolder (localparentfolderid, folderid, syncid, flags, taskcnt, name) VALUES (?, ?, ?, 0, 1, ?)"); psync_sql_bind_uint(res, 1, localparentfolderid); psync_sql_bind_uint(res, 2, folderid); psync_sql_bind_uint(res, 3, syncid); psync_sql_bind_string(res, 4, name); psync_sql_run(res); if (psync_sql_affected_rows()>0){ lfolderid=psync_sql_insertid(); psync_sql_free_result(res); psync_free(vname); return lfolderid; } psync_sql_free_result(res); res=psync_sql_query("SELECT id, folderid FROM localfolder WHERE localparentfolderid=? AND syncid=? AND name=?"); psync_sql_bind_uint(res, 1, localparentfolderid); psync_sql_bind_uint(res, 2, syncid); psync_sql_bind_string(res, 3, name); row=psync_sql_fetch_rowint(res); if (row){ lfolderid=row[0]; dbfolderid=row[1]; } else{ lfolderid=0; debug(D_ERROR, "local folder %s not found in the database", name); } psync_sql_free_result(res); if (lfolderid && dbfolderid!=folderid){ debug(D_NOTICE, "local folder %lu does not have folderid associated, setting to %lu", (unsigned long)lfolderid, (unsigned long)folderid); res=psync_sql_prep_statement("UPDATE localfolder SET folderid=? WHERE id=?"); psync_sql_bind_uint(res, 1, lfolderid); psync_sql_bind_uint(res, 2, folderid); psync_sql_run_free(res); } psync_increase_local_folder_taskcnt(lfolderid); psync_free(vname); return lfolderid; }