Sync usage with man page.
[netbsd-mini2440.git] / share / examples / refuse / pcifs / pcifs.c
bloba364cdb487aff79a11a76dfa84458aad0f1ea6d7
1 /*
2 * Copyright © 2007 Alistair Crooks. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote
13 * products derived from this software without specific prior written
14 * permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <fuse.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "virtdir.h"
40 #include "defs.h"
42 static int verbose; /* how chatty are we? */
44 static virtdir_t pci;
47 /* perform the stat operation */
48 /* if this is the root, then just synthesise the data */
49 /* otherwise, retrieve the data, and be sure to fill in the size */
50 static int
51 pcifs_getattr(const char *path, struct stat *st)
53 virt_dirent_t *ep;
55 if (strcmp(path, "/") == 0) {
56 (void) memset(st, 0x0, sizeof(*st));
57 st->st_mode = S_IFDIR | 0755;
58 st->st_nlink = 2;
59 return 0;
61 if ((ep = virtdir_find(&pci, path, strlen(path))) == NULL) {
62 return -ENOENT;
64 switch(ep->type) {
65 case 'f':
66 break;
67 case 'd':
68 (void) memcpy(st, &pci.dir, sizeof(*st));
69 break;
70 case 'l':
71 (void) memcpy(st, &pci.lnk, sizeof(*st));
72 st->st_size = ep->tgtlen;
73 st->st_mode = S_IFLNK | 0644;
74 break;
76 return 0;
79 /* readdir operation */
80 static int
81 pcifs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
82 off_t offset, struct fuse_file_info * fi)
84 virt_dirent_t *dp;
85 VIRTDIR *dirp;
87 if ((dirp = openvirtdir(&pci, path)) == NULL) {
88 return 0;
90 filler(buf, ".", NULL, 0);
91 filler(buf, "..", NULL, 0);
92 while ((dp = readvirtdir(dirp)) != NULL) {
93 filler(buf, dp->d_name, NULL, 0);
95 closevirtdir(dirp);
96 return 0;
99 /* open the file in the file system */
100 static int
101 pcifs_open(const char *path, struct fuse_file_info * fi)
103 return 0;
106 /* read the file's contents in the file system */
107 static int
108 pcifs_read(const char *path, char *buf, size_t size, off_t offset,
109 struct fuse_file_info * fi)
111 return 0;
114 /* fill in the statvfs struct */
115 static int
116 pcifs_statfs(const char *path, struct statvfs *st)
118 (void) memset(st, 0x0, sizeof(*st));
119 return 0;
122 /* read the symbolic link */
123 static int
124 pcifs_readlink(const char *path, char *buf, size_t size)
126 virt_dirent_t *ep;
128 if ((ep = virtdir_find(&pci, path, strlen(path))) == NULL) {
129 return -ENOENT;
131 if (ep->tgt == NULL) {
132 return -ENOENT;
134 (void) strlcpy(buf, ep->tgt, size);
135 return 0;
138 /* operations struct */
139 static struct fuse_operations pcifs_oper = {
140 .getattr = pcifs_getattr,
141 .readlink = pcifs_readlink,
142 .readdir = pcifs_readdir,
143 .open = pcifs_open,
144 .read = pcifs_read,
145 .statfs = pcifs_statfs
148 /* build up a fuse_tree from the information in the database */
149 static int
150 build_tree(virtdir_t *tp, int bus)
152 struct stat dir;
153 struct stat f;
154 FILE *pp;
155 char name[MAXPATHLEN];
156 char buf[BUFSIZ];
157 char *cp;
158 int cc;
160 (void) stat(".", &dir);
161 (void) memcpy(&f, &dir, sizeof(f));
162 (void) snprintf(buf, sizeof(buf), "pcictl pci%d list", bus);
163 f.st_mode = S_IFREG | 0644;
164 if ((pp = popen(buf, "r")) == NULL) {
165 return 0;
167 while (fgets(buf, sizeof(buf), pp) != NULL) {
168 buf[strlen(buf) - 1] = 0x0;
169 if ((cp = strchr(buf, ' ')) == NULL) {
170 continue;
172 cc = snprintf(name, sizeof(name), "/%.*s", (int)(cp - buf), buf);
173 virtdir_add(tp, name, cc, 'l', cp + 1, strlen(cp + 1));
174 if (verbose) {
175 printf("pcifs: adding symbolic link `%s' -> `%s'\n", name, cp + 1);
178 (void) pclose(pp);
179 return 1;
182 int
183 main(int argc, char **argv)
185 int bus;
186 int i;
188 bus = 0;
189 while ((i = getopt(argc, argv, "b:v")) != -1) {
190 switch(i) {
191 case 'b':
192 bus = atoi(optarg);
193 break;
194 case 'v':
195 verbose += 1;
196 break;
199 if (!build_tree(&pci, bus)) {
200 exit(EXIT_FAILURE);
202 return fuse_main(argc, argv, &pcifs_oper, NULL);