dbfs.c revision 1.1 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 enum {
41 1.1 agc BigEndian = 4321,
42 1.1 agc LittleEndian = 1234
43 1.1 agc };
44 1.1 agc
45 1.1 agc static struct stat dbst; /* stat info of database file */
46 1.1 agc static BTREEINFO bt; /* btree information */
47 1.1 agc static int verbose; /* how chatty are we? */
48 1.1 agc static DB *db; /* data base operations struct */
49 1.1 agc
50 1.1 agc /* perform the stat operation */
51 1.1 agc /* if this is the root, then just synthesise the data */
52 1.1 agc /* otherwise, retrieve the data, and be sure to fill in the size */
53 1.1 agc /* we use the mtime of the database file for each of the records */
54 1.1 agc static int
55 1.1 agc dbfs_getattr(const char *path, struct stat *st)
56 1.1 agc {
57 1.1 agc int res = 0;
58 1.1 agc DBT k;
59 1.1 agc DBT v;
60 1.1 agc
61 1.1 agc (void) memset(st, 0x0, sizeof(*st));
62 1.1 agc if (strcmp(path, "/") == 0) {
63 1.1 agc st->st_mode = S_IFDIR | 0755;
64 1.1 agc st->st_nlink = 2;
65 1.1 agc } else {
66 1.1 agc k.size = strlen(k.data = __UNCONST(path + 1));
67 1.1 agc if ((*db->seq)(db, &k, &v, R_CURSOR) != 0) {
68 1.1 agc return -ENOENT;
69 1.1 agc }
70 1.1 agc st->st_mode = S_IFREG | 0444;
71 1.1 agc st->st_nlink = 1;
72 1.1 agc st->st_size = v.size;
73 1.1 agc st->st_mtime = dbst.st_mtime;
74 1.1 agc st->st_atime = dbst.st_atime;
75 1.1 agc st->st_ctime = dbst.st_ctime;
76 1.1 agc st->st_uid = dbst.st_uid;
77 1.1 agc st->st_gid = dbst.st_gid;
78 1.1 agc }
79 1.1 agc return res;
80 1.1 agc }
81 1.1 agc
82 1.1 agc /* readdir operation */
83 1.1 agc /* run through the database file, returning records by key */
84 1.1 agc static int
85 1.1 agc dbfs_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 static int ret;
89 1.1 agc static DBT k;
90 1.1 agc int flag;
91 1.1 agc DBT v;
92 1.1 agc (void) fi;
93 1.1 agc
94 1.1 agc if (offset == 0) {
95 1.1 agc (void) memset(&k, 0x0, sizeof(k));
96 1.1 agc (void) filler(buf, ".", NULL, 0);
97 1.1 agc (void) filler(buf, "..", NULL, 0);
98 1.1 agc flag = R_FIRST;
99 1.1 agc ret = 0;
100 1.1 agc } else {
101 1.1 agc flag = R_CURSOR;
102 1.1 agc }
103 1.1 agc if (ret != 0) {
104 1.1 agc return 0;
105 1.1 agc }
106 1.1 agc for ( ; (ret = (*db->seq)(db, &k, &v, flag)) == 0 ; flag = R_NEXT) {
107 1.1 agc if (filler(buf, k.data, NULL, 0) != 0) {
108 1.1 agc return 0;
109 1.1 agc }
110 1.1 agc }
111 1.1 agc return 0;
112 1.1 agc }
113 1.1 agc
114 1.1 agc /* open the file in the file system */
115 1.1 agc /* use seq method to get the record - get doesn't seem to work */
116 1.1 agc static int
117 1.1 agc dbfs_open(const char *path, struct fuse_file_info * fi)
118 1.1 agc {
119 1.1 agc DBT k;
120 1.1 agc DBT v;
121 1.1 agc
122 1.1 agc k.size = strlen(k.data = __UNCONST(path));
123 1.1 agc if ((*db->get)(db, &k, &v, 0) != 0) {
124 1.1 agc return -ENOENT;
125 1.1 agc }
126 1.1 agc
127 1.1 agc if ((fi->flags & 3) != O_RDONLY) {
128 1.1 agc return -EACCES;
129 1.1 agc }
130 1.1 agc return 0;
131 1.1 agc }
132 1.1 agc
133 1.1 agc /* read the file's contents in the file system */
134 1.1 agc /* use seq method to get the record - get doesn't seem to work */
135 1.1 agc static int
136 1.1 agc dbfs_read(const char *path, char *buf, size_t size, off_t offset,
137 1.1 agc struct fuse_file_info * fi)
138 1.1 agc {
139 1.1 agc size_t len;
140 1.1 agc DBT k;
141 1.1 agc DBT v;
142 1.1 agc (void) fi;
143 1.1 agc
144 1.1 agc k.size = strlen(k.data = __UNCONST(path + 1));
145 1.1 agc if ((*db->seq)(db, &k, &v, R_CURSOR) != 0) {
146 1.1 agc return -ENOENT;
147 1.1 agc }
148 1.1 agc len = v.size;
149 1.1 agc if (offset < len) {
150 1.1 agc if (offset + size > len) {
151 1.1 agc size = len - offset;
152 1.1 agc }
153 1.1 agc (void) memcpy(buf, (char *) v.data + offset, size);
154 1.1 agc buf[size - 1] = '\n';
155 1.1 agc } else {
156 1.1 agc size = 0;
157 1.1 agc }
158 1.1 agc return size;
159 1.1 agc }
160 1.1 agc
161 1.1 agc /* fill in the statvfs struct */
162 1.1 agc static int
163 1.1 agc dbfs_statfs(const char *path, struct statvfs *st)
164 1.1 agc {
165 1.1 agc (void) memset(st, 0x0, sizeof(*st));
166 1.1 agc st->f_blocks = dbst.st_size / dbst.st_blksize;
167 1.1 agc st->f_bsize = st->f_frsize = st->f_iosize = dbst.st_blksize;
168 1.1 agc st->f_owner = dbst.st_uid;
169 1.1 agc st->f_files = 1;
170 1.1 agc return 0;
171 1.1 agc }
172 1.1 agc
173 1.1 agc /* operations struct */
174 1.1 agc static struct fuse_operations dbfs_oper = {
175 1.1 agc .getattr = dbfs_getattr,
176 1.1 agc .readdir = dbfs_readdir,
177 1.1 agc .open = dbfs_open,
178 1.1 agc .read = dbfs_read,
179 1.1 agc .statfs = dbfs_statfs
180 1.1 agc };
181 1.1 agc
182 1.1 agc int
183 1.1 agc main(int argc, char **argv)
184 1.1 agc {
185 1.1 agc int i;
186 1.1 agc
187 1.1 agc while ((i = getopt(argc, argv, "v")) != -1) {
188 1.1 agc switch(i) {
189 1.1 agc case 'v':
190 1.1 agc verbose = 1;
191 1.1 agc break;
192 1.1 agc }
193 1.1 agc }
194 1.1 agc if (stat(argv[optind], &dbst) != 0) {
195 1.1 agc err(EXIT_FAILURE, "can't stat `%s'", argv[optind]);
196 1.1 agc }
197 1.1 agc bt.lorder = LittleEndian;
198 1.1 agc if ((db = dbopen(argv[optind], O_RDONLY, 0666, DB_BTREE, &bt)) == NULL) {
199 1.1 agc bt.lorder = BigEndian;
200 1.1 agc db = dbopen(argv[optind], O_RDONLY, 0666, DB_BTREE, &bt);
201 1.1 agc if (verbose) {
202 1.1 agc printf("Database not little endian - trying big endian now\n");
203 1.1 agc }
204 1.1 agc }
205 1.1 agc if (db == NULL) {
206 1.1 agc err(EXIT_FAILURE, "can't open db `%s'", argv[optind]);
207 1.1 agc }
208 1.1 agc return fuse_main(argc, argv, &dbfs_oper);
209 1.1 agc }
210