Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmark committed Nov 16, 2024
1 parent 2dac8cf commit 3c7ba8d
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions libatalk/cnid/sqlite/cnid_sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static int init_prepared_stmt_lookup(CNID_sqlite_private * db)
EC_ZERO(sqlite3_finalize(db->cnid_lookup_stmt));
EC_NEG1(asprintf
(&sql,
"SELECT Id,Did,Name,DevNo,InodeNo FROM `%s` "
"SELECT Id,Did,Name,DevNo,InodeNo FROM \"%s\" "
"WHERE (Name=? AND Did=?) OR (DevNo=? AND InodeNo=?)",
db->cnid_sqlite_voluuid_str));
EC_ZERO_LOG(sqlite3_prepare_v2(db->cnid_sqlite_con, sql, strlen(sql), &db->cnid_lookup_stmt, NULL));
Expand All @@ -92,7 +92,7 @@ static int init_prepared_stmt_add(CNID_sqlite_private * db)

EC_ZERO(sqlite3_finalize(db->cnid_add_stmt));
EC_NEG1(asprintf(&sql,
"INSERT INTO `%s` (Name,Did,DevNo,InodeNo) VALUES(?,?,?,?)",
"INSERT INTO \"%s\" (Name,Did,DevNo,InodeNo) VALUES(?,?,?,?)",
db->cnid_sqlite_voluuid_str) );

EC_ZERO_LOG(sqlite3_prepare_v2
Expand All @@ -119,7 +119,7 @@ static int init_prepared_stmt_put(CNID_sqlite_private * db)

EC_ZERO(sqlite3_finalize(db->cnid_put_stmt));
EC_NEG1(asprintf(&sql,
"INSERT INTO `%s` (Id,Name,Did,DevNo,InodeNo) VALUES(?,?,?,?,?)",
"INSERT INTO \"%s\" (Id,Name,Did,DevNo,InodeNo) VALUES(?,?,?,?,?)",
db->cnid_sqlite_voluuid_str));

EC_ZERO_LOG(sqlite3_prepare_v2
Expand Down Expand Up @@ -200,7 +200,7 @@ int cnid_sqlite_delete(struct _cnid_db *cdb, const cnid_t id)
"cnid_sqlite_delete(%" PRIu32 "): BEGIN", ntohl(id));

EC_NEG1(cnid_sqlite_execute(db->cnid_sqlite_con,
"DELETE FROM `%s` WHERE Id=%" PRIu32,
"DELETE FROM \"%s\" WHERE Id=%" PRIu32,
db->cnid_sqlite_voluuid_str,
ntohl(id)));

Expand Down Expand Up @@ -270,18 +270,18 @@ int cnid_sqlite_update(struct _cnid_db *cdb,

do {
EC_NEG1(cnid_sqlite_execute(db->cnid_sqlite_con,
"DELETE FROM `%s` WHERE Id=%"
"DELETE FROM \"%s\" WHERE Id=%"
PRIu32,
db->cnid_sqlite_voluuid_str,
ntohl(id)));
EC_NEG1(cnid_sqlite_execute
(db->cnid_sqlite_con,
"DELETE FROM `%s` WHERE Did=%" PRIu32
" AND Name='%s'", db->cnid_sqlite_voluuid_str,
"DELETE FROM \"%s\" WHERE Did=%" PRIu32
" AND Name=\"%s\"", db->cnid_sqlite_voluuid_str,
ntohl(did), name));
EC_NEG1(cnid_sqlite_execute
(db->cnid_sqlite_con,
"DELETE FROM `%s` WHERE DevNo=%" PRIu64
"DELETE FROM \"%s\" WHERE DevNo=%" PRIu64
" AND InodeNo=%" PRIu64,
db->cnid_sqlite_voluuid_str, dev, ino));

Expand All @@ -294,7 +294,9 @@ int cnid_sqlite_update(struct _cnid_db *cdb,

if (sqlite3_step(db->cnid_put_stmt) != SQLITE_ROW) {
switch (sqlite3_errcode(db->cnid_sqlite_con)) {
// FIXME: Is this a valid error code for sqlite?
/* Can such a race condition occur in SQLite?
* Cf. "(15) SQLITE_PROTOCOL" in https://www.sqlite.org/rescode.html
*/
#if 0
case ER_DUP_ENTRY:
/*
Expand Down Expand Up @@ -408,8 +410,7 @@ cnid_t cnid_sqlite_lookup(struct _cnid_db *cdb,

if (retdid != did || STRCMP(retname, !=, name)) {
LOG(log_debug, logtype_cnid,
"cnid_sqlite_lookup(CNID %" PRIu32 ", DID: %"
PRIu32
"cnid_sqlite_lookup(CNID %" PRIu32 ", DID: %" PRIu32
", name: \"%s\"): server side mv oder reused inode",
ntohl(did), name);
LOG(log_debug, logtype_cnid,
Expand All @@ -436,7 +437,7 @@ cnid_t cnid_sqlite_lookup(struct _cnid_db *cdb,
errno = CNID_INVALID;
EC_FAIL;
} else {
/* everythings good */
/* everything is good */
id = retid;
}

Expand Down Expand Up @@ -532,22 +533,20 @@ cnid_t cnid_sqlite_add(struct _cnid_db *cdb,
EC_NEG1(cnid_sqlite_execute
(db->cnid_sqlite_con,
"BEGIN TRANSACTION;"
"UPDATE volumes SET Depleted=1 WHERE VolUUID='%s';"
"TRUNCATE TABLE %s;"
"COMMIT;",
"UPDATE volumes SET Depleted=1 WHERE VolUUID=\"%s\";"
"TRUNCATE TABLE \"%s\";",
db->cnid_sqlite_voluuid_str,
db->cnid_sqlite_voluuid_str));
db->cnid_sqlite_flags |=
CNID_SQLITE_FLAG_DEPLETED;
hint = CNID_INVALID;
if (cnid_sqlite_execute(db->cnid_sqlite_con,
"BEGIN TRANSACTION;"
"UPDATE sqlite_sequence SET seq = 16 WHERE name = '`%s`';"
"INSERT INTO sqlite_sequence (name,seq) SELECT '`%s`', "
"UPDATE sqlite_sequence SET seq = 16 WHERE name = \"%s\";"
"INSERT INTO sqlite_sequence (name,seq) SELECT \"%s\", "
"16 WHERE NOT EXISTS "
"(SELECT changes() AS change "
"FROM sqlite_sequence WHERE change <> 0);"
"COMMIT;",
"FROM sqlite_sequence WHERE change <> 0);",
db->cnid_sqlite_voluuid_str,
db->cnid_sqlite_voluuid_str)) {
LOG(log_error, logtype_cnid, "cnid_sqlite_open: sqlite query error: %s",
Expand Down Expand Up @@ -608,7 +607,7 @@ cnid_t cnid_sqlite_get(struct _cnid_db *cdb, cnid_t did, const char *name,

EC_NEG1(ret = asprintf
(&sql,
"SELECT Id FROM `%s` WHERE Name='%s' AND Did=?",
"SELECT Id FROM \"%s\" WHERE Name=\"%s\" AND Did=?",
db->cnid_sqlite_voluuid_str, name) );
EC_ZERO_LOG(sqlite3_prepare_v2
(db->cnid_sqlite_con, sql, strlen(sql), &transient_stmt, NULL) );
Expand Down Expand Up @@ -653,7 +652,7 @@ char *cnid_sqlite_resolve(struct _cnid_db *cdb, cnid_t * id, void *buffer,

EC_NEG1(ret = asprintf
(&sql,
"SELECT Did,Name FROM `%s` WHERE Id=?",
"SELECT Did,Name FROM \"%s\" WHERE Id=?",
db->cnid_sqlite_voluuid_str) );

EC_ZERO_LOG(sqlite3_prepare_v2
Expand Down Expand Up @@ -705,7 +704,7 @@ int cnid_sqlite_getstamp(struct _cnid_db *cdb, void *buffer,

EC_NEG1(asprintf
(&sql,
"SELECT Stamp FROM volumes WHERE VolPath='%s'",
"SELECT Stamp FROM volumes WHERE VolPath=\"%s\"",
cdb->cnid_db_vol->v_path));

EC_ZERO_LOG(sqlite3_prepare_v2
Expand Down Expand Up @@ -762,19 +761,17 @@ int cnid_sqlite_wipe(struct _cnid_db *cdb)

EC_NEG1(cnid_sqlite_execute(db->cnid_sqlite_con,
"BEGIN TRANSACTION;"
"UPDATE volumes SET Depleted=0 WHERE VolUUID='%s';"
"TRUNCATE TABLE `%s`;"
"COMMIT;",
"UPDATE volumes SET Depleted=0 WHERE VolUUID=\"%s\";"
"TRUNCATE TABLE \"%s\";",
db->cnid_sqlite_voluuid_str,
db->cnid_sqlite_voluuid_str));
if (cnid_sqlite_execute(db->cnid_sqlite_con,
"BEGIN TRANSACTION;"
"UPDATE sqlite_sequence SET seq = 16 WHERE name = '`%s`';"
"INSERT INTO sqlite_sequence (name,seq) SELECT '`%s`', "
"UPDATE sqlite_sequence SET seq = 16 WHERE name = \"%s\";"
"INSERT INTO sqlite_sequence (name,seq) SELECT \"%s\", "
"16 WHERE NOT EXISTS "
"(SELECT changes() AS change "
"FROM sqlite_sequence WHERE change <> 0);"
"COMMIT;",
"FROM sqlite_sequence WHERE change <> 0);",
db->cnid_sqlite_voluuid_str,
db->cnid_sqlite_voluuid_str)) {
LOG(log_error, logtype_cnid, "cnid_sqlite_open: sqlite query error: %s",
Expand Down Expand Up @@ -847,7 +844,7 @@ struct _cnid_db *cnid_sqlite_open(struct cnid_open_args *args)
(CNID_sqlite_private)));
cdb->cnid_db_private = db;

EC_NULL(dbpath = bformat("%s/%s.sqlite", _PATH_STATEDIR "CNID/sqlite", vol->v_localname));
EC_NULL(dbpath = bformat("%sCNID/sqlite/%s.sqlite", _PATH_STATEDIR, vol->v_localname));
EC_NULL(db->cnid_sqlite_voluuid_str = uuid_strip_dashes(vol->v_uuid));

/* Initialize and connect to sqlite3 database */
Expand All @@ -857,7 +854,8 @@ struct _cnid_db *cnid_sqlite_open(struct cnid_open_args *args)
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL))
{
LOG(log_error, logtype_cnid, "sqlite open error: %s, path: %s",
sqlite3_errmsg(db->cnid_sqlite_con));
sqlite3_errmsg(db->cnid_sqlite_con),
bdata(dbpath));
EC_FAIL;
}

Expand Down Expand Up @@ -950,13 +948,13 @@ struct _cnid_db *cnid_sqlite_open(struct cnid_open_args *args)

/* Create volume table */
if (cnid_sqlite_execute(db->cnid_sqlite_con,
"CREATE TABLE IF NOT EXISTS `%s`"
"CREATE TABLE IF NOT EXISTS \"%s\""
"(Id INTEGER PRIMARY KEY AUTOINCREMENT,"
"Name VARCHAR(255) NOT NULL,"
"Did INTEGER NOT NULL,"
"DevNo INTEGER NOT NULL,"
"InodeNo INTEGER NOT NULL,"
"UNIQUE (Did, Name), UNIQUE (DevNo, InodeNo))",
"UNIQUE (Did, Name), UNIQUE (DevNo, InodeNo));",
db->cnid_sqlite_voluuid_str)) {
LOG(log_error, logtype_cnid, "cnid_sqlite_open: sqlite query error: %s",
sqlite3_errmsg(db->cnid_sqlite_con));
Expand All @@ -975,12 +973,11 @@ struct _cnid_db *cnid_sqlite_open(struct cnid_open_args *args)
*/
if (cnid_sqlite_execute(db->cnid_sqlite_con,
"BEGIN TRANSACTION;"
"UPDATE sqlite_sequence SET seq = 16 WHERE name = '`%s`';"
"INSERT INTO sqlite_sequence (name,seq) SELECT '`%s`', "
"UPDATE sqlite_sequence SET seq = 16 WHERE name = \"%s\";"
"INSERT INTO sqlite_sequence (name,seq) SELECT \"%s\", "
"16 WHERE NOT EXISTS "
"(SELECT changes() AS change "
"FROM sqlite_sequence WHERE change <> 0);"
"COMMIT;",
"FROM sqlite_sequence WHERE change <> 0);",
db->cnid_sqlite_voluuid_str,
db->cnid_sqlite_voluuid_str)) {
LOG(log_error, logtype_cnid, "cnid_sqlite_open: sqlite query error: %s",
Expand Down

0 comments on commit 3c7ba8d

Please sign in to comment.