From 3ec07b5958b2731c4bb4355e1893437a0422e376 Mon Sep 17 00:00:00 2001 From: Not Zed Date: Thu, 10 Jun 2021 14:04:43 +0930 Subject: [PATCH] Change the way the db location is found. Don't auto-create any directories. --- dbindex.c | 22 +++++++++++++++++++++- dbindex.h | 5 ++++- disk-indexer.c | 2 -- disk-util.c | 2 -- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/dbindex.c b/dbindex.c index 76d6596..52fc2dc 100644 --- a/dbindex.c +++ b/dbindex.c @@ -19,6 +19,11 @@ // TODO: list.size is really the next id, not size if list items are deleted +#define _GNU_SOURCE + +#include +#include + #include #include #include @@ -189,10 +194,22 @@ cmp_uint(const MDB_val *a, const MDB_val *b) { *(unsigned int *)a->mv_data > *(unsigned int *)b->mv_data; } -dbindex *dbindex_open(const char *path) { +char *dbindex_home(void) { + char *home = getenv("HOME"); + char *path = NULL; + + if (!home || asprintf(&path, "%s/.local/lib/playerz/index", home) < 0) + abort(); + + return path; +} + +dbindex *dbindex_open(const char *ipath) { dbindex *db = calloc(sizeof(*db), 1); int res; MDB_txn *tx; + char *dpath = ipath ? NULL : dbindex_home(); + const char *path = ipath ? ipath : dpath; res = mdb_env_create(&db->env); if (res) @@ -276,6 +293,7 @@ dbindex *dbindex_open(const char *path) { } printf("dbindex open, disk.id=%d list.id=%d file.id=%d\n", db->diskid, db->listid, db->fileid); + free(dpath); return db; fail: @@ -284,6 +302,8 @@ dbindex *dbindex_open(const char *path) { if (db->env) mdb_env_close(db->env); free(db); + free(dpath); + return NULL; } diff --git a/dbindex.h b/dbindex.h index a87f293..73d53fe 100644 --- a/dbindex.h +++ b/dbindex.h @@ -82,6 +82,9 @@ typedef struct dbindex dbindex; typedef struct MDB_txn dbtxn; typedef struct dbscan dbscan; +// database location, default is ~/.local/lib/playerz/db. free after use. +char *dbindex_home(void); + dbindex *dbindex_open(const char *path); void dbindex_close(dbindex *db); @@ -211,4 +214,4 @@ dbfile *dbscan_list_entry_prev(dbscan *scan); int dbscan_list_entry_seq(dbscan *scan); dbid_t dbscan_list_entry_listid(dbscan *scan); -#define MAIN_INDEX "/home/notzed/playerz.db" +#define MAIN_INDEX NULL diff --git a/disk-indexer.c b/disk-indexer.c index ba8c0e3..5977f8b 100644 --- a/disk-indexer.c +++ b/disk-indexer.c @@ -700,8 +700,6 @@ int main(int argc, char **argv) { argc -= 2; } - mkdir(dbdir, 0700); - if (1) { //suffix(dbdir); search_suffix(dbdir); diff --git a/disk-util.c b/disk-util.c index 765089b..50fb969 100644 --- a/disk-util.c +++ b/disk-util.c @@ -58,8 +58,6 @@ int main(int argc, char **argv) { argc -= 2; } - mkdir(dbdir, 0700); - dbindex *db = dbindex_open(dbdir); for (int i=1;i