xref: /openbsd-src/lib/libfuse/fuse_main.3 (revision d9d32dcbf1ebe1cd03f0b04d14901fd8bec57b05)
1.\" $OpenBSD: fuse_main.3,v 1.7 2020/05/25 16:33:03 jmc Exp $
2.\"
3.\" Copyright (c) 2013 Sylvestre Gallon <ccna.syl@gmail.com>
4.\" Copyright (c) 2018 Helg Bredow <helg@openbsd.org>
5.\"
6.\" Permission to use, copy, modify, and distribute this software for any
7.\" purpose with or without fee is hereby granted, provided that the above
8.\" copyright notice and this permission notice appear in all copies.
9.\"
10.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17.\"
18.Dd $Mdocdate: May 25 2020 $
19.Dt FUSE_MAIN 3
20.Os
21.Sh NAME
22.Nm fuse_main
23.Nd FUSE helper function
24.Sh SYNOPSIS
25.In fuse.h
26.Ft int
27.Fn fuse_main "int argc" "char **argv" "const struct fuse_operations *ops" \
28    "void *data"
29.Sh DESCRIPTION
30There are two ways of implementing a FUSE filesystem:
31by calling only
32.Fn fuse_main
33and passing this function the
34.Em ops
35argument containing all the callbacks of the filesystem,
36or by using the other functions,
37as detailed in
38.Xr fuse_loop 3
39.Pp
40.Fa argv
41is the list of arguments supplied to the program's main method and
42must at a minimum specify the directory on which the file system is to
43be mounted.
44The other arguments can be custom arguments specific to the
45file system or those supported by
46.Xr fuse_parse_cmdline 3 ,
47.Xr fuse_new 3
48and
49.Xr fuse_mount 3 .
50.Sh EXAMPLES
51Here is a simple example of a FUSE implementation:
52.Bd -literal
53#include <errno.h>
54#include <fuse.h>
55#include <string.h>
56
57static int
58fs_readdir(const char *path, void *data, fuse_fill_dir_t filler,
59           off_t off, struct fuse_file_info *ffi)
60{
61	if (strcmp(path, "/") != 0)
62		return -ENOENT;
63
64	filler(data, ".", NULL, 0);
65	filler(data, "..", NULL, 0);
66	filler(data, "file", NULL, 0);
67	return 0;
68}
69
70static int
71fs_read(const char *path, char *buf, size_t size, off_t off,
72        struct fuse_file_info *ffi)
73{
74	size_t len;
75	const char *file_contents = "fuse filesystem example\\n";
76
77	len = strlen(file_contents);
78
79	if (off < len) {
80		if (off + size > len)
81			size = len - off;
82		memcpy(buf, file_contents + off, size);
83	} else
84		size = 0;
85
86	return size;
87}
88
89static int
90fs_open(const char *path, struct fuse_file_info *ffi)
91{
92	if (strncmp(path, "/file", 10) != 0)
93		return -ENOENT;
94
95	if ((ffi->flags & 3) != O_RDONLY)
96		return -EACCES;
97
98	return 0;
99}
100
101static int
102fs_getattr(const char *path, struct stat *st)
103{
104	if (strcmp(path, "/") == 0) {
105		st->st_blksize = 512;
106		st->st_mode = 0755;
107		st->st_nlink = 2;
108	} else if (strcmp(path, "/file") == 0) {
109		st->st_mode = 0644;
110		st->st_blksize = 512;
111		st->st_nlink = 1;
112		st->st_size = 5;
113	} else {
114		return -ENOENT;
115	}
116
117	return 0;
118}
119
120struct fuse_operations fsops = {
121	.readdir = fs_readdir,
122	.read = fs_read,
123	.open = fs_open,
124	.getattr = fs_getattr,
125};
126
127int
128main(int argc, char **argv)
129{
130	return (fuse_main(argc, argv, &fsops, NULL));
131}
132.Ed
133.Sh SEE ALSO
134.Xr fuse_loop 3 ,
135.Xr fuse_mount 3 ,
136.Xr fuse_new 3 ,
137.Xr fuse_parse_cmdline 3 ,
138.Xr fuse_setup 3 ,
139.Xr fuse 4
140.Sh STANDARDS
141The
142.Fn fuse_main
143function conforms to FUSE 2.6.
144.Sh HISTORY
145The
146.Fn fuse_main
147function first appeared in
148.Ox 5.4 .
149.Sh AUTHORS
150.An Sylvestre Gallon Aq Mt ccna.syl@gmail.com
151.An Helg Bredow Aq Mt helg@openbsd.org
152