Merge pull request #2 from falk-werner/integrate_provider

Integrate provider
pull/4/head
Falk Werner 4 years ago committed by GitHub
commit 0b63c07f4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,24 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: 'bug'
assignees: ''
---
**Description**
A clear and concise description of what the bug is.
**Steps To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error
**Expected behavior**
A clear and concise description of what you expected to happen.
**Additional context**
Add any other context about the problem here.

@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''
---
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when \[...\]
**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Additional context**
Add any other context or screenshots about the feature request here.

3
.gitignore vendored

@ -1,4 +1,5 @@
/build/
/.vscode/
/build/
/builddir/
/subprojects/*
!/subprojects/*.wrap

@ -19,13 +19,17 @@ addons:
- python3-setuptools
- python3-wheel
- ninja-build
- lcov
before_install:
- sudo pip3 install --system meson
before_script:
- meson build
- meson -Db_coverage=true build
script:
- cd build
- ninja
- meson test
after_success:
- bash <(curl -s https://codecov.io/bash)

@ -0,0 +1,7 @@
# This is the list of webfuse-provider authors for copyright purposes.
#
# This does not necessarily list everyone who has contributed code, since in
# some cases, their employer may be the copyright holder. To see the full list
# of contributors, see the revision history in source control.
Falk Werner
Osama El Hosami

@ -1,8 +1,9 @@
[![Build Status](https://travis-ci.org/falk-werner/webfuse-provider.svg?branch=master)](https://travis-ci.org/falk-werner/webfuse-provider)
[![codecov](https://codecov.io/gh/falk-werner/webfuse-provider/branch/master/graph/badge.svg)](https://codecov.io/gh/falk-werner/webfuse-provider)
# webfuse-provider
Reference implementation of webfuse provider
webfuse combines libwebsockets and libfuse. It allows ot attach a remote filesystem via websockets.
## Build and run
@ -13,10 +14,12 @@ To install dependecies, see below.
ninja
./webfuse-provider
## Dependencies
## Fellow Repositories
- [webfuse](https://github.com/falk-werner/webfuse)
- [libwebsockets](https://libwebsockets.org/)
- [jansson](https://github.com/akheron/jansson)
- **[webfuse-example](https://github.com/falk-werner/webfuse-example)**: Example of webfuse
- **[webfuse](https://github.com/falk-werner/webfuse)**: webfuse adapter library
- **[webfused](https://github.com/falk-werner/webfused)**: Reference implementation of webfuse daemon
All dependencies are bundled using meson wrap files.
## Further Documentation
- [Webfuse Protocol](https://github.com/falk-werner/webfuse/blob/master/doc/protocol.md)

@ -0,0 +1,11 @@
ignore:
- test
- examples
parsers:
gcov:
branch_detection:
conditional: yes
loop: yes
method: yes
macro: yes

@ -9,7 +9,7 @@
#include <unistd.h>
#include <getopt.h>
#include "webfuse_provider.h"
#include "webfuse_provider/webfuse_provider.h"
struct config
{
@ -193,7 +193,7 @@ static void fs_lookup(
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
wfp_respond_error(request, WFP_BAD_NOENTRY);
}
}
@ -215,7 +215,7 @@ static void fs_getattr(
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
wfp_respond_error(request, WFP_BAD_NOENTRY);
}
}
@ -247,7 +247,7 @@ static void fs_readdir(
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
wfp_respond_error(request, WFP_BAD_NOENTRY);
}
}
@ -268,12 +268,12 @@ static void fs_open(
}
else
{
wfp_respond_error(request, WF_BAD_ACCESS_DENIED);
wfp_respond_error(request, WFP_BAD_ACCESS_DENIED);
}
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
wfp_respond_error(request, WFP_BAD_NOENTRY);
}
}
@ -305,12 +305,12 @@ static void fs_read(
}
else
{
wfp_respond_error(request, WF_BAD);
wfp_respond_error(request, WFP_BAD);
}
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
wfp_respond_error(request, WFP_BAD_NOENTRY);
}
}

@ -4,7 +4,7 @@
#include <stdbool.h>
#include <signal.h>
#include "webfuse_provider.h"
#include "webfuse_provider/webfuse_provider.h"
#include "static_filesystem.h"
struct args

@ -1,8 +1,8 @@
#include "static_filesystem.h"
#include "webfuse/provider/client_config.h"
#include "webfuse/provider/dirbuffer.h"
#include "webfuse/provider/operation/error.h"
#include "webfuse_provider/client_config.h"
#include "webfuse_provider/dirbuffer.h"
#include "webfuse_provider/operation/error.h"
#include "path.h"
@ -255,7 +255,7 @@ static void static_filesystem_lookup(
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
wfp_respond_error(request, WFP_BAD_NOENTRY);
}
}
@ -276,7 +276,7 @@ static void static_filesystem_getattr(
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
wfp_respond_error(request, WFP_BAD_NOENTRY);
}
}
@ -308,7 +308,7 @@ static void static_filesystem_readdir(
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
wfp_respond_error(request, WFP_BAD_NOENTRY);
}
}
@ -329,12 +329,12 @@ static void static_filesystem_open(
}
else
{
wfp_respond_error(request, WF_BAD_ACCESS_DENIED);
wfp_respond_error(request, WFP_BAD_ACCESS_DENIED);
}
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
wfp_respond_error(request, WFP_BAD_NOENTRY);
}
}
@ -361,7 +361,7 @@ static void static_filesystem_read(
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
wfp_respond_error(request, WFP_BAD_NOENTRY);
}
}

@ -0,0 +1,32 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/api.h
/// \brief API define for webfuse provider.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_API_H
#define WFP_API_H
//------------------------------------------------------------------------------
/// \def WFP_API
/// \brief Marks public symbols of libwebfuse_provider.
//------------------------------------------------------------------------------
#ifndef WFP_API
#define WFP_API
#endif
//------------------------------------------------------------------------------
/// \def WFP_EXPORT
/// \brief Marks exported symbols as visible.
///
/// Set WFP_API to WFP_EXPORT when building libwebfuse_provider.so to export
/// public symbols.
//------------------------------------------------------------------------------
#ifndef WFP_EXPORT
#ifdef __GNUC__
#define WFP_EXPORT __attribute__ ((visibility ("default")))
#else
#define WFP_EXPORT
#endif
#endif
#endif

@ -0,0 +1,105 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/client.h
/// \brief Webfuse provider client.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_CLIENT_H
#define WFP_CLIENT_H
#include "webfuse_provider/api.h"
#ifdef __cplusplus
extern "C"
{
#endif
//------------------------------------------------------------------------------
/// \struct wfp_client
/// \brief Webfuse provider client.
//------------------------------------------------------------------------------
struct wfp_client;
struct wfp_client_config;
//------------------------------------------------------------------------------
/// \brief Creates a webfuse provider client.
///
/// \note Client configuration is not managed by the client.
///
/// \param config pointer to client configuration.
/// \return newly created client or NULL in case of an error.
//------------------------------------------------------------------------------
extern WFP_API struct wfp_client * wfp_client_create(
struct wfp_client_config * config);
//------------------------------------------------------------------------------
/// \brief Connects the client to a remote webfuse adapter server.
///
/// \note This call starts to establish a connection. A callback is invoked,
/// when the connection is estanlished.
///
/// \param client pointer to client
/// \param url URL of remote webfuse adapter server
///
/// \see wfp_connected_fn
/// \see wfp_client_config_set_onconnected
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_connect(
struct wfp_client * client,
char const * url);
//------------------------------------------------------------------------------
/// \brief Disconnects a connected client.
///
/// \note This call starts to disconnect the connection. A callback is invoked
/// when conntection is disconnected.
///
/// \param client pointer to client
///
/// \see wfp_disconnected_fn
/// \see wfp_client_config_set_ondisconnected
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_disconnect(
struct wfp_client * client);
//------------------------------------------------------------------------------
/// \brief Disposes a client.
///
/// \note Client configuration is not managed by client.
///
/// \param client pointer to client
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_dispose(
struct wfp_client * client);
//------------------------------------------------------------------------------
/// \brief Triggers the client.
///
/// This function must be invoked in a loop while the client is running. It
/// makes the server wait for the next event and processes it.
///
/// \param client pointer to client
///
/// \see wfp_client_interrupt
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_service(
struct wfp_client * client);
//------------------------------------------------------------------------------
/// \brief interrupt wfp_client_service
///
/// This function can be called from another thread.
///
/// \param client pointer to client
///
/// \see wfp_client_service
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_interrupt(
struct wfp_client * client);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,240 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/client_config.h
/// \brief Client configuration of webfuse provider.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_CLIENT_CONFIG_H
#define WFP_CLIENT_CONFIG_H
#include <webfuse_provider/api.h>
#include <webfuse_provider/operation/lookup.h>
#include <webfuse_provider/operation/getattr.h>
#include <webfuse_provider/operation/readdir.h>
#include <webfuse_provider/operation/open.h>
#include <webfuse_provider/operation/close.h>
#include <webfuse_provider/operation/read.h>
#include <webfuse_provider/credentials.h>
#ifdef __cplusplus
extern "C"
{
#endif
//------------------------------------------------------------------------------
/// \struct wfp_client_config
/// \brief Provider client configuration object.
///
/// Holds configuration of webfuse provider client.
//------------------------------------------------------------------------------
struct wfp_client_config;
//------------------------------------------------------------------------------
/// \brief Callback to signal when the client's connection is established.
///
/// \param user_data user defined context
//------------------------------------------------------------------------------
typedef void wfp_connected_fn(
void * user_data);
//------------------------------------------------------------------------------
/// \brief Callback to signal when a client's connection is disconnected.
///
/// \param user_data user defined context
//------------------------------------------------------------------------------
typedef void wfp_disconnected_fn(
void * user_data);
//------------------------------------------------------------------------------
/// \brief Creates a new client configuration.
///
/// \return newly created client configuration
//------------------------------------------------------------------------------
extern WFP_API struct wfp_client_config * wfp_client_config_create(void);
//------------------------------------------------------------------------------
/// \brief Disposes a client configuration.
///
/// \note The user defined context is not managed by the client configuration.
///
/// \param config pointer to client configuration
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_dispose(
struct wfp_client_config * config);
//------------------------------------------------------------------------------
/// \brief Sets a user defined context.
///
/// \note The user is responsible to manage the lifetime of user data.
///
/// \param config pointer to client configuration
/// \param user_data user defined context
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_userdata(
struct wfp_client_config * config,
void * user_data);
//------------------------------------------------------------------------------
/// \brief Sets the path to clients private key.
///
/// \note To enable TLS both, private key and certificate, must be specified.
/// Otherwise, TLS is not used.
///
/// \param config pointer to client configuration
/// \param key_path path of clients private key (pem file)
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_keypath(
struct wfp_client_config * config,
char const * key_path);
//------------------------------------------------------------------------------
/// \brief Sets the path of clients certificate.
///
/// \note To enable TLS both, private key and certificate, must be specified.
/// Otherwise, TLS is not used.
///
/// \param config pointer to client configuration
/// \param cert_path path of the clients certificate (pem file)
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_certpath(
struct wfp_client_config * config,
char const * cert_path);
//------------------------------------------------------------------------------
/// \brief Sets the path of ca file to verify servers.
///
/// \note To enable TLS both, private key and certificate, must be specified.
/// Otherwise, TLS is not used.
///
/// \param config pointer to client configuration
/// \param ca_filepath path of the ca file (pem file)
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_ca_filepath(
struct wfp_client_config * config,
char const * ca_filepath);
//------------------------------------------------------------------------------
/// \brief Sets the onconnected handler.
///
/// The handler is invoked, when the client's conntection is established.
///
/// \param config pointer to client configuration
/// \param handler pointer to handler
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_onconnected(
struct wfp_client_config * config,
wfp_connected_fn * handler);
//------------------------------------------------------------------------------
/// \brief Sets ondisconnected handler
///
/// The handler is invoked, when the client's conntection is lost.
///
/// \param config pointer to client configuration
/// \param handler pointer to handler
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_ondisconnected(
struct wfp_client_config * config,
wfp_disconnected_fn * handler);
//------------------------------------------------------------------------------
/// \brief Sets onlookup handler.
///
/// The handler is invoked, when the identifier of a file is requested.
///
/// \param config pointer to client configuration
/// \param handler pointer to handler
///
/// \see wfp_lookup_fn
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_onlookup(
struct wfp_client_config * config,
wfp_lookup_fn * handler);
//------------------------------------------------------------------------------
/// \brief Sets ongetattr handler.
///
/// The handler is invoked, when attributes of a file are requested.
///
/// \param config pointer to client configuration
/// \param handler pointer to handler
///
/// \see wfp_getattr_fn
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_ongetattr(
struct wfp_client_config * config,
wfp_getattr_fn * handler);
//------------------------------------------------------------------------------
/// \brief Sets onreaddir handler.
///
/// The handler is invoked, when the contents of directory are requested-
///
/// \param config pointer to client configuration
/// \param handler pointer to handler
///
/// \see wfp_readdir_fn
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_onreaddir(
struct wfp_client_config * config,
wfp_readdir_fn * handler);
//------------------------------------------------------------------------------
/// \brief Sets onopen handler.
///
/// The handler is invoked, whe a file should be opened.
///
/// \param config pointer to client configuration
/// \param handler pointer to handler
///
/// \see wfp_open_fn
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_onopen(
struct wfp_client_config * config,
wfp_open_fn * handler);
//------------------------------------------------------------------------------
/// \brief Sets onclose handler.
///
/// The handler is invoked, when a file is closed.
///
/// \param config pointer to client configuration
/// \param handler pointer to handler
///
/// \see wfp_close_fn
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_onclose(
struct wfp_client_config * config,
wfp_close_fn * handler);
//------------------------------------------------------------------------------
/// \brief Sets onread handler.
///
/// The handler is invoked, when a files content is requested.
///
/// \param config pointer to client configuration
/// \param handler pointer to handler
///
/// \see wfp_read_fn
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_set_onread(
struct wfp_client_config * config,
wfp_read_fn * handler);
//------------------------------------------------------------------------------
/// \brief Enabled authentication.
///
/// \param config pointer to client configuration
/// \param get_credentials pointer to function providing credentials when
// needed.
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_config_enable_authentication(
struct wfp_client_config * config,
wfp_get_credentials_fn * get_credentials);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,117 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/client_protocol.h
/// \brief Provides low level access to libwebsockets protocol.
///
/// By default, libwebfuse encapsulates libwebsockets protocol by \ref
/// wfp_client. But sometimes it might come in handy to have access to
/// libwebsockets protocol. This allows to integrate libwebfuse in existing
/// libwebsockets applications.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_CLIENT_PROTOCOL_H
#define WFP_CLIENT_PROTOCOL_H
#include "webfuse_provider/api.h"
#ifdef __cplusplus
extern "C"
{
#endif
//------------------------------------------------------------------------------
/// \struct wfp_client_protocol
/// \brief Opaque webfuse client protocol..
//------------------------------------------------------------------------------
struct wfp_client_protocol;
//------------------------------------------------------------------------------
/// \struct lws_protocols
/// \brief Forward declaration of libwebsockets protocols structure.
//------------------------------------------------------------------------------
struct lws_protocols;
//------------------------------------------------------------------------------
/// \struct lws_context
/// \brief Forward declaration of libwebsockets context structure.
//------------------------------------------------------------------------------
struct lws_context;
//------------------------------------------------------------------------------
/// \struct wfp_client_config
/// \copydoc wfp_client_config
//------------------------------------------------------------------------------
struct wfp_client_config;
//------------------------------------------------------------------------------
/// \brief Creates a new webfuse provider client protocol.
///
/// \note The user is responsible to manage lifetime of \arg config.
///
/// \note TLS configuration is ignored, since TLS is managed by libwebsockets.
///
/// \param config pointer to client config
/// \return newly created protocol
//------------------------------------------------------------------------------
extern WFP_API struct wfp_client_protocol * wfp_client_protocol_create(
struct wfp_client_config const * config);
//------------------------------------------------------------------------------
/// \brief Disposes a protocol.
///
/// \note The user defined context is not managed by the protocol.
///
/// \param protocol pointer to protocol.
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_protocol_dispose(
struct wfp_client_protocol * protocol);
//------------------------------------------------------------------------------
/// \brief Initialized libwebsockets protocol structure.
///
/// \param protocol pointer to protocol
/// \param lws_protocol pointer to libwebsockets protocol structure.
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_protocol_init_lws(
struct wfp_client_protocol * protocol,
struct lws_protocols * lws_protocol);
//------------------------------------------------------------------------------
/// \brief Connects the protocol to a remote webfuse adapter server.
///
/// \note This call starts to establish a connection. A callback is invoked,
/// when the connection is estanlished.
///
/// \param protocol pointer to protocol
/// \param context lws context
/// \param url URL of remote webfuse adapter server
///
/// \see wfp_connected_fn
/// \see wfp_client_config_set_onconnected
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_protocol_connect(
struct wfp_client_protocol * protocol,
struct lws_context * context,
char const * url);
//------------------------------------------------------------------------------
/// \brief Disconnects the protocol from a remote webfuse adapter server.
///
/// \note This call starts to disconnect. A callback is invoked,
/// when the connection is estanlished.
///
/// \param protocol pointer to protocol
///
/// \see wfp_connected_fn
/// \see wfp_client_config_set_ondisconnected
//------------------------------------------------------------------------------
extern WFP_API void wfp_client_protocol_disconnect(
struct wfp_client_protocol * protocol);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,30 @@
#ifndef WFP_CREDENTIALS_H
#define WFP_CREDENTIALS_H
#include <webfuse_provider/api.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_credentials;
typedef void wfp_get_credentials_fn(
struct wfp_credentials * credentials,
void * user_data);
extern WFP_API void wfp_credentials_set_type(
struct wfp_credentials * credentials,
char const * type);
extern WFP_API void wfp_credentials_add(
struct wfp_credentials * credentials,
char const * key,
char const * value);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,60 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/dirbuffer.h
/// \brief Buffer used for directory listing.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_DIRBUFFER_H
#define WFP_DIRBUFFER_H
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "webfuse_provider/api.h"
#ifdef __cplusplus
extern "C"
{
#endif
//------------------------------------------------------------------------------
/// \struct wfp_dirbuffer
/// \brief Buffer used for directory listing.
///
/// \see wfp_respond_readdir
//------------------------------------------------------------------------------
struct wfp_dirbuffer;
//------------------------------------------------------------------------------
/// \brief Creates a new dir buffer.
///
/// \return newly created dir buffer.
//------------------------------------------------------------------------------
extern WFP_API struct wfp_dirbuffer * wfp_dirbuffer_create(void);
//------------------------------------------------------------------------------
/// \brief Disposes a dir buffer.
///
/// \param buffer pointer to dir buffer
//------------------------------------------------------------------------------
extern WFP_API void wfp_dirbuffer_dispose(
struct wfp_dirbuffer * buffer);
//------------------------------------------------------------------------------
/// \brief Adds an entry to dir buffer.
///
/// \param buffer pointer to dir buffer
/// \param name name of the entry (file or directory)
/// \param inode inode of the entry
//------------------------------------------------------------------------------
extern WFP_API void wfp_dirbuffer_add(
struct wfp_dirbuffer * buffer,
char const * name,
ino_t inode);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,46 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/operation/close.h
/// \brief Provider's close callback.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_OPERATION_CLOSE_H
#define WFP_OPERATION_CLOSE_H
#ifndef __cplusplus
#include <inttypes.h>
#else
#include <cinttypes>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "webfuse_provider/api.h"
#ifdef __cplusplus
extern "C"
{
#endif
//------------------------------------------------------------------------------
/// \brief Callback invoked when a file is invoked.
///
/// This function does not respond.
///
/// \param inode inode of file to close
/// \param handle handle of file to close
/// \param flags file close flags
/// \param user_data user defined context
//------------------------------------------------------------------------------
typedef void wfp_close_fn(
ino_t inode,
uint32_t handle,
int flags,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,36 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/operation/error.h
/// \brief Respond with error code.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_OPERATION_ERROR_H
#define WFP_OPERATION_ERROR_H
#include "webfuse_provider/api.h"
#include "webfuse_provider/status.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_request;
//------------------------------------------------------------------------------
/// \brief Respond to a request with an error.
///
/// A client's callback must respond with exactly one responde, either with a
/// valid reponse regarding to the concrete request or with an error response.
///
/// \param request pointer to request
/// \param status error code
//------------------------------------------------------------------------------
extern WFP_API void wfp_respond_error(
struct wfp_request * request,
wfp_status status);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,55 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/operation/getattr.h
/// \brief Get file attributes.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_OPERATION_GETATTR_H
#define WFP_OPERATION_GETATTR_H
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "webfuse_provider/api.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_request;
//------------------------------------------------------------------------------
/// \brief Get file attributes.
///
/// \note After this function is called, exactly one response must be sent,
/// either via \ref wfp_respond_getattr or via \ref wfp_respond_error.
///
/// \param request pointer to request
/// \param inode inode of file to get attributes
/// \param user_data user defined context
///
/// \see wfp_respond_getattr
/// \see wfp_respond_error
//------------------------------------------------------------------------------
typedef void wfp_getattr_fn(
struct wfp_request * request,
ino_t inode,
void * user_data);
//------------------------------------------------------------------------------
/// \brief Respond to a get attributes request.
///
/// \param request pointer to request
/// \param stat file attributes
//------------------------------------------------------------------------------
extern WFP_API void wfp_respond_getattr(
struct wfp_request * request,
struct stat const * stat);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,57 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/operation/lookup.h
/// \brief Lookup file.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_OPERATION_LOOKUP_H
#define WFP_OPERATION_LOOKUP_H
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "webfuse_provider/api.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_request;
//------------------------------------------------------------------------------
/// \brief Lookup a file or directory.
///
/// \note After this function is called, exactly one response must be sent,
/// either via \ref wfp_respond_lookup or via \ref wfp_respond_error.
///
/// \param request pointer to request
/// \param parent inode of parent
/// \param name name of the filesystem object to lookup
/// \param user_data pointer to user defined context
///
/// \see wfp_respond_lookup
/// \see wfp_respond_error
//------------------------------------------------------------------------------
typedef void wfp_lookup_fn(
struct wfp_request * request,
ino_t parent,
char const * name,
void * user_data);
//------------------------------------------------------------------------------
/// \brief Respond to lookup request.
///
/// \param request pointer to request
/// \param stat attributes of filesystem object
//------------------------------------------------------------------------------
extern WFP_API void wfp_respond_lookup(
struct wfp_request * request,
struct stat const * stat);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,63 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/operation/open.h
/// \brief Open a file.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_OPERATION_OPEN_H
#define WFP_OPERATION_OPEN_H
#ifndef __cplusplus
#include <inttypes.h>
#else
#include <cinttypes>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "webfuse_provider/api.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_request;
//------------------------------------------------------------------------------
/// \brief Open a file.
///
/// \note After this function is called, exactly one response must be sent,
/// either via \ref wfp_respond_open or via \ref wfp_respond_error.
///
/// \param request pointer to request
/// \param inode inode of the file to open
/// \param flags file open flags
/// \param user_data user defined context
///
/// \see wfp_respond_open
/// \see wfp_respond_error
//------------------------------------------------------------------------------
typedef void wfp_open_fn(
struct wfp_request * request,
ino_t inode,
int flags,
void * user_data);
//------------------------------------------------------------------------------
/// \brief Respond to open file.
///
/// \param request pointer to request
/// \param handle handle of the opened file
//------------------------------------------------------------------------------
extern WFP_API void wfp_respond_open(
struct wfp_request * request,
uint32_t handle);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,76 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/operation/read.h
/// \brief Read contents of a file.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_OPERATION_READ_H
#define WFP_OPERATION_READ_H
#ifndef __cplusplus
#include <stddef.h>
#include <inttypes.h>
#else
#include <cstddef>
#include <cinttypes>
using std::size_t;
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "webfuse_provider/api.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_request;
//------------------------------------------------------------------------------
/// \brief Requests content of a file.
///
/// On success, up to \arg length bytes should be returned via \ref
/// wfp_respond_read.
///
/// \note After this function is called, exactly one response must be sent,
/// either via \ref wfp_respond_read or via \ref wfp_respond_error.
///
/// \param request pointer to request
/// \param inode inode of the file to read
/// \param handle handle of the file to read (returned by open)
/// \param offset offset within the file where to start reading
/// \param length amount of bytes to read
/// \param user_data used defined context
///
/// \see wfp_respond_read
/// \see wfp_respond_error
//------------------------------------------------------------------------------
typedef void wfp_read_fn(
struct wfp_request * request,
ino_t inode,
uint32_t handle,
size_t offset,
size_t length,
void * user_data);
//------------------------------------------------------------------------------
/// \brief Respond to read.
///
/// \note The user is responsible to manage lifetime of \arg data.
///
/// \param request pointer to request
/// \param data data read from file
/// \param length amount of bytes read
//------------------------------------------------------------------------------
extern WFP_API void wfp_respond_read(
struct wfp_request * request,
char const * data,
size_t length);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,58 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/operation/readdir.h
/// \brief List directory contents.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_OPERATION_READDIR_H
#define WFP_OPERATION_READDIR_H
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "webfuse_provider/api.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_dirbuffer;
struct wfp_request;
//------------------------------------------------------------------------------
/// \brief Requests the contents of a directory.
///
/// \note After this function is called, exactly one response must be sent,
/// either via \ref wfp_respond_readdir or via \ref wfp_respond_error.
///
/// \param request pointer to request
/// \param directory inode of directory to list
/// \param user_data user defined context
///
/// \see wfp_respond_readdir
/// \see wfp_respond_error
//------------------------------------------------------------------------------
typedef void wfp_readdir_fn(
struct wfp_request * request,
ino_t directory,
void * user_data);
//------------------------------------------------------------------------------
/// \brief Respond to list directory contents.
///
/// \note The user is responsible to manage dirbuffe, p.e. to dispose
/// it after this function is called.
///
/// \param request pointer to request
/// \param dirbuffer contains contents of directory
//------------------------------------------------------------------------------
extern WFP_API void wfp_respond_readdir(
struct wfp_request * request,
struct wfp_dirbuffer * dirbuffer);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,33 @@
////////////////////////////////////////////////////////////////////////////////
/// \file protocol_names.h
/// \brief Names of websocket protocol.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_PROTOCOL_NAMES_H
#define WFP_PROTOCOL_NAMES_H
//------------------------------------------------------------------------------
/// \def WFP_PROTOCOL_NAME_ADAPTER_SERVER
/// \brief Name of the websocket protocol an adapter server is running.
//------------------------------------------------------------------------------
#define WFP_PROTOCOL_NAME_ADAPTER_SERVER ("webfuse-adapter-server")
//------------------------------------------------------------------------------
/// \def WFP_PROTOCOL_NAME_ADAPTER_CLIENT
/// \brief Name of the websocket protocol an adapter client is running.
//------------------------------------------------------------------------------
#define WFP_PROTOCOL_NAME_ADAPTER_CLIENT ("webfuse-adapter-client")
//------------------------------------------------------------------------------
/// \def WFP_PROTOCOL_NAME_PROVIDER_CLIENT
/// \brief Name of the websocket protocol an provider client is running.
//------------------------------------------------------------------------------
#define WFP_PROTOCOL_NAME_PROVIDER_CLIENT ("webfuse-provider-client")
//------------------------------------------------------------------------------
/// \def WFP_PROTOCOL_NAME_PROVIDER_SERVER
/// \brief Name of the websocket protocol an provider server is running.
//------------------------------------------------------------------------------
#define WFP_PROTOCOL_NAME_PROVIDER_SERVER ("webfuse-provider-server")
#endif

@ -0,0 +1,23 @@
////////////////////////////////////////////////////////////////////////////////
/// \file status.h
/// \brief Generic status code.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_STATUS_H
#define WFP_STATUS_H
#define WFP_GOOD 0 ///< Positive status code.
#define WFP_BAD 1 ///< Generic negative status code.
#define WFP_BAD_NOTIMPLEMENTED 2 ///< The called function is not implemented (yet).
#define WFP_BAD_TIMEOUT 3 ///< A timeout occured.
#define WFP_BAD_BUSY 4 ///< Resource is busy, try again later.
#define WFP_BAD_FORMAT 5 ///< Invalid format.
#define WFP_BAD_NOENTRY 101 ///< Entry not found.
#define WFP_BAD_ACCESS_DENIED 102 ///< Access is denied.
/// Status code.
typedef int wfp_status;
#endif

@ -0,0 +1,27 @@
////////////////////////////////////////////////////////////////////////////////
/// \file webfuse_provider.h
/// \brief Convenience header to include all functionality of libfuse_provider.
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_H
#define WFP_H
#include <webfuse_provider/status.h>
#include <webfuse_provider/protocol_names.h>
#include <webfuse_provider/api.h>
#include <webfuse_provider/client.h>
#include <webfuse_provider/client_config.h>
#include <webfuse_provider/client_protocol.h>
#include <webfuse_provider/dirbuffer.h>
#include <webfuse_provider/credentials.h>
#include <webfuse_provider/operation/error.h>
#include <webfuse_provider/operation/lookup.h>
#include <webfuse_provider/operation/getattr.h>
#include <webfuse_provider/operation/readdir.h>
#include <webfuse_provider/operation/open.h>
#include <webfuse_provider/operation/close.h>
#include <webfuse_provider/operation/read.h>
#endif

@ -0,0 +1,281 @@
#include "webfuse_provider/webfuse_provider.h"
#include "webfuse_provider/impl/request.h"
#include "webfuse_provider/impl/operation/getattr.h"
#include "webfuse_provider/impl/operation/lookup.h"
#include "webfuse_provider/impl/operation/readdir.h"
#include "webfuse_provider/impl/operation/open.h"
#include "webfuse_provider/impl/operation/close.h"
#include "webfuse_provider/impl/operation/read.h"
#include "webfuse_provider/impl/client_protocol.h"
#include "webfuse_provider/impl/client_config.h"
#include "webfuse_provider/impl/client.h"
#include "webfuse_provider/impl/dirbuffer.h"
#include "webfuse_provider/impl/credentials.h"
#include "webfuse_provider/impl/util/util.h"
// respond
void wfp_respond_error(
struct wfp_request * request,
wfp_status status)
{
wfp_impl_respond_error(request, status);
}
void wfp_respond_getattr(
struct wfp_request * request,
struct stat const * stat)
{
wfp_impl_respond_getattr(request, stat);
}
void wfp_respond_lookup(
struct wfp_request * request,
struct stat const * stat)
{
wfp_impl_respond_lookup(request, stat);
}
void wfp_respond_open(
struct wfp_request * request,
uint32_t handle)
{
wfp_impl_respond_open(request, handle);
}
void wfp_respond_read(
struct wfp_request * request,
char const * data,
size_t length)
{
wfp_impl_respond_read(request, data, length);
}
void wfp_respond_readdir(
struct wfp_request * request,
struct wfp_dirbuffer * dirbuffer)
{
wfp_impl_respond_readdir(request, dirbuffer);
}
// config
struct wfp_client_config * wfp_client_config_create(void)
{
return wfp_impl_client_config_create();
}
void wfp_client_config_dispose(
struct wfp_client_config * config)
{
wfp_impl_client_config_dispose(config);
}
void wfp_client_config_set_userdata(
struct wfp_client_config * config,
void * user_data)
{
wfp_impl_client_config_set_userdata(config, user_data);
}
void wfp_client_config_set_keypath(
struct wfp_client_config * config,
char const * key_path)
{
wfp_impl_client_config_set_keypath(config, key_path);
}
void wfp_client_config_set_certpath(
struct wfp_client_config * config,
char const * cert_path)
{
wfp_impl_client_config_set_certpath(config, cert_path);
}
void wfp_client_config_set_ca_filepath(
struct wfp_client_config * config,
char const * ca_filepath)
{
wfp_impl_client_config_set_ca_filepath(config, ca_filepath);
}
void wfp_client_config_set_onconnected(
struct wfp_client_config * config,
wfp_connected_fn * handler)
{
wfp_impl_client_config_set_onconnected(config, handler);
}
void wfp_client_config_set_ondisconnected(
struct wfp_client_config * config,
wfp_disconnected_fn * handler)
{
wfp_impl_client_config_set_ondisconnected(config, handler);
}
void wfp_client_config_set_onlookup(
struct wfp_client_config * config,
wfp_lookup_fn * handler)
{
wfp_impl_client_config_set_onlookup(config, handler);
}
void wfp_client_config_set_ongetattr(
struct wfp_client_config * config,
wfp_getattr_fn * handler)
{
wfp_impl_client_config_set_ongetattr(config, handler);
}
void wfp_client_config_set_onreaddir(
struct wfp_client_config * config,
wfp_readdir_fn * handler)
{
wfp_impl_client_config_set_onreaddir(config, handler);
}
void wfp_client_config_set_onopen(
struct wfp_client_config * config,
wfp_open_fn * handler)
{
wfp_impl_client_config_set_onopen(config, handler);
}
void wfp_client_config_set_onclose(
struct wfp_client_config * config,
wfp_close_fn * handler)
{
wfp_impl_client_config_set_onclose(config, handler);
}
void wfp_client_config_set_onread(
struct wfp_client_config * config,
wfp_read_fn * handler)
{
wfp_impl_client_config_set_onread(config, handler);
}
void wfp_client_config_enable_authentication(
struct wfp_client_config * config,
wfp_get_credentials_fn * get_credentials)
{
wfp_impl_client_config_enable_authentication(config, get_credentials);
}
// protocol
struct wfp_client_protocol * wfp_client_protocol_create(
struct wfp_client_config const * config)
{
return wfp_impl_client_protocol_create(config);
}
void wfp_client_protocol_dispose(
struct wfp_client_protocol * protocol)
{
wfp_impl_client_protocol_dispose(protocol);
}
void wfp_client_protocol_init_lws(
struct wfp_client_protocol * protocol,
struct lws_protocols * lws_protocol)
{
wfp_impl_client_protocol_init_lws(protocol, lws_protocol);
}
void wfp_client_protocol_connect(
struct wfp_client_protocol * protocol,
struct lws_context * context,
char const * url)
{
wfp_impl_client_protocol_connect(protocol, context, url);
}
void wfp_client_protocol_disconnect(
struct wfp_client_protocol * protocol)
{
wfp_impl_client_protocol_disconnect(protocol);
}
// client
struct wfp_client * wfp_client_create(
struct wfp_client_config * config)
{
return wfp_impl_client_create(config);
}
void wfp_client_connect(
struct wfp_client * client,
char const * url)
{
wfp_impl_client_connect(client, url);
}
void wfp_client_disconnect(
struct wfp_client * client)
{
wfp_impl_client_disconnect(client);
}
void wfp_client_dispose(
struct wfp_client * client)
{
wfp_impl_client_dispose(client);
}
void wfp_client_service(
struct wfp_client * client)
{
wfp_impl_client_service(client);
}
void wfp_client_interrupt(
struct wfp_client * client)
{
wfp_impl_client_interrupt(client);
}
// dirbuffer
struct wfp_dirbuffer * wfp_dirbuffer_create(void)
{
return wfp_impl_dirbuffer_create();
}
void wfp_dirbuffer_dispose(
struct wfp_dirbuffer * buffer)
{
wfp_impl_dirbuffer_dispose(buffer);
}
void wfp_dirbuffer_add(
struct wfp_dirbuffer * buffer,
char const * name,
ino_t inode)
{
wfp_impl_dirbuffer_add(buffer, name, inode);
}
// credentials
void wfp_credentials_set_type(
struct wfp_credentials * credentials,
char const * type)
{
wfp_impl_credentials_set_type(credentials, type);
}
void wfp_credentials_add(
struct wfp_credentials * credentials,
char const * key,
char const * value)
{
wfp_impl_credentials_add(credentials, key, value);
}

@ -0,0 +1,95 @@
#include "webfuse_provider/impl/client.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <libwebsockets.h>
#include "webfuse_provider/impl/provider.h"
#include "webfuse_provider/impl/client_protocol.h"
#include "webfuse_provider/impl/client_config.h"
#include "webfuse_provider/impl/util/lws_log.h"
#define WFP_CLIENT_PROTOCOL_COUNT 2
struct wfp_client
{
struct wfp_client_protocol protocol;
struct lws_context_creation_info info;
struct lws_protocols protocols[WFP_CLIENT_PROTOCOL_COUNT];
struct lws_context * context;
char * key_path;
char * cert_path;
};
struct wfp_client * wfp_impl_client_create(
struct wfp_client_config * config)
{
wfp_impl_lwslog_disable();
struct wfp_client * client = malloc(sizeof(struct wfp_client));
wfp_impl_client_protocol_init(&client->protocol, &config->provider, config->user_data);
memset(client->protocols, 0, sizeof(struct lws_protocols) * WFP_CLIENT_PROTOCOL_COUNT);
wfp_impl_client_protocol_init_lws(&client->protocol, &client->protocols[0]);
memset(&client->info, 0, sizeof(struct lws_context_creation_info));
client->info.port = CONTEXT_PORT_NO_LISTEN;
client->info.protocols = client->protocols;
client->info.uid = -1;
client->info.gid = -1;
if ((NULL != config->cert_path) && (NULL != config->key_path))
{
client->info.options |= LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
}
client->context = lws_create_context(&client->info);
if ((NULL != config->cert_path) && (NULL != config->key_path))
{
struct lws_vhost * vhost = lws_create_vhost(client->context, &client->info);
client->info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
client->info.client_ssl_cert_filepath = config->cert_path;
client->info.client_ssl_private_key_filepath = config->key_path;
client->info.client_ssl_ca_filepath = config->ca_filepath;
lws_init_vhost_client_ssl(&client->info, vhost);
}
return client;
}
void wfp_impl_client_dispose(
struct wfp_client * client)
{
lws_context_destroy(client->context);
wfp_impl_client_protocol_cleanup(&client->protocol);
free(client);
}
void wfp_impl_client_connect(
struct wfp_client * client,
char const * url)
{
wfp_impl_client_protocol_connect(&client->protocol, client->context, url);
}
void wfp_impl_client_disconnect(
struct wfp_client * client)
{
wfp_impl_client_protocol_disconnect(&client->protocol);
}
void wfp_impl_client_service(
struct wfp_client * client)
{
lws_service(client->context, 0);
}
void wfp_impl_client_interrupt(
struct wfp_client * client)
{
lws_cancel_service(client->context);
}

@ -0,0 +1,48 @@
#ifndef WFP_IMPL_CLIENT_H
#define WFP_IMPL_CLIENT_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_client;
struct wfp_client_config;
extern struct wfp_client * wfp_impl_client_create(
struct wfp_client_config * config);
extern void wfp_impl_client_set_keypath(
struct wfp_client * client,
char * key_path);
extern void wfp_impl_client_set_certpath(
struct wfp_client * client,
char * cert_path);
extern void wfp_impl_client_connect(
struct wfp_client * client,
char const * url);
extern void wfp_impl_client_disconnect(
struct wfp_client * client);
extern void wfp_impl_client_dispose(
struct wfp_client * client);
extern void wfp_impl_client_service(
struct wfp_client * client);
extern void wfp_impl_client_interrupt(
struct wfp_client * client);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,119 @@
#include "webfuse_provider/impl/client_config.h"
#include "webfuse_provider/impl/provider.h"
#include <stdlib.h>
#include <string.h>
struct wfp_client_config * wfp_impl_client_config_create(void)
{
struct wfp_client_config * config = malloc(sizeof(struct wfp_client_config));
wfp_impl_provider_init(&config->provider);
config->user_data = NULL;
config->key_path = NULL;
config->cert_path = NULL;
config->ca_filepath = NULL;
return config;
}
void wfp_impl_client_config_dispose(
struct wfp_client_config * config)
{
free(config->key_path);
free(config->cert_path);
free(config->ca_filepath);
free(config);
}
void wfp_impl_client_config_set_userdata(
struct wfp_client_config * config,
void * user_data)
{
config->user_data = user_data;
}
void wfp_impl_client_config_set_keypath(
struct wfp_client_config * config,
char const * key_path)
{
free(config->key_path);
config->key_path = strdup(key_path);
}
void wfp_impl_client_config_set_certpath(
struct wfp_client_config * config,
char const * cert_path)
{
free(config->cert_path);
config->cert_path = strdup(cert_path);
}
void wfp_impl_client_config_set_ca_filepath(
struct wfp_client_config * config,
char const * ca_filepath)
{
free(config->ca_filepath);
config->ca_filepath = strdup(ca_filepath);
}
void wfp_impl_client_config_set_onconnected(
struct wfp_client_config * config,
wfp_connected_fn * handler)
{
config->provider.connected = handler;
}
void wfp_impl_client_config_set_ondisconnected(
struct wfp_client_config * config,
wfp_disconnected_fn * handler)
{
config->provider.disconnected = handler;
}
void wfp_impl_client_config_set_onlookup(
struct wfp_client_config * config,
wfp_lookup_fn * handler)
{
config->provider.lookup = handler;
}
void wfp_impl_client_config_set_ongetattr(
struct wfp_client_config * config,
wfp_getattr_fn * handler)
{
config->provider.getattr = handler;
}
void wfp_impl_client_config_set_onreaddir(
struct wfp_client_config * config,
wfp_readdir_fn * handler)
{
config->provider.readdir = handler;
}
void wfp_impl_client_config_set_onopen(
struct wfp_client_config * config,
wfp_open_fn * handler)
{
config->provider.open = handler;
}
void wfp_impl_client_config_set_onclose(
struct wfp_client_config * config,
wfp_close_fn * handler)
{
config->provider.close = handler;
}
void wfp_impl_client_config_set_onread(
struct wfp_client_config * config,
wfp_read_fn * handler)
{
config->provider.read = handler;
}
void wfp_impl_client_config_enable_authentication(
struct wfp_client_config * config,
wfp_get_credentials_fn * get_credentials)
{
config->provider.get_credentials = get_credentials;
}

@ -0,0 +1,82 @@
#ifndef WFP_IMPL_CLIENT_CONFIG_H
#define WFP_IMPL_CLIENT_CONFIG_H
#include "webfuse_provider/client_config.h"
#include "webfuse_provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_client_config
{
struct wfp_provider provider;
void * user_data;
char * key_path;
char * cert_path;
char * ca_filepath;
};
extern struct wfp_client_config * wfp_impl_client_config_create(void);
extern void wfp_impl_client_config_dispose(
struct wfp_client_config * config);
extern void wfp_impl_client_config_set_userdata(
struct wfp_client_config * config,
void * user_data);
extern void wfp_impl_client_config_set_keypath(
struct wfp_client_config * config,
char const * key_path);
extern void wfp_impl_client_config_set_certpath(
struct wfp_client_config * config,
char const * cert_path);
extern void wfp_impl_client_config_set_ca_filepath(
struct wfp_client_config * config,
char const * ca_filepath);
extern void wfp_impl_client_config_set_onconnected(
struct wfp_client_config * config,
wfp_connected_fn * handler);
extern void wfp_impl_client_config_set_ondisconnected(
struct wfp_client_config * config,
wfp_disconnected_fn * handler);
extern void wfp_impl_client_config_set_onlookup(
struct wfp_client_config * config,
wfp_lookup_fn * handler);
extern void wfp_impl_client_config_set_ongetattr(
struct wfp_client_config * config,
wfp_getattr_fn * handler);
extern void wfp_impl_client_config_set_onreaddir(
struct wfp_client_config * config,
wfp_readdir_fn * handler);
extern void wfp_impl_client_config_set_onopen(
struct wfp_client_config * config,
wfp_open_fn * handler);
extern void wfp_impl_client_config_set_onclose(
struct wfp_client_config * config,
wfp_close_fn * handler);
extern void wfp_impl_client_config_set_onread(
struct wfp_client_config * config,
wfp_read_fn * handler);
extern void wfp_impl_client_config_enable_authentication(
struct wfp_client_config * config,
wfp_get_credentials_fn * get_credentials);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,332 @@
#include "webfuse_provider/impl/client_protocol.h"
#include <stdlib.h>
#include <string.h>
#include <libwebsockets.h>
#include <jansson.h>
#include "webfuse_provider/impl/client_config.h"
#include "webfuse_provider/impl/provider.h"
#include "webfuse_provider/impl/credentials.h"
#include "webfuse_provider/impl/util/util.h"
#include "webfuse_provider/impl/message.h"
#include "webfuse_provider/impl/message_queue.h"
#include "webfuse_provider/impl/util/container_of.h"
#include "webfuse_provider/impl/util/url.h"
#include "webfuse_provider/protocol_names.h"
#include "webfuse_provider/impl/timer/manager.h"
#include "webfuse_provider/impl/jsonrpc/response.h"
#include "webfuse_provider/impl/jsonrpc/request.h"
#include "webfuse_provider/impl/jsonrpc/proxy.h"
#define WFP_DEFAULT_TIMEOUT (10 * 1000)
static void wfp_impl_client_protocol_respond(
json_t * response,
void * user_data)
{
struct wfp_client_protocol * protocol = (struct wfp_client_protocol *) user_data;
struct wfp_message * message = wfp_message_create(response);
wfp_slist_append(&protocol->messages, &message->item);
lws_callback_on_writable(protocol->wsi);
}
static void wfp_impl_client_protocol_process(
struct wfp_client_protocol * protocol,
char const * data,
size_t length)
{
json_t * message = json_loadb(data, length, 0, NULL);
if (NULL != message)
{
if (wfp_jsonrpc_is_response(message))
{
wfp_jsonrpc_proxy_onresult(protocol->proxy, message);
}
if (wfp_jsonrpc_is_request(message))
{
struct wfp_impl_invokation_context context =
{
.provider = &protocol->provider,
.user_data = protocol->user_data,
.request = &protocol->request
};
wfp_impl_provider_invoke(&context, message);
}
json_decref(message);
}
}
static void
wfp_impl_client_protocol_on_add_filesystem_finished(
void * user_data,
json_t const * result,
json_t const * WFP_UNUSED_PARAM(error))
{
struct wfp_client_protocol * protocol = user_data;
if (NULL == protocol->wsi) { return; }
if (NULL != result)
{
protocol->is_connected = true;
protocol->provider.connected(protocol->user_data);
}
else
{
protocol->is_shutdown_requested = true;
lws_callback_on_writable(protocol->wsi);
}
}
static void wfp_impl_client_protocol_add_filesystem(
struct wfp_client_protocol * protocol)
{
wfp_jsonrpc_proxy_invoke(
protocol->proxy,
&wfp_impl_client_protocol_on_add_filesystem_finished,
protocol,
"add_filesystem",
"s",
"cprovider");
}
static void
wfp_impl_client_protocol_on_authenticate_finished(
void * user_data,
json_t const * result,
json_t const * WFP_UNUSED_PARAM(error))
{
struct wfp_client_protocol * protocol = user_data;
if (NULL == protocol->wsi) { return; }
if (NULL != result)
{
wfp_impl_client_protocol_add_filesystem(protocol);
}
else
{
protocol->is_shutdown_requested = true;
lws_callback_on_writable(protocol->wsi);
}
}
static void wfp_impl_client_protocol_authenticate(
struct wfp_client_protocol * protocol)
{
struct wfp_credentials credentials;
wfp_impl_credentials_init(&credentials);
protocol->provider.get_credentials(&credentials, protocol->user_data);
char const * cred_type = wfp_impl_credentials_get_type(&credentials);
json_t * creds = wfp_impl_credentials_get(&credentials);
json_incref(creds);
wfp_jsonrpc_proxy_invoke(
protocol->proxy,
&wfp_impl_client_protocol_on_authenticate_finished,
protocol,
"authenticate",
"sj",
cred_type, creds);
wfp_impl_credentials_cleanup(&credentials);
}
static void wfp_impl_client_protocol_handshake(
struct wfp_client_protocol * protocol)
{
if (wfp_impl_provider_is_authentication_enabled(&protocol->provider))
{
wfp_impl_client_protocol_authenticate(protocol);
}
else
{
wfp_impl_client_protocol_add_filesystem(protocol);
}
}
static int wfp_impl_client_protocol_callback(
struct lws * wsi,
enum lws_callback_reasons reason,
void * WFP_UNUSED_PARAM(user),
void * in,
size_t len)
{
int result = 0;
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
struct wfp_client_protocol * protocol = (NULL != ws_protocol) ? ws_protocol->user: NULL;
if (NULL != protocol)
{
wfp_timer_manager_check(protocol->timer_manager);
switch (reason)
{
case LWS_CALLBACK_CLIENT_ESTABLISHED:
wfp_impl_client_protocol_handshake(protocol);
break;
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
protocol->is_connected = false;
protocol->provider.disconnected(protocol->user_data);
break;
case LWS_CALLBACK_CLIENT_CLOSED:
protocol->is_connected = false;
protocol->provider.disconnected(protocol->user_data);
protocol->wsi = NULL;
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
wfp_impl_client_protocol_process(protocol, in, len);
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
// fall-through
case LWS_CALLBACK_CLIENT_WRITEABLE:
if (wsi == protocol->wsi)
{
if (protocol->is_shutdown_requested)
{
result = 1;
}
else if (!wfp_slist_empty(&protocol->messages))
{
struct wfp_slist_item * item = wfp_slist_remove_first(&protocol->messages);
struct wfp_message * message = wfp_container_of(item, struct wfp_message, item);
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
wfp_message_dispose(message);
if (!wfp_slist_empty(&protocol->messages))
{
lws_callback_on_writable(wsi);
}
}
}
break;
default:
break;
}
}
return result;
}
static bool wfp_impl_client_protocol_send(
json_t * request,
void * user_data)
{
struct wfp_client_protocol * protocol = user_data;
struct wfp_message * message = wfp_message_create(request);
wfp_slist_append(&protocol->messages, &message->item);
lws_callback_on_writable(protocol->wsi);
return true;
}
void wfp_impl_client_protocol_init(
struct wfp_client_protocol * protocol,
struct wfp_provider const * provider,
void * user_data)
{
protocol->is_connected = false;
protocol->is_shutdown_requested = false;
wfp_slist_init(&protocol->messages);
protocol->wsi = NULL;
protocol->request.respond = &wfp_impl_client_protocol_respond;
protocol->request.user_data = protocol;
protocol->timer_manager = wfp_timer_manager_create();
protocol->proxy = wfp_jsonrpc_proxy_create(protocol->timer_manager, WFP_DEFAULT_TIMEOUT, &wfp_impl_client_protocol_send, protocol);
protocol->user_data = user_data;
wfp_impl_provider_init_from_prototype(&protocol->provider, provider);
}
void wfp_impl_client_protocol_cleanup(
struct wfp_client_protocol * protocol)
{
wfp_jsonrpc_proxy_dispose(protocol->proxy);
wfp_timer_manager_dispose(protocol->timer_manager);
wfp_message_queue_cleanup(&protocol->messages);
}
struct wfp_client_protocol * wfp_impl_client_protocol_create(
struct wfp_client_config const * config)
{
struct wfp_client_protocol * protocol = malloc(sizeof(struct wfp_client_protocol));
wfp_impl_client_protocol_init(protocol, &config->provider, config->user_data);
return protocol;
}
void wfp_impl_client_protocol_dispose(
struct wfp_client_protocol * protocol)
{
wfp_impl_client_protocol_cleanup(protocol);
free(protocol);
}
void wfp_impl_client_protocol_init_lws(
struct wfp_client_protocol * protocol,
struct lws_protocols * lws_protocol)
{
lws_protocol->name = WFP_PROTOCOL_NAME_PROVIDER_CLIENT;
lws_protocol->callback = &wfp_impl_client_protocol_callback;
lws_protocol->per_session_data_size = 0;
lws_protocol->user = protocol;
}
void wfp_impl_client_protocol_connect(
struct wfp_client_protocol * protocol,
struct lws_context * context,
char const * url)
{
struct wfp_url url_data;
bool const success = wfp_url_init(&url_data, url);
if (success)
{
struct lws_client_connect_info info;
memset(&info, 0, sizeof(struct lws_client_connect_info));
info.context = context;
info.port = url_data.port;
info.address = url_data.host;
info.path = url_data.path;
info.host = info.address;
info.origin = info.address;
info.ssl_connection = (url_data.use_tls) ? LCCSCF_USE_SSL : 0;
info.protocol = WFP_PROTOCOL_NAME_ADAPTER_SERVER;
info.local_protocol_name = WFP_PROTOCOL_NAME_PROVIDER_CLIENT;
info.pwsi = &protocol->wsi;
lws_client_connect_via_info(&info);
wfp_url_cleanup(&url_data);
}
else
{
protocol->provider.disconnected(protocol->user_data);
}
}
void wfp_impl_client_protocol_disconnect(
struct wfp_client_protocol * protocol)
{
if (protocol->is_connected)
{
protocol->is_shutdown_requested = true;
lws_callback_on_writable(protocol->wsi);
}
else
{
protocol->provider.disconnected(protocol->user_data);
}
}

@ -0,0 +1,63 @@
#ifndef WFP_IMPL_CLIENT_PROTOCOL_H
#define WFP_IMPL_CLIENT_PROTOCOL_H
#include "webfuse_provider/impl/provider.h"
#include "webfuse_provider/impl/request.h"
#include "webfuse_provider/impl/util/slist.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_client_config;
struct lws_protocols;
struct lws_context;
struct wfp_jsonrpc_proxy;
struct wfp_timer_manager;
struct wfp_client_protocol
{
bool is_connected;
bool is_shutdown_requested;
struct wfp_request request;
struct wfp_provider provider;
void * user_data;
struct lws * wsi;
struct wfp_timer_manager * timer_manager;
struct wfp_jsonrpc_proxy * proxy;
struct wfp_slist messages;
};
extern void wfp_impl_client_protocol_init(
struct wfp_client_protocol * protocol,
struct wfp_provider const * provider,
void * user_data);
extern void wfp_impl_client_protocol_cleanup(
struct wfp_client_protocol * protocol);
extern struct wfp_client_protocol * wfp_impl_client_protocol_create(
struct wfp_client_config const * config);
extern void wfp_impl_client_protocol_dispose(
struct wfp_client_protocol * protocol);
extern void wfp_impl_client_protocol_init_lws(
struct wfp_client_protocol * protocol,
struct lws_protocols * lws_protocol);
extern void wfp_impl_client_protocol_connect(
struct wfp_client_protocol * protocol,
struct lws_context * context,
char const * url);
extern void wfp_impl_client_protocol_disconnect(
struct wfp_client_protocol * protocol);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,46 @@
#include "webfuse_provider/impl/credentials.h"
#include <stdlib.h>
#include <string.h>
void wfp_impl_credentials_init(
struct wfp_credentials * credentials)
{
credentials->type = NULL;
credentials->contents = json_object();
}
void wfp_impl_credentials_cleanup(
struct wfp_credentials * credentials)
{
free(credentials->type);
json_decref(credentials->contents);
}
void wfp_impl_credentials_set_type(
struct wfp_credentials * credentials,
char const * type)
{
free(credentials->type);
credentials->type = strdup(type);
}
void wfp_impl_credentials_add(
struct wfp_credentials * credentials,
char const * key,
char const * value)
{
json_object_set_new(credentials->contents, key, json_string(value));
}
char const * wfp_impl_credentials_get_type(
struct wfp_credentials * credentials)
{
return credentials->type;
}
json_t * wfp_impl_credentials_get(
struct wfp_credentials * credentials)
{
return credentials->contents;
}

@ -0,0 +1,43 @@
#ifndef WFP_IMPL_CREDENTIALS_H
#define WFP_IMPL_CREDENTIALS_H
#include "webfuse_provider/credentials.h"
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_credentials
{
char * type;
json_t * contents;
};
extern void wfp_impl_credentials_init(
struct wfp_credentials * credentials);
extern void wfp_impl_credentials_cleanup(
struct wfp_credentials * credentials);
extern void wfp_impl_credentials_set_type(
struct wfp_credentials * credentials,
char const * type);
extern void wfp_impl_credentials_add(
struct wfp_credentials * credentials,
char const * key,
char const * value);
extern char const * wfp_impl_credentials_get_type(
struct wfp_credentials * credentials);
extern json_t * wfp_impl_credentials_get(
struct wfp_credentials * credentials);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,42 @@
#include "webfuse_provider/impl/dirbuffer.h"
#include <stdlib.h>
struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void)
{
struct wfp_dirbuffer * buffer = malloc(sizeof(struct wfp_dirbuffer));
buffer->entries = json_array();
return buffer;
}
void wfp_impl_dirbuffer_dispose(
struct wfp_dirbuffer * buffer)
{
if (NULL != buffer->entries)
{
json_decref(buffer->entries);
}
free(buffer);
}
void wfp_impl_dirbuffer_add(
struct wfp_dirbuffer * buffer,
char const * name,
ino_t inode)
{
json_t * entry = json_object();
json_object_set_new(entry, "name", json_string(name));
json_object_set_new(entry, "inode", json_integer(inode));
json_array_append_new(buffer->entries, entry);
}
json_t * wfp_impl_dirbuffer_take(
struct wfp_dirbuffer * buffer)
{
json_t * entries = buffer->entries;
buffer->entries = NULL;
return entries;
}

@ -0,0 +1,37 @@
#ifndef WFP_IMPL_DIRBUFFER_H
#define WFP_IMPL_DIRBUFFER_H
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_dirbuffer
{
json_t * entries;
};
extern struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void);
extern void wfp_impl_dirbuffer_dispose(
struct wfp_dirbuffer * buffer);
extern void wfp_impl_dirbuffer_add(
struct wfp_dirbuffer * buffer,
char const * name,
ino_t inode);
extern json_t * wfp_impl_dirbuffer_take(
struct wfp_dirbuffer * buffer);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,27 @@
#include "webfuse_provider/impl/jsonrpc/error.h"
json_t *
wfp_jsonrpc_error(
int code,
char const * message)
{
json_t * error = json_object();
json_object_set_new(error, "code", json_integer(code));
json_object_set_new(error, "message", json_string(message));
return error;
}
void
wfp_jsonrpc_propate_error(
wfp_jsonrpc_proxy_finished_fn * finised,
void * user_data,
int code,
char const * message)
{
json_t * error = wfp_jsonrpc_error(code, message);
finised(user_data, NULL, error);
json_decref(error);
}

@ -0,0 +1,29 @@
#ifndef WFP_JSONRPC_ERROR_H
#define WFP_JSONRPC_ERROR_H
#include <jansson.h>
#include "webfuse_provider/impl/jsonrpc/proxy_finished_fn.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern json_t *
wfp_jsonrpc_error(
int code,
char const * message);
extern void
wfp_jsonrpc_propate_error(
wfp_jsonrpc_proxy_finished_fn * finised,
void * user_data,
int code,
char const * message);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,216 @@
#include "webfuse_provider/impl/jsonrpc/proxy_intern.h"
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
#include "webfuse_provider/impl/jsonrpc/error.h"
#include "webfuse_provider/status.h"
#include "webfuse_provider/impl/timer/timer.h"
#include <stdlib.h>
#include <string.h>
struct wfp_jsonrpc_proxy *
wfp_jsonrpc_proxy_create(
struct wfp_timer_manager * manager,
int timeout,
wfp_jsonrpc_send_fn * send,
void * user_data)
{
struct wfp_jsonrpc_proxy * proxy = malloc(sizeof(struct wfp_jsonrpc_proxy));
wfp_jsonrpc_proxy_init(proxy, manager, timeout, send, user_data);
return proxy;
}
void wfp_jsonrpc_proxy_dispose(
struct wfp_jsonrpc_proxy * proxy)
{
wfp_jsonrpc_proxy_cleanup(proxy);
free(proxy);
}
static void wfp_jsonrpc_proxy_on_timeout(
struct wfp_timer * timer, void * proxy_ptr)
{
struct wfp_jsonrpc_proxy * proxy = proxy_ptr;
if (proxy->request.is_pending)
{
wfp_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
void * user_data = proxy->request.user_data;
proxy->request.is_pending = false;
proxy->request.id = 0;
proxy->request.user_data = NULL;
proxy->request.finished = NULL;
wfp_timer_cancel(timer);
wfp_jsonrpc_propate_error(finished, user_data, WFP_BAD_TIMEOUT, "Timeout");
}
}
static json_t * wfp_jsonrpc_request_create(
char const * method,
int id,
char const * param_info,
va_list args)
{
json_t * request = json_object();
json_object_set_new(request, "method", json_string(method));
json_t * params = json_array();
for (char const * param_type = param_info; '\0' != *param_type; param_type++)
{
switch(*param_type)
{
case 's':
{
char const * const value = va_arg(args, char const *);
json_array_append_new(params, json_string(value));
}
break;
case 'i':
{
int const value = va_arg(args, int);
json_array_append_new(params, json_integer(value));
}
break;
case 'j':
{
json_t * const value = va_arg(args, json_t *);
json_array_append_new(params, value);
}
break;
default:
fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type);
json_decref(params);
json_decref(request);
return NULL;
}
}
json_object_set_new(request, "params", params);
if (0 != id)
{
json_object_set_new(request, "id", json_integer(id));
}
return request;
}
void wfp_jsonrpc_proxy_init(
struct wfp_jsonrpc_proxy * proxy,
struct wfp_timer_manager * timeout_manager,
int timeout,
wfp_jsonrpc_send_fn * send,
void * user_data)
{
proxy->send = send;
proxy->timeout = timeout;
proxy->user_data = user_data;
proxy->request.is_pending = false;
proxy->request.timer = wfp_timer_create(timeout_manager,
&wfp_jsonrpc_proxy_on_timeout, proxy);
}
void wfp_jsonrpc_proxy_cleanup(
struct wfp_jsonrpc_proxy * proxy)
{
if (proxy->request.is_pending)
{
void * user_data = proxy->request.user_data;
wfp_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
proxy->request.is_pending = false;
proxy->request.finished = NULL;
proxy->request.user_data = NULL;
proxy->request.id = 0;
wfp_timer_cancel(proxy->request.timer);
wfp_jsonrpc_propate_error(finished, user_data, WFP_BAD, "Bad: cancelled pending request during shutdown");
}
wfp_timer_dispose(proxy->request.timer);
}
void wfp_jsonrpc_proxy_vinvoke(
struct wfp_jsonrpc_proxy * proxy,
wfp_jsonrpc_proxy_finished_fn * finished,
void * user_data,
char const * method_name,
char const * param_info,
va_list args)
{
if (!proxy->request.is_pending)
{
proxy->request.is_pending = true;
proxy->request.finished = finished;
proxy->request.user_data = user_data;
proxy->request.id = 42;
wfp_timer_start(proxy->request.timer, proxy->timeout);
json_t * request = wfp_jsonrpc_request_create(method_name, proxy->request.id, param_info, args);
bool const is_send = ((NULL != request) && (proxy->send(request, proxy->user_data)));
if (!is_send)
{
proxy->request.is_pending = false;
proxy->request.finished = NULL;
proxy->request.user_data = NULL;
proxy->request.id = 0;
wfp_timer_cancel(proxy->request.timer);
wfp_jsonrpc_propate_error(finished, user_data, WFP_BAD, "Bad: requenst is not sent");
}
if (NULL != request)
{
json_decref(request);
}
}
else
{
wfp_jsonrpc_propate_error(finished, user_data, WFP_BAD_BUSY, "Busy");
}
}
extern void wfp_jsonrpc_proxy_vnotify(
struct wfp_jsonrpc_proxy * proxy,
char const * method_name,
char const * param_info,
va_list args)
{
json_t * request = wfp_jsonrpc_request_create(method_name, 0, param_info, args);
if (NULL != request)
{
proxy->send(request, proxy->user_data);
json_decref(request);
}
}
void wfp_jsonrpc_proxy_onresult(
struct wfp_jsonrpc_proxy * proxy,
json_t * message)
{
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, message);
if ((proxy->request.is_pending) && (response.id == proxy->request.id))
{
wfp_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
void * user_data = proxy->request.user_data;
proxy->request.is_pending = false;
proxy->request.id = 0;
proxy->request.user_data = NULL;
proxy->request.finished = NULL;
wfp_timer_cancel(proxy->request.timer);
finished(user_data, response.result, response.error);
}
wfp_jsonrpc_response_cleanup(&response);
}

@ -0,0 +1,74 @@
#ifndef WFP_JSONRPC_PROXY_H
#define WFP_JSONRPC_PROXY_H
#ifndef __cplusplus
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#else
#include <cstdarg>
#include <cstddef>
using std::size_t;
#endif
#include <jansson.h>
#include "webfuse_provider/impl/jsonrpc/send_fn.h"
#include "webfuse_provider/impl/jsonrpc/proxy_finished_fn.h"
#ifdef __cplusplus
extern "C" {
#endif
struct wfp_jsonrpc_proxy;
struct wfp_timer_manager;
extern struct wfp_jsonrpc_proxy *
wfp_jsonrpc_proxy_create(
struct wfp_timer_manager * manager,
int timeout,
wfp_jsonrpc_send_fn * send,
void * user_data);
extern void wfp_jsonrpc_proxy_dispose(
struct wfp_jsonrpc_proxy * proxy);
//------------------------------------------------------------------------------
/// \brief Invokes a method.
///
/// Creates a method an sends it using the send function.
/// Proxy keeps track of method invokation. If no response is returned within
/// timeout, an error is propagated.
///
/// \param proxy pointer to proxy instance
/// \param finished function which is called exactly once, either on success or
/// on failure.
/// \param method_name name of the method to invoke
/// \param param_info types of the param (s = string, i = integer, j = json)
/// \param ... params
//------------------------------------------------------------------------------
extern void wfp_jsonrpc_proxy_invoke(
struct wfp_jsonrpc_proxy * proxy,
wfp_jsonrpc_proxy_finished_fn * finished,
void * user_data,
char const * method_name,
char const * param_info,
...
);
extern void wfp_jsonrpc_proxy_notify(
struct wfp_jsonrpc_proxy * proxy,
char const * method_name,
char const * param_info,
...
);
extern void wfp_jsonrpc_proxy_onresult(
struct wfp_jsonrpc_proxy * proxy,
json_t * message);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,20 @@
#ifndef WFP_JSONRPC_PROXY_FINISHED_FN_H
#define WFP_JSONRPC_PROXY_FINISHED_FN_H
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef void wfp_jsonrpc_proxy_finished_fn(
void * user_data,
json_t const * result,
json_t const * error);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,62 @@
#ifndef WFP_JSONRPC_PROXY_INTERN_H
#define WFP_JSONRPC_PROXY_INTERN_H
#include "webfuse_provider/impl/jsonrpc/proxy.h"
#include "webfuse_provider/impl/jsonrpc/proxy_finished_fn.h"
#include "webfuse_provider/impl/jsonrpc/send_fn.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_timer;
struct wfp_jsonrpc_request
{
bool is_pending;
wfp_jsonrpc_proxy_finished_fn * finished;
void * user_data;
int id;
struct wfp_timer * timer;
};
struct wfp_jsonrpc_proxy
{
struct wfp_jsonrpc_request request;
int timeout;
wfp_jsonrpc_send_fn * send;
void * user_data;
};
extern void
wfp_jsonrpc_proxy_init(
struct wfp_jsonrpc_proxy * proxy,
struct wfp_timer_manager * manager,
int timeout,
wfp_jsonrpc_send_fn * send,
void * user_data);
extern void
wfp_jsonrpc_proxy_cleanup(
struct wfp_jsonrpc_proxy * proxy);
extern void wfp_jsonrpc_proxy_vinvoke(
struct wfp_jsonrpc_proxy * proxy,
wfp_jsonrpc_proxy_finished_fn * finished,
void * user_data,
char const * method_name,
char const * param_info,
va_list args);
extern void wfp_jsonrpc_proxy_vnotify(
struct wfp_jsonrpc_proxy * proxy,
char const * method_name,
char const * param_info,
va_list args);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,28 @@
#include "webfuse_provider/impl/jsonrpc/proxy_intern.h"
void wfp_jsonrpc_proxy_invoke(
struct wfp_jsonrpc_proxy * proxy,
wfp_jsonrpc_proxy_finished_fn * finished,
void * user_data,
char const * method_name,
char const * param_info,
...)
{
va_list args;
va_start(args, param_info);
wfp_jsonrpc_proxy_vinvoke(proxy, finished, user_data, method_name, param_info, args);
va_end(args);
}
extern void wfp_jsonrpc_proxy_notify(
struct wfp_jsonrpc_proxy * proxy,
char const * method_name,
char const * param_info,
...
)
{
va_list args;
va_start(args, param_info);
wfp_jsonrpc_proxy_vnotify(proxy, method_name, param_info, args);
va_end(args);
}

@ -0,0 +1,81 @@
#include "webfuse_provider/impl/jsonrpc/request.h"
#include "webfuse_provider/impl/jsonrpc/error.h"
#include <stdlib.h>
struct wfp_jsonrpc_request
{
int id;
wfp_jsonrpc_send_fn * send;
void * user_data;
};
bool
wfp_jsonrpc_is_request(
json_t * message)
{
json_t * id = json_object_get(message, "id");
json_t * method = json_object_get(message, "method");
json_t * params = json_object_get(message, "params");
return (json_is_integer(id) && json_is_string(method) &&
(json_is_array(params) || json_is_object(params)));
}
struct wfp_jsonrpc_request *
wfp_jsonrpc_request_create(
int id,
wfp_jsonrpc_send_fn * send,
void * user_data)
{
struct wfp_jsonrpc_request * request = malloc(sizeof(struct wfp_jsonrpc_request));
request->id = id;
request->send = send;
request->user_data = user_data;
return request;
}
void
wfp_jsonrpc_request_dispose(
struct wfp_jsonrpc_request * request)
{
free(request);
}
void *
wfp_jsonrpc_request_get_userdata(
struct wfp_jsonrpc_request * request)
{
return request->user_data;
}
void
wfp_jsonrpc_respond(
struct wfp_jsonrpc_request * request,
json_t * result)
{
json_t * response = json_object();
json_object_set_new(response, "result", result);
json_object_set_new(response, "id", json_integer(request->id));
request->send(response, request->user_data);
json_decref(response);
wfp_jsonrpc_request_dispose(request);
}
void wfp_jsonrpc_respond_error(
struct wfp_jsonrpc_request * request,
int code,
char const * message)
{
json_t * response = json_object();
json_object_set_new(response, "error", wfp_jsonrpc_error(code, message));
json_object_set_new(response, "id", json_integer(request->id));
request->send(response, request->user_data);
json_decref(response);
wfp_jsonrpc_request_dispose(request);
}

@ -0,0 +1,53 @@
#ifndef WFP_JSONRPC_REQUEST_H
#define WFP_JSONRPC_REQUEST_H
#ifndef __cplusplus
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#else
#include <cstdarg>
#include <cstddef>
using std::size_t;
#endif
#include <jansson.h>
#include "webfuse_provider/impl/jsonrpc/send_fn.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_jsonrpc_request;
extern bool wfp_jsonrpc_is_request(
json_t * message);
extern struct wfp_jsonrpc_request *
wfp_jsonrpc_request_create(
int id,
wfp_jsonrpc_send_fn * send,
void * user_data);
extern void wfp_jsonrpc_request_dispose(
struct wfp_jsonrpc_request * request);
extern void * wfp_jsonrpc_request_get_userdata(
struct wfp_jsonrpc_request * request);
extern void wfp_jsonrpc_respond(
struct wfp_jsonrpc_request * request,
json_t * result);
extern void wfp_jsonrpc_respond_error(
struct wfp_jsonrpc_request * request,
int code,
char const * message);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,68 @@
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
#include "webfuse_provider/impl/jsonrpc/error.h"
#include "webfuse_provider/status.h"
bool
wfp_jsonrpc_is_response(
json_t * message)
{
json_t * id = json_object_get(message, "id");
json_t * err = json_object_get(message, "error");
json_t * result = json_object_get(message, "result");
return (json_is_integer(id) &&
(json_is_object(err) || (NULL != result)));
}
void
wfp_jsonrpc_response_init(
struct wfp_jsonrpc_response * result,
json_t * response)
{
result->id = -1;
result->result = NULL;
result->error = NULL;
json_t * id_holder = json_object_get(response, "id");
if (!json_is_integer(id_holder))
{
result->error = wfp_jsonrpc_error(WFP_BAD_FORMAT, "invalid format: missing id");
return;
}
result->id = json_integer_value(id_holder);
result->result = json_object_get(response, "result");
if (NULL != result->result)
{
json_incref(result->result);
}
else
{
json_t * error = json_object_get(response, "error");
if ((json_is_object(error)) && (json_is_integer(json_object_get(error, "code"))))
{
result->error = error;
json_incref(result->error);
}
else
{
result->error = wfp_jsonrpc_error(WFP_BAD_FORMAT, "invalid format: invalid error object");
}
}
}
void
wfp_jsonrpc_response_cleanup(
struct wfp_jsonrpc_response * response)
{
if (NULL != response->result)
{
json_decref(response->result);
}
if (NULL != response->error)
{
json_decref(response->error);
}
}

@ -0,0 +1,22 @@
#ifndef WFP_JSONRPC_RESPONSE_H
#define WFP_JSONRPC_RESPONSE_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
extern bool wfp_jsonrpc_is_response(
json_t * message);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,36 @@
#ifndef WFP_JSONRPC_RESPONSE_INTERN_H
#define WFP_JSONRPC_RESPONSE_INTERN_H
#include "webfuse_provider/impl/jsonrpc/response.h"
#ifndef __cplusplus
#include <stddef.h>
#else
#include <cstddef>
using std::size_t;
#endif
#ifdef __cplusplus
extern "C" {
#endif
struct wfp_jsonrpc_response
{
json_t * result;
json_t * error;
int id;
};
extern void wfp_jsonrpc_response_init(
struct wfp_jsonrpc_response * response,
json_t * message);
extern void wfp_jsonrpc_response_cleanup(
struct wfp_jsonrpc_response * response);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,23 @@
#ifndef WFP_JSONRPC_SEND_FN_H
#define WFP_JSONRPC_SEND_FN_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef bool wfp_jsonrpc_send_fn(
json_t * request,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,24 @@
#include "webfuse_provider/impl/message.h"
#include <stdlib.h>
#include <libwebsockets.h>
extern struct wfp_message * wfp_message_create(json_t const * value)
{
size_t const length = json_dumpb(value, NULL, 0, JSON_COMPACT);
char * data = malloc(sizeof(struct wfp_message) + LWS_PRE + length);
struct wfp_message * message = (struct wfp_message *) data;
message->data = &data[sizeof(struct wfp_message) + LWS_PRE];
message->length = length;
json_dumpb(value, message->data, length, JSON_COMPACT);
return message;
}
void wfp_message_dispose(
struct wfp_message * message)
{
free(message);
}

@ -0,0 +1,36 @@
#ifndef WFP_MESSAGE_H
#define WFP_MESSAGE_H
#ifndef __cplusplus
#include <stddef.h>
#else
#include <cstddef>
using std::size_t;
#endif
#include <jansson.h>
#include "webfuse_provider/impl/util/slist.h"
struct wfp_message
{
struct wfp_slist_item item;
char * data;
size_t length;
};
#ifdef __cplusplus
extern "C"
{
#endif
extern struct wfp_message * wfp_message_create(
json_t const * value);
extern void wfp_message_dispose(
struct wfp_message * message);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,17 @@
#include "webfuse_provider/impl/message_queue.h"
#include "webfuse_provider/impl/message.h"
#include "webfuse_provider/impl/util/container_of.h"
void wfp_message_queue_cleanup(
struct wfp_slist * queue)
{
struct wfp_slist_item * item = wfp_slist_first(queue);
while (NULL != item)
{
struct wfp_slist_item * next = item->next;
struct wfp_message * message = wfp_container_of(item, struct wfp_message, item);
wfp_message_dispose(message);
item = next;
}
wfp_slist_init(queue);
}

@ -0,0 +1,19 @@
#ifndef WFP_MESSAGE_QUEUE_H
#define WFP_MESSAGE_QUEUE_H
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_slist;
extern void wfp_message_queue_cleanup(
struct wfp_slist * queue);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,38 @@
#include "webfuse_provider/impl/operation/close.h"
#include <limits.h>
#include "webfuse_provider/impl/util/util.h"
void wfp_impl_close(
struct wfp_impl_invokation_context * context,
json_t * params,
int WFP_UNUSED_PARAM(id))
{
size_t const param_count = json_array_size(params);
if (4 == param_count)
{
json_t * inode_holder = json_array_get(params, 1);
json_t * handle_holder = json_array_get(params, 2);
json_t * flags_holder = json_array_get(params, 3);
if (json_is_integer(inode_holder) &&
json_is_integer(handle_holder) &&
json_is_integer(flags_holder))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
uint32_t handle = (uint32_t) (json_integer_value(handle_holder) & UINT32_MAX);
int flags = json_integer_value(flags_holder);
context->provider->close(inode, handle, flags, context->user_data);
}
}
}
void wfp_impl_close_default(
ino_t WFP_UNUSED_PARAM(inode),
uint32_t WFP_UNUSED_PARAM(handle),
int WFP_UNUSED_PARAM(flags),
void * WFP_UNUSED_PARAM(user_data))
{
// empty
}

@ -0,0 +1,26 @@
#ifndef WFP_IMPL_OPERATION_CLOSE_H
#define WFP_IMPL_OPERATION_CLOSE_H
#include "webfuse_provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_close(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_close_default(
ino_t inode,
uint32_t handle,
int flags,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,22 @@
#ifndef WFP_OPERATION_IMPL_ERROR_H
#define WFP_OPERATION_IMPL_ERROR_H
#include "webfuse_provider/api.h"
#include "webfuse_provider/status.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_request;
extern WFP_API void wfp_impl_respond_error(
struct wfp_request * request,
wfp_status status);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,64 @@
#include "webfuse_provider/impl/operation/getattr.h"
#include <stdbool.h>
#include "webfuse_provider/impl/operation/error.h"
#include "webfuse_provider/impl/request.h"
#include "webfuse_provider/impl/util/util.h"
void wfp_impl_getattr(
struct wfp_impl_invokation_context * context,
json_t * params,
int id)
{
size_t const count = json_array_size(params);
if (2 == count)
{
json_t * inode_holder = json_array_get(params, 1);
if (json_is_integer(inode_holder))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
struct wfp_request * request = wfp_impl_request_create(context->request, id);
context->provider->getattr(request, inode, context->user_data);
}
}
}
void wfp_impl_getattr_default(
struct wfp_request * request,
ino_t WFP_UNUSED_PARAM(inode),
void * WFP_UNUSED_PARAM(user_data))
{
wfp_impl_respond_error(request, WFP_BAD_NOENTRY);
}
void wfp_impl_respond_getattr(
struct wfp_request * request,
struct stat const * stat)
{
bool const is_file = (0 != (stat->st_mode & S_IFREG));
bool const is_dir = (0 != (stat->st_mode & S_IFDIR));
json_t * result = json_object();
json_object_set_new(result, "inode", json_integer(stat->st_ino));
json_object_set_new(result, "mode", json_integer(stat->st_mode & 0777));
json_object_set_new(result, "atime", json_integer(stat->st_atime));
json_object_set_new(result, "mtime", json_integer(stat->st_mtime));
json_object_set_new(result, "ctime", json_integer(stat->st_ctime));
if (is_file)
{
json_object_set_new(result, "type", json_string("file"));
json_object_set_new(result, "size", json_integer(stat->st_size));
}
if (is_dir)
{
json_object_set_new(result, "type", json_string("dir"));
}
wfp_impl_respond(request, result);
}

@ -0,0 +1,29 @@
#ifndef WFP_IMPL_OPERATION_GETATTR_H
#define WFP_IMPL_OPERATION_GETATTR_H
#include "webfuse_provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_respond_getattr(
struct wfp_request * request,
struct stat const * stat);
extern void wfp_impl_getattr(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_getattr_default(
struct wfp_request * request,
ino_t inode,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,68 @@
#include "webfuse_provider/impl/operation/lookup.h"
#include <stdbool.h>
#include "webfuse_provider/impl/operation/error.h"
#include "webfuse_provider/impl/request.h"
#include "webfuse_provider/impl/util/util.h"
void wfp_impl_lookup(
struct wfp_impl_invokation_context * context,
json_t * params,
int id)
{
size_t const count = json_array_size(params);
if (3 == count)
{
json_t * inode_holder = json_array_get(params, 1);
json_t * name_holder = json_array_get(params, 2);
if (json_is_integer(inode_holder) &&
json_is_string(name_holder))
{
ino_t inode = json_integer_value(inode_holder);
char const * name = json_string_value(name_holder);
struct wfp_request * request = wfp_impl_request_create(context->request, id);
context->provider->lookup(request, inode, name, context->user_data);
}
}
}
void wfp_impl_respond_lookup(
struct wfp_request * request,
struct stat const * stat)
{
bool const is_file = (0 != (stat->st_mode & S_IFREG));
bool const is_dir = (0 != (stat->st_mode & S_IFDIR));
json_t * result = json_object();
json_object_set_new(result, "inode", json_integer(stat->st_ino));
json_object_set_new(result, "mode", json_integer(stat->st_mode & 0777));
json_object_set_new(result, "atime", json_integer(stat->st_atime));
json_object_set_new(result, "mtime", json_integer(stat->st_mtime));
json_object_set_new(result, "ctime", json_integer(stat->st_ctime));
if (is_file)
{
json_object_set_new(result, "type", json_string("file"));
json_object_set_new(result, "size", json_integer(stat->st_size));
}
if (is_dir)
{
json_object_set_new(result, "type", json_string("dir"));
}
wfp_impl_respond(request, result);
}
void wfp_impl_lookup_default(
struct wfp_request * request,
ino_t WFP_UNUSED_PARAM(parent),
char const * WFP_UNUSED_PARAM(name),
void * WFP_UNUSED_PARAM(user_data))
{
wfp_impl_respond_error(request, WFP_BAD_NOENTRY);
}

@ -0,0 +1,30 @@
#ifndef WFP_IMPL_OPERATION_LOOKUP_H
#define WFP_IMPL_OPERATION_LOOKUP_H
#include "webfuse_provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_respond_lookup(
struct wfp_request * request,
struct stat const * stat);
extern void wfp_impl_lookup(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_lookup_default(
struct wfp_request * request,
ino_t parent,
char const * name,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,47 @@
#include "webfuse_provider/impl/operation/open.h"
#include "webfuse_provider/impl/operation/error.h"
#include "webfuse_provider/impl/request.h"
#include "webfuse_provider/impl/util/util.h"
void wfp_impl_open(
struct wfp_impl_invokation_context * context,
json_t * params,
int id)
{
size_t const count = json_array_size(params);
if (3 == count)
{
json_t * inode_holder = json_array_get(params, 1);
json_t * flags_holder = json_array_get(params, 2);
if (json_is_integer(inode_holder) &&
json_is_integer(flags_holder))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
int flags = (ino_t) json_integer_value(flags_holder);
struct wfp_request * request = wfp_impl_request_create(context->request, id);
context->provider->open(request, inode, flags, context->user_data);
}
}
}
void wfp_impl_open_default(
struct wfp_request * request,
ino_t WFP_UNUSED_PARAM(inode),
int WFP_UNUSED_PARAM(flags),
void * WFP_UNUSED_PARAM(user_data))
{
wfp_impl_respond_error(request, WFP_BAD_NOENTRY);
}
void wfp_impl_respond_open(
struct wfp_request * request,
uint32_t handle)
{
json_t * result = json_object();
json_object_set_new(result, "handle", json_integer((int) handle));
wfp_impl_respond(request, result);
}

@ -0,0 +1,30 @@
#ifndef WFP_IMPL_OPERATION_OPEN_H
#define WFP_IMPL_OPERATION_OPEN_H
#include "webfuse_provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_respond_open(
struct wfp_request * request,
uint32_t handle);
extern void wfp_impl_open(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_open_default(
struct wfp_request * request,
ino_t inode,
int flags,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,78 @@
#include "webfuse_provider/impl/operation/read.h"
#include <stdlib.h>
#include "webfuse_provider/impl/operation/error.h"
#include "webfuse_provider/impl/request.h"
#include "webfuse_provider/impl/util/util.h"
#include "webfuse_provider/impl/util/base64.h"
void wfp_impl_read(
struct wfp_impl_invokation_context * context,
json_t * params,
int id)
{
size_t const count = json_array_size(params);
if (5 == count)
{
json_t * inode_holder = json_array_get(params, 1);
json_t * handle_holder = json_array_get(params, 2);
json_t * offset_holder = json_array_get(params, 3);
json_t * length_holder = json_array_get(params, 4);
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 wfp_request * request = wfp_impl_request_create(context->request, id);
context->provider->read(request, inode, handle, offset, length, context->user_data);
}
}
}
void wfp_impl_read_default(
struct wfp_request * request,
ino_t WFP_UNUSED_PARAM(inode),
uint32_t WFP_UNUSED_PARAM(handle),
size_t WFP_UNUSED_PARAM(offset),
size_t WFP_UNUSED_PARAM(length),
void * WFP_UNUSED_PARAM(user_data))
{
wfp_impl_respond_error(request, WFP_BAD_NOENTRY);
}
void wfp_impl_respond_read(
struct wfp_request * request,
char const * data,
size_t length)
{
if (0 < length)
{
size_t const size = wfp_impl_base64_encoded_size(length) + 1;
char * buffer = malloc(size);
wfp_impl_base64_encode((uint8_t const *) 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));
wfp_impl_respond(request, result);
free(buffer);
}
else
{
json_t * result = json_object();
json_object_set_new(result, "data", json_string(""));
json_object_set_new(result, "format", json_string("identity"));
json_object_set_new(result, "count", json_integer(0));
wfp_impl_respond(request, result);
}
}

@ -0,0 +1,33 @@
#ifndef WFP_IMPL_OPERATION_READ_H
#define WFP_IMPL_OPERATION_READ_H
#include "webfuse_provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_respond_read(
struct wfp_request * request,
char const * data,
size_t length);
extern void wfp_impl_read(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_read_default(
struct wfp_request * request,
ino_t inode,
uint32_t handle,
size_t offset,
size_t length,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,42 @@
#include "webfuse_provider/impl/operation/readdir.h"
#include "webfuse_provider/impl/operation/error.h"
#include "webfuse_provider/impl/dirbuffer.h"
#include "webfuse_provider/impl/request.h"
#include "webfuse_provider/impl/util/util.h"
void wfp_impl_readdir(
struct wfp_impl_invokation_context * context,
json_t * params,
int id)
{
size_t const count = json_array_size(params);
if (2 == count)
{
json_t * inode_holder = json_array_get(params, 1);
if (json_is_integer(inode_holder))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
struct wfp_request * request = wfp_impl_request_create(context->request, id);
context->provider->readdir(request, inode, context->user_data);
}
}
}
void wfp_impl_readdir_default(
struct wfp_request * request,
ino_t WFP_UNUSED_PARAM(directory),
void * WFP_UNUSED_PARAM(user_data))
{
wfp_impl_respond_error(request, WFP_BAD_NOENTRY);
}
void wfp_impl_respond_readdir(
struct wfp_request * request,
struct wfp_dirbuffer * dirbuffer)
{
json_t * result = wfp_impl_dirbuffer_take(dirbuffer);
wfp_impl_respond(request, result);
}

@ -0,0 +1,29 @@
#ifndef WFP_IMPL_OPERATION_READDIR_H
#define WFP_IMPL_OPERATION_READDIR_H
#include "webfuse_provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_respond_readdir(
struct wfp_request * request,
struct wfp_dirbuffer * dirbuffer);
extern void wfp_impl_readdir(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_readdir_default(
struct wfp_request * request,
ino_t directory,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,125 @@
#include "webfuse_provider/impl/provider.h"
#include <stdbool.h>
#include <string.h>
#include "webfuse_provider/impl/request.h"
#include "webfuse_provider/impl/operation/lookup.h"
#include "webfuse_provider/impl/operation/getattr.h"
#include "webfuse_provider/impl/operation/readdir.h"
#include "webfuse_provider/impl/operation/open.h"
#include "webfuse_provider/impl/operation/close.h"
#include "webfuse_provider/impl/operation/read.h"
typedef void wfp_impl_invoke_fn(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
struct wfp_impl_method
{
char const * name;
wfp_impl_invoke_fn * invoke;
bool is_notification;
};
static void wfp_impl_provider_invoke_method(
struct wfp_impl_invokation_context * context,
char const * method_name,
json_t * params,
int id)
{
static struct wfp_impl_method const methods[] =
{
{"lookup", &wfp_impl_lookup, false},
{"getattr", &wfp_impl_getattr, false},
{"readdir", &wfp_impl_readdir, false},
{"open", &wfp_impl_open, false},
{"close", &wfp_impl_close, true},
{"read", &wfp_impl_read, false}
};
static size_t const count = sizeof(methods) / sizeof(methods[0]);
for (size_t i = 0; i < count; i++)
{
struct wfp_impl_method const * method = &methods[i];
if (0 == strcmp(method_name, method->name))
{
if ((0 < id) || (method->is_notification))
{
method->invoke(context, params, id);
}
break;
}
}
}
void wfp_impl_provider_init(
struct wfp_provider * provider)
{
provider->lookup = &wfp_impl_lookup_default;
provider->getattr = &wfp_impl_getattr_default;
provider->readdir = &wfp_impl_readdir_default;
provider->open = &wfp_impl_open_default;
provider->close = &wfp_impl_close_default;
provider->read = &wfp_impl_read_default;
provider->connected = &wfp_impl_connected_default;
provider->disconnected = &wfp_impl_disconnected_default;
provider->get_credentials = NULL;
}
void wfp_impl_provider_init_from_prototype(
struct wfp_provider * provider,
struct wfp_provider const * prototype)
{
provider->lookup = prototype->lookup;
provider->getattr = prototype->getattr;
provider->readdir = prototype->readdir;
provider->open = prototype->open;
provider->close = prototype->close;
provider->read = prototype->read;
provider->connected = prototype->connected;
provider->disconnected = prototype->disconnected;
provider->get_credentials = prototype->get_credentials;
}
void wfp_impl_provider_invoke(
struct wfp_impl_invokation_context * context,
json_t * request)
{
json_t * method_holder = json_object_get(request, "method");
json_t * params = json_object_get(request, "params");
json_t * id_holder = json_object_get(request, "id");
if ((json_is_string(method_holder)) && (json_is_array(params)))
{
char const * method = json_string_value(method_holder);
int id = json_is_integer(id_holder) ? json_integer_value(id_holder) : 0;
wfp_impl_provider_invoke_method(context, method, params, id);
}
}
void wfp_impl_connected_default(
void * user_data)
{
(void) user_data;
// empty
}
void wfp_impl_disconnected_default(
void * user_data)
{
(void) user_data;
// empty
}
bool wfp_impl_provider_is_authentication_enabled(
struct wfp_provider * provider)
{
return (NULL != provider->get_credentials);
}

@ -0,0 +1,62 @@
#ifndef WFP_IMPL_PROVIDER_H
#define WFP_IMPL_PROVIDER_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include <jansson.h>
#include "webfuse_provider/client_config.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_provider
{
wfp_connected_fn * connected;
wfp_disconnected_fn * disconnected;
wfp_lookup_fn * lookup;
wfp_getattr_fn * getattr;
wfp_readdir_fn * readdir;
wfp_open_fn * open;
wfp_close_fn * close;
wfp_read_fn * read;
wfp_get_credentials_fn * get_credentials;
};
struct wfp_impl_invokation_context
{
struct wfp_provider const * provider;
void * user_data;
struct wfp_request * request;
};
extern void wfp_impl_provider_init(
struct wfp_provider * provider);
extern void wfp_impl_provider_init_from_prototype(
struct wfp_provider * provider,
struct wfp_provider const * prototype);
extern void wfp_impl_provider_invoke(
struct wfp_impl_invokation_context * context,
json_t * request);
extern bool wfp_impl_provider_is_authentication_enabled(
struct wfp_provider * provider);
extern void wfp_impl_connected_default(
void * user_data);
extern void wfp_impl_disconnected_default(
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,52 @@
#include "webfuse_provider/impl/request.h"
#include <stdlib.h>
#include "webfuse_provider/impl/operation/error.h"
struct wfp_request * wfp_impl_request_create(
struct wfp_request * prototype,
int id)
{
struct wfp_request * request = malloc(sizeof(struct wfp_request));
request->respond = prototype->respond;
request->user_data = prototype->user_data;
request->id = id;
return request;
}
void wfp_impl_request_dispose(
struct wfp_request * request)
{
free(request);
}
extern void wfp_impl_respond(
struct wfp_request * request,
json_t * result)
{
json_t * response = json_object();
json_object_set_new(response, "result", result);
json_object_set_new(response, "id", json_integer(request->id));
request->respond(response, request->user_data);
json_decref(response);
wfp_impl_request_dispose(request);
}
void wfp_impl_respond_error(
struct wfp_request * request,
wfp_status status)
{
json_t * response = json_object();
json_t * error = json_object();
json_object_set_new(error, "code", json_integer(status));
json_object_set_new(response, "error", error);
json_object_set_new(response, "id", json_integer(request->id));
request->respond(response, request->user_data);
json_decref(response);
wfp_impl_request_dispose(request);
}

@ -0,0 +1,43 @@
#ifndef WFP_IMPL_REQUEST_H
#define WFP_IMPL_REQUEST_H
#include <jansson.h>
#include "webfuse_provider/impl/provider.h"
#include "webfuse_provider/status.h"
#ifdef __cplusplus
extern "C"
{
#endif
typedef void wfp_impl_request_respond_fn(
json_t * response,
void * user_data);
struct wfp_request
{
wfp_impl_request_respond_fn * respond;
void * user_data;
int id;
};
extern void wfp_impl_respond_error(
struct wfp_request * request,
wfp_status status);
extern struct wfp_request * wfp_impl_request_create(
struct wfp_request * prototype,
int id);
extern void wfp_impl_request_dispose(
struct wfp_request * request);
extern void wfp_impl_respond(
struct wfp_request * request,
json_t * result);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,34 @@
#include "webfuse_provider/impl/status_intern.h"
#include <errno.h>
int wfp_status_to_rc(wfp_status status)
{
switch(status)
{
case WFP_GOOD: return 0;
case WFP_BAD_NOTIMPLEMENTED: return -ENOSYS;
case WFP_BAD_TIMEOUT: return -ETIMEDOUT;
case WFP_BAD_BUSY: return -ENOENT;
case WFP_BAD_FORMAT: return -ENOENT;
case WFP_BAD_NOENTRY: return -ENOENT;
case WFP_BAD_ACCESS_DENIED: return -EACCES;
default: return -ENOENT;
}
}
char const * wfp_status_tostring(wfp_status status)
{
switch(status)
{
case WFP_GOOD: return "Good";
case WFP_BAD: return "Bad";
case WFP_BAD_NOTIMPLEMENTED: return "Bad (not implemented)";
case WFP_BAD_TIMEOUT: return "Bad (timeout)";
case WFP_BAD_BUSY: return "Bad (busy)";
case WFP_BAD_FORMAT: return "Bad (format)";
case WFP_BAD_NOENTRY: return "Bad (no entry)";
case WFP_BAD_ACCESS_DENIED: return "Bad (access denied)";
default: return "Bad (unknown)";
}
}

@ -0,0 +1,19 @@
#ifndef WFP_STATUS_INTERN_H
#define WFP_STATUS_INTERN_H
#include "webfuse_provider/status.h"
#ifdef __cplusplus
extern "C" {
#endif
extern int wfp_status_to_rc(wfp_status status);
extern char const * wfp_status_tostring(wfp_status status);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,93 @@
#include "webfuse_provider/impl/timer/manager_intern.h"
#include "webfuse_provider/impl/timer/timer_intern.h"
#include "webfuse_provider/impl/timer/timepoint.h"
#include <stddef.h>
#include <stdlib.h>
struct wfp_timer_manager
{
struct wfp_timer * timers;
};
struct wfp_timer_manager *
wfp_timer_manager_create(void)
{
struct wfp_timer_manager * manager = malloc(sizeof(struct wfp_timer_manager));
manager->timers = NULL;
return manager;
}
void
wfp_timer_manager_dispose(
struct wfp_timer_manager * manager)
{
struct wfp_timer * timer = manager->timers;
while (NULL != timer)
{
struct wfp_timer * next = timer->next;
wfp_timer_trigger(timer);
timer = next;
}
free(manager);
}
void wfp_timer_manager_check(
struct wfp_timer_manager * manager)
{
struct wfp_timer * timer = manager->timers;
while (NULL != timer)
{
struct wfp_timer * next = timer->next;
if (wfp_timer_is_timeout(timer))
{
wfp_timer_manager_removetimer(manager, timer);
wfp_timer_trigger(timer);
}
timer = next;
}
}
void wfp_timer_manager_addtimer(
struct wfp_timer_manager * manager,
struct wfp_timer * timer)
{
if (NULL != manager->timers)
{
manager->timers->prev = timer;
}
timer->next = manager->timers;
timer->prev = NULL;
manager->timers = timer;
}
void wfp_timer_manager_removetimer(
struct wfp_timer_manager * manager,
struct wfp_timer * timer)
{
struct wfp_timer * prev = timer->prev;
struct wfp_timer * next = timer->next;
if (NULL != prev)
{
prev->next = next;
}
if (NULL != next)
{
next->prev = prev;
}
if (manager->timers == timer)
{
manager->timers = next;
}
}

@ -0,0 +1,27 @@
#ifndef WFP_TIMER_MANAGER_H
#define WFP_TIMER_MANAGER_H
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_timer_manager;
extern struct wfp_timer_manager *
wfp_timer_manager_create(void);
extern void
wfp_timer_manager_dispose(
struct wfp_timer_manager * manager);
extern void
wfp_timer_manager_check(
struct wfp_timer_manager * manager);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,26 @@
#ifndef WFP_TIMER_MANAGER_INTERN_H
#define WFP_TIMER_MANAGER_INTERN_H
#include "webfuse_provider/impl/timer/manager.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_timer;
extern void wfp_timer_manager_addtimer(
struct wfp_timer_manager * manager,
struct wfp_timer * timer);
extern void wfp_timer_manager_removetimer(
struct wfp_timer_manager * manager,
struct wfp_timer * timer);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,19 @@
#ifndef WFP_TIMER_ON_TIMER_FN_H
#define WFP_TIMER_ON_TIMER_FN_H
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_timer;
typedef void wfp_timer_on_timer_fn(
struct wfp_timer * timer,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,31 @@
#include "webfuse_provider/impl/timer/timepoint.h"
#include <time.h>
#define WFP_TIMER_MSEC_PER_SEC ((wfp_timer_timepoint) 1000)
#define WFP_TIMER_NSEC_PER_MSEC ((wfp_timer_timepoint) 1000 * 1000)
wfp_timer_timepoint wfp_timer_timepoint_now(void)
{
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
wfp_timer_timepoint const now = (tp.tv_sec * WFP_TIMER_MSEC_PER_SEC) + (tp.tv_nsec / WFP_TIMER_NSEC_PER_MSEC);
return now;
}
wfp_timer_timepoint wfp_timer_timepoint_in_msec(wfp_timer_timediff value)
{
wfp_timer_timepoint const now = wfp_timer_timepoint_now();
wfp_timer_timepoint result = now + ((wfp_timer_timepoint) value);
return result;
}
bool wfp_timer_timepoint_is_elapsed(wfp_timer_timepoint tp)
{
wfp_timer_timepoint const now = wfp_timer_timepoint_now();
wfp_timer_timediff const diff = (wfp_timer_timediff) (tp - now);
return (0 > diff);
}

@ -0,0 +1,31 @@
#ifndef WFP_TIMER_TIMEPOINT_H
#define WFP_TIMER_TIMEPOINT_H
#ifndef __cplusplus
#include <stdbool.h>
#include <inttypes.h>
#else
#include <cinttypes>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
typedef uint64_t wfp_timer_timepoint;
typedef int64_t wfp_timer_timediff;
extern wfp_timer_timepoint wfp_timer_timepoint_now(void);
extern wfp_timer_timepoint wfp_timer_timepoint_in_msec(
wfp_timer_timediff value);
extern bool wfp_timer_timepoint_is_elapsed(
wfp_timer_timepoint timepoint);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,67 @@
#include "webfuse_provider/impl/timer/timer_intern.h"
#include "webfuse_provider/impl/timer/manager_intern.h"
#include "webfuse_provider/impl/timer/timepoint.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
struct wfp_timer *
wfp_timer_create(
struct wfp_timer_manager * manager,
wfp_timer_on_timer_fn * on_timer,
void * user_data)
{
struct wfp_timer * timer = malloc(sizeof(struct wfp_timer));
timer->manager = manager;
timer->timeout = 0;
timer->on_timer = on_timer;
timer->user_data = user_data;
timer->prev = NULL;
timer->next = NULL;
return timer;
}
void
wfp_timer_dispose(
struct wfp_timer * timer)
{
free(timer);
}
void wfp_timer_start(
struct wfp_timer * timer,
int timeout_ms)
{
timer->timeout = wfp_timer_timepoint_in_msec(timeout_ms);
wfp_timer_manager_addtimer(timer->manager, timer);
}
void wfp_timer_cancel(
struct wfp_timer * timer)
{
wfp_timer_manager_removetimer(timer->manager, timer);
timer->timeout = 0;
}
bool wfp_timer_is_timeout(
struct wfp_timer * timer)
{
return wfp_timer_timepoint_is_elapsed(timer->timeout);
}
void wfp_timer_trigger(
struct wfp_timer * timer)
{
if (0 != timer->on_timer)
{
timer->prev = NULL;
timer->next = NULL;
timer->on_timer(timer, timer->user_data);
}
}

@ -0,0 +1,37 @@
#ifndef WFP_TIMER_TIMER_H
#define WFP_TIMER_TIMER_H
#include "webfuse_provider/impl/timer/on_timer_fn.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_timer;
struct wfp_timer_manager;
extern struct wfp_timer *
wfp_timer_create(
struct wfp_timer_manager * manager,
wfp_timer_on_timer_fn * on_timer,
void * user_data);
extern void
wfp_timer_dispose(
struct wfp_timer * timer);
extern void
wfp_timer_start(
struct wfp_timer * timer,
int timeout_ms);
extern void
wfp_timer_cancel(
struct wfp_timer * timer);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,40 @@
#ifndef WFP_TIMER_TIMER_H
#define WFP_TIMER_TIMER_H
#include "webfuse_provider/impl/timer/timer.h"
#include "webfuse_provider/impl/timer/on_timer_fn.h"
#include "webfuse_provider/impl/timer/timepoint.h"
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_timer
{
struct wfp_timer_manager * manager;
wfp_timer_timepoint timeout;
wfp_timer_on_timer_fn * on_timer;
void * user_data;
struct wfp_timer * next;
struct wfp_timer * prev;
};
extern bool wfp_timer_is_timeout(
struct wfp_timer * timer);
extern void wfp_timer_trigger(
struct wfp_timer * timer);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,183 @@
#include "webfuse_provider/impl/util/base64.h"
static const uint8_t wfp_impl_base64_decode_table[256] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 1
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 62, 0x80, 0x80, 0x80, 63, // 2
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0x80, 0x80, 0x80, 0, 0x80, 0x80, // 3
0x80, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 4
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0x80, 0x80, 0x80, 0x80, 0x80, // 5
0x80, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 6
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0x80, 0x80, 0x80, 0x80, 0x80, // 7
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 8
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 9
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // A
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // B
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // C
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // D
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // E
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // F
};
size_t wfp_impl_base64_encoded_size(size_t length)
{
return ((length + 2) / 3) * 4;
}
size_t wfp_impl_base64_encode(
uint8_t const * data,
size_t length,
char * buffer,
size_t buffer_size)
{
// 0 1 2 3 4 5 6
// 0123456789012345678901234567890123456789012345678901234567890123
static char const table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t const length_needed = wfp_impl_base64_encoded_size(length);
if (buffer_size < length_needed)
{
return 0;
}
size_t pos = 0;
size_t out_pos = 0;
for(; (length - pos) >= 3; pos += 3)
{
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos ] & 0x03) << 4) | (data[pos + 1] >> 4) ];
buffer[out_pos++] = table[ ((data[pos + 1] & 0x0f) << 2) | (data[pos + 2] >> 6) ];
buffer[out_pos++] = table[ data[pos + 2] & 0x3f ];
}
switch((length - pos))
{
case 1:
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos] & 0x03) << 4) ];
buffer[out_pos++] = '=';
buffer[out_pos++] = '=';
break;
case 2:
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos ] & 0x03) << 4) | (data[pos + 1] >> 4) ];
buffer[out_pos++] = table[ ((data[pos + 1] & 0x0f) << 2) ];
buffer[out_pos++] = '=';
break;
default:
break;
}
if (buffer_size > out_pos)
{
buffer[out_pos] = '\0';
}
return out_pos;
}
size_t wfp_impl_base64_decoded_size(char const * data, size_t length)
{
size_t result = 0;
if ((length > 0) && ((length % 4) == 0))
{
result = (length / 4) * 3;
if ('=' == data[length - 1])
{
result--;
if ('=' == data[length - 2])
{
result--;
}
}
}
return result;
}
size_t wfp_impl_base64_decode(
char const * data,
size_t length,
uint8_t * buffer,
size_t buffer_size)
{
uint8_t const * table = wfp_impl_base64_decode_table;
size_t needed_size = wfp_impl_base64_decoded_size(data, length);
if ((0 == needed_size) || (buffer_size < needed_size))
{
return 0;
}
size_t out_pos = 0;
size_t pos = 0;
for(; pos < length - 4; pos += 4)
{
uint8_t a = table[ (unsigned char) data[pos ] ];
uint8_t b = table[ (unsigned char) data[pos + 1] ];
uint8_t c = table[ (unsigned char) data[pos + 2] ];
uint8_t d = table[ (unsigned char) data[pos + 3] ];
buffer[out_pos++] = (a << 2) | (b >> 4);
buffer[out_pos++] = (b << 4) | (c >> 2);
buffer[out_pos++] = (c << 6) | d;
}
// decode last block
{
uint8_t a = table[ (unsigned char) data[pos ] ];
uint8_t b = table[ (unsigned char) data[pos + 1] ];
uint8_t c = table[ (unsigned char) data[pos + 2] ];
uint8_t d = table[ (unsigned char) data[pos + 3] ];
buffer[out_pos++] = (a << 2) | (b >> 4);
if ('=' != data[pos + 2])
{
buffer[out_pos++] = (b << 4) | (c >> 2);
if ('=' != data[pos + 3])
{
buffer[out_pos++] = (c << 6) | d;
}
}
}
return out_pos;
}
extern bool wfp_impl_base64_isvalid(char const * data, size_t length)
{
uint8_t const * table = wfp_impl_base64_decode_table;
if ((length == 0) || ((length % 4) != 0))
{
return false;
}
size_t pos = 0;
for(; pos < (length - 2); pos++)
{
unsigned char c = (unsigned char) data[pos];
if (('=' == c) || (0x80 == table[c]))
{
return false;
}
}
if (('=' == data[pos]) && ('=' != data[pos + 1]))
{
return false;
}
for(;pos < length; pos++)
{
char c = data[pos];
if (0x80 == table[ (unsigned char) c])
{
return false;
}
}
return true;
}

@ -0,0 +1,40 @@
#ifndef WFP_UTIL_BASE64_H
#define WFP_UTILBASE64_H
#ifndef __cplusplus
#include <inttypes.h>
#include <stddef.h>
#include <stdbool.h>
#else
#include <cinttypes>
#include <cstddef>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
extern size_t wfp_impl_base64_encoded_size(size_t length);
extern size_t wfp_impl_base64_encode(
uint8_t const * data,
size_t length,
char * buffer,
size_t buffer_size);
extern size_t wfp_impl_base64_decoded_size(char const * data, size_t length);
extern size_t wfp_impl_base64_decode(
char const * data,
size_t length,
uint8_t * buffer,
size_t buffer_size);
extern bool wfp_impl_base64_isvalid(char const * data, size_t length);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,21 @@
#ifndef WFP_UTIL_CONTAINER_OF_H
#define WFP_UTIL_CONTAINER_OF_H
#ifndef __cplusplus
#include <stddef.h>
#else
#include <cstddef>
#endif
#ifdef __GNUC__
#define wfp_container_of(pointer, type, member) \
({ \
const typeof( ((type *)0)->member ) * __member = (pointer); \
(type *)( (char *)__member - offsetof(type, member) ); \
})
#else
#define wfp_container_of(pointer, type, member) \
(type *) (((char *) pointer) - offsetof(type, member))
#endif
#endif

@ -0,0 +1,27 @@
#include "webfuse_provider/impl/util/json_util.h"
int wfp_impl_json_get_int(json_t const * object, char const * key, int default_value)
{
int result = default_value;
json_t * holder = json_object_get(object, key);
if (json_is_integer(holder))
{
result = json_integer_value(holder);
}
return result;
}
wfp_status
wfp_impl_jsonrpc_get_status(
json_t const * error)
{
wfp_status status = WFP_GOOD;
if (NULL != error)
{
status = wfp_impl_json_get_int(error, "code", WFP_BAD_FORMAT);
}
return status;
}

@ -0,0 +1,26 @@
#ifndef WFP_JSON_UTIL_H
#define WFP_JSON_UTIL_H
#include <jansson.h>
#include "webfuse_provider/status.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern int
wfp_impl_json_get_int(
json_t const * object,
char const * key,
int default_value);
extern wfp_status
wfp_impl_jsonrpc_get_status(
json_t const * error);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,18 @@
#include "webfuse_provider/impl/util/lws_log.h"
#include <stdbool.h>
#include <libwebsockets.h>
#define WFP_LWSLOG_DISABLE 0
static bool wfp_impl_lwslog_is_diabled = false;
void wfp_impl_lwslog_disable(void)
{
if (!wfp_impl_lwslog_is_diabled)
{
lws_set_log_level(WFP_LWSLOG_DISABLE, NULL);
wfp_impl_lwslog_is_diabled = true;
}
}

@ -0,0 +1,15 @@
#ifndef WFP_LWS_LOG_H
#define WFP_LWS_LOG_H
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_lwslog_disable(void);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,56 @@
#include "webfuse_provider/impl/util/slist.h"
#include <stddef.h>
void wfp_slist_init(
struct wfp_slist * list)
{
list->head.next = NULL;
list->last = &list->head;
}
bool wfp_slist_empty(
struct wfp_slist * list)
{
return (list->last == &list->head);
}
struct wfp_slist_item * wfp_slist_first(
struct wfp_slist * list)
{
return list->head.next;
}
void wfp_slist_append(
struct wfp_slist * list,
struct wfp_slist_item * item)
{
item->next = NULL;
list->last->next = item;
list->last = item;
}
struct wfp_slist_item * wfp_slist_remove_first(
struct wfp_slist * list)
{
return wfp_slist_remove_after(list, &list->head);
}
struct wfp_slist_item * wfp_slist_remove_after(
struct wfp_slist * list,
struct wfp_slist_item * prev)
{
struct wfp_slist_item * result = prev->next;
if (NULL != result)
{
prev->next = result->next;
if (list->last == result)
{
list->last = prev;
}
}
return result;
}

@ -0,0 +1,48 @@
#ifndef WFP_UTIL_SLIST_H
#define WFP_UTIL_SLIST_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_slist_item
{
struct wfp_slist_item * next;
};
struct wfp_slist
{
struct wfp_slist_item head;
struct wfp_slist_item * last;
};
extern void wfp_slist_init(
struct wfp_slist * list);
extern bool wfp_slist_empty(
struct wfp_slist * list);
extern struct wfp_slist_item * wfp_slist_first(
struct wfp_slist * list);
extern void wfp_slist_append(
struct wfp_slist * list,
struct wfp_slist_item * item);
extern struct wfp_slist_item * wfp_slist_remove_first(
struct wfp_slist * list);
extern struct wfp_slist_item * wfp_slist_remove_after(
struct wfp_slist * list,
struct wfp_slist_item * prev);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,125 @@
#include "webfuse_provider/impl/util/url.h"
#include <stdlib.h>
#include <string.h>
struct wfp_url_protocol
{
char const * name;
size_t name_length;
int default_port;
bool use_tls;
};
static bool wfp_url_readprotocol(
struct wfp_url * url,
char const * * data)
{
static struct wfp_url_protocol const known_protocols[] =
{
{"ws://", 5, 80, false},
{"wss://", 6, 443, true}
};
static size_t const count = (sizeof(known_protocols) / sizeof(known_protocols[0]));
bool found = false;
for(size_t i = 0; (!found) && (i < count); i++)
{
struct wfp_url_protocol const * protocol = &known_protocols[i];
if (0 == strncmp(*data, protocol->name, protocol->name_length))
{
url->port = protocol->default_port;
url->use_tls = protocol->use_tls;
*data = *data + protocol->name_length;
found = true;
}
}
return found;
}
static bool wfp_url_readhost(
struct wfp_url * url,
char const * * data)
{
char * end = strpbrk(*data, ":/");
bool const result = (NULL != end);
if (result)
{
size_t length = end - *data;
url->host = strndup(*data, length);
*data = end;
}
return result;
}
static bool wfp_url_readport(
struct wfp_url * url,
char const * * data)
{
bool result;
if (':' == **data)
{
*data = *data + 1;
char * end = strchr(*data, '/');
result = (NULL != end);
if (result)
{
url->port = atoi(*data);
*data = end;
}
}
else
{
result = ('/' == **data);
}
return result;
}
static bool wfp_url_readpath(
struct wfp_url * url,
char const * * data)
{
bool const result = ('/' == **data);
url->path = strdup(*data);
*data = NULL;
return result;
}
bool wfp_url_init(
struct wfp_url * url,
char const * value)
{
memset(url, 0, sizeof(struct wfp_url));
char const * data = value;
bool const result =
wfp_url_readprotocol(url, &data) &&
wfp_url_readhost(url, &data) &&
wfp_url_readport(url, &data) &&
wfp_url_readpath(url, &data)
;
if (!result)
{
wfp_url_cleanup(url);
}
return result;
}
void wfp_url_cleanup(
struct wfp_url * url)
{
free(url->host);
free(url->path);
memset(url, 0, sizeof(struct wfp_url));
}

@ -0,0 +1,32 @@
#ifndef WFP_UTIL_URL_H
#define WFP_UTIL_URL_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_url
{
char * host;
int port;
char * path;
bool use_tls;
};
extern bool wfp_url_init(
struct wfp_url * url,
char const * value);
extern void wfp_url_cleanup(
struct wfp_url * url);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,10 @@
#ifndef WFP_UTIL_H
#define WFP_UTIL_H
#ifdef __GNUC__
#define WFP_UNUSED_PARAM(param) param __attribute__((unused))
#else
#define WFP_UNUSED_PARAM(param)
#endif
#endif

@ -1,15 +1,181 @@
project('webfuse-provider', version: '0.1.0', license: 'LGPL-3.0+')
project('webfuse-provider', 'c', 'cpp', version: '0.4.0', license: 'LGPL-3.0+')
without_tests = get_option('without_tests')
without_examples = get_option('without_examples')
webfuse_provider_dep = dependency('webfuse_provider', version: '>=0.3.0',
fallback: ['webfuse', 'webfuse_provider_dep'], default_options: ['without_adapter=true'])
libwebsockets_dep = dependency('libwebsockets', version: '>=4.0.13', required: false)
if not libwebsockets_dep.found()
cmake = import('cmake')
libwebsockets = cmake.subproject('libwebsockets')
libwebsockets_dep = libwebsockets.dependency('websockets_shared')
endif
jansson_dep = dependency('jansson', version: '>=2.11', fallback: ['jansson', 'jansson_dep'])
pkg_config = import('pkgconfig')
# Webfuse provider
webfuse_provider_static = static_library('webfuse_provider',
'lib/webfuse_provider/impl/message.c',
'lib/webfuse_provider/impl/message_queue.c',
'lib/webfuse_provider/impl/status.c',
'lib/webfuse_provider/impl/util/slist.c',
'lib/webfuse_provider/impl/util/base64.c',
'lib/webfuse_provider/impl/util/lws_log.c',
'lib/webfuse_provider/impl/util/json_util.c',
'lib/webfuse_provider/impl/util/url.c',
'lib/webfuse_provider/impl/timer/manager.c',
'lib/webfuse_provider/impl/timer/timepoint.c',
'lib/webfuse_provider/impl/timer/timer.c',
'lib/webfuse_provider/impl/jsonrpc/proxy.c',
'lib/webfuse_provider/impl/jsonrpc/proxy_variadic.c',
'lib/webfuse_provider/impl/jsonrpc/request.c',
'lib/webfuse_provider/impl/jsonrpc/response.c',
'lib/webfuse_provider/impl/jsonrpc/error.c',
'lib/webfuse_provider/api.c',
'lib/webfuse_provider/impl/client.c',
'lib/webfuse_provider/impl/client_config.c',
'lib/webfuse_provider/impl/client_protocol.c',
'lib/webfuse_provider/impl/provider.c',
'lib/webfuse_provider/impl/request.c',
'lib/webfuse_provider/impl/dirbuffer.c',
'lib/webfuse_provider/impl/credentials.c',
'lib/webfuse_provider/impl/operation/lookup.c',
'lib/webfuse_provider/impl/operation/getattr.c',
'lib/webfuse_provider/impl/operation/readdir.c',
'lib/webfuse_provider/impl/operation/open.c',
'lib/webfuse_provider/impl/operation/close.c',
'lib/webfuse_provider/impl/operation/read.c',
c_args: ['-fvisibility=hidden'],
include_directories: ['include', 'lib'],
dependencies: [libwebsockets_dep, jansson_dep])
webfuse_provider_static_dep = declare_dependency(
include_directories: ['include'],
link_with: webfuse_provider_static,
dependencies: [libwebsockets_dep, jansson_dep])
webfuse_provider = shared_library('webfuse_provider',
'lib/webfuse_provider/api.c',
version: meson.project_version(),
c_args: ['-fvisibility=hidden', '-DWFP_API=WFP_EXPORT'],
include_directories: ['include', 'lib'],
dependencies: [webfuse_provider_static_dep, libwebsockets_dep, jansson_dep],
install: true)
webfuse_provider_dep = declare_dependency(
include_directories: ['include'],
link_with: [webfuse_provider],
dependencies: [libwebsockets_dep, jansson_dep])
install_subdir('include/webfuse_provider', install_dir: 'include')
pkg_config.generate(
libraries: [webfuse_provider],
requires: ['libwebsockets', 'jansson'],
subdirs: '.',
version: meson.project_version(),
name: 'libwebfuse_provider',
filebase: 'webfuse_provider',
description: 'Provider library for websockets filesystem')
# Unit Tests
if not without_tests
gtest_dep = dependency('gtest', version: '>=1.10.0', fallback: ['gtest', 'gtest_dep'])
gmock_main_dep = dependency('gmock_main', version: '>=1.10.0', fallback: ['gtest', 'gmock_main_dep'])
openssl = find_program('openssl')
test_server_certs = custom_target('test_server_certs',
output: ['server-key.pem', 'server-cert.pem'],
command: [openssl, 'req', '-x509', '-newkey', 'rsa:4096', '-keyout', 'server-key.pem', '-out', 'server-cert.pem', '-days', '365', '-nodes', '-batch', '-subj', '/CN=localhost'])
test_client_certs = custom_target('test_client_certs',
output: ['client-key.pem', 'client-cert.pem'],
command: [openssl, 'req', '-x509', '-newkey', 'rsa:4096', '-keyout', 'client-key.pem', '-out', 'client-cert.pem', '-days', '365', '-nodes', '-batch', '-subj', '/CN=localhost'])
test_certs_dep = declare_dependency(
sources: [test_server_certs, test_client_certs])
alltests = executable('alltests',
'test/webfuse_provider/test_util/timeout_watcher.cc',
'test/webfuse_provider/test_util/ws_server.cc',
'test/webfuse_provider/test_util/webfuse_server.cc',
'test/webfuse_provider/test_util/client.cc',
'test/webfuse_provider/test_util/jansson_test_environment.cc',
'test/webfuse_provider/mocks/fake_invokation_context.cc',
'test/webfuse_provider/mocks/mock_request.cc',
'test/webfuse_provider/mocks/mock_provider_client.cc',
'test/webfuse_provider/mocks/mock_provider.cc',
'test/webfuse_provider/mocks/mock_jsonrpc_proxy.cc',
'test/webfuse_provider/jsonrpc/mock_timer_callback.cc',
'test/webfuse_provider/jsonrpc/mock_timer.cc',
'test/webfuse_provider/jsonrpc/test_is_request.cc',
'test/webfuse_provider/jsonrpc/test_request.cc',
'test/webfuse_provider/jsonrpc/test_is_response.cc',
'test/webfuse_provider/jsonrpc/test_response.cc',
'test/webfuse_provider/jsonrpc/test_proxy.cc',
'test/webfuse_provider/jsonrpc/test_response_parser.cc',
'test/webfuse_provider/timer/test_timepoint.cc',
'test/webfuse_provider/timer/test_timer.cc',
'test/webfuse_provider/util/test_util.cc',
'test/webfuse_provider/util/test_container_of.cc',
'test/webfuse_provider/util/test_slist.cc',
'test/webfuse_provider/util/test_base64.cc',
'test/webfuse_provider/util/test_status.cc',
'test/webfuse_provider/util/test_message.cc',
'test/webfuse_provider/util/test_message_queue.cc',
'test/webfuse_provider/util/test_url.cc',
'test/webfuse_provider/provider/test_client.cc',
'test/webfuse_provider/provider/test_client_protocol.cc',
'test/webfuse_provider/provider/test_dirbuffer.cc',
'test/webfuse_provider/provider/operation/test_close.cc',
'test/webfuse_provider/provider/operation/test_getattr.cc',
'test/webfuse_provider/provider/operation/test_lookup.cc',
'test/webfuse_provider/provider/operation/test_open.cc',
'test/webfuse_provider/provider/operation/test_read.cc',
'test/webfuse_provider/provider/operation/test_readdir.cc',
link_args: [
'-Wl,--wrap=wfp_timer_manager_create',
'-Wl,--wrap=wfp_timer_manager_dispose',
'-Wl,--wrap=wfp_timer_manager_check',
'-Wl,--wrap=wfp_timer_create',
'-Wl,--wrap=wfp_timer_dispose',
'-Wl,--wrap=wfp_timer_start',
'-Wl,--wrap=wfp_timer_cancel',
'-Wl,--wrap=wfp_impl_operation_context_get_proxy',
'-Wl,--wrap=wfp_jsonrpc_proxy_vinvoke',
'-Wl,--wrap=wfp_jsonrpc_proxy_vnotify',
],
include_directories: ['include', 'lib', 'test'],
dependencies: [
webfuse_provider_static_dep,
libwebsockets_dep,
jansson_dep,
gtest_dep,
gmock_main_dep,
test_certs_dep
])
test('alltests', alltests)
endif
# Examples
if not without_examples
executable('webfuse-provider',
'src/main.c',
'examples/provider/main.c',
dependencies: [webfuse_provider_dep])
executable('static-filesystem-provider',
'src/static_filesystem/static_filesystem.c',
'src/static_filesystem/path.c',
'src/static_filesystem/main.c',
'examples/static_filesystem/static_filesystem.c',
'examples/static_filesystem/path.c',
'examples/static_filesystem/main.c',
dependencies: [webfuse_provider_dep])
endif

@ -0,0 +1,2 @@
option('without_tests', type: 'boolean', value: false, description: 'disable unit tests')
option('without_examples', type: 'boolean', value: false, description: 'disable examples')

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save