forked from winfsp/winfsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuse_main.c
183 lines (155 loc) · 4.89 KB
/
fuse_main.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @file dll/fuse/fuse_main.c
*
* @copyright 2015-2019 Bill Zissimopoulos
*/
/*
* This file is part of WinFsp.
*
* You can redistribute it and/or modify it under the terms of the GNU
* General Public License version 3 as published by the Free Software
* Foundation.
*
* Licensees holding a valid commercial license may use this software
* in accordance with the commercial license agreement provided in
* conjunction with the software. The terms and conditions of any such
* commercial license agreement shall govern, supersede, and render
* ineffective any application of the GPLv3 license to this software,
* notwithstanding of any reference thereto in the software or
* associated repository.
*/
#include <dll/fuse/library.h>
#define FSP_FUSE_MAIN_OPT(n, f, v) { n, offsetof(struct fsp_fuse_main_opt_data, f), v }
struct fsp_fuse_main_opt_data
{
struct fsp_fuse_env *env;
char *mountpoint;
int singlethread;
int foreground;
};
static struct fuse_opt fsp_fuse_main_opts[] =
{
FUSE_OPT_KEY("-h", 'h'),
FUSE_OPT_KEY("--help", 'h'),
FUSE_OPT_KEY("-ho", 'H'),
FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
FSP_FUSE_MAIN_OPT("-d", foreground, 1),
FSP_FUSE_MAIN_OPT("debug", foreground, 1),
FSP_FUSE_MAIN_OPT("-f", foreground, 1),
FSP_FUSE_MAIN_OPT("-s", singlethread, 1),
FUSE_OPT_END,
};
static int fsp_fuse_main_opt_proc(void *opt_data0, const char *arg, int key,
struct fuse_args *outargs)
{
static PWSTR HeaderHelp = L""
"\n"
" -o opt,[opt...] mount options\n"
" -h --help print help\n"
" -V --version print version\n";
static PWSTR MainHelp = L""
"FUSE options:\n"
" -d -o debug enable debug output (implies -f)\n"
" -f foreground operation\n"
" -s disable multi-threaded operation\n";
struct fsp_fuse_main_opt_data *opt_data = opt_data0;
switch (key)
{
default:
return 1;
case 'h':
FspServiceLog(EVENTLOG_ERROR_TYPE, L""
"usage: %s mountpoint [options]\n"
"%s"
"\n"
"%s",
FspDiagIdent(), HeaderHelp, MainHelp);
return 1;
case 'H':
FspServiceLog(EVENTLOG_ERROR_TYPE, L""
"%s",
MainHelp);
fsp_fuse_opt_add_arg(opt_data->env, outargs, "-h");
return 0;
case FUSE_OPT_KEY_NONOPT:
if (0 == opt_data->mountpoint)
{
size_t size = lstrlenA(arg) + 1;
opt_data->mountpoint = opt_data->env->memalloc(size);
if (0 == opt_data->mountpoint)
return -1;
memcpy(opt_data->mountpoint, arg, size);
}
else
FspServiceLog(EVENTLOG_ERROR_TYPE,
L"Invalid argument \"%S\"", arg);
return 1;
}
}
FSP_FUSE_API int fsp_fuse_parse_cmdline(struct fsp_fuse_env *env,
struct fuse_args *args,
char **mountpoint, int *multithreaded, int *foreground)
{
struct fsp_fuse_main_opt_data opt_data;
memset(&opt_data, 0, sizeof opt_data);
opt_data.env = env;
if (-1 == fsp_fuse_opt_parse(env, args, &opt_data, fsp_fuse_main_opts, fsp_fuse_main_opt_proc))
return -1;
if (0 != mountpoint)
*mountpoint = opt_data.mountpoint;
else
env->memfree(mountpoint);
if (0 != multithreaded)
*multithreaded = !opt_data.singlethread;
if (0 != foreground)
*foreground = opt_data.foreground;
return 0;
}
FSP_FUSE_API int fsp_fuse_main_real(struct fsp_fuse_env *env,
int argc, char *argv[],
const struct fuse_operations *ops, size_t opsize, void *data)
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
char *mountpoint = 0;
int multithreaded = 0;
int foreground = 0;
struct fuse_chan *ch = 0;
struct fuse *f = 0;
int signal_handlers = 0;
int result;
result = fsp_fuse_parse_cmdline(env, &args, &mountpoint, &multithreaded, &foreground);
if (-1 == result)
goto exit;
ch = fsp_fuse_mount(env, mountpoint, &args);
if (0 == ch)
{
result = -1;
goto exit;
}
f = fsp_fuse_new(env, ch, &args, ops, opsize, data);
if (0 == f)
{
result = -1;
goto exit;
}
result = env->daemonize(foreground);
if (-1 == result)
goto exit;
result = env->set_signal_handlers(f);
if (-1 == result)
goto exit;
signal_handlers = 1;
result = multithreaded ? fsp_fuse_loop_mt(env, f) : fsp_fuse_loop(env, f);
exit:
if (signal_handlers)
env->set_signal_handlers(0);
if (0 != f)
fsp_fuse_destroy(env, f);
if (0 != ch)
fsp_fuse_unmount(env, mountpoint, ch);
env->memfree(mountpoint);
fsp_fuse_opt_free_args(env, &args);
/* main() style return: 0 success, 1 error */
return !!result;
}