static int remove_task_from_db (SeafCloneManager *mgr, const char *repo_id) { char sql[256]; snprintf (sql, sizeof(sql), "DELETE FROM CloneTasks WHERE repo_id='%s'", repo_id); if (sqlite_query_exec (mgr->db, sql) < 0) return -1; snprintf (sql, sizeof(sql), "DELETE FROM CloneEncInfo WHERE repo_id='%s'", repo_id); if (sqlite_query_exec (mgr->db, sql) < 0) return -1; snprintf (sql, sizeof(sql), "DELETE FROM CloneVersionInfo WHERE repo_id='%s'", repo_id); if (sqlite_query_exec (mgr->db, sql) < 0) return -1; snprintf (sql, sizeof(sql), "DELETE FROM CloneTasksMoreInfo WHERE repo_id='%s'", repo_id); if (sqlite_query_exec (mgr->db, sql) < 0) return -1; return 0; }
static int open_db (SeafBranchManager *mgr) { #ifndef SEAFILE_SERVER char *db_path; const char *sql; db_path = g_build_filename (mgr->seaf->seaf_dir, BRANCH_DB, NULL); if (sqlite_open_db (db_path, &mgr->priv->db) < 0) { g_critical ("[Branch mgr] Failed to open branch db\n"); g_free (db_path); return -1; } g_free (db_path); sql = "CREATE TABLE IF NOT EXISTS Branch (" "name TEXT, repo_id TEXT, commit_id TEXT);"; if (sqlite_query_exec (mgr->priv->db, sql) < 0) return -1; sql = "CREATE INDEX IF NOT EXISTS branch_index ON Branch(repo_id, name);"; if (sqlite_query_exec (mgr->priv->db, sql) < 0) return -1; #else char *sql = "CREATE TABLE IF NOT EXISTS Branch (" "name VARCHAR(10), repo_id CHAR(41), commit_id CHAR(41)," "PRIMARY KEY (repo_id, name))"; if (seaf_db_query (mgr->seaf->db, sql) < 0) return -1; #endif return 0; }
int seaf_clone_manager_init (SeafCloneManager *mgr) { const char *sql; sql = "CREATE TABLE IF NOT EXISTS CloneTasks " "(repo_id TEXT PRIMARY KEY, repo_name TEXT, " "token TEXT, dest_id TEXT," "worktree_parent TEXT, passwd TEXT, " "server_addr TEXT, server_port TEXT, email TEXT);"; if (sqlite_query_exec (mgr->db, sql) < 0) return -1; sql = "CREATE TABLE IF NOT EXISTS CloneTasksMoreInfo " "(repo_id TEXT PRIMARY KEY, more_info TEXT);"; if (sqlite_query_exec (mgr->db, sql) < 0) return -1; sql = "CREATE TABLE IF NOT EXISTS CloneEncInfo " "(repo_id TEXT PRIMARY KEY, enc_version INTEGER, random_key TEXT);"; if (sqlite_query_exec (mgr->db, sql) < 0) return -1; sql = "CREATE TABLE IF NOT EXISTS CloneVersionInfo " "(repo_id TEXT PRIMARY KEY, repo_version INTEGER);"; if (sqlite_query_exec (mgr->db, sql) < 0) return -1; sql = "CREATE TABLE IF NOT EXISTS CloneServerURL " "(repo_id TEXT PRIMARY KEY, server_url TEXT);"; if (sqlite_query_exec (mgr->db, sql) < 0) return -1; return 0; }
int AccountManager::start() { const char *errmsg; const char *sql; QString db_path = QDir(seafApplet->configurator()->seafileDir()).filePath("accounts.db"); if (sqlite3_open (toCStr(db_path), &db)) { errmsg = sqlite3_errmsg (db); qCritical("failed to open account database %s: %s", toCStr(db_path), errmsg ? errmsg : "no error given"); seafApplet->errorAndExit(tr("failed to open account database")); return -1; } // enabling foreign keys, it must be done manually from each connection // and this feature is only supported from sqlite 3.6.19 sql = "PRAGMA foreign_keys=ON;"; if (sqlite_query_exec (db, sql) < 0) { qCritical("sqlite version is too low to support foreign key feature\n"); sqlite3_close(db); db = NULL; return -1; } sql = "CREATE TABLE IF NOT EXISTS Accounts (url VARCHAR(24), " "username VARCHAR(15), token VARCHAR(40), lastVisited INTEGER, " "PRIMARY KEY(url, username))"; if (sqlite_query_exec (db, sql) < 0) { qCritical("failed to create accounts table\n"); sqlite3_close(db); db = NULL; return -1; } updateAccountDatabaseForColumnShibbolethUrl(db); // create ServerInfo table sql = "CREATE TABLE IF NOT EXISTS ServerInfo (" "key TEXT NOT NULL, value TEXT, " "url VARCHAR(24), username VARCHAR(15), " "PRIMARY KEY(url, username, key), " "FOREIGN KEY(url, username) REFERENCES Accounts(url, username) " "ON DELETE CASCADE ON UPDATE CASCADE )"; if (sqlite_query_exec (db, sql) < 0) { qCritical("failed to create server_info table\n"); sqlite3_close(db); db = NULL; return -1; } loadAccounts(); connect(this, SIGNAL(accountsChanged()), this, SLOT(onAccountsChanged())); return 0; }
static int open_db (SeafBranchManager *mgr) { #ifndef SEAFILE_SERVER char *db_path; const char *sql; db_path = g_build_filename (mgr->seaf->seaf_dir, BRANCH_DB, NULL); if (sqlite_open_db (db_path, &mgr->priv->db) < 0) { g_critical ("[Branch mgr] Failed to open branch db\n"); g_free (db_path); return -1; } g_free (db_path); sql = "CREATE TABLE IF NOT EXISTS Branch (" "name TEXT, repo_id TEXT, commit_id TEXT);"; if (sqlite_query_exec (mgr->priv->db, sql) < 0) return -1; sql = "CREATE INDEX IF NOT EXISTS branch_index ON Branch(repo_id, name);"; if (sqlite_query_exec (mgr->priv->db, sql) < 0) return -1; #elif defined FULL_FEATURE char *sql; switch (seaf_db_type (mgr->seaf->db)) { case SEAF_DB_TYPE_MYSQL: sql = "CREATE TABLE IF NOT EXISTS Branch (" "name VARCHAR(10), repo_id CHAR(41), commit_id CHAR(41)," "PRIMARY KEY (repo_id, name)) ENGINE = INNODB"; if (seaf_db_query (mgr->seaf->db, sql) < 0) return -1; break; case SEAF_DB_TYPE_PGSQL: sql = "CREATE TABLE IF NOT EXISTS Branch (" "name VARCHAR(10), repo_id CHAR(40), commit_id CHAR(40)," "PRIMARY KEY (repo_id, name))"; if (seaf_db_query (mgr->seaf->db, sql) < 0) return -1; break; case SEAF_DB_TYPE_SQLITE: sql = "CREATE TABLE IF NOT EXISTS Branch (" "name VARCHAR(10), repo_id CHAR(41), commit_id CHAR(41)," "PRIMARY KEY (repo_id, name))"; if (seaf_db_query (mgr->seaf->db, sql) < 0) return -1; break; } #endif return 0; }
void AvatarService::start() { QDir seafile_dir(seafApplet->configurator()->seafileDir()); if (!seafile_dir.mkpath(kAvatarsDirName)) { qWarning("Failed to create avatars folder"); QString err_msg = tr("Failed to create avatars folder"); seafApplet->errorAndExit(err_msg); } avatars_dir_ = seafile_dir.filePath(kAvatarsDirName); do { const char *errmsg; QString db_path = QDir(seafApplet->configurator()->seafileDir()).filePath("accounts.db"); if (sqlite3_open (db_path.toUtf8().data(), &autoupdate_db_)) { errmsg = sqlite3_errmsg (autoupdate_db_); qWarning("failed to avatar autoupdate database %s: %s", db_path.toUtf8().data(), errmsg ? errmsg : "no error given"); sqlite3_close(autoupdate_db_); autoupdate_db_ = NULL; break; } // enabling foreign keys, it must be done manually from each connection // and this feature is only supported from sqlite 3.6.19 const char *sql = "PRAGMA foreign_keys=ON;"; if (sqlite_query_exec (autoupdate_db_, sql) < 0) { qWarning("sqlite version is too low to support foreign key feature\n"); qWarning("feature avatar autoupdate is disabled\n"); sqlite3_close(autoupdate_db_); autoupdate_db_ = NULL; break; } // create SyncedSubfolder table sql = "CREATE TABLE IF NOT EXISTS Avatar (" "filename TEXT PRIMARY KEY, timestamp BIGINT, " "url VARCHAR(24), username VARCHAR(15), " "FOREIGN KEY(url, username) REFERENCES Accounts(url, username) " "ON DELETE CASCADE ON UPDATE CASCADE )"; if (sqlite_query_exec (autoupdate_db_, sql) < 0) { qWarning("failed to create avatar table\n"); sqlite3_close(autoupdate_db_); autoupdate_db_ = NULL; } } while (0); timer_->start(kCheckPendingInterval); }
void FileCacheDB::start() { const char *errmsg; const char *sql; sqlite3 *db; QString db_path = QDir(seafApplet->configurator()->seafileDir()).filePath("file-cache.db"); if (sqlite3_open (toCStr(db_path), &db)) { errmsg = sqlite3_errmsg (db); qDebug("failed to open file cache database %s: %s", toCStr(db_path), errmsg ? errmsg : "no error given"); seafApplet->errorAndExit(QObject::tr("failed to open file cache database")); return; } sql = "CREATE TABLE IF NOT EXISTS FileCache (" " repo_id VARCHAR(36), " " path VARCHAR(4096), " " file_id VARCHAR(40) NOT NULL, " " PRIMARY KEY (repo_id, path))"; sqlite_query_exec (db, sql); db_ = db; }
bool AccountManager::clearAccountToken(const Account& account) { for (size_t i = 0; i < accounts_.size(); i++) { if (accounts_[i] == account) { accounts_[i].token = ""; break; } } char *zql = sqlite3_mprintf( "UPDATE Accounts " "SET token = NULL " "WHERE url = %Q " " AND username = %Q", // url account.serverUrl.toEncoded().data(), // username account.username.toUtf8().data() ); sqlite_query_exec(db, zql); sqlite3_free(zql); emit accountsChanged(); return true; }
int seaf_branch_manager_update_branch (SeafBranchManager *mgr, SeafBranch *branch) { #ifndef SEAFILE_SERVER sqlite3 *db; char *sql; pthread_mutex_lock (&mgr->priv->db_lock); db = mgr->priv->db; sql = sqlite3_mprintf ("UPDATE Branch SET commit_id = %Q " "WHERE name = %Q AND repo_id = %Q", branch->commit_id, branch->name, branch->repo_id); sqlite_query_exec (db, sql); sqlite3_free (sql); pthread_mutex_unlock (&mgr->priv->db_lock); return 0; #else int rc = seaf_db_statement_query (mgr->seaf->db, "UPDATE Branch SET commit_id = ? " "WHERE name = ? AND repo_id = ?", 3, "string", branch->commit_id, "string", branch->name, "string", branch->repo_id); if (rc < 0) return -1; return 0; #endif }
int seaf_branch_manager_add_branch (SeafBranchManager *mgr, SeafBranch *branch) { #ifndef SEAFILE_SERVER char *sql; pthread_mutex_lock (&mgr->priv->db_lock); sql = sqlite3_mprintf ("INSERT INTO Branch VALUES (%Q, %Q, %Q)", branch->name, branch->repo_id, branch->commit_id); sqlite_query_exec (mgr->priv->db, sql); sqlite3_free (sql); pthread_mutex_unlock (&mgr->priv->db_lock); return 0; #else char sql[256]; snprintf (sql, sizeof(sql), "REPLACE INTO Branch VALUES ('%s', '%s', '%s')", branch->name, branch->repo_id, branch->commit_id); if (seaf_db_query (mgr->seaf->db, sql) < 0) return -1; return 0; #endif }
int seaf_branch_manager_update_branch (SeafBranchManager *mgr, SeafBranch *branch) { #ifndef SEAFILE_SERVER sqlite3 *db; char *sql; pthread_mutex_lock (&mgr->priv->db_lock); db = mgr->priv->db; sql = sqlite3_mprintf ("UPDATE Branch SET commit_id = %Q " "WHERE name = %Q AND repo_id = %Q", branch->commit_id, branch->name, branch->repo_id); sqlite_query_exec (db, sql); sqlite3_free (sql); pthread_mutex_unlock (&mgr->priv->db_lock); return 0; #else char sql[256]; snprintf (sql, sizeof(sql), "UPDATE Branch SET commit_id = '%s' " "WHERE name = '%s' AND repo_id = '%s'", branch->commit_id, branch->name, branch->repo_id); if (seaf_db_query (mgr->seaf->db, sql) < 0) return -1; return 0; #endif }
static int save_task_to_db (SeafCloneManager *mgr, CloneTask *task) { GString *sql = g_string_new (NULL); if (task->passwd) g_string_append_printf (sql, "REPLACE INTO CloneTasks VALUES " "('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", task->repo_id, task->repo_name, task->token, task->peer_id, task->worktree, task->passwd, task->peer_addr, task->peer_port, task->email); else g_string_append_printf (sql, "REPLACE INTO CloneTasks VALUES " "('%s', '%s', '%s', '%s', '%s', NULL, '%s', '%s', '%s')", task->repo_id, task->repo_name, task->token, task->peer_id, task->worktree, task->peer_addr, task->peer_port, task->email); if (sqlite_query_exec (mgr->db, sql->str) < 0) { g_string_free (sql, TRUE); return -1; } g_string_free (sql, TRUE); return 0; }
int seaf_branch_manager_del_branch (SeafBranchManager *mgr, const char *repo_id, const char *name) { #ifndef SEAFILE_SERVER char *sql; pthread_mutex_lock (&mgr->priv->db_lock); sql = sqlite3_mprintf ("DELETE FROM Branch WHERE name = %Q AND " "repo_id = '%s'", name, repo_id); if (sqlite_query_exec (mgr->priv->db, sql) < 0) seaf_warning ("Delete branch %s failed\n", name); sqlite3_free (sql); pthread_mutex_unlock (&mgr->priv->db_lock); return 0; #else int rc = seaf_db_statement_query (mgr->seaf->db, "DELETE FROM Branch WHERE name=? AND repo_id=?", 2, "string", name, "string", repo_id); if (rc < 0) return -1; return 0; #endif }
int seaf_branch_manager_del_branch (SeafBranchManager *mgr, const char *repo_id, const char *name) { #ifndef SEAFILE_SERVER char *sql; pthread_mutex_lock (&mgr->priv->db_lock); sql = sqlite3_mprintf ("DELETE FROM Branch WHERE name = %Q AND " "repo_id = '%s'", name, repo_id); if (sqlite_query_exec (mgr->priv->db, sql) < 0) g_warning ("Delete branch %s failed\n", name); sqlite3_free (sql); pthread_mutex_unlock (&mgr->priv->db_lock); return 0; #else char sql[256]; snprintf (sql, sizeof(sql), "DELETE FROM Branch WHERE name = '%s' AND repo_id = '%s'", name, repo_id); if (seaf_db_query (mgr->seaf->db, sql) < 0) return -1; return 0; #endif }
void FileCacheDB::saveCachedFileId(const QString& repo_id, const QString& path, const QString& file_id) { QString sql = "REPLACE INTO FileCache VALUES ('%1', '%2', '%3')"; sql = sql.arg(repo_id).arg(path).arg(file_id); sqlite_query_exec (db_, toCStr(sql)); }
int AccountManager::saveAccount(const Account& account) { Account new_account = account; bool account_exist = false; { QMutexLocker lock(&accounts_mutex_); for (size_t i = 0; i < accounts_.size(); i++) { if (accounts_[i] == account) { accounts_.erase(accounts_.begin() + i); account_exist = true; break; } } accounts_.insert(accounts_.begin(), new_account); } updateServerInfo(0); qint64 timestamp = QDateTime::currentMSecsSinceEpoch(); char *zql; if (account_exist) { zql = sqlite3_mprintf( "UPDATE Accounts SET token = %Q, lastVisited = %Q, isShibboleth = %Q" "WHERE url = %Q AND username = %Q", // token new_account.token.toUtf8().data(), // lastVisited QString::number(timestamp).toUtf8().data(), // isShibboleth QString::number(new_account.isShibboleth).toUtf8().data(), // url new_account.serverUrl.toEncoded().data(), // username new_account.username.toUtf8().data()); } else { zql = sqlite3_mprintf( "INSERT INTO Accounts(url, username, token, lastVisited, isShibboleth) VALUES (%Q, %Q, %Q, %Q, %Q) ", // url new_account.serverUrl.toEncoded().data(), // username new_account.username.toUtf8().data(), // token new_account.token.toUtf8().data(), // lastVisited QString::number(timestamp).toUtf8().data(), // isShibboleth QString::number(new_account.isShibboleth).toUtf8().data()); } sqlite_query_exec(db, zql); sqlite3_free(zql); emit accountsChanged(); return 0; }
void AccountManager::updateAccountLastVisited(const Account& account) { const char *url = account.serverUrl.toEncoded().data(); QString sql = "UPDATE Accounts SET lastVisited = %1 " "WHERE username = '******' AND url = '%3'"; qint64 timestamp = QDateTime::currentMSecsSinceEpoch(); sql = sql.arg(QString::number(timestamp)).arg(account.username).arg(url); sqlite_query_exec (db, toCStr(sql)); }
bool AvatarService::avatarFileExists(const QString& email) { QString path = getAvatarFilePath(email); bool ret = QFileInfo(path).exists(); if (!ret) { char *zql = sqlite3_mprintf("DELETE FROM Avatar WHERE filename = %Q", path.toUtf8().data()); sqlite_query_exec (autoupdate_db_, zql); sqlite3_free(zql); } return ret; }
int seaf_cs_manager_add_chunk_server (SeafCSManager *mgr, const char *cs_id) { char sql[256]; snprintf (sql, 256, "INSERT INTO chunkservers VALUES (NULL, '%s');", cs_id); if (sqlite_query_exec (mgr->db, sql) < 0) return -1; g_hash_table_insert (mgr->chunk_servers, g_strdup(cs_id), NULL); return 0; }
int seaf_cs_manager_del_chunk_server (SeafCSManager *mgr, const char *cs_id) { char sql[256]; snprintf (sql, 256, "DELETE FROM chunkservers WHERE cs_id = '%s';", cs_id); if (sqlite_query_exec (mgr->db, sql) < 0) return -1; g_hash_table_remove (mgr->chunk_servers, cs_id); return 0; }
void AccountManager::updateAccountLastVisited(const Account& account) { char *zql = sqlite3_mprintf( "UPDATE Accounts SET lastVisited = %Q " "WHERE url = %Q AND username = %Q", // lastVisted QString::number(QDateTime::currentMSecsSinceEpoch()).toUtf8().data(), // url account.serverUrl.toEncoded().data(), // username account.username.toUtf8().data()); sqlite_query_exec(db, zql); sqlite3_free(zql); }
int AccountManager::replaceAccount(const Account& old_account, const Account& new_account) { { QMutexLocker lock(&accounts_mutex_); for (size_t i = 0; i < accounts_.size(); i++) { if (accounts_[i] == old_account) { // TODO copy new_account and old_account before this operation // we might have invalid old_account or new_account after it accounts_[i] = new_account; updateServerInfo(i); break; } } } qint64 timestamp = QDateTime::currentMSecsSinceEpoch(); char *zql = sqlite3_mprintf( "UPDATE Accounts " "SET url = %Q, " " username = %Q, " " token = %Q, " " lastVisited = %Q, " " isShibboleth = %Q " "WHERE url = %Q " " AND username = %Q", // new_url new_account.serverUrl.toEncoded().data(), // username new_account.username.toUtf8().data(), // token new_account.token.toUtf8().data(), // lastvisited QString::number(timestamp).toUtf8().data(), // isShibboleth QString::number(new_account.isShibboleth).toUtf8().data(), // old_url old_account.serverUrl.toEncoded().data(), // username new_account.username.toUtf8().data() ); sqlite_query_exec(db, zql); sqlite3_free(zql); emit accountsChanged(); return 0; }
int seaf_clone_manager_init (SeafCloneManager *mgr) { const char *sql; sql = "CREATE TABLE IF NOT EXISTS CloneTasks " "(repo_id TEXT, repo_name TEXT, " "token TEXT, dest_id TEXT," "worktree_parent TEXT, passwd TEXT, " "server_addr TEXT, server_port TEXT, email TEXT);"; if (sqlite_query_exec (mgr->db, sql) < 0) return -1; return 0; }
int seafile_session_config_set_int (SeafileSession *session, const char *key, int value) { char sql[256]; sqlite3_snprintf (sizeof(sql), sql, "REPLACE INTO Config VALUES ('%q', %d);", key, value); if (sqlite_query_exec (session->config_db, sql) < 0) return -1; return 0; }
int AccountManager::removeAccount(const Account& account) { QString url = account.serverUrl.toEncoded().data(); QString sql = "DELETE FROM Accounts WHERE url = '%1' AND username = '******'"; sql = sql.arg(url).arg(account.username); sqlite_query_exec (db, toCStr(sql)); accounts_.erase(std::remove(accounts_.begin(), accounts_.end(), account), accounts_.end()); emit accountsChanged(); return 0; }
void AvatarService::onGetAvatarSuccess(const QImage& img) { if (!get_avatar_req_) { return; } const QString email = get_avatar_req_->email(); // if no change? early return if (img.isNull()) { get_avatar_req_->deleteLater(); get_avatar_req_ = NULL; queue_->clearWait(email); return; } image_ = img; cache_[email] = img; // save image to avatars/ folder QString path = avatarPathForEmail(get_avatar_req_->account(), email); if (!img.save(path, "PNG")) { qWarning("Unable to save new avatar file %s", path.toUtf8().data()); } // update cache db if (autoupdate_db_) { QString mtime = QString::number(get_avatar_req_->mtime()); char *zql = sqlite3_mprintf( "REPLACE INTO Avatar(filename, timestamp, url, username) " "VALUES (%Q, %Q, %Q, %Q)", path.toUtf8().data(), mtime.toUtf8().data(), get_avatar_req_->account().serverUrl.toEncoded().data(), get_avatar_req_->account().username.toUtf8().data()); sqlite_query_exec(autoupdate_db_, zql); sqlite3_free(zql); } emit avatarUpdated(email, img); get_avatar_req_->deleteLater(); get_avatar_req_ = NULL; queue_->clearWait(email); }
int AccountManager::removeAccount(const Account& account) { char *zql = sqlite3_mprintf( "DELETE FROM Accounts WHERE url = %Q AND username = %Q", // url account.serverUrl.toEncoded().data(), // username account.username.toUtf8().data()); sqlite_query_exec(db, zql); sqlite3_free(zql); QMutexLocker lock(&accounts_mutex_); accounts_.erase(std::remove(accounts_.begin(), accounts_.end(), account), accounts_.end()); emit accountsChanged(); return 0; }
sqlite3 * seafile_session_config_open_db (const char *db_path) { sqlite3 *db; if (sqlite_open_db (db_path, &db) < 0) return NULL; /* * Values are stored in text. You should convert it * back to integer if needed when you read it from * db. */ char *sql = "CREATE TABLE IF NOT EXISTS Config (" "key TEXT PRIMARY KEY, " "value TEXT);"; sqlite_query_exec (db, sql); return db; }
int seafile_session_config_set_string (SeafileSession *session, const char *key, const char *value) { char sql[256]; sqlite3_snprintf (sizeof(sql), sql, "REPLACE INTO Config VALUES ('%q', '%q');", key, value); if (sqlite_query_exec (session->config_db, sql) < 0) return -1; if (g_strcmp0(key, KEY_SYNC_EXTRA_TEMP_FILE) == 0) { if (g_strcmp0(value, "true") == 0) session->sync_extra_temp_file = TRUE; else session->sync_extra_temp_file = FALSE; } return 0; }
int seaf_clone_manager_init (SeafCloneManager *mgr) { const char *sql; sql = "CREATE TABLE IF NOT EXISTS CloneTasks " "(repo_id TEXT, repo_name TEXT, " "token TEXT, dest_id TEXT," "worktree_parent TEXT, passwd TEXT, " "server_addr TEXT, server_port TEXT, email TEXT);"; if (sqlite_query_exec (mgr->db, sql) < 0) return -1; sql = "SELECT * FROM CloneTasks"; if (sqlite_foreach_selected_row (mgr->db, sql, restart_task, mgr) < 0) return -1; g_signal_connect (seaf, "repo-fetched", (GCallback)on_repo_fetched, mgr); return 0; }