diff options
author | lulf@carrot.studby.ntnu.no <lulf@carrot.studby.ntnu.no> | 2008-04-24 12:49:22 +0200 |
---|---|---|
committer | lulf@carrot.studby.ntnu.no <lulf@carrot.studby.ntnu.no> | 2008-04-24 12:49:22 +0200 |
commit | 0e11f57efb4ccb0460db3f045b21fc9729b6b656 (patch) | |
tree | 946dd1518d31046a707d7e6c3428028ec832dfc8 /src | |
parent | 3f5896ff3ccdea939d3586dd40d9b603d6c807f2 (diff) |
- Move out directory traversal routines into mp3_subr
Diffstat (limited to 'src')
-rw-r--r-- | src/mp3_subr.c | 95 | ||||
-rwxr-xr-x | src/mp3_vnops.c | 98 |
2 files changed, 97 insertions, 96 deletions
diff --git a/src/mp3_subr.c b/src/mp3_subr.c index a570139..03bc42b 100644 --- a/src/mp3_subr.c +++ b/src/mp3_subr.c @@ -1 +1,96 @@ /* Miscellaneous subroutines for mp3fs. */ +#define FUSE_USE_VERSION 26 +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <err.h> + +#include <sys/types.h> +#include <dirent.h> +#include <fuse.h> +#include <sys/param.h> + +#include <id3.h> +#include <mp3fs.h> + +/* + * Traverse a hierarchy given a directory path. Perform fileoperations on the + * files in the directory giving data as arguments, as well as recursing on + * sub-directories. + */ +void +traverse_hierarchy(char *dirpath, traverse_fn_t fileop, void *data) +{ + DIR *dirp; + struct dirent *dp; + char filepath[MAXPATHLEN]; + struct stat st; + struct filler_data *fd; + + fd = data; + dirp = opendir(dirpath); + if (dirp == NULL) { + fd->filler(fd->buf, "lolerr", NULL, 0); + return; + } + + /* Loop through all files. */ + while ((dp = readdir(dirp)) != NULL) { + if (!strcmp(dp->d_name, ".") || + !strcmp(dp->d_name, "..")) + continue; + + snprintf(filepath, sizeof(filepath), "%s/%s", + dirpath, dp->d_name); + if (stat(filepath, &st) < 0) + err(1, "error doing stat on %s", filepath); + /* Recurse if it's a directory. */ + if (st.st_mode & S_IFDIR) { + traverse_hierarchy(filepath, fileop, data); + continue; + } + /* If it's a regular file, perform the operation. */ + if (st.st_mode & S_IFREG) { + fileop(filepath, data); + } + } + closedir(dirp); +} + + + +/* + * Retrieve artist name given a file, and put in a buffer with a passed filler + * function + */ +void +mp3_artist(char *filepath, void *data) +{ + struct filler_data *fd; + ID3Tag *tag; + ID3Frame *artist; + ID3Field *field; + fuse_fill_dir_t filler; + void *buf; + char name[MAXPATHLEN]; + + /* Retrieve the filler function and pointers. */ + fd = (struct filler_data *)data; + filler = fd->filler; + buf = fd->buf; + + /* Get the tag. */ + tag = ID3Tag_New(); + ID3Tag_Link(tag, filepath); + artist = ID3Tag_FindFrameWithID(tag, ID3FID_LEADARTIST); + if (artist == NULL) + return; + field = ID3Frame_GetField(artist, ID3FN_TEXT); + if (field == NULL) + return; + ID3Field_GetASCII(field, name, ID3Field_Size(field)); + /* XXX: doesn't show up... why? */ + filler(buf, name, NULL, 0); + ID3Tag_Delete(tag); + return; +} diff --git a/src/mp3_vnops.c b/src/mp3_vnops.c index b04f0dc..8348064 100755 --- a/src/mp3_vnops.c +++ b/src/mp3_vnops.c @@ -8,25 +8,13 @@ #include <sys/types.h> #include <dirent.h> #include <fuse.h> +#include <sys/param.h> #include <id3.h> - -#define MAXPATHLEN 100000 +#include <mp3fs.h> char *musicpath = "/home/lulf/dev/mp3fs/music"; -typedef void traverse_fn_t(char *, void *); - -void traverse_hierarchy(char *, traverse_fn_t, void *); -traverse_fn_t mp3_artist; -/* - * Data passed to traverse function pointers.' - */ -struct filler_data { - void *buf; - fuse_fill_dir_t filler; -}; - static int mp3_getattr (const char *path, struct stat *stbuf) { memset (stbuf, 0, sizeof (struct stat)); @@ -82,88 +70,6 @@ call_lol(char *path) printf("Path %s\n", path); } -/* - * Traverse a hierarchy given a directory path. Perform fileoperations on the - * files in the directory giving data as arguments, as well as recursing on - * sub-directories. - */ -void -traverse_hierarchy(char *dirpath, traverse_fn_t fileop, void *data) -{ - DIR *dirp; - struct dirent *dp; - char filepath[MAXPATHLEN]; - struct stat st; - struct filler_data *fd; - - fd = data; - dirp = opendir(dirpath); - if (dirp == NULL) { - fd->filler(fd->buf, "lolerr", NULL, 0); - return; - } - - /* Loop through all files. */ - while ((dp = readdir(dirp)) != NULL) { - if (!strcmp(dp->d_name, ".") || - !strcmp(dp->d_name, "..")) - continue; - - snprintf(filepath, sizeof(filepath), "%s/%s", - dirpath, dp->d_name); - if (stat(filepath, &st) < 0) - err(1, "error doing stat on %s", filepath); - /* Recurse if it's a directory. */ - if (st.st_mode & S_IFDIR) { - traverse_hierarchy(filepath, fileop, data); - continue; - } - /* If it's a regular file, perform the operation. */ - if (st.st_mode & S_IFREG) { - fileop(filepath, data); - } - } - closedir(dirp); -} - - - -/* - * Retrieve artist name given a file, and put in a buffer with a passed filler - * function - */ -void -mp3_artist(char *filepath, void *data) -{ - struct filler_data *fd; - ID3Tag *tag; - ID3Frame *artist; - ID3Field *field; - fuse_fill_dir_t filler; - void *buf; - char name[MAXPATHLEN]; - - /* Retrieve the filler function and pointers. */ - fd = (struct filler_data *)data; - filler = fd->filler; - buf = fd->buf; - - /* Get the tag. */ - tag = ID3Tag_New(); - ID3Tag_Link(tag, filepath); - artist = ID3Tag_FindFrameWithID(tag, ID3FID_LEADARTIST); - if (artist == NULL) - return; - field = ID3Frame_GetField(artist, ID3FN_TEXT); - if (field == NULL) - return; - ID3Field_GetASCII(field, name, ID3Field_Size(field)); - /* XXX: doesn't show up... why? */ - filler(buf, name, NULL, 0); - ID3Tag_Delete(tag); - return; -} - static int mp3_open (const char *path, struct fuse_file_info *fi) { if (strcmp (path, "/Artists") == 0) |