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