Skip to content

Commit

Permalink
fops: refactor around lws_fops_fd_t
Browse files Browse the repository at this point in the history
  • Loading branch information
lws-team committed Feb 25, 2017
1 parent 2e1dcc5 commit d909cb9
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 154 deletions.
26 changes: 19 additions & 7 deletions README.coding.md
Expand Up @@ -347,25 +347,37 @@ and then can use helpers to also leverage these platform-independent
file handling apis

```
static inline lws_filefd_type
`lws_plat_file_open`(struct lws *wsi, const char *filename, unsigned long *filelen, int flags)
static inline lws_fop_fd_t
`lws_plat_file_open`(struct lws_plat_file_ops *fops, const char *filename,
lws_filepos_t *filelen, lws_fop_flags_t *flags)
static inline int
`lws_plat_file_close`(struct lws *wsi, lws_filefd_type fd)
`lws_plat_file_close`(lws_fop_fd_t fop_fd)
static inline unsigned long
`lws_plat_file_seek_cur`(struct lws *wsi, lws_filefd_type fd, long offset_from_cur_pos)
`lws_plat_file_seek_cur`(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
static inline int
`lws_plat_file_read`(struct lws *wsi, lws_filefd_type fd, unsigned long *amount, unsigned char *buf, unsigned long len)
`lws_plat_file_read`(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
static inline int
`lws_plat_file_write`(struct lws *wsi, lws_filefd_type fd, unsigned long *amount, unsigned char *buf, unsigned long len)
`lws_plat_file_write`(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len )
```

The user code can also override or subclass the file operations, to either
wrap or replace them. An example is shown in test server.

### Changes from v2.1 and before fops

There are three changes:

1) Pre-2.2 fops directly used platform file descriptors. Current fops returns and accepts a wrapper type lws_fop_fd_t which is a pointer to a malloc'd struct containing information specific to the filesystem implementation.

2) Pre-2.2 fops bound the fops to a wsi. This is completely removed, you just give a pointer to the fops struct that applies to this file when you open it. Afterwards, the operations in the fops just need the lws_fop_fd_t returned from the open.

3) Everything is wrapped in typedefs. See lws-plat-unix.c for examples of how to implement.

@section ecdh ECDH Support

ECDH Certs are now supported. Enable the CMake option
Expand Down
2 changes: 1 addition & 1 deletion lib/http2.c
Expand Up @@ -476,7 +476,7 @@ int lws_http2_do_pps_send(struct lws_context *context, struct lws *wsi)
if (wsi->state == LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS) {
wsi->state = LWSS_HTTP2_ESTABLISHED;

wsi->u.http.fd = LWS_INVALID_FILE;
wsi->u.http.fop_fd = NULL;

if (lws_is_ssl(lws_http2_get_network_wsi(wsi))) {
lwsl_info("skipping nonexistent ssl upgrade headers\n");
Expand Down
6 changes: 3 additions & 3 deletions lib/libwebsockets.c
Expand Up @@ -229,9 +229,9 @@ lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason)
}

if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED &&
wsi->u.http.fd != LWS_INVALID_FILE) {
lws_plat_file_close(wsi, wsi->u.http.fd);
wsi->u.http.fd = LWS_INVALID_FILE;
wsi->u.http.fop_fd != NULL) {
lws_plat_file_close(wsi->u.http.fop_fd);
wsi->u.http.fop_fd = NULL;
wsi->vhost->protocols->callback(wsi,
LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
wsi->told_user_closed = 1;
Expand Down
74 changes: 44 additions & 30 deletions lib/libwebsockets.h
Expand Up @@ -4233,9 +4233,29 @@ lws_cgi_kill(struct lws *wsi);
#define LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP (1 << 24)
#define LWS_FOP_FLAG_COMPR_IS_GZIP (1 << 25)

struct lws_plat_file_ops;
struct lws_fop_fd {
lws_filefd_type fd;
struct lws_plat_file_ops *fops;
void *filesystem_priv;
};
#if defined(WIN32) || defined(_WIN32)
/* ... */
typedef SSIZE_T ssize_t;
/* !!! >:-[ */
typedef unsigned __int32 uint32_t;
typedef unsigned __int8 uint8_t;
#endif
typedef struct lws_fop_fd *lws_fop_fd_t;
typedef size_t lws_filepos_t;
typedef ssize_t lws_fileofs_t;
typedef uint32_t lws_fop_flags_t;

struct lws_plat_file_ops {
lws_filefd_type (*LWS_FOP_OPEN)(struct lws *wsi, const char *filename,
unsigned long *filelen, int *flags);
lws_fop_fd_t (*LWS_FOP_OPEN)(struct lws_plat_file_ops *fops,
const char *filename,
lws_filepos_t *filelen,
lws_fop_flags_t *flags);
/**< Open file (always binary access if plat supports it)
* filelen is filled on exit to be the length of the file
* *flags & LWS_FOP_FLAGS_MASK should be set to O_RDONLY or O_RDWR.
Expand All @@ -4244,18 +4264,16 @@ struct lws_plat_file_ops {
* gzip-compressed, then the open handler should OR
* LWS_FOP_FLAG_COMPR_IS_GZIP on to *flags before returning.
*/
int (*LWS_FOP_CLOSE)(struct lws *wsi, lws_filefd_type fd);
int (*LWS_FOP_CLOSE)(lws_fop_fd_t fop_fd);
/**< close file */
unsigned long (*LWS_FOP_SEEK_CUR)(struct lws *wsi, lws_filefd_type fd,
long offset_from_cur_pos);
lws_fileofs_t (*LWS_FOP_SEEK_CUR)(lws_fop_fd_t fop_fd,
lws_fileofs_t offset_from_cur_pos);
/**< seek from current position */
int (*LWS_FOP_READ)(struct lws *wsi, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf,
unsigned long len);
int (*LWS_FOP_READ)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len);
/**< Read from file, on exit *amount is set to amount actually read */
int (*LWS_FOP_WRITE)(struct lws *wsi, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf,
unsigned long len);
int (*LWS_FOP_WRITE)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len);
/**< Write to file, on exit *amount is set to amount actually written */

/* Add new things just above here ---^
Expand All @@ -4279,12 +4297,11 @@ lws_set_fops(struct lws_context *context, struct lws_plat_file_ops *fops);
* \param filelen: length of file (filled in by call)
* \param flags: open flags
*/
static LWS_INLINE lws_filefd_type LWS_WARN_UNUSED_RESULT
lws_plat_file_open(struct lws *wsi, const char *filename,
unsigned long *filelen, int *flags)
static LWS_INLINE lws_fop_fd_t LWS_WARN_UNUSED_RESULT
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
lws_filepos_t *filelen, lws_fop_flags_t *flags)
{
return lws_get_fops(lws_get_context(wsi))->LWS_FOP_OPEN(wsi, filename,
filelen, flags);
return fops->LWS_FOP_OPEN(fops, filename, filelen, flags);
}

/**
Expand All @@ -4294,9 +4311,9 @@ lws_plat_file_open(struct lws *wsi, const char *filename,
* \param fd: file descriptor
*/
static LWS_INLINE int
lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
lws_plat_file_close(lws_fop_fd_t fop_fd)
{
return lws_get_fops(lws_get_context(wsi))->LWS_FOP_CLOSE(wsi, fd);
return fop_fd->fops->LWS_FOP_CLOSE(fop_fd);
}

/**
Expand All @@ -4306,11 +4323,10 @@ lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
* \param fd: file descriptor
* \param offset: position to seek to
*/
static LWS_INLINE unsigned long
lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
static LWS_INLINE lws_fileofs_t
lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
{
return lws_get_fops(lws_get_context(wsi))->LWS_FOP_SEEK_CUR(wsi,
fd, offset);
return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset);
}
/**
* lws_plat_file_read() - read from file
Expand All @@ -4322,11 +4338,10 @@ lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
* \param len: max length
*/
static LWS_INLINE int LWS_WARN_UNUSED_RESULT
lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
unsigned char *buf, unsigned long len)
lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{
return lws_get_fops(lws_get_context(wsi))->LWS_FOP_READ(wsi, fd,
amount, buf, len);
return fop_fd->fops->LWS_FOP_READ(fop_fd, amount, buf, len);
}
/**
* lws_plat_file_write() - write from file
Expand All @@ -4338,11 +4353,10 @@ lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
* \param len: max length
*/
static LWS_INLINE int LWS_WARN_UNUSED_RESULT
lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
unsigned char *buf, unsigned long len)
lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{
return lws_get_fops(lws_get_context(wsi))->LWS_FOP_WRITE(wsi, fd,
amount, buf, len);
return fop_fd->fops->LWS_FOP_WRITE(fop_fd, amount, buf, len);
}
//@}

Expand Down
58 changes: 37 additions & 21 deletions lib/lws-plat-esp32.c
Expand Up @@ -395,44 +395,60 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
return inet_ntop(af, src, dst, cnt);
}

static lws_filefd_type
_lws_plat_file_open(struct lws *wsi, const char *filename,
unsigned long *filelen, int *flags)
static lws_fop_fd_t
_lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
lws_filepos_t *filelen, lws_fop_flags_t *flags)
{
struct stat stat_buf;

lws_fop_fd_t fop_fd;
int ret = open(filename, *flags, 0664);

if (ret < 0)
return LWS_INVALID_FILE;
return NULL;

if (fstat(ret, &stat_buf) < 0)

fop_fd = malloc(sizeof(*fop_fd));
if (!fop_fd)
goto bail;

fop_fd->fops = fops;
fop_fd->fd = ret;
fop_fd->filesystem_priv = NULL; /* we don't use it */

if (fstat(ret, &stat_buf) < 0) {
close(ret);
return LWS_INVALID_FILE;
}
*filelen = stat_buf.st_size;
return (lws_filefd_type)ret;

return fop_fd;

bail:
close(ret);

return NULL;
}

static int
_lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
_lws_plat_file_close(lws_fop_fd_t fops_fd)
{
return close((int)fd);
int fd = fops_fd->fd;

free(fd);

return close(fd);
}

unsigned long
_lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
lws_fileofs_t
_lws_plat_file_seek_cur(lws_fop_fd_t fops_fd, lws_fileofs_t offset)
{
return lseek((int)fd, offset, SEEK_CUR);
return lseek(fops_fd->fd, offset, SEEK_CUR);
}

static int
_lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
unsigned char *buf, unsigned long len)
_lws_plat_file_read(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{
long n;

n = read((int)fd, buf, len);
n = read(fops_fd->fd, buf, len);
if (n == -1) {
*amount = 0;
return -1;
Expand All @@ -444,12 +460,12 @@ _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
}

static int
_lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
unsigned char *buf, unsigned long len)
_lws_plat_file_write(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{
long n;

n = write((int)fd, buf, len);
n = write(fops_fd->fd, buf, len);
if (n == -1) {
*amount = 0;
return -1;
Expand Down
23 changes: 12 additions & 11 deletions lib/lws-plat-optee.c
Expand Up @@ -261,36 +261,37 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
return "lws_plat_inet_ntop";
}

static lws_filefd_type
_lws_plat_file_open(struct lws *wsi, const char *filename,
unsigned long *filelen, int *flags)
static lws_fop_fd_t
_lws_plat_file_open(lws_plat_file_open(struct lws_plat_file_ops *fops,
const char *filename, lws_filepos_t *filelen,
lws_fop_flags_t *flags)
{
return 0;
return NULL;
}

static int
_lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
_lws_plat_file_close(lws_fop_fd_t fop_fd)
{
return 0;
}

unsigned long
_lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
unsigned lws_fileofs_t
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
{
return 0;
}

static int
_lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
unsigned char *buf, unsigned long len)
_lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{

return 0;
}

static int
_lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
unsigned char *buf, unsigned long len)
_lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{

return 0;
Expand Down

0 comments on commit d909cb9

Please sign in to comment.