From b1c92220ad9febbe1d7a41cbc9055146ea4c09f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Thu, 14 Aug 2008 17:47:54 +0200 Subject: - Added path table to dbschema --- dbschema.sql | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dbschema.sql b/dbschema.sql index 2fcf0c9..6d730d9 100644 --- a/dbschema.sql +++ b/dbschema.sql @@ -23,3 +23,8 @@ CREATE TABLE genre ( name varchar(200) NOT NULL, PRIMARY KEY(name) ); + +CREATE TABLE path ( + path varchar(255), + PRIMARY KEY(path) +); -- cgit v1.2.3 From 1ecfff071d64d5b788bd718c2606ffc8428e1a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Thu, 14 Aug 2008 17:59:57 +0200 Subject: - Add musicpath specified on command line --- src/mfs_subr.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/mfs_subr.c b/src/mfs_subr.c index ab847f7..026783c 100644 --- a/src/mfs_subr.c +++ b/src/mfs_subr.c @@ -54,6 +54,7 @@ int mfs_initscan(char *musicpath) { int error; + sqlite3_stmt *st; /* Open database. */ error = sqlite3_open(DBNAME, &handle); @@ -62,6 +63,38 @@ mfs_initscan(char *musicpath) sqlite3_close(handle); return (-1); } + + /* Add path to registered paths in DB */ + error = sqlite3_prepare_v2(handle, + "SELECT path FROM path WHERE path LIKE ?", + -1, &st, NULL); + if (error != SQLITE_OK) { + warnx("Error preparing stamtement: %s\n", + sqlite3_errmsg(handle)); + return (-1); + } + sqlite3_bind_text(st, 1, musicpath, -1, SQLITE_TRANSIENT); + error = sqlite3_step(st); + if (error == SQLITE_DONE) { + /* Doesn't exist. Insert it */ + error = sqlite3_prepare_v2(handle, + "INSERT INTO path(path) VALUES(?)", + -1, &st, NULL); + if (error != SQLITE_OK) { + warnx("Error preparing stamtement: %s\n", + sqlite3_errmsg(handle)); + return (-1); + } + sqlite3_bind_text(st, 1, musicpath, -1, SQLITE_TRANSIENT); + error = sqlite3_step(st); + sqlite3_finalize(st); + if (error != SQLITE_DONE) { + warnx("Error inserting into database: %s\n", + sqlite3_errmsg(handle)); + return (-1); + } + } + traverse_hierarchy(musicpath, mfs_scan); sqlite3_close(handle); return (0); -- cgit v1.2.3 From cfe2daca8b53d3bffe7e5566f7ed87dfa5093548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Thu, 14 Aug 2008 18:20:52 +0200 Subject: - Added /paths - Return an informative message when reading from it --- src/mfs_vnops.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c index 60c9ad6..4a11c2c 100755 --- a/src/mfs_vnops.c +++ b/src/mfs_vnops.c @@ -41,6 +41,12 @@ char musicpath[MAXPATHLEN]; // = "/home/lulf/dev/musicfs/music"; char *logpath = "/home/lulf/dev/musicfs/musicfs.log"; +/* Information when reading /paths */ +const char* paths_info = +"# This is the paths I've registered for your music collection.\n" +"# Add additional paths on their own line in this file, and I'll\n" +"# scan them.\n"; + static int mfs_getattr (const char *path, struct stat *stbuf) { struct file_data fd; @@ -50,12 +56,19 @@ static int mfs_getattr (const char *path, struct stat *stbuf) int status = 0; memset (stbuf, 0, sizeof (struct stat)); - if (strcmp (path, "/") == 0) { + if (strcmp(path, "/") == 0) { stbuf->st_mode = S_IFDIR | 0755; stbuf->st_nlink = 2; return 0; } + if (strcmp(path, "/paths") == 0) { + stbuf->st_mode = S_IFREG | 0744; + stbuf->st_nlink = 2; + stbuf->st_size = strlen(paths_info); + return 0; + } + enum mfs_filetype type = mfs_get_filetype(path); switch (type) { case MFS_DIRECTORY: @@ -102,6 +115,7 @@ static int mfs_readdir (const char *path, void *buf, fuse_fill_dir_t filler, filler(buf, "Genres", NULL, 0); filler(buf, "Tracks", NULL, 0); filler(buf, "Albums", NULL, 0); + filler(buf, "paths", NULL, 0); return (0); } @@ -136,6 +150,9 @@ static int mfs_open (const char *path, struct fuse_file_info *fi) fd.fd = -1; fd.found = 0; + if (strcmp(path, "/paths") == 0) + return (0); + int status = mfs_file_data_for_path(path, &fd); if (status != 0) return (status); @@ -164,8 +181,18 @@ static int mfs_read (const char *path, char *buf, size_t size, off_t offset, struct file_data fd; fd.fd = -1; fd.found = 0; + int len; size_t bytes; + if (strcmp(path, "/paths") == 0) { + DEBUG("read from paths, offset(%d), size(%d)\n", offset, size); + len = strlen(paths_info); + if (size > (len - offset)) + size = len - offset; + strncpy(buf, paths_info + offset, size); + return (size); + } + int status = mfs_file_data_for_path(path, &fd); if (status != 0) return (status); -- cgit v1.2.3 From cbcf7e33be82bea9b0adc49c061e38121db0a0c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Thu, 14 Aug 2008 18:23:56 +0200 Subject: - Updated README with future usage :-) --- README | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README b/README index 07fe6e4..d70046c 100755 --- a/README +++ b/README @@ -18,11 +18,15 @@ $ sqlite3 music.db < dbschema.sql 3. Run: -$ ./musicfs -d +$ ./musicfs -d On my computer, it only behaves correctly in debug mode (-d) due to a bug of some kind. I don't really know why :'( +4. Add music directory: + +Follow the instructions in /paths. + Dependencies ~~~~~~~~~~~~ -- cgit v1.2.3 From bbc60fc5a6ef982d93671950e8ffb902f6c507ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Thu, 14 Aug 2008 18:32:56 +0200 Subject: - Renamed /paths to /.config --- README | 2 +- src/mfs_vnops.c | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README b/README index d70046c..a32a6e2 100755 --- a/README +++ b/README @@ -25,7 +25,7 @@ bug of some kind. I don't really know why :'( 4. Add music directory: -Follow the instructions in /paths. +Edit /.config and follow the instructions. Dependencies diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c index 4a11c2c..de5146a 100755 --- a/src/mfs_vnops.c +++ b/src/mfs_vnops.c @@ -41,7 +41,7 @@ char musicpath[MAXPATHLEN]; // = "/home/lulf/dev/musicfs/music"; char *logpath = "/home/lulf/dev/musicfs/musicfs.log"; -/* Information when reading /paths */ +/* Information when reading /.config */ const char* paths_info = "# This is the paths I've registered for your music collection.\n" "# Add additional paths on their own line in this file, and I'll\n" @@ -62,7 +62,7 @@ static int mfs_getattr (const char *path, struct stat *stbuf) return 0; } - if (strcmp(path, "/paths") == 0) { + if (strcmp(path, "/.config") == 0) { stbuf->st_mode = S_IFREG | 0744; stbuf->st_nlink = 2; stbuf->st_size = strlen(paths_info); @@ -115,7 +115,7 @@ static int mfs_readdir (const char *path, void *buf, fuse_fill_dir_t filler, filler(buf, "Genres", NULL, 0); filler(buf, "Tracks", NULL, 0); filler(buf, "Albums", NULL, 0); - filler(buf, "paths", NULL, 0); + filler(buf, ".config", NULL, 0); return (0); } @@ -150,7 +150,7 @@ static int mfs_open (const char *path, struct fuse_file_info *fi) fd.fd = -1; fd.found = 0; - if (strcmp(path, "/paths") == 0) + if (strcmp(path, "/.config") == 0) return (0); int status = mfs_file_data_for_path(path, &fd); @@ -184,7 +184,7 @@ static int mfs_read (const char *path, char *buf, size_t size, off_t offset, int len; size_t bytes; - if (strcmp(path, "/paths") == 0) { + if (strcmp(path, "/.config") == 0) { DEBUG("read from paths, offset(%d), size(%d)\n", offset, size); len = strlen(paths_info); if (size > (len - offset)) -- cgit v1.2.3 From d1655ce25dbda83b7bbf8874d48654beaa9b9000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 14:35:16 +0200 Subject: - Added vnops needed for writing .config --- src/mfs_vnops.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c index de5146a..6e9e0b7 100755 --- a/src/mfs_vnops.c +++ b/src/mfs_vnops.c @@ -184,8 +184,9 @@ static int mfs_read (const char *path, char *buf, size_t size, off_t offset, int len; size_t bytes; + DEBUG("read: path(%s) offset(%d) size(%d)\n", path, (int)offset, (int)size); if (strcmp(path, "/.config") == 0) { - DEBUG("read from paths, offset(%d), size(%d)\n", offset, size); + DEBUG("read from config, offset(%d), size(%d)\n", (int)offset, (int)size); len = strlen(paths_info); if (size > (len - offset)) size = len - offset; @@ -213,11 +214,38 @@ static int mfs_read (const char *path, char *buf, size_t size, off_t offset, */ } +static int mfs_write(const char *path, const char *buf, size_t size, + off_t off, struct fuse_file_info *fi) +{ + DEBUG("writing to %s, size(%d), offset(%d)\n\t%s\n", + path, size, (int)off, buf); + return (size); +} + +static int mfs_mknod(const char *path, mode_t mode, dev_t rdev) +{ + DEBUG("mknod %s\n", path); + return (-1); +} + +static int mfs_truncate(const char *path, off_t size) +{ + DEBUG("truncating %s\n", path); + + if (strcmp(path, "/.config") == 0) + return (0); + + return (-1); +} + static struct fuse_operations mfs_ops = { .getattr = mfs_getattr, .readdir = mfs_readdir, .open = mfs_open, .read = mfs_read, + .write = mfs_write, + .mknod = mfs_mknod, + .truncate = mfs_truncate, }; static int musicfs_opt_proc (void *data, const char *arg, int key, -- cgit v1.2.3 From f1f72be2fef35564755a9ba27c8329750a62b1ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 15:06:50 +0200 Subject: - Started working on ~/.mfsrc as config file --- src/mfs_vnops.c | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c index 6e9e0b7..a8e01b8 100755 --- a/src/mfs_vnops.c +++ b/src/mfs_vnops.c @@ -41,11 +41,29 @@ char musicpath[MAXPATHLEN]; // = "/home/lulf/dev/musicfs/music"; char *logpath = "/home/lulf/dev/musicfs/musicfs.log"; -/* Information when reading /.config */ -const char* paths_info = -"# This is the paths I've registered for your music collection.\n" -"# Add additional paths on their own line in this file, and I'll\n" -"# scan them.\n"; +/* + * Returns the path to $HOME[/extra] + */ +char *mfs_get_home_path(const char *extra) +{ + int hlen, exlen = 0; + char *res; + const char *home = getenv("HOME"); + + hlen = strlen(home); + if (extra) + exlen = strlen(extra); + + res = malloc(sizeof(char) * (hlen + exlen + 2)); + strcpy(res, home); + + if (extra) { + res[hlen] = '/'; + strcpy(res + hlen + 1, extra); + } + + return (res); +} static int mfs_getattr (const char *path, struct stat *stbuf) { @@ -63,10 +81,11 @@ static int mfs_getattr (const char *path, struct stat *stbuf) } if (strcmp(path, "/.config") == 0) { - stbuf->st_mode = S_IFREG | 0744; - stbuf->st_nlink = 2; - stbuf->st_size = strlen(paths_info); - return 0; + char *mfsrc = mfs_get_home_path(".mfsrc"); + int res = stat(mfsrc, stbuf); + DEBUG("stat result for %s: %d\n", mfsrc, res); + free(mfsrc); + return (res); } enum mfs_filetype type = mfs_get_filetype(path); @@ -151,7 +170,7 @@ static int mfs_open (const char *path, struct fuse_file_info *fi) fd.found = 0; if (strcmp(path, "/.config") == 0) - return (0); + return (-open("~/.mfsrc", O_RDONLY)); int status = mfs_file_data_for_path(path, &fd); if (status != 0) @@ -181,17 +200,16 @@ static int mfs_read (const char *path, char *buf, size_t size, off_t offset, struct file_data fd; fd.fd = -1; fd.found = 0; - int len; size_t bytes; DEBUG("read: path(%s) offset(%d) size(%d)\n", path, (int)offset, (int)size); if (strcmp(path, "/.config") == 0) { - DEBUG("read from config, offset(%d), size(%d)\n", (int)offset, (int)size); - len = strlen(paths_info); - if (size > (len - offset)) - size = len - offset; - strncpy(buf, paths_info + offset, size); - return (size); +/* DEBUG("read from config, offset(%d), size(%d)\n", (int)offset, (int)size); */ +/* len = strlen(paths_info); */ +/* if (size > (len - offset)) */ +/* size = len - offset; */ +/* strncpy(buf, paths_info + offset, size); */ + return (0); } int status = mfs_file_data_for_path(path, &fd); @@ -230,7 +248,7 @@ static int mfs_mknod(const char *path, mode_t mode, dev_t rdev) static int mfs_truncate(const char *path, off_t size) { - DEBUG("truncating %s\n", path); + DEBUG("truncating %s to size %d\n", path, (int)size); if (strcmp(path, "/.config") == 0) return (0); -- cgit v1.2.3 From 1c34d40bbd9adbb2eb651fdc2c42766aed9606de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 15:11:30 +0200 Subject: - mfs_read for /.config --- src/mfs_vnops.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c index a8e01b8..48c225e 100755 --- a/src/mfs_vnops.c +++ b/src/mfs_vnops.c @@ -170,7 +170,7 @@ static int mfs_open (const char *path, struct fuse_file_info *fi) fd.found = 0; if (strcmp(path, "/.config") == 0) - return (-open("~/.mfsrc", O_RDONLY)); + return (0); int status = mfs_file_data_for_path(path, &fd); if (status != 0) @@ -204,12 +204,13 @@ static int mfs_read (const char *path, char *buf, size_t size, off_t offset, DEBUG("read: path(%s) offset(%d) size(%d)\n", path, (int)offset, (int)size); if (strcmp(path, "/.config") == 0) { -/* DEBUG("read from config, offset(%d), size(%d)\n", (int)offset, (int)size); */ -/* len = strlen(paths_info); */ -/* if (size > (len - offset)) */ -/* size = len - offset; */ -/* strncpy(buf, paths_info + offset, size); */ - return (0); + char *mfsrc = mfs_get_home_path(".mfsrc"); + int fd = open(mfsrc, O_RDONLY); + free(mfsrc); + lseek(fd, offset, SEEK_CUR); + bytes = read(fd, buf, size); + close(fd); + return (bytes); } int status = mfs_file_data_for_path(path, &fd); -- cgit v1.2.3 From 9632d3fb1233eb728f84ac8aaf1daec72c9974d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 15:34:08 +0200 Subject: - More vnops (to make vim behave) --- src/mfs_vnops.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 109 insertions(+), 6 deletions(-) diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c index 48c225e..f6cdd88 100755 --- a/src/mfs_vnops.c +++ b/src/mfs_vnops.c @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -202,7 +203,8 @@ static int mfs_read (const char *path, char *buf, size_t size, off_t offset, fd.found = 0; size_t bytes; - DEBUG("read: path(%s) offset(%d) size(%d)\n", path, (int)offset, (int)size); + DEBUG("read: path(%s) offset(%d) size(%d)\n", path, (int)offset, + (int)size); if (strcmp(path, "/.config") == 0) { char *mfsrc = mfs_get_home_path(".mfsrc"); int fd = open(mfsrc, O_RDONLY); @@ -234,11 +236,30 @@ static int mfs_read (const char *path, char *buf, size_t size, off_t offset, } static int mfs_write(const char *path, const char *buf, size_t size, - off_t off, struct fuse_file_info *fi) + off_t offset, struct fuse_file_info *fi) { - DEBUG("writing to %s, size(%d), offset(%d)\n\t%s\n", - path, size, (int)off, buf); - return (size); + size_t bytes; + + DEBUG("write: path(%s) offset(%d) size(%d)\n", path, (int)offset, + (int)size); + + if (strcmp(path, "/.config") == 0) { + char *mfsrc = mfs_get_home_path(".mfsrc"); + int fd = open(mfsrc, O_WRONLY); + free(mfsrc); + lseek(fd, offset, SEEK_CUR); + bytes = write(fd, buf, size); + close(fd); + return (bytes); + } + + return (-1); +} + +static int mfs_create(const char *path, mode_t mode, struct fuse_file_info *fi) +{ + DEBUG("create %s\n", path); + return (-1); } static int mfs_mknod(const char *path, mode_t mode, dev_t rdev) @@ -249,14 +270,90 @@ static int mfs_mknod(const char *path, mode_t mode, dev_t rdev) static int mfs_truncate(const char *path, off_t size) { + char *mfsrc; + int res; DEBUG("truncating %s to size %d\n", path, (int)size); - if (strcmp(path, "/.config") == 0) + if (strcmp(path, "/.config") == 0) { + mfsrc = mfs_get_home_path(".mfsrc"); + res = truncate(mfsrc, size); + DEBUG("truncated %s with result: %d\n", mfsrc, res) + free(mfsrc); + return (res); + } + + return (-1); +} + +static int mfs_flush(const char *path, struct fuse_file_info *fi) +{ + DEBUG("flushing path %s\n", path); + if (strcmp(path, "/.config") == 0) { return (0); + } return (-1); } +static int mfs_fsync(const char *path, int datasync, + struct fuse_file_info *fi) +{ + DEBUG("fsync path %s\n", path); + return (0); +} + +static int mfs_release(const char *path, struct fuse_file_info *fi) +{ + DEBUG("release %s\n", path); + return (0); +} + +static int mfs_chmod(const char *path, mode_t mode) +{ + char *mfsrc; + int ret; + + DEBUG("chmod %s, %d\n", path, (int)mode); + if (strcmp(path, "/.config") == 0) { + mfsrc = mfs_get_home_path(".mfsrc"); + ret = chmod(mfsrc, mode); + free(mfsrc); + return (ret); + } + + return (-1); +} + +static int mfs_utimens(const char *path, const struct timespec tv[2]) +{ + char *mfsrc; + int ret; + + DEBUG("utime %s\n", path); + + if (strcmp(path, "/.config") == 0) { + mfsrc = mfs_get_home_path(".mfsrc"); + ret = 0; /* utimes(mfsrc, tval); */ + free(mfsrc); + return (ret); + } + + return (-1); +} + +static int mfs_access(const char *path, int mask) +{ + DEBUG("access called for path %s\n", path); + return (0); +} + +static int mfs_setxattr(const char *path, const char *name, + const char *val, size_t size, int flags) +{ + DEBUG("setxattr on path %s: %s=%s\n", path, name, val); + return (0); +} + static struct fuse_operations mfs_ops = { .getattr = mfs_getattr, .readdir = mfs_readdir, @@ -264,7 +361,13 @@ static struct fuse_operations mfs_ops = { .read = mfs_read, .write = mfs_write, .mknod = mfs_mknod, + .create = mfs_create, .truncate = mfs_truncate, + .fsync = mfs_fsync, + .chmod = mfs_chmod, + .release = mfs_release, + .utimens = mfs_utimens, + .setxattr = mfs_setxattr, }; static int musicfs_opt_proc (void *data, const char *arg, int key, -- cgit v1.2.3 From e296703a90ce7c6039614562dd6981c28b7ab4bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 19:44:32 +0200 Subject: - Aligned code --- src/mfs_vnops.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c index f6cdd88..87ade5a 100755 --- a/src/mfs_vnops.c +++ b/src/mfs_vnops.c @@ -355,10 +355,10 @@ static int mfs_setxattr(const char *path, const char *name, } static struct fuse_operations mfs_ops = { - .getattr = mfs_getattr, - .readdir = mfs_readdir, - .open = mfs_open, - .read = mfs_read, + .getattr = mfs_getattr, + .readdir = mfs_readdir, + .open = mfs_open, + .read = mfs_read, .write = mfs_write, .mknod = mfs_mknod, .create = mfs_create, -- cgit v1.2.3 From e5b2bcc8894b59191050c7f45f261059f3a08907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 19:46:22 +0200 Subject: - Helper function to add music path --- src/mfs_subr.c | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/mfs_subr.c b/src/mfs_subr.c index 026783c..e8f28b2 100644 --- a/src/mfs_subr.c +++ b/src/mfs_subr.c @@ -51,49 +51,61 @@ struct lookuphandle { sqlite3 *handle; int -mfs_initscan(char *musicpath) +mfs_insert_path(char *path) { - int error; + int res; sqlite3_stmt *st; - /* Open database. */ - error = sqlite3_open(DBNAME, &handle); - if (error) { - warnx("Can't open database: %s\n", sqlite3_errmsg(handle)); - sqlite3_close(handle); - return (-1); - } - /* Add path to registered paths in DB */ - error = sqlite3_prepare_v2(handle, + res = sqlite3_prepare_v2(handle, "SELECT path FROM path WHERE path LIKE ?", -1, &st, NULL); - if (error != SQLITE_OK) { + if (res != SQLITE_OK) { warnx("Error preparing stamtement: %s\n", sqlite3_errmsg(handle)); return (-1); } - sqlite3_bind_text(st, 1, musicpath, -1, SQLITE_TRANSIENT); - error = sqlite3_step(st); - if (error == SQLITE_DONE) { + sqlite3_bind_text(st, 1, path, -1, SQLITE_TRANSIENT); + res = sqlite3_step(st); + if (res == SQLITE_DONE) { /* Doesn't exist. Insert it */ - error = sqlite3_prepare_v2(handle, + res = sqlite3_prepare_v2(handle, "INSERT INTO path(path) VALUES(?)", -1, &st, NULL); - if (error != SQLITE_OK) { + if (res != SQLITE_OK) { warnx("Error preparing stamtement: %s\n", sqlite3_errmsg(handle)); return (-1); } - sqlite3_bind_text(st, 1, musicpath, -1, SQLITE_TRANSIENT); - error = sqlite3_step(st); + sqlite3_bind_text(st, 1, path, -1, SQLITE_TRANSIENT); + res = sqlite3_step(st); sqlite3_finalize(st); - if (error != SQLITE_DONE) { + if (res != SQLITE_DONE) { warnx("Error inserting into database: %s\n", sqlite3_errmsg(handle)); return (-1); } } + return (0); +} + +int +mfs_initscan(char *musicpath) +{ + int error; + sqlite3_stmt *st; + + /* Open database. */ + error = sqlite3_open(DBNAME, &handle); + if (error) { + warnx("Can't open database: %s\n", sqlite3_errmsg(handle)); + sqlite3_close(handle); + return (-1); + } + + error = mfs_insert_path(musicpath); + if (error != 0) + return (error); traverse_hierarchy(musicpath, mfs_scan); sqlite3_close(handle); -- cgit v1.2.3 From 5e6d5d75e5d2903988b792dd803859b31c23b935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 19:51:33 +0200 Subject: - Moved $HOME function to mfs_subr --- include/musicfs.h | 1 + src/mfs_subr.c | 37 ++++++++++++++++++++++++++++++++++++- src/mfs_vnops.c | 24 ------------------------ 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/include/musicfs.h b/include/musicfs.h index 3da10da..a376f4d 100644 --- a/include/musicfs.h +++ b/include/musicfs.h @@ -78,6 +78,7 @@ void mfs_lookup_album(const char *, struct filler_data *); char *mfs_gettoken(const char *, int); int mfs_numtoken(const char *); int mfs_file_data_for_path(const char *, void *); +char *mfs_get_home_path(const char *); enum mfs_filetype mfs_get_filetype(const char *); #endif diff --git a/src/mfs_subr.c b/src/mfs_subr.c index e8f28b2..4ee807b 100644 --- a/src/mfs_subr.c +++ b/src/mfs_subr.c @@ -50,6 +50,33 @@ struct lookuphandle { sqlite3 *handle; +/* + * Returns the path to $HOME[/extra] + */ +char *mfs_get_home_path(const char *extra) +{ + int hlen, exlen = 0; + char *res; + const char *home = getenv("HOME"); + + hlen = strlen(home); + if (extra) + exlen = strlen(extra); + + res = malloc(sizeof(char) * (hlen + exlen + 2)); + strcpy(res, home); + + if (extra) { + res[hlen] = '/'; + strcpy(res + hlen + 1, extra); + } + + return (res); +} + +/* + * Insert a musicpath into the database. + */ int mfs_insert_path(char *path) { @@ -89,11 +116,19 @@ mfs_insert_path(char *path) return (0); } +/* + * Reload the configuration from $HOME/.mfsrc + */ +int +mfs_reload_config() +{ + return (0); +} + int mfs_initscan(char *musicpath) { int error; - sqlite3_stmt *st; /* Open database. */ error = sqlite3_open(DBNAME, &handle); diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c index 87ade5a..ccd44b3 100755 --- a/src/mfs_vnops.c +++ b/src/mfs_vnops.c @@ -42,30 +42,6 @@ char musicpath[MAXPATHLEN]; // = "/home/lulf/dev/musicfs/music"; char *logpath = "/home/lulf/dev/musicfs/musicfs.log"; -/* - * Returns the path to $HOME[/extra] - */ -char *mfs_get_home_path(const char *extra) -{ - int hlen, exlen = 0; - char *res; - const char *home = getenv("HOME"); - - hlen = strlen(home); - if (extra) - exlen = strlen(extra); - - res = malloc(sizeof(char) * (hlen + exlen + 2)); - strcpy(res, home); - - if (extra) { - res[hlen] = '/'; - strcpy(res + hlen + 1, extra); - } - - return (res); -} - static int mfs_getattr (const char *path, struct stat *stbuf) { struct file_data fd; -- cgit v1.2.3 From 82be959253b213abdbd180c683c82ad4de64d58e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 20:03:56 +0200 Subject: - Implemented mfs_reload_config --- include/musicfs.h | 1 + src/mfs_subr.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/musicfs.h b/include/musicfs.h index a376f4d..b3f2ebe 100644 --- a/include/musicfs.h +++ b/include/musicfs.h @@ -78,6 +78,7 @@ void mfs_lookup_album(const char *, struct filler_data *); char *mfs_gettoken(const char *, int); int mfs_numtoken(const char *); int mfs_file_data_for_path(const char *, void *); +int mfs_reload_config(); char *mfs_get_home_path(const char *); enum mfs_filetype mfs_get_filetype(const char *); diff --git a/src/mfs_subr.c b/src/mfs_subr.c index 4ee807b..468e253 100644 --- a/src/mfs_subr.c +++ b/src/mfs_subr.c @@ -122,6 +122,20 @@ mfs_insert_path(char *path) int mfs_reload_config() { + int res; + char *mfsrc = mfs_get_home_path(".mfsrc"); + FILE *f = fopen(mfsrc, "r"); + char line[4096]; + + /* XXX: Just adding the paths for now. queue.h for the rest*/ + fgets(line, 4096, f); + + if (line[0] != '#') { + res = mfs_insert_path(line); + DEBUG("inserted path %s, returned(%d)\n", line, res); + } + + free (mfsrc); return (0); } -- cgit v1.2.3 From 85ec65543f15a9b722e122dd7ef30e147585950f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 20:31:22 +0200 Subject: - Fixed insert_path and reload_config --- src/mfs_subr.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/mfs_subr.c b/src/mfs_subr.c index 468e253..b4dfde4 100644 --- a/src/mfs_subr.c +++ b/src/mfs_subr.c @@ -78,7 +78,7 @@ char *mfs_get_home_path(const char *extra) * Insert a musicpath into the database. */ int -mfs_insert_path(char *path) +mfs_insert_path(char *path, sqlite3 *handle) { int res; sqlite3_stmt *st; @@ -94,7 +94,10 @@ mfs_insert_path(char *path) } sqlite3_bind_text(st, 1, path, -1, SQLITE_TRANSIENT); res = sqlite3_step(st); + sqlite3_finalize(st); + if (res == SQLITE_DONE) { + DEBUG("Inserting path '%s' to paths\n", path); /* Doesn't exist. Insert it */ res = sqlite3_prepare_v2(handle, "INSERT INTO path(path) VALUES(?)", @@ -122,19 +125,33 @@ mfs_insert_path(char *path) int mfs_reload_config() { - int res; + int res, len; char *mfsrc = mfs_get_home_path(".mfsrc"); FILE *f = fopen(mfsrc, "r"); char line[4096]; + sqlite3 *handle; + res = sqlite3_open(DBNAME, &handle); + if (res) { + warnx("Can't open database: %s\n", sqlite3_errmsg(handle)); + sqlite3_close(handle); + return (-1); + } + /* XXX: Just adding the paths for now. queue.h for the rest*/ - fgets(line, 4096, f); - if (line[0] != '#') { - res = mfs_insert_path(line); - DEBUG("inserted path %s, returned(%d)\n", line, res); + while (fgets(line, 4096, f) != NULL) { + len = strlen(line); + if (len > 0 && line[0] != '\n' && line[0] != '#') { + if (line[len-1] == '\n') + line[len-1] = '\0'; + + res = mfs_insert_path(line, handle); + DEBUG("inserted path %s, returned(%d)\n", line, res); + } } + sqlite3_close(handle); free (mfsrc); return (0); } @@ -152,7 +169,11 @@ mfs_initscan(char *musicpath) return (-1); } - error = mfs_insert_path(musicpath); + error = mfs_insert_path(musicpath, handle); + if (error != 0) + return (error); + + error = mfs_reload_config(); if (error != 0) return (error); -- cgit v1.2.3 From 73786a63d67d7f63620c55b20a9937fed2f9718c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 20:48:05 +0200 Subject: - Added lookup functions for scanning paths --- include/musicfs.h | 4 +++- src/mfs_subr.c | 25 ++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/include/musicfs.h b/include/musicfs.h index b3f2ebe..21fd0b4 100644 --- a/include/musicfs.h +++ b/include/musicfs.h @@ -33,7 +33,7 @@ int mfs_initscan(char *); * files, for instance scanning the collection. */ typedef void traverse_fn_t(char *); -void traverse_hierarchy(char *, traverse_fn_t); +void traverse_hierarchy(const char *, traverse_fn_t); traverse_fn_t mfs_scan; /* @@ -65,6 +65,8 @@ lookup_fn_t mfs_lookup_list; lookup_fn_t mfs_lookup_open; /* Lookup function stat'ing a file. */ lookup_fn_t mfs_lookup_stat; +/* Lookup function loading a path into DB */ +lookup_fn_t mfs_lookup_load_path; struct lookuphandle; diff --git a/src/mfs_subr.c b/src/mfs_subr.c index b4dfde4..21cc5d6 100644 --- a/src/mfs_subr.c +++ b/src/mfs_subr.c @@ -130,6 +130,7 @@ mfs_reload_config() FILE *f = fopen(mfsrc, "r"); char line[4096]; sqlite3 *handle; + struct lookuphandle *lh; res = sqlite3_open(DBNAME, &handle); if (res) { @@ -151,8 +152,14 @@ mfs_reload_config() } } - sqlite3_close(handle); free (mfsrc); + + /* Do the actual loading */ + lh = mfs_lookup_start(0, handle, mfs_lookup_load_path, + "SELECT path FROM path"); + mfs_lookup_finish(lh); + + sqlite3_close(handle); return (0); } @@ -177,7 +184,6 @@ mfs_initscan(char *musicpath) if (error != 0) return (error); - traverse_hierarchy(musicpath, mfs_scan); sqlite3_close(handle); return (0); } @@ -188,8 +194,9 @@ mfs_initscan(char *musicpath) * sub-directories. */ void -traverse_hierarchy(char *dirpath, traverse_fn_t fileop) +traverse_hierarchy(const char *dirpath, traverse_fn_t fileop) { + DEBUG("traversing %s\n", dirpath); DIR *dirp; struct dirent *dp; char filepath[MAXPATHLEN]; @@ -849,6 +856,18 @@ mfs_lookup_stat(void *data, const char *str) return (1); } +/* + * Load a path into database + */ +int +mfs_lookup_load_path(void *data, const char *str) +{ + handle = (sqlite3 *)data; + traverse_hierarchy(str, mfs_scan); + + return (1); +} + /* * Guess on a filetype for a path. * -- cgit v1.2.3 From d94d0a8f5b3d5847cf70da02d5aa5522cd7b4c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 21:00:10 +0200 Subject: - Fixed traversing - Added a special flag to give the lookup functions access to the DB handle. --- src/mfs_subr.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/mfs_subr.c b/src/mfs_subr.c index 21cc5d6..2ac8537 100644 --- a/src/mfs_subr.c +++ b/src/mfs_subr.c @@ -38,6 +38,8 @@ #include #include +#define MFS_HANDLE ((void*)-1) + struct lookuphandle { sqlite3 *handle; sqlite3_stmt *st; @@ -121,6 +123,7 @@ mfs_insert_path(char *path, sqlite3 *handle) /* * Reload the configuration from $HOME/.mfsrc + * */ int mfs_reload_config() @@ -129,9 +132,9 @@ mfs_reload_config() char *mfsrc = mfs_get_home_path(".mfsrc"); FILE *f = fopen(mfsrc, "r"); char line[4096]; - sqlite3 *handle; struct lookuphandle *lh; - + sqlite3 *handle; + res = sqlite3_open(DBNAME, &handle); if (res) { warnx("Can't open database: %s\n", sqlite3_errmsg(handle)); @@ -153,9 +156,10 @@ mfs_reload_config() } free (mfsrc); + sqlite3_close(handle); /* Do the actual loading */ - lh = mfs_lookup_start(0, handle, mfs_lookup_load_path, + lh = mfs_lookup_start(0, MFS_HANDLE, mfs_lookup_load_path, "SELECT path FROM path"); mfs_lookup_finish(lh); @@ -176,9 +180,9 @@ mfs_initscan(char *musicpath) return (-1); } - error = mfs_insert_path(musicpath, handle); - if (error != 0) - return (error); +/* error = mfs_insert_path(musicpath, handle); */ +/* if (error != 0) */ +/* return (error); */ error = mfs_reload_config(); if (error != 0) @@ -426,7 +430,7 @@ mfs_lookup_start(int field, void *data, lookup_fn_t *fn, const char *query) return (NULL); lh->field = field; lh->lookup = fn; - lh->priv = data; + /* Open database. */ error = sqlite3_open(DBNAME, &lh->handle); if (error) { @@ -435,6 +439,13 @@ mfs_lookup_start(int field, void *data, lookup_fn_t *fn, const char *query) free(lh); return (NULL); } + + if (data == MFS_HANDLE) + /* Workaround to give access to the handle */ + lh->priv = lh->handle; + else + lh->priv = data; + ret = sqlite3_prepare_v2(lh->handle, query, -1, &lh->st, NULL); if (ret != SQLITE_OK) { free(lh); @@ -865,7 +876,7 @@ mfs_lookup_load_path(void *data, const char *str) handle = (sqlite3 *)data; traverse_hierarchy(str, mfs_scan); - return (1); + return (0); } /* -- cgit v1.2.3 From 194feb8f68c6c92e14302471177adf40fc357e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 21:30:11 +0200 Subject: - Removed the musicfolder argument --- src/mfs_vnops.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c index ccd44b3..1b71b38 100755 --- a/src/mfs_vnops.c +++ b/src/mfs_vnops.c @@ -349,14 +349,15 @@ static struct fuse_operations mfs_ops = { static int musicfs_opt_proc (void *data, const char *arg, int key, struct fuse_args *outargs) { - static int musicpath_set = 0; + /* Using config now. This is how we added the additional parameter: */ + /* static int musicpath_set = 0; */ + /* if (key == FUSE_OPT_KEY_NONOPT && !musicpath_set) { */ + /* /\* The source directory isn't already set, let's do it *\/ */ + /* strcpy(musicpath, arg); */ + /* musicpath_set = 1; */ + /* return (0); */ + /* } */ - if (key == FUSE_OPT_KEY_NONOPT && !musicpath_set) { - /* The source directory isn't already set, let's do it */ - strcpy(musicpath, arg); - musicpath_set = 1; - return (0); - } return (1); } @@ -364,13 +365,10 @@ int mfs_run(int argc, char **argv) { int ret; - /* - * XXX: Build index of mp3's. - */ /* Update tables. */ - if (argc < 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); + if (argc < 1) { + fprintf(stderr, "Usage: %s \n", argv[0]); return (-1); } -- cgit v1.2.3 From 97b8102bd1d1da7e99276abfc1c823a220dfa7e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 21:35:34 +0200 Subject: Added initialization script --- initialize.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 initialize.sh diff --git a/initialize.sh b/initialize.sh new file mode 100644 index 0000000..e167ee7 --- /dev/null +++ b/initialize.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +sqlite3 $HOME/.mfs.db < dbschema.sql + +cat > $HOME/.mfsrc < Date: Fri, 15 Aug 2008 21:51:16 +0200 Subject: Changed dbpath to ~/.mfs.db --- include/musicfs.h | 4 +--- src/mfs_subr.c | 19 +++++++++++++------ src/mfs_vnops.c | 7 +------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/musicfs.h b/include/musicfs.h index 21fd0b4..8443198 100644 --- a/include/musicfs.h +++ b/include/musicfs.h @@ -22,10 +22,8 @@ #define _MP3FS_H_ struct fuse_args; -#define DBNAME "music.db" - int mfs_run(int, char **); -int mfs_initscan(char *); +int mfs_initscan(); /* diff --git a/src/mfs_subr.c b/src/mfs_subr.c index 2ac8537..d3d3c0b 100644 --- a/src/mfs_subr.c +++ b/src/mfs_subr.c @@ -50,6 +50,7 @@ struct lookuphandle { lookup_fn_t *lookup; }; +char *db_path; sqlite3 *handle; /* @@ -130,12 +131,18 @@ mfs_reload_config() { int res, len; char *mfsrc = mfs_get_home_path(".mfsrc"); - FILE *f = fopen(mfsrc, "r"); char line[4096]; struct lookuphandle *lh; sqlite3 *handle; + FILE *f = fopen(mfsrc, "r"); + + if (f == NULL) { + warnx("Couldn't open configuration file %s\n", + mfsrc); + return (-1); + } - res = sqlite3_open(DBNAME, &handle); + res = sqlite3_open(db_path, &handle); if (res) { warnx("Can't open database: %s\n", sqlite3_errmsg(handle)); sqlite3_close(handle); @@ -168,12 +175,12 @@ mfs_reload_config() } int -mfs_initscan(char *musicpath) +mfs_initscan() { int error; - + db_path = mfs_get_home_path(".mfs.db"); /* Open database. */ - error = sqlite3_open(DBNAME, &handle); + error = sqlite3_open(db_path, &handle); if (error) { warnx("Can't open database: %s\n", sqlite3_errmsg(handle)); sqlite3_close(handle); @@ -432,7 +439,7 @@ mfs_lookup_start(int field, void *data, lookup_fn_t *fn, const char *query) lh->lookup = fn; /* Open database. */ - error = sqlite3_open(DBNAME, &lh->handle); + error = sqlite3_open(db_path, &lh->handle); if (error) { warnx("Can't open database: %s\n", sqlite3_errmsg(lh->handle)); sqlite3_close(lh->handle); diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c index 1b71b38..e182be2 100755 --- a/src/mfs_vnops.c +++ b/src/mfs_vnops.c @@ -39,9 +39,6 @@ #include #include -char musicpath[MAXPATHLEN]; // = "/home/lulf/dev/musicfs/music"; -char *logpath = "/home/lulf/dev/musicfs/musicfs.log"; - static int mfs_getattr (const char *path, struct stat *stbuf) { struct file_data fd; @@ -366,7 +363,6 @@ mfs_run(int argc, char **argv) { int ret; - /* Update tables. */ if (argc < 1) { fprintf(stderr, "Usage: %s \n", argv[0]); return (-1); @@ -377,8 +373,7 @@ mfs_run(int argc, char **argv) if (fuse_opt_parse(&args, NULL, NULL, musicfs_opt_proc) != 0) exit (1); - DEBUG("musicpath: %s\n", musicpath); - mfs_initscan(musicpath); + mfs_initscan(); ret = 0; ret = fuse_main(args.argc, args.argv, &mfs_ops, NULL); -- cgit v1.2.3 From aaccd1929a253e6e687f50a810877bb9eb4ea8df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 15 Aug 2008 21:53:51 +0200 Subject: - Reloading config when .config is changed --- src/mfs_vnops.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c index e182be2..bfb4fe1 100755 --- a/src/mfs_vnops.c +++ b/src/mfs_vnops.c @@ -278,6 +278,12 @@ static int mfs_fsync(const char *path, int datasync, static int mfs_release(const char *path, struct fuse_file_info *fi) { DEBUG("release %s\n", path); + + if (strcmp(path, "/.config") == 0) { + /* Reload configuration file */ + mfs_reload_config(); + } + return (0); } -- cgit v1.2.3