#include "lwip/apps/httpd_opts.h"
#include "lwip/def.h"
#include "lwip/apps/fs.h"
#include <string.h>
#include HTTPD_FSDATA_FILE
err_t
fs_open(struct fs_file *file, const char *name)
{
const struct fsdata_file *f;
if ((file == NULL) || (name == NULL)) {
return ERR_ARG;
}
#if LWIP_HTTPD_CUSTOM_FILES
if (fs_open_custom(file, name)) {
file->flags |= FS_FILE_FLAGS_CUSTOM;
return ERR_OK;
}
#endif
for (f = FS_ROOT; f != NULL; f = f->next) {
if (!strcmp(name, (const char *)f->name)) {
file->data = (const char *)f->data;
file->len = f->len;
file->index = f->len;
file->flags = f->flags;
#if HTTPD_PRECALCULATED_CHECKSUM
file->chksum_count = f->chksum_count;
file->chksum = f->chksum;
#endif
#if LWIP_HTTPD_FILE_EXTENSION
file->pextension = NULL;
#endif
#if LWIP_HTTPD_FILE_STATE
file->state = fs_state_init(file, name);
#endif
return ERR_OK;
}
}
return ERR_VAL;
}
void
fs_close(struct fs_file *file)
{
#if LWIP_HTTPD_CUSTOM_FILES
if ((file->flags & FS_FILE_FLAGS_CUSTOM) != 0) {
fs_close_custom(file);
}
#endif
#if LWIP_HTTPD_FILE_STATE
fs_state_free(file, file->state);
#endif
LWIP_UNUSED_ARG(file);
}
#if LWIP_HTTPD_DYNAMIC_FILE_READ
#if LWIP_HTTPD_FS_ASYNC_READ
int
fs_read_async(struct fs_file *file, char *buffer, int count, fs_wait_cb callback_fn, void *callback_arg)
#else
int
fs_read(struct fs_file *file, char *buffer, int count)
#endif
{
int read;
if (file->index == file->len) {
return FS_READ_EOF;
}
#if LWIP_HTTPD_FS_ASYNC_READ
LWIP_UNUSED_ARG(callback_fn);
LWIP_UNUSED_ARG(callback_arg);
#endif
#if LWIP_HTTPD_CUSTOM_FILES
if ((file->flags & FS_FILE_FLAGS_CUSTOM) != 0) {
#if LWIP_HTTPD_FS_ASYNC_READ
return fs_read_async_custom(file, buffer, count, callback_fn, callback_arg);
#else
return fs_read_custom(file, buffer, count);
#endif
}
#endif
read = file->len - file->index;
if (read > count) {
read = count;
}
MEMCPY(buffer, (file->data + file->index), read);
file->index += read;
return (read);
}
#endif
#if LWIP_HTTPD_FS_ASYNC_READ
int
fs_is_file_ready(struct fs_file *file, fs_wait_cb callback_fn, void *callback_arg)
{
if (file != NULL) {
#if LWIP_HTTPD_FS_ASYNC_READ
#if LWIP_HTTPD_CUSTOM_FILES
if (!fs_canread_custom(file)) {
if (fs_wait_read_custom(file, callback_fn, callback_arg)) {
return 0;
}
}
#else
LWIP_UNUSED_ARG(callback_fn);
LWIP_UNUSED_ARG(callback_arg);
#endif
#endif
}
return 1;
}
#endif
int
fs_bytes_left(struct fs_file *file)
{
return file->len - file->index;
}