Commit 7bdbd805 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Cesanta Bot

Fix ESP32 encr. OTA; clean up {open,read,close}dir

Fixed a bunch of things to make OTA work on ESP32 with flash encryption:
writes to app aprtitions must be 32-byte aligned and mod 32 in size.
When merging filesystems during update, use spiffs_vfs_* functions so
that old fs is properly decrypted.

Refactored cs_dirent stuff: SPIFFS support moved to spiffs_vfs.c,
added dirent.h on ESP8266 and CC3200 which includes cs_dirent.h
Define DIR and dirent only if asked (ESP8266, CC3200, WIN32).

PUBLISHED_FROM=58b0d05cdc41b1a9e02d341e2a1cdcb012829232
parent 577ad259
...@@ -548,54 +548,31 @@ int cs_base64_decode(const unsigned char *s, int len, char *dst, int *dec_len) { ...@@ -548,54 +548,31 @@ int cs_base64_decode(const unsigned char *s, int len, char *dst, int *dec_len) {
#ifndef CS_COMMON_CS_DIRENT_H_ #ifndef CS_COMMON_CS_DIRENT_H_
#define CS_COMMON_CS_DIRENT_H_ #define CS_COMMON_CS_DIRENT_H_
#include <limits.h>
/* Amalgamated: #include "common/platform.h" */ /* Amalgamated: #include "common/platform.h" */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
#ifndef CS_ENABLE_SPIFFS #ifdef CS_DEFINE_DIRENT
#define CS_ENABLE_SPIFFS 0 typedef struct { int dummy; } DIR;
#endif
#if CS_ENABLE_SPIFFS
#include <spiffs.h>
typedef struct {
spiffs_DIR dh;
struct spiffs_dirent de;
} DIR;
#define d_name name
#define dirent spiffs_dirent
int rmdir(const char *path);
int mkdir(const char *path, mode_t mode);
#endif
#if defined(_WIN32)
struct dirent { struct dirent {
int d_ino;
#ifdef _WIN32
char d_name[MAX_PATH]; char d_name[MAX_PATH];
}; #else
/* TODO(rojer): Use PATH_MAX but make sure it's sane on every platform */
typedef struct DIR { char d_name[256];
HANDLE handle;
WIN32_FIND_DATAW info;
struct dirent result;
} DIR;
#endif
#if CS_ENABLE_SPIFFS
extern spiffs *cs_spiffs_get_fs(void);
#endif #endif
};
#if defined(_WIN32) || CS_ENABLE_SPIFFS
DIR *opendir(const char *dir_name); DIR *opendir(const char *dir_name);
int closedir(DIR *dir); int closedir(DIR *dir);
struct dirent *readdir(DIR *dir); struct dirent *readdir(DIR *dir);
#endif #endif /* CS_DEFINE_DIRENT */
#ifdef __cplusplus #ifdef __cplusplus
} }
...@@ -628,14 +605,21 @@ struct dirent *readdir(DIR *dir); ...@@ -628,14 +605,21 @@ struct dirent *readdir(DIR *dir);
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
struct win32_dir {
DIR d;
HANDLE handle;
WIN32_FIND_DATAW info;
struct dirent result;
};
DIR *opendir(const char *name) { DIR *opendir(const char *name) {
DIR *dir = NULL; struct win32_dir *dir = NULL;
wchar_t wpath[MAX_PATH]; wchar_t wpath[MAX_PATH];
DWORD attrs; DWORD attrs;
if (name == NULL) { if (name == NULL) {
SetLastError(ERROR_BAD_ARGUMENTS); SetLastError(ERROR_BAD_ARGUMENTS);
} else if ((dir = (DIR *) MG_MALLOC(sizeof(*dir))) == NULL) { } else if ((dir = (struct win32_dir *) MG_MALLOC(sizeof(*dir))) == NULL) {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); SetLastError(ERROR_NOT_ENOUGH_MEMORY);
} else { } else {
to_wchar(name, wpath, ARRAY_SIZE(wpath)); to_wchar(name, wpath, ARRAY_SIZE(wpath));
...@@ -650,10 +634,11 @@ DIR *opendir(const char *name) { ...@@ -650,10 +634,11 @@ DIR *opendir(const char *name) {
} }
} }
return dir; return (DIR *) dir;
} }
int closedir(DIR *dir) { int closedir(DIR *d) {
struct win32_dir *dir = (struct win32_dir *) d;
int result = 0; int result = 0;
if (dir != NULL) { if (dir != NULL) {
...@@ -668,10 +653,12 @@ int closedir(DIR *dir) { ...@@ -668,10 +653,12 @@ int closedir(DIR *dir) {
return result; return result;
} }
struct dirent *readdir(DIR *dir) { struct dirent *readdir(DIR *d) {
struct win32_dir *dir = (struct win32_dir *) d;
struct dirent *result = NULL; struct dirent *result = NULL;
if (dir) { if (dir) {
memset(&dir->result, 0, sizeof(dir->result));
if (dir->handle != INVALID_HANDLE_VALUE) { if (dir->handle != INVALID_HANDLE_VALUE) {
result = &dir->result; result = &dir->result;
(void) WideCharToMultiByte(CP_UTF8, 0, dir->info.cFileName, -1, (void) WideCharToMultiByte(CP_UTF8, 0, dir->info.cFileName, -1,
...@@ -694,52 +681,6 @@ struct dirent *readdir(DIR *dir) { ...@@ -694,52 +681,6 @@ struct dirent *readdir(DIR *dir) {
} }
#endif #endif
#if CS_ENABLE_SPIFFS
DIR *opendir(const char *dir_name) {
DIR *dir = NULL;
spiffs *fs = cs_spiffs_get_fs();
if (dir_name == NULL || fs == NULL ||
(dir = (DIR *) calloc(1, sizeof(*dir))) == NULL) {
return NULL;
}
if (SPIFFS_opendir(fs, dir_name, &dir->dh) == NULL) {
free(dir);
dir = NULL;
}
return dir;
}
int closedir(DIR *dir) {
if (dir != NULL) {
SPIFFS_closedir(&dir->dh);
free(dir);
}
return 0;
}
struct dirent *readdir(DIR *dir) {
return SPIFFS_readdir(&dir->dh, &dir->de);
}
/* SPIFFs doesn't support directory operations */
int rmdir(const char *path) {
(void) path;
return ENOTSUP;
}
int mkdir(const char *path, mode_t mode) {
(void) path;
(void) mode;
/* for spiffs supports only root dir, which comes from mongoose as '.' */
return (strlen(path) == 1 && *path == '.') ? 0 : ENOTSUP;
}
#endif /* CS_ENABLE_SPIFFS */
#endif /* EXCLUDE_COMMON */ #endif /* EXCLUDE_COMMON */
/* ISO C requires a translation unit to contain at least one declaration */ /* ISO C requires a translation unit to contain at least one declaration */
...@@ -7505,9 +7446,8 @@ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data, ...@@ -7505,9 +7446,8 @@ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
(struct mg_http_multipart_part *) ev_data; (struct mg_http_multipart_part *) ev_data;
struct file_upload_state *fus = struct file_upload_state *fus =
(struct file_upload_state *) calloc(1, sizeof(*fus)); (struct file_upload_state *) calloc(1, sizeof(*fus));
mp->user_data = NULL;
struct mg_str lfn = local_name_fn(nc, mg_mk_str(mp->file_name)); struct mg_str lfn = local_name_fn(nc, mg_mk_str(mp->file_name));
mp->user_data = NULL;
if (lfn.p == NULL || lfn.len == 0) { if (lfn.p == NULL || lfn.len == 0) {
LOG(LL_ERROR, ("%p Not allowed to upload %s", nc, mp->file_name)); LOG(LL_ERROR, ("%p Not allowed to upload %s", nc, mp->file_name));
mg_printf(nc, mg_printf(nc,
......
...@@ -274,6 +274,7 @@ typedef struct _stati64 cs_stat_t; ...@@ -274,6 +274,7 @@ typedef struct _stati64 cs_stat_t;
#define S_ISREG(x) (((x) &_S_IFMT) == _S_IFREG) #define S_ISREG(x) (((x) &_S_IFMT) == _S_IFREG)
#endif #endif
#define DIRSEP '\\' #define DIRSEP '\\'
#define CS_DEFINE_DIRENT
#ifndef va_copy #ifndef va_copy
#ifdef __va_copy #ifdef __va_copy
...@@ -528,6 +529,8 @@ typedef struct stat cs_stat_t; ...@@ -528,6 +529,8 @@ typedef struct stat cs_stat_t;
#define SIZE_T_FMT "u" #define SIZE_T_FMT "u"
typedef struct stat cs_stat_t; typedef struct stat cs_stat_t;
#define DIRSEP '/' #define DIRSEP '/'
#define CS_DEFINE_DIRENT
#define to64(x) strtoll(x, NULL, 10) #define to64(x) strtoll(x, NULL, 10)
#define INT64_FMT PRId64 #define INT64_FMT PRId64
#define INT64_X_FMT PRIx64 #define INT64_X_FMT PRIx64
...@@ -708,22 +711,6 @@ int _stat(const char *pathname, struct stat *st); ...@@ -708,22 +711,6 @@ int _stat(const char *pathname, struct stat *st);
#endif /* __TI_COMPILER_VERSION__ */ #endif /* __TI_COMPILER_VERSION__ */
#ifdef CC3200_FS_SPIFFS
#include <common/spiffs/spiffs.h>
typedef struct {
spiffs_DIR dh;
struct spiffs_dirent de;
} DIR;
#define d_name name
#define dirent spiffs_dirent
DIR *opendir(const char *dir_name);
int closedir(DIR *dir);
struct dirent *readdir(DIR *dir);
#endif /* CC3200_FS_SPIFFS */
#ifdef CC3200_FS_SLFS #ifdef CC3200_FS_SLFS
#define MG_FS_SLFS #define MG_FS_SLFS
#endif #endif
...@@ -731,6 +718,7 @@ struct dirent *readdir(DIR *dir); ...@@ -731,6 +718,7 @@ struct dirent *readdir(DIR *dir);
#if (defined(CC3200_FS_SPIFFS) || defined(CC3200_FS_SLFS)) && \ #if (defined(CC3200_FS_SPIFFS) || defined(CC3200_FS_SLFS)) && \
!defined(MG_ENABLE_FILESYSTEM) !defined(MG_ENABLE_FILESYSTEM)
#define MG_ENABLE_FILESYSTEM 1 #define MG_ENABLE_FILESYSTEM 1
#define CS_DEFINE_DIRENT
#endif #endif
#ifndef CS_ENABLE_STDIO #ifndef CS_ENABLE_STDIO
...@@ -1302,6 +1290,7 @@ typedef uint32_t in_addr_t; ...@@ -1302,6 +1290,7 @@ typedef uint32_t in_addr_t;
#define SIZE_T_FMT "u" #define SIZE_T_FMT "u"
#define DIRSEP '\\' #define DIRSEP '\\'
#define CS_DEFINE_DIRENT
#ifndef va_copy #ifndef va_copy
#ifdef __va_copy #ifdef __va_copy
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment