summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/musicfs.h16
-rw-r--r--src/mfs_subr.c50
-rwxr-xr-xsrc/mfs_vnops.c57
3 files changed, 38 insertions, 85 deletions
diff --git a/include/musicfs.h b/include/musicfs.h
index 8443198..5c141c3 100644
--- a/include/musicfs.h
+++ b/include/musicfs.h
@@ -42,14 +42,6 @@ struct filler_data {
fuse_fill_dir_t filler;
};
-/*
- * Data passed to mfs_lookup_open
- */
-struct file_data {
- int fd;
- int found;
-};
-
#define LIST_DATATYPE_STRING 1
#define LIST_DATATYPE_INT 2
@@ -59,10 +51,8 @@ enum mfs_filetype { MFS_NOTFOUND, MFS_FILE, MFS_DIRECTORY };
typedef int lookup_fn_t(void *, const char *);
/* Lookup function listing each row. */
lookup_fn_t mfs_lookup_list;
-/* Lookup function opening the first file match. */
-lookup_fn_t mfs_lookup_open;
-/* Lookup function stat'ing a file. */
-lookup_fn_t mfs_lookup_stat;
+/* Lookup real pathname for a file. */
+lookup_fn_t mfs_lookup_path;
/* Lookup function loading a path into DB */
lookup_fn_t mfs_lookup_load_path;
@@ -77,7 +67,7 @@ void mfs_lookup_genre(const char *, struct filler_data *);
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_realpath(const char *, char **);
int mfs_reload_config();
char *mfs_get_home_path(const char *);
diff --git a/src/mfs_subr.c b/src/mfs_subr.c
index d70bade..6f4c7b6 100644
--- a/src/mfs_subr.c
+++ b/src/mfs_subr.c
@@ -559,22 +559,17 @@ mfs_lookup_finish(struct lookuphandle *lh)
}
/*
- * Build a file_data struct for a path.
+ * Return the realpath given a path in the musicfs tree.
*
- * data should be a pointer to a file handle
- * returns 0 on success
+ * Returns pointer to real path or NULL if not found.
*/
-int
-mfs_file_data_for_path(const char *path, void *data) {
- DEBUG("getting file data for %s\n", path);
- struct file_data *fd;
- fd = (struct file_data *)data;
+int
+mfs_realpath(const char *path, char **realpath) {
+ DEBUG("getting real path for %s\n", path);
struct lookuphandle *lh;
char *artist, *album, *title;
lh = NULL;
- fd->fd = -1;
- fd->found = 0;
/* Open a specific track. */
if (strncmp(path, "/Tracks", 7) == 0) {
@@ -583,7 +578,7 @@ mfs_file_data_for_path(const char *path, void *data) {
title = mfs_gettoken(path, 2);
if (title == NULL)
break;
- lh = mfs_lookup_start(0, fd, mfs_lookup_open,
+ lh = mfs_lookup_start(0, realpath, mfs_lookup_path,
"SELECT filepath FROM song "
"WHERE (artistname||' - '||title||'.'||extension) LIKE ?");
if (lh == NULL)
@@ -602,7 +597,7 @@ mfs_file_data_for_path(const char *path, void *data) {
title = mfs_gettoken(path, 3);
if (title == NULL)
break;
- lh = mfs_lookup_start(0, fd, mfs_lookup_open,
+ lh = mfs_lookup_start(0, realpath, mfs_lookup_path,
"SELECT filepath FROM song "
"WHERE (title||'.'||extension) LIKE ? AND "
"album LIKE ?");
@@ -624,7 +619,7 @@ mfs_file_data_for_path(const char *path, void *data) {
if (!(artist && album && title)) {
break;
}
- lh = mfs_lookup_start(0, fd, mfs_lookup_open,
+ 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 ?");
@@ -642,7 +637,8 @@ mfs_file_data_for_path(const char *path, void *data) {
if (lh) {
mfs_lookup_finish(lh);
}
-
+ if (*realpath == NULL)
+ return (-ENOMEM);
return 0;
}
@@ -846,31 +842,15 @@ mfs_lookup_list(void *data, const char *str)
}
/*
- * Lookup a file and open it if found.
- */
-int
-mfs_lookup_open(void *data, const char *str)
-{
- struct file_data *fd;
-
- fd = (struct file_data *)data;
- fd->fd = open(str, O_RDONLY);
- fd->found = 1;
- return (1);
-}
-
-/*
- * Stat a file.
- * XXX: watch out for duplicates, we might stat more than once.
+ * Lookup the real path of a file.
*/
int
-mfs_lookup_stat(void *data, const char *str)
+mfs_lookup_path(void *data, const char *str)
{
- struct stat *st;
- st = (struct stat *)data;
- if (stat(str, st) < 0)
- return (0);
+ char **path;
+ path = data;
+ *path = strdup(str);
return (1);
}
diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c
index 55907d9..07faf44 100755
--- a/src/mfs_vnops.c
+++ b/src/mfs_vnops.c
@@ -41,9 +41,7 @@
static int mfs_getattr (const char *path, struct stat *stbuf)
{
- struct file_data fd;
- fd.fd = -1;
- fd.found = 0;
+ char *realpath;
int status = 0;
memset (stbuf, 0, sizeof (struct stat));
@@ -71,19 +69,16 @@ static int mfs_getattr (const char *path, struct stat *stbuf)
return 0;
case MFS_FILE:
- status = mfs_file_data_for_path(path, &fd);
+ realpath = NULL;
+ status = mfs_realpath(path, &realpath);
if (status != 0)
- return (status);
-
- if (!fd.found)
- return (-ENOENT);
- if (fd.fd < 0)
- return (-EIO);
-
- if (fstat(fd.fd, stbuf) == 0)
- return (0);
- else
- return (-ENOENT);
+ return status;
+ if (stat(realpath, stbuf) != 0) {
+ free(realpath);
+ return -ENOENT;
+ }
+ free(realpath);
+ return 0;
case MFS_NOTFOUND:
default:
@@ -139,32 +134,20 @@ static int mfs_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
static int mfs_open (const char *path, struct fuse_file_info *fi)
{
- struct file_data fd;
- fd.fd = -1;
- fd.found = 0;
+ char *realpath;
+ int fd, status;
if (strcmp(path, "/.config") == 0)
return (0);
- int status = mfs_file_data_for_path(path, &fd);
+ status = mfs_realpath(path, &realpath);
if (status != 0)
- return (status);
-
- if (!fd.found)
- return (-ENOENT);
- if (fd.fd < 0)
- return (-EIO);
- fi->fh = fd.fd;
+ return status;
+ fd = open(realpath, O_RDONLY);
+ if (fd < 0)
+ return (-errno);
+ fi->fh = (uint64_t)fd;
return (0);
-
- /*
- * 1. Have a lookup cache for names?.
- * Parse Genre, Album, Artist etc.
- * 2. Find MP3s that match. XXX what do we do with duplicates? just
- * return the first match?
- * 3. Put the mnode of the mp3 in our cache.
- * 4. Signal in mnode that the mp3 is being read?
- */
}
static int mfs_read (const char *path, char *buf, size_t size, off_t offset,
@@ -185,7 +168,7 @@ static int mfs_read (const char *path, char *buf, size_t size, off_t offset,
return (bytes);
}
- fd = fi->fh;
+ fd = (int)fi->fh;
if (fd < 0)
return (-EIO);
bytes = read(fd, buf, size);
@@ -276,7 +259,7 @@ static int mfs_release(const char *path, struct fuse_file_info *fi)
mfs_reload_config();
}
- fd = fi->fh;
+ fd = (int)fi->fh;
if (fd >= 0)
close(fd);