Backport support for (non)recursive disk add, plus update command.
authorNot Zed <notzed@gmail.com>
Mon, 8 Jan 2024 05:18:59 +0000 (15:48 +1030)
committerNot Zed <notzed@gmail.com>
Mon, 8 Jan 2024 05:18:59 +0000 (15:48 +1030)
Makefile
blobs.c
dbindex.c
dbindex.h
disk-indexer.c
disk-monitor.c
index-util.c
indexer-cmd.c

index 57b111f..bc15db5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -34,7 +34,7 @@ LDFLAGS=$(foreach x,$(pkgs),$(LDFLAGS_$(x)))
 LDLIBS=$(foreach x,$(pkgs),$(LDLIBS_$(x))) -lrt -lpthread ../libeze/libeze.a
 
 PROGS=disk-indexer disk-monitor audio-cmd music-player input-monitor http-monitor index-util
-
+TESTS=test-index
 GENERATED=dbmarshal.c dbmarshal.h player.h
 
 all: $(PROGS)
@@ -61,6 +61,7 @@ dbmarshal.o: dbmarshal.c dbmarshal.h
 
 clean:
        rm -f $(PROGS)
+       rm -f $(TESTS)
        rm -f $(GENERATED)
        rm -f *.o *.d
 
diff --git a/blobs.c b/blobs.c
index 67d2058..bb8f819 100644 (file)
--- a/blobs.c
+++ b/blobs.c
@@ -27,9 +27,9 @@
 
 ez_blob_desc DBDISK_DESC[] = {
        EZ_BLOB_START(dbdisk, 1, 4),
-       EZ_BLOB_STRING(dbdisk, 1, uuid),
-       EZ_BLOB_STRING(dbdisk, 2, label),
-       EZ_BLOB_STRING(dbdisk, 3, type),
+       EZ_BLOB_INT32(dbdisk, 1, flags),
+       EZ_BLOB_STRING(dbdisk, 2, uuid),
+       EZ_BLOB_STRING(dbdisk, 3, label),
        EZ_BLOB_STRING(dbdisk, 4, mount),
 };
 
index 7352251..c282f6d 100644 (file)
--- a/dbindex.c
+++ b/dbindex.c
@@ -231,29 +231,6 @@ dbindex *dbindex_open(const char *ipath) {
                }
        }
 
-
-       if (0) {
-               MDB_cursor *cursor;
-               MDB_val key = { 0 }, data = { 0 };
-               int r;
-
-               printf("Known disks:\n");
-               mdb_cursor_open(tx, db->disk, &cursor);
-               r = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
-               while (r == 0) {
-                       dbdisk *p = ez_basic_decode(DBDISK_DESC, (ez_blob *)&data);
-                       p->id = *(int *)key.mv_data;
-                       printf("id=%d\n", p->id);
-                       printf(" uuid=%s\n", p->uuid);
-                       printf(" label=%s\n", p->label);
-                       printf(" type=%s\n", p->type);
-                       printf(" mount=%s\n", p->mount);
-                       ez_blob_free(DBDISK_DESC, p);
-                       r = mdb_cursor_get(cursor, &key, &data, MDB_NEXT);
-               }
-               mdb_cursor_close(cursor);
-       }
-
        res |= mdb_txn_commit(tx);
 
        if (res) {
@@ -624,8 +601,6 @@ int dbfile_node_scan_path(dbtxn *tx, dbindex *db, dbid_t diskid, const char *pat
        // path must end in /
        // scan all items >= path where there is only 1 / more
 
-       printf("@ dir '%s'\n", start);
-
        key.mv_data = start;
        key.mv_size = len;
 
@@ -641,12 +616,9 @@ int dbfile_node_scan_path(dbtxn *tx, dbindex *db, dbid_t diskid, const char *pat
                        node->file = dbfile_get(tx, db, files[i]);
                        node->path = node->file->path;
                        ez_tree_put(tree, node);
-
-                       printf("%4d: path '%.*s'\n", *(dbid_t *)dat.mv_data, (int)key.mv_size, (char *)key.mv_data);
                }
                res = mdb_cursor_get(cursor, &key, &dat, MDB_NEXT_MULTIPLE);
        }
-       printf("res=%d '%s'\n", res, mdb_strerror(res));
 
        mdb_cursor_close(cursor);
 fail:
@@ -1910,9 +1882,9 @@ void dbindex_dump(dbindex *db) {
                        dbdisk *p = ez_basic_decode(DBDISK_DESC, (ez_blob *)&data);
                        p->id = *(int *)key.mv_data;
                        printf("id=%d\n", p->id);
+                       printf(" flags=%08x\n", p->flags);
                        printf(" uuid=%s\n", p->uuid);
                        printf(" label=%s\n", p->label);
-                       printf(" type=%s\n", p->type);
                        printf(" mount=%s\n", p->mount);
                        ez_blob_free(DBDISK_DESC, p);
                        r = mdb_cursor_get(cursor, &key, &data, MDB_NEXT);
index 6b53ef5..fc74aba 100644 (file)
--- a/dbindex.h
+++ b/dbindex.h
@@ -26,12 +26,16 @@ typedef uint32_t dbid_t;
 
 struct dbdisk {
        dbid_t id;
+       uint32_t flags;
        char *uuid;
        char *label;
-       char *type;
        char *mount;            // last mount point
 };
 
+// dbdisk flags
+#define DBDISKF_RECURSIVE 1
+#define DBDISKF_MOUNT 2
+
 typedef struct dblist dblist;
 
 struct dblist {
index 9b47b00..0f14b62 100644 (file)
@@ -29,6 +29,7 @@
 #include <string.h>
 #include <locale.h>
 
+#include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
 
 #include <regex.h>
 #define HAVE_FSTATAT 1
 
 struct indexer {
-
-       char *root;
-
        dbindex *db;
        dbdisk *disk;
 
        dbtxn *tx; // global transaction for insert 1 disk
 
-       int isnew;
-
        ez_list queue;
 
        // files to care about
@@ -66,7 +62,6 @@ struct indexer {
        int unchanged;
 };
 
-
 /*
 
   Take a filename and generate some indexing info.
@@ -157,13 +152,6 @@ static dbfile *scan_file(const char *path) {
        return f;
 }
 
-
-
-
-
-
-
-
 struct dir_node {
        ez_node ln;
        char path[0];
@@ -183,17 +171,13 @@ static void dir_free(struct dir_node *d) {
 
 static void indexer_destroy(struct indexer *ix);
 
-static int indexer_init(struct indexer *ix, dbindex *db, const char *path, const char *uuid) {
+static int indexer_init(struct indexer *ix, dbindex *db, const dbdisk *info) {
        int res;
 
        memset(ix, 0, sizeof(*ix));
 
        ez_list_init(&ix->queue);
-       ez_list_addtail(&ix->queue, dir_new(path));
-
-       ix->root = strdup(path);
-
-       res = regcomp(&ix->match, "\\.(mp3|avi|mpeg|mp2|mpg|mp4|mov)$", REG_ICASE | REG_NOSUB | REG_EXTENDED);
+       res = regcomp(&ix->match, "\\.(mp3|avi|mpeg|mp2|mpg|mp4|mov|aac)$", REG_ICASE | REG_NOSUB | REG_EXTENDED);
        if (res != 0) {
                perror("regcomp");
                exit(1);
@@ -202,29 +186,29 @@ static int indexer_init(struct indexer *ix, dbindex *db, const char *path, const
        // Lookup this disk
        ix->db = db;
        ix->tx = dbindex_begin(db, NULL, 0);
+       ix->disk = dbdisk_get_uuid(ix->tx, db, info->uuid);
 
-       ix->disk = dbdisk_get_uuid(ix->tx, db, uuid);
        if (!ix->disk) {
                dbdisk *disk;
 
                disk = malloc(sizeof(*ix->disk));
 
                disk->id = 0;
-               disk->uuid = strdup(uuid);
-               disk->label = strdup("unknown");
-               disk->type = strdup("jfs");
-               disk->mount = strdup(path);
+               disk->flags = info->flags;
+               disk->uuid = strdup(info->uuid);
+               disk->label = strdup(info->label);
+               disk->mount = strdup(info->mount);
 
                res = dbdisk_add(ix->tx, db, disk);
                if (res != 0)
                        goto fail;
-               printf("%8d : add new disk %s\n", disk->id,  uuid);
+               printf("%8d : add new disk %s\n", disk->id,  disk->uuid);
                ix->disk = disk;
        } else {
-               printf("%8d : add old disk %s\n", ix->disk->id,  uuid);
+               printf("%8d : add old disk %s\n", ix->disk->id,  ix->disk->uuid);
        }
 
-       // FIXME: error handling
+       ez_list_addtail(&ix->queue, dir_new(ix->disk->mount));
 
        return 0;
 
@@ -243,7 +227,6 @@ static void indexer_destroy(struct indexer *ix) {
        while ((scan = ez_list_remhead(&ix->queue)))
                dir_free(scan);
 
-       free(ix->root);
        regfree(&ix->match);
 }
 
@@ -361,7 +344,7 @@ static int indexer_scan(struct indexer *ix) {
        while ((scan = ez_list_remhead(&ix->queue))) {
 
                printf("\nscan %s\n", scan->path);
-               res = dbfile_node_scan_path(ix->tx, ix->db, ix->disk->id, scan->path + strlen(ix->root), &files);
+               res = dbfile_node_scan_path(ix->tx, ix->db, ix->disk->id, scan->path + strlen(ix->disk->mount), &files);
                if (res != 0)
                        goto fail;
                printf("existing: %d\n", ez_tree_size(&files));
@@ -398,13 +381,13 @@ static int indexer_scan(struct indexer *ix) {
                                if (res == 0) {
                                        if (S_ISREG(st.st_mode)) {
                                                if (regexec(&ix->match, ep->d_name, 0, NULL, 0) == 0) {
-                                                       if ((res = indexer_add_file(ix, &st, name, name + strlen(ix->root), &files)))
+                                                       if ((res = indexer_add_file(ix, &st, name, name + strlen(ix->disk->mount), &files)))
                                                                goto fail;
                                                        count++;
                                                        if ((count % 1000) == 0)
                                                                indexer_info(ix);
                                                }
-                                       } else if (S_ISDIR(st.st_mode)) {
+                                       } else if (S_ISDIR(st.st_mode) && (ix->disk->flags & DBDISKF_RECURSIVE)) {
                                                ez_list_addtail(&ix->queue, dir_new(name));
                                        } else if (S_ISLNK(st.st_mode)) {
                                                // softlinks are ignored for simplicity reasons
@@ -477,7 +460,7 @@ static void indexer(const char *path) {
 
                        printf("Indexer: scanning '%s'.\n", disk->mount);
 
-                       res = indexer_init(&ix, db, disk->mount, disk->uuid);
+                       res = indexer_init(&ix, db, disk);
                        if (res == 0) {
                                res = indexer_scan(&ix);
 
index 3dfcf15..e1388cb 100644 (file)
@@ -146,7 +146,6 @@ static void mdisk_free(void *mp) {
 
        free(m->disk.uuid);
        free(m->disk.label);
-       free(m->disk.type);
        free(m->disk.mount);
 
        free(m);
@@ -237,7 +236,7 @@ static void partition_add(struct monitor *m, const char *dev) {
 
                        md->dev = strdup(dev);
                        md->disk.uuid = strdup(uuid);
-                       md->disk.type = strdup(type);
+                       md->disk.flags = DBDISKF_RECURSIVE | DBDISKF_MOUNT;
                        md->disk.label = plabel ? strdup(label) : NULL;
                        md->disk.mount = mountp;
 
index 12962e9..778795c 100644 (file)
@@ -298,7 +298,7 @@ int main(int argc, char **argv) {
                        dbscan scan;
 
                        for (dbdisk *disk = dbscan_disk(tx, &scan, db, 0); disk; disk = dbscan_disk_next(&scan)) {
-                               printf("did=%4d uuid='%s' type='%s' label='%s' mount='%s'\n", disk->id, disk->uuid, disk->type, disk->label, disk->mount);
+                               printf("did=%4d flags=%08x uuid='%s' label='%s' mount='%s'\n", disk->id, disk->flags, disk->uuid, disk->label, disk->mount);
                                dbdisk_free(disk);
                        }
                        dbscan_free(&scan);
index 921780a..72c6380 100644 (file)
@@ -6,28 +6,60 @@
 #include "dbindex.h"
 #include "notify.h"
 
+static void usage(const char *self) {
+       printf("usage: %s [-d dbdir]\n"
+               "   add [-r] uuid mount | update uuid | shuffle | quit\n", self);
+}
+
 int main(int argc, char **argv) {
-       if (argc > 1) {
-               notify_t q = notify_writer_new(NOTIFY_INDEXER);
-
-               if (q) {
-                       if (strcmp(argv[1], "quit") == 0) {
-                               notify_msg_send(q, NOTIFY_QUIT, 0, 0);
-                       } else if (strcmp(argv[1], "shuffle") == 0) {
-                               notify_msg_send(q, NOTIFY_SHUFFLE, 0, 0);
-                       } else if (strcmp(argv[1], "add") == 0 && argc == 4) {
+       const char *self = argv[0];
+       notify_t q = notify_writer_new(NOTIFY_INDEXER);
+       const char *cmd = "";
+
+       if (q) {
+               if (argc > 1) {
+                       cmd = argv[1];
+                       argc -= 2;
+                       argv += 2;
+               }
+
+               if (strcmp(cmd, "quit") == 0) {
+                       notify_msg_send(q, NOTIFY_QUIT, 0, 0);
+               } else if (strcmp(cmd, "shuffle") == 0) {
+                       notify_msg_send(q, NOTIFY_SHUFFLE, 0, 0);
+               } else if (strcmp(cmd, "add") == 0) {
+                       uint32_t flags = 0;
+
+                       if (argc > 1 && strcmp(argv[0], "-r") == 0) {
+                               flags = DBDISKF_RECURSIVE;
+                               argc--;
+                               argv++;
+                       }
+
+                       if (argc == 2) {
                                dbdisk disk = {
-                                       .uuid = argv[2],
+                                       .flags = flags,
+                                       .uuid = argv[0],
                                        .label = "system",
-                                       .type = "system",
-                                       .mount = argv[3]
+                                       .mount = argv[1]
                                };
                                notify_msg_send(q, NOTIFY_DISK_ADD, 0, &disk);
+                       } else {
+                               usage(self);
                        }
-                       notify_close(q);
+               } else if (strcmp(cmd, "update") == 0 && argc == 1) {
+                       dbdisk disk = {
+                               .uuid = argv[0],
+                               .label = "",
+                               .mount = "",
+                       };
+                       notify_msg_send(q, NOTIFY_DISK_ADD, 0, &disk);
                } else {
-                       fprintf(stderr, "error: Unable to open IPC channel\n");
+                       usage(self);
                }
+               notify_close(q);
+       } else {
+               fprintf(stderr, "error: Unable to open IPC channel\n");
        }
 
        return 0;