summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Lilleengen <lulf@nobby.studby.ntnu.no>2008-08-03 20:57:48 +0200
committerUlf Lilleengen <lulf@nobby.studby.ntnu.no>2008-08-03 20:57:48 +0200
commit1304f27adbc3c4dd3b34f0a91b8ccb02c8d66c01 (patch)
tree10e85f8e2e46990da5d714438dbd5715c36af9ce
parentbd55a14447d53fef7e50797ed830b2d12f8ad334 (diff)
- Correct typo in db schema.
- Add mp3_list which given a query and a field number, prints out values from the query.
-rw-r--r--dbschema.sql2
-rw-r--r--include/mp3fs.h18
-rw-r--r--src/mp3_subr.c160
-rwxr-xr-xsrc/mp3_vnops.c5
4 files changed, 111 insertions, 74 deletions
diff --git a/dbschema.sql b/dbschema.sql
index b46aa88..7528591 100644
--- a/dbschema.sql
+++ b/dbschema.sql
@@ -18,4 +18,4 @@ CREATE TABLE song (
CREATE TABLE genre (
name varchar(200) NOT NULL,
PRIMARY KEY(name)
-)
+);
diff --git a/include/mp3fs.h b/include/mp3fs.h
index cbb1537..f8c5c5c 100644
--- a/include/mp3fs.h
+++ b/include/mp3fs.h
@@ -2,18 +2,11 @@
#define _MP3FS_H_
struct fuse_args;
-#define DBNAME "music.db"
+#define DBNAME "/home/lulf/dev/mp3fs/music.db"
int mp3_run(int, char **);
int mp3_initscan(char *);
-/*
- * Data passed to traverse function pointers.'
- */
-struct filler_data {
- void *buf;
- fuse_fill_dir_t filler;
-};
/*
* Functions traversing the underlying filesystem and do operations on the
@@ -23,4 +16,13 @@ typedef void traverse_fn_t(char *);
void traverse_hierarchy(char *, traverse_fn_t);
traverse_fn_t mp3_scan;
+/*
+ * Data passed to mp3_list.
+ */
+struct filler_data {
+ void *buf;
+ fuse_fill_dir_t filler;
+};
+
+void mp3_list(char *, int, struct filler_data *);
#endif
diff --git a/src/mp3_subr.c b/src/mp3_subr.c
index 869eadb..25c5c01 100644
--- a/src/mp3_subr.c
+++ b/src/mp3_subr.c
@@ -75,12 +75,6 @@ traverse_hierarchy(char *dirpath, traverse_fn_t fileop)
closedir(dirp);
}
-static int
-mp3_scan_callback(void *ignore, int argc, char **argv, char **colname)
-{
- return 0;
-}
-
/* Scan the music initially. */
void
mp3_scan(char *filepath)
@@ -88,7 +82,6 @@ mp3_scan(char *filepath)
TagLib_File *file;
TagLib_Tag *tag;
char *artist, *album, *genre, *title;
- char *query, *errmsg;
int ret;
unsigned int year;
sqlite3_stmt *st;
@@ -111,33 +104,33 @@ mp3_scan(char *filepath)
if (artist == NULL)
break;
/* First find out if it exists. */
- asprintf(&query, "SELECT * FROM artist WHERE name='%s'",
- artist);
- if (query == NULL)
- break;
- ret = sqlite3_prepare(handle, query, -1, &st, NULL);
+ ret = sqlite3_prepare(handle, "SELECT * FROM artist WHERE "
+ "name=?", -1, &st, NULL);
if (ret != SQLITE_OK) {
- warnx("Error preparing statement\n");
- free(query);
+ warnx("Error preparing statement: %s\n",
+ sqlite3_errmsg(handle));
break;
}
+ sqlite3_bind_text(st, 1, artist, -1, SQLITE_STATIC);
ret = sqlite3_step(st);
sqlite3_finalize(st);
/* Already exists or generic error. */
if (ret != SQLITE_DONE)
break;
/* Doesn't exist, so we can insert it. */
- free(query);
- asprintf(&query, "INSERT INTO artist(name) VALUES('%s')",
- artist);
- if (query == NULL)
- break;
- ret = sqlite3_exec(handle, query, mp3_scan_callback, 0,
- &errmsg);
- free(query);
+ ret = sqlite3_prepare(handle, "INSERT INTO artist(name) "
+ "VALUES(?)", -1, &st, NULL);
if (ret != SQLITE_OK) {
- warnx("Error inserting into database: %s\n", errmsg);
- sqlite3_free(errmsg);
+ warnx("Error preparing statement: %s\n",
+ sqlite3_errmsg(handle));
+ break;
+ }
+ sqlite3_bind_text(st, 1, artist, -1, SQLITE_STATIC);
+ ret = sqlite3_step(st);
+ sqlite3_finalize(st);
+ if (ret != SQLITE_DONE) {
+ warnx("Error inserting into database: %s\n",
+ sqlite3_errmsg(handle));
break;
}
} while (0);
@@ -148,30 +141,33 @@ mp3_scan(char *filepath)
if (genre == NULL)
break;
/* First find out if it exists. */
- asprintf(&query, "SELECT * FROM genre WHERE name='%s'", genre);
- if (query == NULL)
- break;
- ret = sqlite3_prepare(handle, query, -1, &st, NULL);
+ ret = sqlite3_prepare(handle, "SELECT * FROM genre WHERE "
+ "name=?", -1, &st, NULL);
if (ret != SQLITE_OK) {
- warnx("Error preparing statement\n");
- free(query);
+ warnx("Error preparing statement: %s\n",
+ sqlite3_errmsg(handle));
break;
}
+ sqlite3_bind_text(st, 1, genre, -1, SQLITE_STATIC);
ret = sqlite3_step(st);
sqlite3_finalize(st);
/* Already exists or generic error. */
if (ret != SQLITE_DONE)
break;
- free(query);
- asprintf(&query, "INSERT INTO genre(name) VALUES('%s')", genre);
- if (query == NULL)
- break;
- ret = sqlite3_exec(handle, query, mp3_scan_callback, 0,
- &errmsg);
- free(query);
+ /* Doesn't exist, so we can insert it. */
+ ret = sqlite3_prepare(handle, "INSERT INTO genre(name) "
+ "VALUES(?)", -1, &st, NULL);
if (ret != SQLITE_OK) {
- warnx("Error inserting into database: %s\n", errmsg);
- sqlite3_free(errmsg);
+ warnx("Error preparing statement: %s\n",
+ sqlite3_errmsg(handle));
+ break;
+ }
+ sqlite3_bind_text(st, 1, genre, -1, SQLITE_STATIC);
+ ret = sqlite3_step(st);
+ sqlite3_finalize(st);
+ if (ret != SQLITE_DONE) {
+ warnx("Error inserting into database: %s\n",
+ sqlite3_errmsg(handle));
break;
}
} while (0);
@@ -186,40 +182,78 @@ mp3_scan(char *filepath)
break;
/* First find out if it exists. */
- asprintf(&query, "SELECT * FROM song, artist WHERE "
- "artist.name=song.artistname AND title='%s' AND year=%u",
- title, year);
- if (query == NULL)
- break;
- ret = sqlite3_prepare(handle, query, -1, &st, NULL);
+ ret = sqlite3_prepare(handle, "SELECT * FROM song, artist "
+ " WHERE artist.name=song.artistname AND title=? AND year=?",
+ -1, &st, NULL);
if (ret != SQLITE_OK) {
- warnx("Error preparing statement\n");
- free(query);
+ warnx("Error preparing statement: %s\n",
+ sqlite3_errmsg(handle));
break;
}
+ sqlite3_bind_text(st, 1, title, -1, SQLITE_STATIC);
+ sqlite3_bind_int(st, 2, year);
ret = sqlite3_step(st);
sqlite3_finalize(st);
- free(query);
/* Already exists or generic error. */
- if (ret != SQLITE_DONE) {
- printf("Duplicate song!\n");
+ if (ret != SQLITE_DONE)
break;
- }
/* Now, finally insert it. */
- asprintf(&query, "INSERT INTO song(title, artistname, album, "
- "genrename, year) VALUES('%s', '%s', '%s', '%s', %u)",
- title, artist, album, genre, year);
- if (query == NULL)
- break;
- ret = sqlite3_exec(handle, query, mp3_scan_callback, 0,
- &errmsg);
- free(query);
+ ret = sqlite3_prepare(handle, "INSERT INTO song(title, "
+ "artistname, album, genrename, year) VALUES(?, ?, ?, ?, ?)",
+ -1, &st, NULL);
if (ret != SQLITE_OK) {
- warnx("Error inserting into database: %s\n", errmsg);
- sqlite3_free(errmsg);
+ warnx("Error preparing insert statement: %s\n",
+ sqlite3_errmsg(handle));
break;
- }
-
+ }
+ sqlite3_bind_text(st, 1, title, -1, SQLITE_STATIC);
+ sqlite3_bind_text(st, 2, artist, -1, SQLITE_STATIC);
+ sqlite3_bind_text(st, 3, album, -1, SQLITE_STATIC);
+ sqlite3_bind_text(st, 4, genre, -1, SQLITE_STATIC);
+ sqlite3_bind_int(st, 5, year);
+ ret = sqlite3_step(st);
+ sqlite3_finalize(st);
+ if (ret != SQLITE_DONE) {
+ warnx("Error inserting into database: %s\n",
+ sqlite3_errmsg(handle));
+ break;
+ }
} while (0);
taglib_tag_free_strings();
}
+
+/*
+ * Perform query and list the result from field.
+ */
+void
+mp3_list(char *query, int field, struct filler_data *fd)
+{
+ sqlite3_stmt *st;
+ fuse_fill_dir_t filler;
+ void *buf;
+ const unsigned char *value;
+ int error, ret;
+
+ filler = fd->filler;
+ buf = fd->buf;
+ /* Open database. */
+ error = sqlite3_open(DBNAME, &handle);
+ if (error) {
+ warnx("Can't open database: %s\n", sqlite3_errmsg(handle));
+ sqlite3_close(handle);
+ return;
+ }
+ ret = sqlite3_prepare_v2(handle, query, -1, &st, NULL);
+ if (ret != SQLITE_OK) {
+ // warnx("Error preparing statement\n");
+ return;
+ }
+ ret = sqlite3_step(st);
+ while (ret == SQLITE_ROW) {
+ value = sqlite3_column_text(st, field);
+ filler(buf, (const char *)value, NULL, 0);
+ ret = sqlite3_step(st);
+ }
+ // XXX: Check for errors too.
+ sqlite3_close(handle);
+}
diff --git a/src/mp3_vnops.c b/src/mp3_vnops.c
index 0df1cb9..436be50 100755
--- a/src/mp3_vnops.c
+++ b/src/mp3_vnops.c
@@ -58,9 +58,10 @@ static int mp3_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
* 2. Find the mp3s that matches the tags given from the path.
* 3. Return the list of those mp3s.
*/
+ DEBUG("Going to run query\n");
if (!strcmp(path, "/Artists")) {
+ mp3_list("SELECT name FROM artist", 0, &fd);
/* List artists. */
- /* XXX: need to free selection structure!. */
return (0);
}
return (-ENOENT);
@@ -148,6 +149,6 @@ mp3_run(int argc, char **argv)
mp3_initscan(musicpath);
ret = 0;
-// ret = fuse_main(args.argc, args.argv, &mp3_ops, NULL);
+ ret = fuse_main(args.argc, args.argv, &mp3_ops, NULL);
return (ret);
}