aboutsummaryrefslogtreecommitdiff
path: root/src/npassd/collection.c
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-05-01 11:07:30 +0530
committersinanmohd <sinan@sinanmohd.com>2024-05-01 14:32:03 +0530
commit5eda6884585eda2f8493dcb101ae2ed255ea29d1 (patch)
tree4e5d372f9a68e142dfe1d7835246b25a569bd804 /src/npassd/collection.c
parent176bea08417a3ad89848491a357f73591045ed3c (diff)
npassd/service/ReadAlias: init
Diffstat (limited to 'src/npassd/collection.c')
-rw-r--r--src/npassd/collection.c81
1 files changed, 76 insertions, 5 deletions
diff --git a/src/npassd/collection.c b/src/npassd/collection.c
index a190b79..4bf6ca7 100644
--- a/src/npassd/collection.c
+++ b/src/npassd/collection.c
@@ -18,8 +18,8 @@ static int collection_alloc(sd_bus *bus, struct collection **p,
const char *root, const char *alias,
const char *label, uint64_t created,
uint64_t modified);
-static int collection_db_read(sd_bus *bus, struct sqlite3 *db,
- struct collection **p, const char *alias);
+static int collection_db_root_read(sd_bus *bus, struct sqlite3 *db,
+ struct collection **p, const char *alias);
static int collection_db_write(struct sqlite3 *db, struct collection *c);
static int handle_create_item(__attribute__((unused)) sd_bus_message *msg,
@@ -55,8 +55,8 @@ void collection_free(struct collection *c)
free(c);
}
-static int collection_db_read(sd_bus *bus, struct sqlite3 *db,
- struct collection **p, const char *root)
+static int collection_db_root_read(sd_bus *bus, struct sqlite3 *db,
+ struct collection **p, const char *root)
{
const char *query, *alias, *label;
uint64_t created, modified;
@@ -96,6 +96,60 @@ static int collection_db_read(sd_bus *bus, struct sqlite3 *db,
created = sqlite3_column_int64(stmt, 2);
modified = sqlite3_column_int64(stmt, 3);
+ if (alias == NULL || label == NULL || created == 0 || modified == 0) {
+ print_err("%s", "Found null value in column");
+ ret = -EINVAL;
+ goto out_stmt_finalize;
+ }
+
+ ret = collection_alloc(bus, p, root, alias, label, created, modified);
+
+out_stmt_finalize:
+ sqlite3_finalize(stmt);
+ return ret;
+}
+
+int collection_db_alias_read(sd_bus *bus, struct sqlite3 *db,
+ struct collection **p, const char *alias)
+{
+ const char *query, *root, *label;
+ uint64_t created, modified;
+ sqlite3_stmt *stmt;
+ int ret;
+
+ query = "SELECT root, label, created, modified "
+ "FROM collections "
+ "WHERE collections.alias = ? ";
+
+ ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
+ if (ret != SQLITE_OK) {
+ print_err("%s", "Failed to prepare sql");
+ return -EPERM;
+ }
+
+ ret = sqlite3_bind_text(stmt, 1, alias, -1, NULL);
+ if (ret != SQLITE_OK) {
+ print_err("%s", "Failed to bind sql");
+ ret = -EPERM;
+ goto out_stmt_finalize;
+ }
+
+ ret = sqlite3_step(stmt);
+ if (ret == SQLITE_DONE) {
+ ret = -ENOENT;
+ goto out_stmt_finalize;
+ }
+ if (ret != SQLITE_ROW) {
+ print_err("%s", "Failed to step sql");
+ ret = -EPERM;
+ goto out_stmt_finalize;
+ }
+
+ root = (char *)sqlite3_column_text(stmt, 0);
+ label = (char *)sqlite3_column_text(stmt, 1);
+ created = sqlite3_column_int64(stmt, 2);
+ modified = sqlite3_column_int64(stmt, 3);
+
if (root == NULL || label == NULL || created == 0 || modified == 0) {
print_err("%s", "Found null value in column");
ret = -EINVAL;
@@ -261,6 +315,23 @@ static int collection_alloc(sd_bus *bus, struct collection **p,
return 0;
}
+int collection_alias_search(struct collection_dlist *collections,
+ const char *alias, struct collection **collection)
+{
+ struct collection *c;
+ int ret;
+
+ LIST_FOREACH (c, collections, dlist) {
+ ret = strcmp(c->alias, alias);
+ if (ret == 0) {
+ *collection = c;
+ return 0;
+ }
+ }
+
+ return -ENOENT;
+}
+
int collection_root_make(const char *label, const char *alias, char **root)
{
int ret;
@@ -286,7 +357,7 @@ int collection_new(sd_bus *bus, struct sqlite3 *db, struct collection **p,
uint64_t epoch;
int ret;
- ret = collection_db_read(bus, db, p, root);
+ ret = collection_db_root_read(bus, db, p, root);
if (ret != -ENOENT)
return ret;