dll: fuse: help system refactoring

This commit is contained in:
Bill Zissimopoulos 2016-06-01 00:39:27 -07:00
parent 4b9945d9bf
commit 3f3c02f3ce
2 changed files with 126 additions and 55 deletions

View File

@ -18,6 +18,28 @@
#include <dll/library.h> #include <dll/library.h>
#include <fuse/fuse.h> #include <fuse/fuse.h>
#define FSP_FUSE_CORE_OPT(n, f, v) { n, offsetof(struct fsp_fuse_core_opt_data, f), v }
struct fsp_fuse_core_opt_data
{
struct fsp_fuse_env *env;
int debug;
int help;
};
static struct fuse_opt fsp_fuse_core_opts[] =
{
FSP_FUSE_CORE_OPT("-d", debug, 1),
FSP_FUSE_CORE_OPT("debug", debug, 1),
FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("-h", 'h'),
FUSE_OPT_KEY("--help", 'h'),
FUSE_OPT_KEY("-V", 'V'),
FUSE_OPT_KEY("--version", 'V'),
FUSE_OPT_END,
};
struct fuse_chan struct fuse_chan
{ {
PWSTR MountPoint; PWSTR MountPoint;
@ -103,11 +125,14 @@ FSP_FUSE_API struct fuse_chan *fsp_fuse_mount(struct fsp_fuse_env *env,
struct fuse_chan *ch = 0; struct fuse_chan *ch = 0;
int Size; int Size;
if (0 == mountpoint)
mountpoint = "";
Size = MultiByteToWideChar(CP_UTF8, 0, mountpoint, -1, 0, 0); Size = MultiByteToWideChar(CP_UTF8, 0, mountpoint, -1, 0, 0);
if (0 == Size) if (0 == Size)
goto fail; goto fail;
ch = MemAlloc(sizeof *ch + Size); ch = MemAlloc(sizeof *ch + Size * sizeof(WCHAR));
if (0 == ch) if (0 == ch)
goto fail; goto fail;
@ -119,8 +144,6 @@ FSP_FUSE_API struct fuse_chan *fsp_fuse_mount(struct fsp_fuse_env *env,
return ch; return ch;
fail: fail:
FspServiceLog(EVENTLOG_ERROR_TYPE, L"Invalid mount point.");
MemFree(ch); MemFree(ch);
return 0; return 0;
@ -135,8 +158,34 @@ FSP_FUSE_API void fsp_fuse_unmount(struct fsp_fuse_env *env,
FSP_FUSE_API int fsp_fuse_is_lib_option(struct fsp_fuse_env *env, FSP_FUSE_API int fsp_fuse_is_lib_option(struct fsp_fuse_env *env,
const char *opt) const char *opt)
{ {
// !!!: NEEDIMPL return fsp_fuse_opt_match(env, fsp_fuse_core_opts, opt);
return 0; }
static int fsp_fuse_core_opt_proc(void *opt_data0, const char *arg, int key,
struct fuse_args *outargs)
{
struct fsp_fuse_core_opt_data *opt_data = opt_data0;
switch (key)
{
default:
return 1;
case 'h':
#if 0
FspServiceLog(EVENTLOG_ERROR_TYPE, L""
"Core options:\n"
" -d -o debug enable debug output (implies -f)\n"
"\n");
#endif
opt_data->help = 1;
return 1;
case 'V':
FspServiceLog(EVENTLOG_ERROR_TYPE,
L"" LIBRARY_NAME "-FUSE version %d.%d",
FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
opt_data->help = 1;
return 1;
}
} }
static NTSTATUS fsp_fuse_svcstart(FSP_SERVICE *Service, ULONG argc, PWSTR *argv) static NTSTATUS fsp_fuse_svcstart(FSP_SERVICE *Service, ULONG argc, PWSTR *argv)
@ -159,11 +208,21 @@ FSP_FUSE_API struct fuse *fsp_fuse_new(struct fsp_fuse_env *env,
struct fuse_chan *ch, struct fuse_args *args, struct fuse_chan *ch, struct fuse_args *args,
const struct fuse_operations *ops, size_t opsize, void *data) const struct fuse_operations *ops, size_t opsize, void *data)
{ {
struct fsp_fuse_core_opt_data opt_data;
struct fuse *f = 0; struct fuse *f = 0;
PWSTR ServiceName = FspDiagIdent(); PWSTR ServiceName = FspDiagIdent();
FSP_FSCTL_VOLUME_PARAMS VolumeParams; FSP_FSCTL_VOLUME_PARAMS VolumeParams;
PWSTR ErrorMessage = L".";
NTSTATUS Result; NTSTATUS Result;
memset(&opt_data, 0, sizeof opt_data);
opt_data.env = env;
if (-1 == fsp_fuse_opt_parse(env, args, &opt_data, fsp_fuse_core_opts, fsp_fuse_core_opt_proc))
return 0;
if (opt_data.help)
return 0;
memset(&VolumeParams, 0, sizeof VolumeParams); memset(&VolumeParams, 0, sizeof VolumeParams);
#if 0 #if 0
/* initialize VolumeParams from command line args */ /* initialize VolumeParams from command line args */
@ -193,11 +252,21 @@ FSP_FUSE_API struct fuse *fsp_fuse_new(struct fsp_fuse_env *env,
Result = FspFileSystemCreate(L"" FSP_FSCTL_NET_DEVICE_NAME, &VolumeParams, 0, &f->FileSystem); Result = FspFileSystemCreate(L"" FSP_FSCTL_NET_DEVICE_NAME, &VolumeParams, 0, &f->FileSystem);
if (!NT_SUCCESS(Result)) if (!NT_SUCCESS(Result))
{
ErrorMessage = L": cannot create " LIBRARY_NAME " file system object.";
goto fail; goto fail;
}
Result = FspFileSystemSetMountPoint(f->FileSystem, ch->MountPoint); if (L'\0' != ch->MountPoint)
{
Result = FspFileSystemSetMountPoint(f->FileSystem,
L'*' == ch->MountPoint[0] && L'\0' == ch->MountPoint[1] ? 0 : ch->MountPoint);
if (!NT_SUCCESS(Result)) if (!NT_SUCCESS(Result))
{
ErrorMessage = L": cannot set mount point.";
goto fail; goto fail;
}
}
f->Ops = ops; f->Ops = ops;
f->OpSize = opsize; f->OpSize = opsize;
@ -209,7 +278,7 @@ FSP_FUSE_API struct fuse *fsp_fuse_new(struct fsp_fuse_env *env,
return f; return f;
fail: fail:
FspServiceLog(EVENTLOG_ERROR_TYPE, L"Unable to create FUSE file system."); FspServiceLog(EVENTLOG_ERROR_TYPE, L"Unable to create FUSE file system%s");
if (0 != f) if (0 != f)
{ {

View File

@ -18,9 +18,9 @@
#include <dll/library.h> #include <dll/library.h>
#include <fuse/fuse.h> #include <fuse/fuse.h>
#define FSP_FUSE_DEFAULT_OPT(n, f, v) { n, offsetof(struct fsp_fuse_default_opt_data, f), v } #define FSP_FUSE_MAIN_OPT(n, f, v) { n, offsetof(struct fsp_fuse_main_opt_data, f), v }
struct fsp_fuse_default_opt_data struct fsp_fuse_main_opt_data
{ {
struct fsp_fuse_env *env; struct fsp_fuse_env *env;
char *mountpoint; char *mountpoint;
@ -28,10 +28,24 @@ struct fsp_fuse_default_opt_data
int foreground; int foreground;
}; };
static int fsp_fuse_default_opt_proc(void *data0, const char *arg, int key, static struct fuse_opt fsp_fuse_main_opts[] =
{
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_KEY("-d", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("-h", 'h'),
FUSE_OPT_KEY("--help", 'h'),
FUSE_OPT_KEY("-ho", 'H'),
FUSE_OPT_END,
};
static int fsp_fuse_main_opt_proc(void *opt_data0, const char *arg, int key,
struct fuse_args *outargs) struct fuse_args *outargs)
{ {
struct fsp_fuse_default_opt_data *data = data0; struct fsp_fuse_main_opt_data *opt_data = opt_data0;
switch (key) switch (key)
{ {
@ -51,25 +65,28 @@ static int fsp_fuse_default_opt_proc(void *data0, const char *arg, int key,
" -s disable multi-threaded operation\n" " -s disable multi-threaded operation\n"
"\n", "\n",
FspDiagIdent()); FspDiagIdent());
fsp_fuse_opt_add_arg(data->env, outargs, "-h");
return 0;
case 'V':
FspServiceLog(EVENTLOG_ERROR_TYPE,
L"WinFsp-FUSE v%d.%d",
FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
return 1; return 1;
case 'H':
FspServiceLog(EVENTLOG_ERROR_TYPE, L""
"FUSE options:\n"
" -d -o debug enable debug output (implies -f)\n"
" -f foreground operation\n"
" -s disable multi-threaded operation\n"
"\n");
fsp_fuse_opt_add_arg(opt_data->env, outargs, "-h");
return 0;
case FUSE_OPT_KEY_NONOPT: case FUSE_OPT_KEY_NONOPT:
if (0 == data->mountpoint) if (0 == opt_data->mountpoint)
{ {
size_t size = lstrlenA(arg) + 1; size_t size = lstrlenA(arg) + 1;
data->mountpoint = data->env->memalloc(size); opt_data->mountpoint = opt_data->env->memalloc(size);
if (0 == data->mountpoint) if (0 == opt_data->mountpoint)
return -1; return -1;
memcpy(data->mountpoint, arg, size); memcpy(opt_data->mountpoint, arg, size);
} }
else else
FspServiceLog(EVENTLOG_ERROR_TYPE, FspServiceLog(EVENTLOG_ERROR_TYPE,
L"invalid argument \"%S\"", arg); L"Invalid argument \"%S\"", arg);
return 1; return 1;
} }
} }
@ -78,39 +95,24 @@ FSP_FUSE_API int fsp_fuse_parse_cmdline(struct fsp_fuse_env *env,
struct fuse_args *args, struct fuse_args *args,
char **mountpoint, int *multithreaded, int *foreground) char **mountpoint, int *multithreaded, int *foreground)
{ {
static struct fuse_opt opts[] = struct fsp_fuse_main_opt_data opt_data;
{
FSP_FUSE_DEFAULT_OPT("-d", foreground, 1),
FSP_FUSE_DEFAULT_OPT("debug", foreground, 1),
FSP_FUSE_DEFAULT_OPT("-f", foreground, 1),
FSP_FUSE_DEFAULT_OPT("-s", singlethread, 1),
FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("-h", 'h'),
FUSE_OPT_KEY("--help", 'h'),
FUSE_OPT_KEY("--ho", 'h'),
FUSE_OPT_KEY("-V", 'V'),
FUSE_OPT_KEY("--version", 'V'),
FUSE_OPT_END,
};
struct fsp_fuse_default_opt_data data;
memset(&data, 0, sizeof data); memset(&opt_data, 0, sizeof opt_data);
data.env = env; opt_data.env = env;
if (-1 == fsp_fuse_opt_parse(env, args, &data, opts, fsp_fuse_default_opt_proc)) if (-1 == fsp_fuse_opt_parse(env, args, &opt_data, fsp_fuse_main_opts, fsp_fuse_main_opt_proc))
return -1; return -1;
if (0 != mountpoint) if (0 != mountpoint)
*mountpoint = data.mountpoint; *mountpoint = opt_data.mountpoint;
else else
env->memfree(mountpoint); env->memfree(mountpoint);
if (0 != multithreaded) if (0 != multithreaded)
*multithreaded = !data.singlethread; *multithreaded = !opt_data.singlethread;
if (0 != foreground) if (0 != foreground)
*foreground = data.foreground; *foreground = opt_data.foreground;
return 0; return 0;
} }
@ -161,12 +163,12 @@ exit:
if (signal_handlers) if (signal_handlers)
env->remove_signal_handlers(f/* !!!: REVISIT */); env->remove_signal_handlers(f/* !!!: REVISIT */);
if (0 != ch)
fsp_fuse_unmount(env, mountpoint, ch);
if (0 != f) if (0 != f)
fsp_fuse_destroy(env, f); fsp_fuse_destroy(env, f);
if (0 != ch)
fsp_fuse_unmount(env, mountpoint, ch);
env->memfree(mountpoint); env->memfree(mountpoint);
fsp_fuse_opt_free_args(env, &args); fsp_fuse_opt_free_args(env, &args);