Skip to content

Commit

Permalink
Added support for in flash storage of read-only files.
Browse files Browse the repository at this point in the history
  • Loading branch information
terjeio committed Oct 11, 2021
1 parent 0a564ec commit a9cae63
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
40 changes: 33 additions & 7 deletions fs.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// fs.c - wrapper for vfs.c for use by httpd
//
// v0.1 / 2021-09-08 / Io Engineering / Terje
// v0.2 / 2021-10-04 / Io Engineering / Terje
//

/*
Expand Down Expand Up @@ -45,8 +45,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#if HTTP_ENABLE

#include "vfs.h"
#include "lwip/apps/fs.h"
#include "httpd.h"

static const embedded_file_t **ro_files = NULL;
static stream_write_ptr wrptr;
static stream_block_tx_buffer_t txbuf = {0};

Expand Down Expand Up @@ -92,7 +93,7 @@ static void fs_write (const char *s)

while(length > txbuf.max_length) {
txbuf.length = txbuf.max_length;
memcpy(txbuf.data, s, txbuf.length);
memcpy(txbuf.s, s, txbuf.length);
if(!vf_write())
return;
length -= txbuf.max_length;
Expand Down Expand Up @@ -131,8 +132,26 @@ struct fs_file *fs_create (void)
return &v_file;
}

static const embedded_file_t *file_is_embedded (const char *name)
{
uint_fast8_t idx = 0;
const embedded_file_t *file = NULL;

if(*name == '/')
name++;

if(ro_files) do {
if(!strcmp(ro_files[idx]->name, name))
file = ro_files[idx];
} while(file == NULL && ro_files[++idx] != NULL);

return file;
}

err_t fs_open (struct fs_file *file, const char *name)
{
const embedded_file_t *ro_file;

if(name == NULL)
return ERR_ARG;

Expand All @@ -154,6 +173,11 @@ err_t fs_open (struct fs_file *file, const char *name)
if((file->pextension = vfs_open(NULL, fname, "r"))) {
file->len = vfs_size((vfs_file_t *)file->pextension);
file->is_custom_file = 0;
} else if((ro_file = file_is_embedded(name))) {
file->len = ro_file->size;
file->data = (char *)ro_file->data;
file->is_custom_file = true;
file->pextension = (void *)ro_file;
} else
file->pextension = NULL;
}
Expand Down Expand Up @@ -197,11 +221,8 @@ int fs_read (struct fs_file *file, char *buffer, int count)
count = count > file->len ? file->len : count;
memcpy(buffer, file->data, count);
file->data += count;
} else {
} else
file->len = count = 0;

}

}

file->len -= count;
Expand All @@ -227,4 +248,9 @@ void fs_reset (void)
}
}

void fs_register_embedded_files (const embedded_file_t **files)
{
ro_files = files;
}

#endif
6 changes: 6 additions & 0 deletions httpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ typedef struct {
void *private_data;
} httpd_uri_handler_t;

typedef struct {
const char *name;
size_t size;
const uint8_t data[];
} embedded_file_t; // TODO: move to new vfs.c

uint8_t http_get_param_count (http_request_t *request);
const char *http_get_uri (http_request_t *request);
char *http_get_param_value (http_request_t *request, const char *key, char *value, uint32_t size);
Expand Down

0 comments on commit a9cae63

Please sign in to comment.