2019-02-16 15:28:14 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-02-17 13:31:04 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
2019-02-16 15:28:14 +00:00
|
|
|
|
2019-03-03 15:29:02 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2019-03-04 20:17:37 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
2019-03-03 15:29:02 +00:00
|
|
|
|
2019-02-16 15:28:14 +00:00
|
|
|
#include "wsfs_provider.h"
|
|
|
|
|
2019-03-04 20:17:37 +00:00
|
|
|
struct config
|
|
|
|
{
|
|
|
|
char * url;
|
|
|
|
struct wsfsp_client_config * client_config;
|
|
|
|
bool show_help;
|
|
|
|
};
|
|
|
|
|
2019-03-03 14:29:39 +00:00
|
|
|
enum fs_entry_type
|
|
|
|
{
|
|
|
|
FS_FILE,
|
|
|
|
FS_DIR
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fs_entry
|
|
|
|
{
|
|
|
|
ino_t parent;
|
|
|
|
ino_t inode;
|
|
|
|
char const * name;
|
|
|
|
int mode;
|
|
|
|
enum fs_entry_type type;
|
|
|
|
size_t content_length;
|
|
|
|
char const * content;
|
|
|
|
};
|
2019-02-17 13:31:04 +00:00
|
|
|
|
|
|
|
struct fs
|
|
|
|
{
|
2019-03-03 14:29:39 +00:00
|
|
|
struct fs_entry const * entries;
|
2019-02-17 13:31:04 +00:00
|
|
|
};
|
|
|
|
|
2019-03-04 20:17:37 +00:00
|
|
|
static void show_help()
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"wsfs-provider, Copyright (c) 2019 fuse-wsfs authors <https://github.com/falk-werner/fuse-wsfs>\n"
|
|
|
|
"Example for websocket file system provider\n"
|
|
|
|
"\n"
|
|
|
|
"Usage: wsfs-provider -u <url> [-k <key_path>] [-c <cert_path>]\n"
|
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
"\t-u, --url URL of WSFS server (required)\n"
|
|
|
|
"\t-k, --key_path Path to private key of provider (default: not set, TLS disabled)\n"
|
|
|
|
"\t-c, --cert_path Path to certificate of provider (defautl: not set, TLS disabled)\n"
|
|
|
|
"\t-h, --help prints this message\n"
|
|
|
|
"\n"
|
|
|
|
"Example:\n"
|
|
|
|
"\twsfs-provider -u ws://localhost:8080/\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_arguments(
|
|
|
|
int argc,
|
|
|
|
char* argv[],
|
|
|
|
struct config * config)
|
|
|
|
{
|
|
|
|
static struct option const options[] =
|
|
|
|
{
|
|
|
|
{"url", required_argument, NULL, 'u'},
|
|
|
|
{"key_path", required_argument, NULL, 'k'},
|
|
|
|
{"cert_path", required_argument, NULL, 'c'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
int result = EXIT_SUCCESS;
|
|
|
|
bool finished = false;
|
|
|
|
while (!finished)
|
|
|
|
{
|
|
|
|
int option_index = 0;
|
|
|
|
int const c = getopt_long(argc, argv, "u:k:c:h", options, &option_index);
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
finished = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
config->show_help = true;
|
|
|
|
finished = true;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
free(config->url);
|
|
|
|
config->url = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
wsfsp_client_config_set_keypath(config->client_config, optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
wsfsp_client_config_set_certpath(config->client_config, optarg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "error: unknown argument\n");
|
|
|
|
finished = true;
|
|
|
|
result = EXIT_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == config->url)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "error: missing required argument \"-u\"\n");
|
|
|
|
result = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != EXIT_SUCCESS)
|
|
|
|
{
|
|
|
|
config->show_help = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-03 14:29:39 +00:00
|
|
|
static struct fs_entry const * fs_getentry(
|
2019-03-03 11:48:58 +00:00
|
|
|
struct fs * fs,
|
|
|
|
ino_t inode)
|
|
|
|
{
|
2019-03-03 14:29:39 +00:00
|
|
|
for (size_t i = 0; 0 != fs->entries[i].inode; i++)
|
2019-03-03 11:48:58 +00:00
|
|
|
{
|
2019-03-03 14:29:39 +00:00
|
|
|
struct fs_entry const * entry = &fs->entries[i];
|
|
|
|
if (inode == entry->inode)
|
2019-03-03 11:48:58 +00:00
|
|
|
{
|
2019-03-03 14:29:39 +00:00
|
|
|
return entry;
|
2019-03-03 11:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-03 14:44:34 +00:00
|
|
|
static struct fs_entry const * fs_getentry_byname(
|
|
|
|
struct fs * fs,
|
|
|
|
ino_t parent,
|
|
|
|
char const * name)
|
|
|
|
{
|
|
|
|
for( size_t i = 0; 0 != fs->entries[i].inode; i++)
|
|
|
|
{
|
|
|
|
struct fs_entry const * entry = &fs->entries[i];
|
|
|
|
if ((parent == entry->parent) && (0 == strcmp(name, entry->name)))
|
|
|
|
{
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fs_stat(
|
|
|
|
struct fs_entry const * entry,
|
|
|
|
struct stat * stat)
|
|
|
|
{
|
|
|
|
memset(stat, 0, sizeof(struct stat));
|
|
|
|
|
|
|
|
stat->st_ino = entry->inode;
|
|
|
|
stat->st_mode = entry->mode;
|
|
|
|
|
|
|
|
if (FS_DIR == entry->type)
|
|
|
|
{
|
|
|
|
stat->st_mode |= S_IFDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FS_FILE == entry->type)
|
|
|
|
{
|
|
|
|
stat->st_mode |= S_IFREG;
|
|
|
|
stat->st_size = entry->content_length;
|
|
|
|
}
|
|
|
|
}
|
2019-03-03 11:48:58 +00:00
|
|
|
|
2019-02-17 13:31:04 +00:00
|
|
|
static void fs_lookup(
|
|
|
|
struct wsfsp_request * request,
|
|
|
|
ino_t parent,
|
|
|
|
char const * name,
|
|
|
|
void * user_data)
|
|
|
|
{
|
2019-03-03 14:29:39 +00:00
|
|
|
struct fs * fs = (struct fs*) user_data;
|
2019-03-03 14:44:34 +00:00
|
|
|
struct fs_entry const * entry = fs_getentry_byname(fs, parent, name);
|
|
|
|
if (NULL != entry)
|
2019-03-03 14:29:39 +00:00
|
|
|
{
|
2019-03-03 14:44:34 +00:00
|
|
|
struct stat stat;
|
|
|
|
fs_stat(entry, &stat);
|
2019-03-03 14:29:39 +00:00
|
|
|
|
2019-03-03 14:44:34 +00:00
|
|
|
wsfsp_respond_lookup(request, &stat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 18:19:04 +00:00
|
|
|
wsfsp_respond_error(request, WSFS_BAD_NOENTRY);
|
2019-03-03 14:29:39 +00:00
|
|
|
}
|
2019-02-17 13:31:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 11:48:58 +00:00
|
|
|
|
2019-02-17 13:31:04 +00:00
|
|
|
static void fs_getattr(
|
|
|
|
struct wsfsp_request * request,
|
|
|
|
ino_t inode,
|
|
|
|
void * user_data)
|
|
|
|
{
|
2019-03-03 11:48:58 +00:00
|
|
|
struct fs * fs = (struct fs*) user_data;
|
2019-03-03 14:29:39 +00:00
|
|
|
struct fs_entry const * entry = fs_getentry(fs, inode);
|
2019-03-03 12:34:43 +00:00
|
|
|
|
2019-03-03 14:29:39 +00:00
|
|
|
if (NULL != entry)
|
2019-03-03 11:48:58 +00:00
|
|
|
{
|
|
|
|
struct stat stat;
|
2019-03-03 14:44:34 +00:00
|
|
|
fs_stat(entry, &stat);
|
2019-03-03 11:48:58 +00:00
|
|
|
|
|
|
|
wsfsp_respond_getattr(request, &stat);
|
|
|
|
}
|
2019-03-03 12:34:43 +00:00
|
|
|
else
|
|
|
|
{
|
2019-03-04 18:19:04 +00:00
|
|
|
wsfsp_respond_error(request, WSFS_BAD_NOENTRY);
|
2019-03-03 12:34:43 +00:00
|
|
|
}
|
2019-02-17 13:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fs_readdir(
|
|
|
|
struct wsfsp_request * request,
|
|
|
|
ino_t directory,
|
|
|
|
void * user_data)
|
|
|
|
{
|
2019-03-03 12:34:43 +00:00
|
|
|
struct fs * fs = (struct fs*) user_data;
|
2019-02-17 13:31:04 +00:00
|
|
|
|
2019-03-03 14:29:39 +00:00
|
|
|
struct fs_entry const * dir = fs_getentry(fs, directory);
|
|
|
|
if ((NULL != dir) && (FS_DIR == dir->type))
|
2019-03-03 12:34:43 +00:00
|
|
|
{
|
|
|
|
struct wsfsp_dirbuffer * buffer = wsfsp_dirbuffer_create();
|
|
|
|
wsfsp_dirbuffer_add(buffer, ".", dir->inode);
|
2019-03-03 14:44:34 +00:00
|
|
|
wsfsp_dirbuffer_add(buffer, "..", dir->inode);
|
2019-03-03 12:34:43 +00:00
|
|
|
|
2019-03-03 14:29:39 +00:00
|
|
|
for(size_t i = 0; 0 != fs->entries[i].inode; i++)
|
2019-03-03 12:34:43 +00:00
|
|
|
{
|
2019-03-03 14:29:39 +00:00
|
|
|
struct fs_entry const * entry = &fs->entries[i];
|
|
|
|
if (directory == entry->parent)
|
2019-03-03 12:34:43 +00:00
|
|
|
{
|
2019-03-03 14:29:39 +00:00
|
|
|
wsfsp_dirbuffer_add(buffer, entry->name, entry->inode);
|
2019-03-03 12:34:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wsfsp_respond_readdir(request, buffer);
|
|
|
|
wsfsp_dirbuffer_dispose(buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 18:19:04 +00:00
|
|
|
wsfsp_respond_error(request, WSFS_BAD_NOENTRY);
|
2019-03-03 12:34:43 +00:00
|
|
|
}
|
2019-02-17 13:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fs_open(
|
|
|
|
struct wsfsp_request * request,
|
|
|
|
ino_t inode,
|
|
|
|
int flags,
|
|
|
|
void * user_data)
|
|
|
|
{
|
2019-03-03 15:29:02 +00:00
|
|
|
struct fs * fs = (struct fs*) user_data;
|
2019-02-17 13:31:04 +00:00
|
|
|
|
2019-03-03 15:29:02 +00:00
|
|
|
struct fs_entry const * entry = fs_getentry(fs, inode);
|
|
|
|
if ((NULL != entry) && (FS_FILE == entry->type))
|
|
|
|
{
|
|
|
|
if (O_RDONLY == (flags & O_ACCMODE))
|
|
|
|
{
|
|
|
|
wsfsp_respond_open(request, 0U);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 18:19:04 +00:00
|
|
|
wsfsp_respond_error(request, WSFS_BAD_NOACCESS);
|
2019-03-03 15:29:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 18:19:04 +00:00
|
|
|
wsfsp_respond_error(request, WSFS_BAD_NOENTRY);
|
2019-03-03 15:29:02 +00:00
|
|
|
}
|
2019-02-17 13:31:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 17:02:30 +00:00
|
|
|
static size_t min(size_t const a, size_t const b)
|
|
|
|
{
|
|
|
|
return (a < b) ? a : b;
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:31:04 +00:00
|
|
|
static void fs_read(
|
|
|
|
struct wsfsp_request * request,
|
|
|
|
ino_t inode,
|
|
|
|
uint32_t handle,
|
|
|
|
size_t offset,
|
|
|
|
size_t length,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
(void) handle;
|
|
|
|
|
2019-03-03 17:02:30 +00:00
|
|
|
struct fs * fs = (struct fs*) user_data;
|
|
|
|
struct fs_entry const * entry = fs_getentry(fs, inode);
|
|
|
|
if ((NULL != entry) && (FS_FILE == entry->type))
|
|
|
|
{
|
|
|
|
if (entry->content_length > offset)
|
|
|
|
{
|
|
|
|
size_t const remaining = entry->content_length - offset;
|
|
|
|
size_t const count = min(remaining, length);
|
|
|
|
|
|
|
|
wsfsp_respond_read(request, &entry->content[offset], count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 18:19:04 +00:00
|
|
|
wsfsp_respond_error(request, WSFS_BAD);
|
2019-03-03 17:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 18:19:04 +00:00
|
|
|
wsfsp_respond_error(request, WSFS_BAD_NOENTRY);
|
2019-03-03 17:02:30 +00:00
|
|
|
}
|
2019-02-17 13:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct wsfsp_client * client;
|
|
|
|
|
|
|
|
static void on_interrupt(int signal_id)
|
|
|
|
{
|
|
|
|
(void) signal_id;
|
|
|
|
|
|
|
|
wsfsp_client_shutdown(client);
|
|
|
|
}
|
|
|
|
|
2019-02-16 15:28:14 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2019-03-04 20:17:37 +00:00
|
|
|
struct config config;
|
|
|
|
config.url = NULL;
|
|
|
|
config.show_help = false;
|
|
|
|
config.client_config = wsfsp_client_config_create();
|
|
|
|
int result = parse_arguments(argc, argv, &config);
|
2019-02-16 15:28:14 +00:00
|
|
|
|
2019-03-04 20:17:37 +00:00
|
|
|
if (EXIT_SUCCESS == result)
|
2019-02-17 13:31:04 +00:00
|
|
|
{
|
2019-03-04 20:17:37 +00:00
|
|
|
static struct fs_entry const entries[]=
|
2019-02-17 13:31:04 +00:00
|
|
|
{
|
2019-03-04 20:17:37 +00:00
|
|
|
{.parent = 0, .inode = 1, .name = "<root>", .mode = 0555, .type = FS_DIR},
|
|
|
|
{
|
|
|
|
.parent = 1,
|
|
|
|
.inode = 2,
|
|
|
|
.name = "hello.txt",
|
|
|
|
.mode = 0555,
|
|
|
|
.type = FS_FILE,
|
|
|
|
.content="hello, world!",
|
|
|
|
.content_length = 13,
|
|
|
|
},
|
|
|
|
{.parent = 0, .inode = 0, .name = NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fs fs =
|
|
|
|
{
|
|
|
|
.entries = entries
|
|
|
|
};
|
2019-02-17 13:31:04 +00:00
|
|
|
|
2019-03-04 20:17:37 +00:00
|
|
|
signal(SIGINT, &on_interrupt);
|
2019-02-17 13:31:04 +00:00
|
|
|
|
2019-03-04 20:17:37 +00:00
|
|
|
wsfsp_client_config_set_userdata(config.client_config, &fs);
|
|
|
|
wsfsp_client_config_set_onlookup(config.client_config, &fs_lookup);
|
|
|
|
wsfsp_client_config_set_ongetattr(config.client_config, &fs_getattr);
|
|
|
|
wsfsp_client_config_set_onreaddir(config.client_config, &fs_readdir);
|
|
|
|
wsfsp_client_config_set_onopen(config.client_config, &fs_open);
|
|
|
|
wsfsp_client_config_set_onread(config.client_config, &fs_read);
|
2019-02-17 13:31:04 +00:00
|
|
|
|
2019-03-04 20:17:37 +00:00
|
|
|
client = wsfsp_client_create(config.client_config);
|
|
|
|
wsfsp_client_connect(client, config.url);
|
2019-02-17 13:31:04 +00:00
|
|
|
|
2019-03-04 20:17:37 +00:00
|
|
|
wsfsp_client_run(client);
|
|
|
|
|
|
|
|
wsfsp_client_dispose(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.show_help)
|
|
|
|
{
|
|
|
|
show_help();
|
|
|
|
}
|
2019-02-17 13:31:04 +00:00
|
|
|
|
2019-03-04 20:17:37 +00:00
|
|
|
free(config.url);
|
|
|
|
wsfsp_client_config_dispose(config.client_config);
|
|
|
|
return result;
|
2019-02-16 15:28:14 +00:00
|
|
|
}
|