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

static int
fs_readdir(const char *path, void *data, fuse_fill_dir_t filler,
           off_t off, struct fuse_file_info *ffi)
{
	if (strcmp(path, "/") != 0)
		return -ENOENT;

	filler(data, ".", NULL, 0);
	filler(data, "..", NULL, 0);
	filler(data, "file", NULL, 0);
	return 0;
}

static int
fs_read(const char *path, char *buf, size_t size, off_t off,
        struct fuse_file_info *ffi)
{
	size_t len;
	const char *file_contents = "fuse filesystem example\\n";

	len = strlen(file_contents);

	if (off < len) {
		if (off + size > len)
			size = len - off;
		memcpy(buf, file_contents + off, size);
	} else
		size = 0;

	return size;
}

static int
fs_open(const char *path, struct fuse_file_info *ffi)
{
	if (strncmp(path, "/file", 10) != 0)
		return -ENOENT;

	if ((ffi->flags & 3) != O_RDONLY)
		return -EACCES;

	return 0;
}

static int
fs_getattr(const char *path, struct stat *st)
{
	if (strcmp(path, "/") == 0) {
		st->st_blksize = 512;
		st->st_mode = 0755;
		st->st_nlink = 2;
	} else if (strcmp(path, "/file") == 0) {
		st->st_mode = 0644;
		st->st_blksize = 512;
		st->st_nlink = 1;
		st->st_size = 5;
	} else {
		return -ENOENT;
	}

	return 0;
}

struct fuse_operations fsops = {
	.readdir = fs_readdir,
	.read = fs_read,
	.open = fs_open,
	.getattr = fs_getattr,
};

int
main(int argc, char **argv)
{
	return (fuse_main(argc, argv, &fsops, NULL));
}
.Ed
.Sh SEE ALSO
.Xr fuse_loop 3 ,
.Xr fuse_mount 3 ,
.Xr fuse_new 3 ,
.Xr fuse_parse_cmdline 3 ,
.Xr fuse_setup 3 ,
.Xr fuse 4
.Sh STANDARDS
The
.Fn fuse_main
function conforms to FUSE 2.6.
.Sh HISTORY
The
.Fn fuse_main
function first appeared in
.Ox 5.4 .
.Sh AUTHORS
.An Sylvestre Gallon Aq Mt ccna.syl@gmail.com
.An Helg Bredow Aq Mt helg@openbsd.org