#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <new>
#include "Debug.h"
#include "ServerDefs.h"
#include "UserlandFSServer.h"
static const char* kServerSignature
= "application/x-vnd.haiku.userlandfs-server";
static const char* kUsage =
"Usage: %s <options> <file system> [ <port> ]\n"
"\n"
"Runs the userlandfs server for a given file system. Typically this is done\n"
"automatically by the kernel module when a volume is requested to be mounted,\n"
"but running the server manually can be useful for debugging purposes. The\n"
"<file system> argument specifies the name of the file system to be loaded.\n"
"<port> should not be given when starting the server manually; it is used by\n"
"the kernel module only.\n"
"\n"
"Options:\n"
" --debug - the file system server enters the debugger after the\n"
" userland file system add-on has been loaded and is\n"
" ready to be used.\n"
" -h, --help - print this text\n"
;
static int kArgC;
static char** kArgV;
void
print_usage(bool toStdErr = true)
{
fprintf((toStdErr ? stderr : stdout), kUsage, kArgV[0], kArgV[0]);
}
int
main(int argc, char** argv)
{
kArgC = argc;
kArgV = argv;
init_debugging();
struct DebuggingExiter {
DebuggingExiter() {}
~DebuggingExiter() { exit_debugging(); }
} _;
int argi = 1;
for (; argi < argc; argi++) {
const char* arg = argv[argi];
int32 argLen = strlen(arg);
if (argLen == 0) {
print_usage();
return 1;
}
if (arg[0] != '-')
break;
if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
print_usage(false);
return 0;
} else if (strcmp(arg, "--debug") == 0) {
gServerSettings.SetEnterDebugger(true);
}
}
bool dispatcher = true;
const char* fileSystem = NULL;
if (argi < argc) {
fileSystem = argv[argi++];
dispatcher = false;
}
int32 port = -1;
if (argi < argc) {
port = atol(argv[argi++]);
if (port <= 0) {
print_usage();
return 1;
}
}
if (argi < argc) {
print_usage();
return 1;
}
status_t error = B_OK;
UserlandFSServer* server
= new(std::nothrow) UserlandFSServer(kServerSignature);
if (!server) {
fprintf(stderr, "Failed to create server.\n");
return 1;
}
error = server->Init(fileSystem, port);
if (error == B_OK)
server->Run();
delete server;
return 0;
}