SpriteHTTPD - embedded HTTP server with read-only filesystem and templating, originally developed for ESP8266, now stand-alone and POSIX compatible.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
spritehttpd/espfsbuilder/parsing.c

67 lines
1.6 KiB

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "parsing.h"
#include "espfs.h"
static size_t espfs_parse_filesize = -1;
static int espfs_parse_fd = -1;
/**
* Parse an image file and show the files contained.
* This is a simple sanity test.
*
* @param[in] filename - image file to parse
*/
void parseEspfsFileAndShowItsContents(const char *filename)
{
int rv;
fprintf(stderr, "Parsing: %s\n", filename);
FILE *f = fopen(filename, "r");
if (!f) {
perror(filename);
exit(1);
}
int fd = fileno(f);
espfs_parse_filesize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
espfs_parse_fd = fd;
rv = espFsInit();
if (rv != 0) {
fprintf(stderr, "Fail to init FS\n");
exit(1);
}
EspFsWalk walk;
espFsWalkInit(&walk);
EspFsHeader header;
uint32_t offset;
char namebuf[1024];
while (espFsWalkNext(&walk, &header, namebuf, 1024, &offset)) {
fprintf(stderr, "at %04x: \"%s\", flags: %02x, comp: %s, compLen: %d, plainLen: %d\n", offset, namebuf, header.flags,
header.compression == 1 ? "HS" : "None", header.fileLenComp, header.fileLenDecomp);
}
fclose(f);
}
int httpdPlatEspfsRead(void *dest, uint32_t offset, size_t len)
{
// fprintf(stderr, "FS read @ %d, len %d\n", offset, (int) len);
if (offset + len > espfs_parse_filesize) {
// fprintf(stderr, "Read out fo range!\n");
return -1;
}
lseek(espfs_parse_fd, offset, SEEK_SET);
read(espfs_parse_fd, dest, len);
return 0;
}