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-02-16 15:28:14 +00:00
|
|
|
#include "wsfs_provider.h"
|
|
|
|
|
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_dir
|
|
|
|
{
|
|
|
|
ino_t parent;
|
|
|
|
ino_t inode;
|
|
|
|
char const * name;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fs_file
|
|
|
|
{
|
|
|
|
ino_t parent;
|
|
|
|
ino_t inode;
|
|
|
|
char const * name;
|
|
|
|
char const * content;
|
|
|
|
size_t content_length;
|
|
|
|
bool is_executable;
|
|
|
|
};
|
|
|
|
|
|
|
|
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-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
|
|
|
|
{
|
|
|
|
wsfsp_respond_error(request, -1);
|
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
|
|
|
|
{
|
|
|
|
wsfsp_respond_error(request, -1);
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
wsfsp_respond_error(request, -1);
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
wsfsp_respond_error(request, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wsfsp_respond_error(request, -1);
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
wsfsp_respond_error(request, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wsfsp_respond_error(request, -1);
|
|
|
|
}
|
2019-02-17 13:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct wsfsp_provider fs_provider =
|
|
|
|
{
|
|
|
|
.lookup = &fs_lookup,
|
|
|
|
.getattr = &fs_getattr,
|
|
|
|
.readdir = &fs_readdir,
|
|
|
|
.open = &fs_open,
|
|
|
|
.read = &fs_read
|
|
|
|
};
|
|
|
|
|
|
|
|
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[])
|
|
|
|
{
|
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
|
|
|
|
2019-03-03 14:29:39 +00:00
|
|
|
static struct fs_entry const entries[]=
|
2019-02-17 13:31:04 +00:00
|
|
|
{
|
2019-03-03 14:29:39 +00:00
|
|
|
{.parent = 0, .inode = 1, .name = "<root>", .mode = 0555, .type = FS_DIR},
|
2019-02-17 13:31:04 +00:00
|
|
|
{
|
|
|
|
.parent = 1,
|
|
|
|
.inode = 2,
|
|
|
|
.name = "hello.txt",
|
2019-03-03 14:29:39 +00:00
|
|
|
.mode = 0555,
|
|
|
|
.type = FS_FILE,
|
2019-02-17 13:31:04 +00:00
|
|
|
.content="hello, world!",
|
|
|
|
.content_length = 13,
|
|
|
|
},
|
2019-03-03 14:29:39 +00:00
|
|
|
{.parent = 0, .inode = 0, .name = NULL}
|
2019-02-17 13:31:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct fs fs =
|
|
|
|
{
|
2019-03-03 14:29:39 +00:00
|
|
|
.entries = entries
|
2019-02-17 13:31:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
signal(SIGINT, &on_interrupt);
|
|
|
|
|
|
|
|
client = wsfsp_client_create(&fs_provider, &fs);
|
|
|
|
wsfsp_client_connect(client, "ws://localhost:8080/");
|
|
|
|
|
|
|
|
wsfsp_client_run(client);
|
|
|
|
|
|
|
|
wsfsp_client_dispose(client);
|
|
|
|
return EXIT_SUCCESS;
|
2019-02-16 15:28:14 +00:00
|
|
|
}
|