diff options
author | Kjetil Ørbekk <orbekk@pvv.ntnu.no> | 2008-08-12 15:04:55 +0200 |
---|---|---|
committer | Kjetil Ørbekk <orbekk@pvv.ntnu.no> | 2008-08-12 21:42:56 +0200 |
commit | b401c0765b296f2660550592cd4bd043f5f8203b (patch) | |
tree | 77a6a8ba451282ff9f4b68b210cf934ff0f16253 | |
parent | b4bb0a2ce7f9edd8de508432d17bc165fb108b63 (diff) |
- Restructured mp3_getattr
-rw-r--r-- | include/mp3fs.h | 3 | ||||
-rw-r--r-- | src/mp3_subr.c | 53 | ||||
-rwxr-xr-x | src/mp3_vnops.c | 94 |
3 files changed, 87 insertions, 63 deletions
diff --git a/include/mp3fs.h b/include/mp3fs.h index adc41db..4ecb6c8 100644 --- a/include/mp3fs.h +++ b/include/mp3fs.h @@ -35,6 +35,8 @@ struct file_data { #define LIST_DATATYPE_STRING 1 #define LIST_DATATYPE_INT 2 +enum mp3_filetype { MP3_NOTFOUND, MP3_FILE, MP3_DIRECTORY }; + /* A lookup functions returns 0 if it's finished. */ typedef int lookup_fn_t(void *, const char *); /* Lookup function listing each row. */ @@ -57,4 +59,5 @@ char *mp3_gettoken(const char *, int); int mp3_numtoken(const char *); int mp3_file_data_for_path(const char *, void *); +enum mp3_filetype mp3_get_filetype(const char *); #endif diff --git a/src/mp3_subr.c b/src/mp3_subr.c index 24ca7c7..6668b57 100644 --- a/src/mp3_subr.c +++ b/src/mp3_subr.c @@ -643,3 +643,56 @@ mp3_lookup_stat(void *data, const char *str) return (0); return (1); } + +/* + * Guess on a filetype for a path. + * + * Examples: + * + * - /Artists/Whoever should be a directory, since it's an + * Album. + * + * - /Tracks/Whatever should be a file, since Whatever is a song + * + * Note: This should only be used for paths inside the mp3fs + * directories (Artists, Tracks, ...) + */ +enum mp3_filetype +mp3_get_filetype(const char *path) { + int tokens = mp3_numtoken(path); + + if (strncmp(path, "/Genres", 7) == 0 || + strncmp(path, "/Artists", 8) == 0) { + switch (tokens) { + case 1: + case 2: + case 3: + return (MP3_DIRECTORY); + case 4: + return (MP3_FILE); + default: + return (MP3_NOTFOUND); + } + } else if (strncmp(path, "/Albums", 7) == 0) { + switch (tokens) { + case 1: + case 2: + return (MP3_DIRECTORY); + case 3: + return (MP3_FILE); + default: + return (MP3_NOTFOUND); + } + } else if (strncmp(path, "/Tracks", 7) == 0) { + switch (tokens) { + case 1: + return (MP3_DIRECTORY); + case 2: + return (MP3_FILE); + default: + return (MP3_NOTFOUND); + } + } else { + return (MP3_NOTFOUND); + } +} diff --git a/src/mp3_vnops.c b/src/mp3_vnops.c index 569e3d2..3260550 100755 --- a/src/mp3_vnops.c +++ b/src/mp3_vnops.c @@ -22,78 +22,46 @@ char *logpath = "/home/lulf/dev/mp3fs/mp3fs.log"; static int mp3_getattr (const char *path, struct stat *stbuf) { - struct lookuphandle *lh; - char *title, *album; - int tokens; + struct file_data fd; + fd.fd = -1; + fd.found = 0; + int status = 0; memset (stbuf, 0, sizeof (struct stat)); + if (strcmp (path, "/") == 0) { stbuf->st_mode = S_IFDIR | 0755; stbuf->st_nlink = 2; return 0; } - if (strncmp(path, "/Genres", 7) == 0 || - strncmp(path, "/Artists", 8) == 0) { - tokens = mp3_numtoken(path); - switch (tokens) { - case 1: - case 2: - case 3: - stbuf->st_mode = S_IFDIR | 0444; - stbuf->st_nlink = 1; - stbuf->st_size = 12; - return 0; - case 4: - stbuf->st_mode = S_IFREG | 0644; - stbuf->st_nlink = 1; - stbuf->st_size = 512; - return 0; - } - } else if (strncmp(path, "/Albums", 7) == 0) { - tokens = mp3_numtoken(path); - switch (tokens) { - case 1: - case 2: - stbuf->st_mode = S_IFDIR | 0444; - stbuf->st_nlink = 1; - stbuf->st_size = 12; - return (0); - case 3: - album = mp3_gettoken(path, 2); - if (album == NULL) - break; - title = mp3_gettoken(path, 3); - if (title == NULL) - break; - lh = mp3_lookup_start(0, stbuf, mp3_lookup_stat, - "SELECT filepath FROM song WHERE title LIKE ? AND " - "album LIKE ?"); - mp3_lookup_insert(lh, title, LIST_DATATYPE_STRING); - mp3_lookup_insert(lh, album, LIST_DATATYPE_STRING); - mp3_lookup_finish(lh); - return 0; - } - } else if (strncmp(path, "/Tracks", 7) == 0) { - tokens = mp3_numtoken(path); - switch (tokens) { - case 1: - stbuf->st_mode = S_IFDIR | 0444; - stbuf->st_nlink = 1; - stbuf->st_size = 12; - return 0; - case 2: - title = mp3_gettoken(path, 2); - if (title == NULL) - break; - lh = mp3_lookup_start(0, stbuf, mp3_lookup_stat, - "SELECT filepath FROM song WHERE title LIKE ?"); - mp3_lookup_insert(lh, title, LIST_DATATYPE_STRING); - mp3_lookup_finish(lh); - break; - } + + enum mp3_filetype type = mp3_get_filetype(path); + switch (type) { + case MP3_DIRECTORY: + stbuf->st_mode = S_IFDIR | 0444; + stbuf->st_nlink = 1; + stbuf->st_size = 12; return 0; + + case MP3_FILE: + status = mp3_file_data_for_path(path, &fd); + 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); + + case MP3_NOTFOUND: + default: + return -ENOENT; } - return -ENOENT; } |