1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

implements operation read

This commit is contained in:
Falk Werner 2019-03-03 18:02:30 +01:00
parent de454516cb
commit e4d95f5dc9
2 changed files with 80 additions and 13 deletions

View File

@ -203,6 +203,11 @@ static void fs_open(
} }
} }
static size_t min(size_t const a, size_t const b)
{
return (a < b) ? a : b;
}
static void fs_read( static void fs_read(
struct wsfsp_request * request, struct wsfsp_request * request,
ino_t inode, ino_t inode,
@ -211,13 +216,28 @@ static void fs_read(
size_t length, size_t length,
void * user_data) void * user_data)
{ {
(void) inode;
(void) handle; (void) handle;
(void) offset;
(void) length;
(void) user_data;
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); wsfsp_respond_error(request, -1);
}
}
else
{
wsfsp_respond_error(request, -1);
}
} }
static struct wsfsp_provider fs_provider = static struct wsfsp_provider fs_provider =

View File

@ -1,5 +1,8 @@
#include "wsfs/provider/operation/read_intern.h" #include "wsfs/provider/operation/read_intern.h"
#include <stdio.h>
#include <stdlib.h>
#include <libwebsockets.h>
#include "wsfs/provider/operation/error.h" #include "wsfs/provider/operation/error.h"
#include "wsfs/util.h" #include "wsfs/util.h"
@ -8,11 +11,28 @@ void wsfsp_read(
json_t * params, json_t * params,
int id) int id)
{ {
(void) context; size_t const count = json_array_size(params);
(void) params; if (4 == count)
(void) id; {
json_t * inode_holder = json_array_get(params, 0);
json_t * handle_holder = json_array_get(params, 1);
json_t * offset_holder = json_array_get(params, 2);
json_t * length_holder = json_array_get(params, 3);
puts("read"); if (json_is_integer(inode_holder) &&
json_is_integer(handle_holder) &&
json_is_integer(offset_holder) &&
json_is_integer(length_holder))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
int handle = json_integer_value(handle_holder);
size_t offset = json_integer_value(offset_holder);
size_t length = json_integer_value(length_holder);
struct wsfsp_request * request = wsfsp_request_create(context->request, id);
context->provider->read(request, inode, handle, offset, length, context->user_data);
}
}
} }
void wsfsp_read_default( void wsfsp_read_default(
@ -31,7 +51,34 @@ void wsfsp_respond_read(
char const * data, char const * data,
size_t length) size_t length)
{ {
(void) request; if (0 < length)
(void) data; {
(void) length; size_t const size = 4 * ((length / 3) + 2);
char * buffer = malloc(size);
if (NULL != buffer)
{
lws_b64_encode_string(data, length, buffer, size);
json_t * result = json_object();
json_object_set_new(result, "data", json_string(buffer));
json_object_set_new(result, "format", json_string("base64"));
json_object_set_new(result, "count", json_integer((int) length));
wsfsp_respond(request, result);
free(buffer);
}
else
{
wsfsp_respond_error(request, -1);
}
}
else
{
json_t * result = json_object();
json_object_set_new(result, "data", json_string(""));
json_object_set_new(result, "format", json_string("identitiy"));
json_object_set_new(result, "count", json_integer(0));
wsfsp_respond(request, result);
}
} }