summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Lilleengen <lulf@pvv.ntnu.no>2008-09-07 13:24:31 +0200
committerUlf Lilleengen <lulf@pvv.ntnu.no>2008-09-07 13:24:31 +0200
commitc74bd922870214c48ac5d032f13b0751ef32be59 (patch)
tree4482c8fc302a3fa35a8aed82e2e10c0b92584c8f
parent2cbb65112e9fb20a3532e0cf7b8ba49a44e0b024 (diff)
- Fix serialization of debug printouts.
- Add a MFS_DB_LOCK/UNLOCK which handles locking of the SQLITE database if SQLITE_THREADED compile option is not specified. - Remove duplicate open/close of database during initial scan.
-rw-r--r--include/debug.h11
-rw-r--r--include/musicfs.h2
-rw-r--r--src/mfs_subr.c72
-rwxr-xr-xsrc/mfs_vnops.c5
4 files changed, 63 insertions, 27 deletions
diff --git a/include/debug.h b/include/debug.h
index a30af63..350a27e 100644
--- a/include/debug.h
+++ b/include/debug.h
@@ -25,10 +25,15 @@
#ifdef DEBUGGING
# include <stdio.h>
# define DEBUGPATH "debug.txt"
+extern pthread_mutex_t __debug_lock__;
FILE *__debug_handle__;
-# define DEBUG(...) __debug_handle__ = fopen(DEBUGPATH, "a"); \
- fprintf (__debug_handle__, __VA_ARGS__); \
- fclose(__debug_handle__);
+# define DEBUG(...) do { \
+ pthread_mutex_lock(&__debug_lock__); \
+ __debug_handle__ = fopen(DEBUGPATH, "a"); \
+ fprintf (__debug_handle__, __VA_ARGS__); \
+ fclose(__debug_handle__); \
+ pthread_mutex_unlock(&__debug_lock__); \
+ } while (0)
#else
# define DEBUG(...)
#endif
diff --git a/include/musicfs.h b/include/musicfs.h
index ca5d785..2a26880 100644
--- a/include/musicfs.h
+++ b/include/musicfs.h
@@ -23,7 +23,7 @@
struct fuse_args;
int mfs_run(int, char **);
-int mfs_initscan();
+int mfs_init();
/*
diff --git a/src/mfs_subr.c b/src/mfs_subr.c
index fbfb0b1..4ddd81e 100644
--- a/src/mfs_subr.c
+++ b/src/mfs_subr.c
@@ -32,6 +32,7 @@
#include <sys/param.h>
#include <dirent.h>
#include <fuse.h>
+#include <pthread.h>
#include <tag_c.h>
#include <musicfs.h>
@@ -40,6 +41,14 @@
#define MFS_HANDLE ((void*)-1)
+#ifdef SQLITE_THREADED
+#define MFS_DB_LOCK()
+#define MFS_DB_UNLOCK()
+#else
+#define MFS_DB_LOCK() pthread_mutex_lock(&dblock)
+#define MFS_DB_UNLOCK() pthread_mutex_unlock(&dblock)
+#endif
+
struct lookuphandle {
sqlite3 *handle;
sqlite3_stmt *st;
@@ -52,6 +61,8 @@ struct lookuphandle {
char *db_path;
sqlite3 *handle;
+pthread_mutex_t dblock;
+pthread_mutex_t __debug_lock__;
/*
* Returns the path to $HOME[/extra]
@@ -225,10 +236,12 @@ mfs_reload_config()
return (-1);
}
+ MFS_DB_LOCK();
res = sqlite3_open(db_path, &handle);
if (res) {
DEBUG("Can't open database: %s\n", sqlite3_errmsg(handle));
sqlite3_close(handle);
+ MFS_DB_UNLOCK();
return (-1);
}
@@ -238,6 +251,7 @@ mfs_reload_config()
if (res != SQLITE_OK) {
DEBUG("Error preparing statement: %s\n",
sqlite3_errmsg(handle));
+ MFS_DB_UNLOCK();
return (-1);
}
res = sqlite3_step(st);
@@ -267,31 +281,29 @@ mfs_reload_config()
"SELECT path FROM path");
mfs_lookup_finish(lh);
+ MFS_DB_UNLOCK();
return (0);
}
+
int
-mfs_initscan()
+mfs_init()
{
int error;
db_path = mfs_get_home_path(".mfs.db");
- /* Open database. */
- error = sqlite3_open(db_path, &handle);
- if (error) {
- DEBUG("Can't open database: %s\n", sqlite3_errmsg(handle));
- sqlite3_close(handle);
- return (-1);
- }
+
+ /* Init locks. */
+ pthread_mutex_init(&dblock, NULL);
+ pthread_mutex_init(&__debug_lock__, NULL);
/* error = mfs_insert_path(musicpath, handle); */
/* if (error != 0) */
/* return (error); */
error = mfs_reload_config();
- if (error != 0)
+ if (error != 0) {
return (error);
-
- sqlite3_close(handle);
+ }
return (0);
}
@@ -678,46 +690,57 @@ mfs_realpath(const char *path, char **realpath) {
DEBUG("getting real path for %s\n", path);
struct lookuphandle *lh;
char *artist, *album, *title;
+ int error;
lh = NULL;
+ error = 0;
/* Open a specific track. */
+ MFS_DB_LOCK();
if (strncmp(path, "/Tracks", 7) == 0) {
switch (mfs_numtoken(path)) {
case 2:
title = mfs_gettoken(path, 2);
- if (title == NULL)
+ if (title == NULL) {
+ error = -ENOENT;
break;
+ }
lh = mfs_lookup_start(0, realpath, mfs_lookup_path,
"SELECT filepath FROM song "
"WHERE (artistname||' - '||title||'.'||extension) LIKE ?");
- if (lh == NULL)
- return (-EIO);
+ if (lh == NULL) {
+ error = -EIO;
+ break;
+ }
mfs_lookup_insert(lh, title, LIST_DATATYPE_STRING);
break;
default:
- return (-ENOENT);
+ error = -ENOENT;
}
} else if (strncmp(path, "/Albums", 7) == 0) {
switch (mfs_numtoken(path)) {
case 3:
album = mfs_gettoken(path, 2);
+ error = -ENOENT;
if (album == NULL)
break;
title = mfs_gettoken(path, 3);
if (title == NULL)
break;
+ error = 0;
lh = mfs_lookup_start(0, realpath, mfs_lookup_path,
"SELECT filepath FROM song "
"WHERE (title||'.'||extension) LIKE ? AND "
"album LIKE ?");
- if (lh == NULL)
- return (-EIO);
+ if (lh == NULL) {
+ error = -EIO;
+ break;
+ }
mfs_lookup_insert(lh, title, LIST_DATATYPE_STRING);
mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING);
break;
default:
- return (-ENOENT);
+ error = -ENOENT;
}
} else if (strncmp(path, "/Artists", 8) == 0) {
switch (mfs_numtoken(path)) {
@@ -727,26 +750,33 @@ mfs_realpath(const char *path, char **realpath) {
title = mfs_gettoken(path, 4);
DEBUG("artist(%s) album(%s) title(%s)\n", artist, album, title);
if (!(artist && album && title)) {
+ error = -ENOENT;
break;
}
lh = mfs_lookup_start(0, realpath, mfs_lookup_path,
"SELECT filepath FROM song WHERE artistname LIKE ? AND "
"album LIKE ? AND "
"(LTRIM(track||' ')||title||'.'||extension) LIKE ?");
- if (lh == NULL)
- return (-EIO);
+ if (lh == NULL) {
+ error = -EIO;
+ break;
+ }
mfs_lookup_insert(lh, artist, LIST_DATATYPE_STRING);
mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING);
mfs_lookup_insert(lh, title, LIST_DATATYPE_STRING);
break;
default:
- return (-ENOENT);
+ error = -ENOENT;
}
}
if (lh) {
mfs_lookup_finish(lh);
}
+
+ MFS_DB_UNLOCK();
+ if (error != 0)
+ return (error);
if (*realpath == NULL)
return (-ENOMEM);
return 0;
diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c
index edd1414..03e5118 100755
--- a/src/mfs_vnops.c
+++ b/src/mfs_vnops.c
@@ -34,6 +34,7 @@
#include <sys/param.h>
#include <sys/uio.h>
#include <unistd.h>
+#include <pthread.h>
#include <tag_c.h>
#include <musicfs.h>
@@ -228,7 +229,7 @@ static int mfs_truncate(const char *path, off_t size)
if (mfsrc == NULL)
return (-ENOMEM);
res = truncate(mfsrc, size);
- DEBUG("truncated %s with result: %d\n", mfsrc, res)
+ DEBUG("truncated %s with result: %d\n", mfsrc, res);
free(mfsrc);
return (res);
}
@@ -390,7 +391,7 @@ mfs_run(int argc, char **argv)
if (fuse_opt_parse(&args, NULL, NULL, musicfs_opt_proc) != 0)
exit (1);
- mfs_initscan();
+ mfs_init();
ret = 0;
ret = fuse_main(args.argc, args.argv, &mfs_ops, NULL);