id3fs.c revision 1.3 1 1.1 agc /*
2 1.1 agc * Copyright 2007 Alistair Crooks. All rights reserved.
3 1.1 agc *
4 1.1 agc * Redistribution and use in source and binary forms, with or without
5 1.1 agc * modification, are permitted provided that the following conditions
6 1.1 agc * are met:
7 1.1 agc * 1. Redistributions of source code must retain the above copyright
8 1.1 agc * notice, this list of conditions and the following disclaimer.
9 1.1 agc * 2. Redistributions in binary form must reproduce the above copyright
10 1.1 agc * notice, this list of conditions and the following disclaimer in the
11 1.1 agc * documentation and/or other materials provided with the distribution.
12 1.1 agc * 3. The name of the author may not be used to endorse or promote
13 1.1 agc * products derived from this software without specific prior written
14 1.1 agc * permission.
15 1.1 agc *
16 1.1 agc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 1.1 agc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.1 agc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 1.1 agc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 1.1 agc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 agc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 1.1 agc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 1.1 agc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 1.1 agc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 agc */
28 1.1 agc #include <sys/types.h>
29 1.1 agc
30 1.1 agc #include <db.h>
31 1.1 agc #include <err.h>
32 1.1 agc #include <errno.h>
33 1.1 agc #include <fcntl.h>
34 1.1 agc #include <fuse.h>
35 1.1 agc #include <stdio.h>
36 1.1 agc #include <stdlib.h>
37 1.1 agc #include <string.h>
38 1.1 agc #include <unistd.h>
39 1.1 agc
40 1.1 agc #include "virtdir.h"
41 1.1 agc #include "defs.h"
42 1.1 agc
43 1.1 agc static char *prefix; /* where the music is */
44 1.1 agc static int verbose; /* how chatty are we? */
45 1.1 agc
46 1.1 agc static BTREEINFO bt;
47 1.1 agc
48 1.1 agc static virtdir_t id3;
49 1.1 agc
50 1.1 agc
51 1.1 agc /* perform the stat operation */
52 1.1 agc /* if this is the root, then just synthesise the data */
53 1.1 agc /* otherwise, retrieve the data, and be sure to fill in the size */
54 1.1 agc static int
55 1.1 agc id3fs_getattr(const char *path, struct stat *st)
56 1.1 agc {
57 1.1 agc virt_dirent_t *ep;
58 1.1 agc
59 1.1 agc if (strcmp(path, "/") == 0) {
60 1.1 agc (void) memset(st, 0x0, sizeof(*st));
61 1.1 agc st->st_mode = S_IFDIR | 0755;
62 1.1 agc st->st_nlink = 2;
63 1.1 agc return 0;
64 1.1 agc }
65 1.1 agc if ((ep = virtdir_find(&id3, path, strlen(path))) == NULL) {
66 1.1 agc return -ENOENT;
67 1.1 agc }
68 1.1 agc switch(ep->type) {
69 1.1 agc case 'f':
70 1.1 agc (void) memcpy(st, &id3.file, sizeof(*st));
71 1.1 agc break;
72 1.1 agc case 'd':
73 1.1 agc (void) memcpy(st, &id3.dir, sizeof(*st));
74 1.1 agc break;
75 1.1 agc case 'l':
76 1.1 agc (void) memcpy(st, &id3.lnk, sizeof(*st));
77 1.1 agc st->st_size = ep->tgtlen;
78 1.1 agc break;
79 1.1 agc }
80 1.1 agc return 0;
81 1.1 agc }
82 1.1 agc
83 1.1 agc /* readdir operation */
84 1.1 agc static int
85 1.1 agc id3fs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
86 1.1 agc off_t offset, struct fuse_file_info * fi)
87 1.1 agc {
88 1.1 agc virt_dirent_t *dp;
89 1.1 agc VIRTDIR *dirp;
90 1.1 agc
91 1.1 agc if ((dirp = openvirtdir(&id3, path)) == NULL) {
92 1.1 agc return 0;
93 1.1 agc }
94 1.1 agc filler(buf, ".", NULL, 0);
95 1.1 agc filler(buf, "..", NULL, 0);
96 1.1 agc while ((dp = readvirtdir(dirp)) != NULL) {
97 1.1 agc filler(buf, dp->d_name, NULL, 0);
98 1.1 agc }
99 1.1 agc closevirtdir(dirp);
100 1.1 agc return 0;
101 1.1 agc }
102 1.1 agc
103 1.1 agc /* open the file in the file system */
104 1.1 agc static int
105 1.1 agc id3fs_open(const char *path, struct fuse_file_info * fi)
106 1.1 agc {
107 1.1 agc return 0;
108 1.1 agc }
109 1.1 agc
110 1.1 agc /* read the file's contents in the file system */
111 1.1 agc static int
112 1.1 agc id3fs_read(const char *path, char *buf, size_t size, off_t offset,
113 1.1 agc struct fuse_file_info * fi)
114 1.1 agc {
115 1.1 agc return 0;
116 1.1 agc }
117 1.1 agc
118 1.1 agc /* fill in the statvfs struct */
119 1.1 agc static int
120 1.1 agc id3fs_statfs(const char *path, struct statvfs *st)
121 1.1 agc {
122 1.1 agc (void) memset(st, 0x0, sizeof(*st));
123 1.1 agc return 0;
124 1.1 agc }
125 1.1 agc
126 1.1 agc /* read the symbolic link */
127 1.1 agc static int
128 1.1 agc id3fs_readlink(const char *path, char *buf, size_t size)
129 1.1 agc {
130 1.1 agc virt_dirent_t *ep;
131 1.1 agc
132 1.1 agc if ((ep = virtdir_find(&id3, path, strlen(path))) == NULL) {
133 1.1 agc return -ENOENT;
134 1.1 agc }
135 1.1 agc if (ep->tgt == NULL) {
136 1.1 agc return -ENOENT;
137 1.1 agc }
138 1.1 agc (void) strlcpy(buf, ep->tgt, size);
139 1.1 agc return 0;
140 1.1 agc }
141 1.1 agc
142 1.1 agc /* operations struct */
143 1.1 agc static struct fuse_operations id3fs_oper = {
144 1.1 agc .getattr = id3fs_getattr,
145 1.1 agc .readlink = id3fs_readlink,
146 1.1 agc .readdir = id3fs_readdir,
147 1.1 agc .open = id3fs_open,
148 1.1 agc .read = id3fs_read,
149 1.1 agc .statfs = id3fs_statfs
150 1.1 agc };
151 1.1 agc
152 1.1 agc /* build up a fuse_tree from the information in the database */
153 1.1 agc static void
154 1.1 agc build_id3_tree(DB *db, virtdir_t *tp)
155 1.1 agc {
156 1.1 agc struct stat dir;
157 1.1 agc struct stat f;
158 1.1 agc const char *d;
159 1.1 agc char name[MAXPATHLEN];
160 1.1 agc char *slash;
161 1.1 agc char *key;
162 1.1 agc char *val;
163 1.1 agc int flag;
164 1.1 agc DBT k;
165 1.1 agc DBT v;
166 1.1 agc
167 1.1 agc (void) stat(".", &dir);
168 1.1 agc (void) memcpy(&f, &dir, sizeof(f));
169 1.1 agc f.st_mode = S_IFREG | 0644;
170 1.1 agc (void) memset(&k, 0x0, sizeof(k));
171 1.1 agc (void) memset(&v, 0x0, sizeof(v));
172 1.1 agc for (flag = R_FIRST ; (*db->seq)(db, &k, &v, flag) == 0 ; flag = R_NEXT) {
173 1.1 agc key = (char *) k.data;
174 1.1 agc switch (*key) {
175 1.1 agc case 'a':
176 1.1 agc d = "/artists";
177 1.1 agc break;
178 1.1 agc case 'g':
179 1.1 agc d = "/genre";
180 1.1 agc break;
181 1.1 agc case 'y':
182 1.1 agc d = "/year";
183 1.1 agc break;
184 1.1 agc default:
185 1.1 agc continue;
186 1.1 agc }
187 1.1 agc val = (char *) v.data;
188 1.1 agc if ((slash = strrchr(key, '/')) == NULL) {
189 1.1 agc slash = key;
190 1.1 agc }
191 1.1 agc slash += 1;
192 1.1 agc /* add the symbolic link in that directory */
193 1.1 agc (void) snprintf(name, sizeof(name), "%s/%s/%s", d, val, slash);
194 1.1 agc if (!virtdir_find(tp, name, strlen(name))) {
195 1.3 agc virtdir_add(tp, name, strlen(name), 'l', key + 1,
196 1.3 agc strlen(key + 1));
197 1.1 agc }
198 1.1 agc }
199 1.1 agc }
200 1.1 agc
201 1.1 agc int
202 1.1 agc main(int argc, char **argv)
203 1.1 agc {
204 1.1 agc char name[MAXPATHLEN];
205 1.1 agc int i;
206 1.1 agc DB *db;
207 1.1 agc
208 1.1 agc while ((i = getopt(argc, argv, "p:v")) != -1) {
209 1.1 agc switch(i) {
210 1.1 agc case 'p':
211 1.1 agc prefix = optarg;
212 1.1 agc break;
213 1.1 agc case 'v':
214 1.1 agc verbose = 1;
215 1.1 agc break;
216 1.1 agc }
217 1.1 agc }
218 1.1 agc bt.lorder = 4321;
219 1.1 agc (void) snprintf(name, sizeof(name), "%s/%s", (prefix) ? prefix : "/usr/music", ".id3.db");
220 1.1 agc if ((db = dbopen(name, O_RDONLY, 0666, DB_BTREE, &bt)) == NULL) {
221 1.1 agc warn("null id3 database");
222 1.1 agc }
223 1.1 agc build_id3_tree(db, &id3);
224 1.3 agc return fuse_main(argc, argv, &id3fs_oper, NULL);
225 1.1 agc }
226