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
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
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>
42 static int verbose
; /* how chatty are we? */
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 */
51 pcifs_getattr(const char *path
, struct stat
*st
)
55 if (strcmp(path
, "/") == 0) {
56 (void) memset(st
, 0x0, sizeof(*st
));
57 st
->st_mode
= S_IFDIR
| 0755;
61 if ((ep
= virtdir_find(&pci
, path
, strlen(path
))) == NULL
) {
68 (void) memcpy(st
, &pci
.dir
, sizeof(*st
));
71 (void) memcpy(st
, &pci
.lnk
, sizeof(*st
));
72 st
->st_size
= ep
->tgtlen
;
73 st
->st_mode
= S_IFLNK
| 0644;
79 /* readdir operation */
81 pcifs_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
82 off_t offset
, struct fuse_file_info
* fi
)
87 if ((dirp
= openvirtdir(&pci
, path
)) == NULL
) {
90 filler(buf
, ".", NULL
, 0);
91 filler(buf
, "..", NULL
, 0);
92 while ((dp
= readvirtdir(dirp
)) != NULL
) {
93 filler(buf
, dp
->d_name
, NULL
, 0);
99 /* open the file in the file system */
101 pcifs_open(const char *path
, struct fuse_file_info
* fi
)
106 /* read the file's contents in the file system */
108 pcifs_read(const char *path
, char *buf
, size_t size
, off_t offset
,
109 struct fuse_file_info
* fi
)
114 /* fill in the statvfs struct */
116 pcifs_statfs(const char *path
, struct statvfs
*st
)
118 (void) memset(st
, 0x0, sizeof(*st
));
122 /* read the symbolic link */
124 pcifs_readlink(const char *path
, char *buf
, size_t size
)
128 if ((ep
= virtdir_find(&pci
, path
, strlen(path
))) == NULL
) {
131 if (ep
->tgt
== NULL
) {
134 (void) strlcpy(buf
, ep
->tgt
, size
);
138 /* operations struct */
139 static struct fuse_operations pcifs_oper
= {
140 .getattr
= pcifs_getattr
,
141 .readlink
= pcifs_readlink
,
142 .readdir
= pcifs_readdir
,
145 .statfs
= pcifs_statfs
148 /* build up a fuse_tree from the information in the database */
150 build_tree(virtdir_t
*tp
, int bus
)
155 char name
[MAXPATHLEN
];
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
) {
167 while (fgets(buf
, sizeof(buf
), pp
) != NULL
) {
168 buf
[strlen(buf
) - 1] = 0x0;
169 if ((cp
= strchr(buf
, ' ')) == NULL
) {
172 cc
= snprintf(name
, sizeof(name
), "/%.*s", (int)(cp
- buf
), buf
);
173 virtdir_add(tp
, name
, cc
, 'l', cp
+ 1, strlen(cp
+ 1));
175 printf("pcifs: adding symbolic link `%s' -> `%s'\n", name
, cp
+ 1);
183 main(int argc
, char **argv
)
189 while ((i
= getopt(argc
, argv
, "b:v")) != -1) {
199 if (!build_tree(&pci
, bus
)) {
202 return fuse_main(argc
, argv
, &pcifs_oper
, NULL
);