|
@@ -8,7 +8,17 @@
|
|
#include "spiffs.h"
|
|
#include "spiffs.h"
|
|
#include "spiffs_nucleus.h"
|
|
#include "spiffs_nucleus.h"
|
|
|
|
|
|
|
|
+#if SPIFFS_FILEHDL_OFFSET
|
|
|
|
+#define SPIFFS_FH_OFFS(fs, fh) ((fh) != 0 ? ((fh) + (fs)->cfg.fh_ix_offset) : 0)
|
|
|
|
+#define SPIFFS_FH_UNOFFS(fs, fh) ((fh) != 0 ? ((fh) - (fs)->cfg.fh_ix_offset) : 0)
|
|
|
|
+#else
|
|
|
|
+#define SPIFFS_FH_OFFS(fs, fh) (fh)
|
|
|
|
+#define SPIFFS_FH_UNOFFS(fs, fh) (fh)
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+#if SPIFFS_CACHE == 1
|
|
static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh);
|
|
static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh);
|
|
|
|
+#endif
|
|
|
|
|
|
#if SPIFFS_BUFFER_HELP
|
|
#if SPIFFS_BUFFER_HELP
|
|
u32_t SPIFFS_buffer_bytes_for_filedescs(spiffs *fs, u32_t num_descs) {
|
|
u32_t SPIFFS_buffer_bytes_for_filedescs(spiffs *fs, u32_t num_descs) {
|
|
@@ -26,6 +36,10 @@ u8_t SPIFFS_mounted(spiffs *fs) {
|
|
}
|
|
}
|
|
|
|
|
|
s32_t SPIFFS_format(spiffs *fs) {
|
|
s32_t SPIFFS_format(spiffs *fs) {
|
|
|
|
+#if SPIFFS_READ_ONLY
|
|
|
|
+ (void)fs;
|
|
|
|
+ return SPIFFS_ERR_RO_NOT_IMPL;
|
|
|
|
+#else
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
if (SPIFFS_CHECK_MOUNT(fs)) {
|
|
if (SPIFFS_CHECK_MOUNT(fs)) {
|
|
fs->err_code = SPIFFS_ERR_MOUNTED;
|
|
fs->err_code = SPIFFS_ERR_MOUNTED;
|
|
@@ -49,25 +63,35 @@ s32_t SPIFFS_format(spiffs *fs) {
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
|
|
+#endif // SPIFFS_READ_ONLY
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
|
|
|
|
+
|
|
|
|
+s32_t SPIFFS_probe_fs(spiffs_config *config) {
|
|
|
|
+ s32_t res = spiffs_probe(config);
|
|
|
|
+ return res;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif // SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
|
|
|
|
+
|
|
s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work,
|
|
s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work,
|
|
u8_t *fd_space, u32_t fd_space_size,
|
|
u8_t *fd_space, u32_t fd_space_size,
|
|
void *cache, u32_t cache_size,
|
|
void *cache, u32_t cache_size,
|
|
spiffs_check_callback check_cb_f) {
|
|
spiffs_check_callback check_cb_f) {
|
|
|
|
+ void *user_data;
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
- c_memset(fs, 0, sizeof(spiffs));
|
|
|
|
- c_memcpy(&fs->cfg, config, sizeof(spiffs_config));
|
|
|
|
|
|
+ user_data = fs->user_data;
|
|
|
|
+ memset(fs, 0, sizeof(spiffs));
|
|
|
|
+ memcpy(&fs->cfg, config, sizeof(spiffs_config));
|
|
|
|
+ fs->user_data = user_data;
|
|
fs->block_count = SPIFFS_CFG_PHYS_SZ(fs) / SPIFFS_CFG_LOG_BLOCK_SZ(fs);
|
|
fs->block_count = SPIFFS_CFG_PHYS_SZ(fs) / SPIFFS_CFG_LOG_BLOCK_SZ(fs);
|
|
fs->work = &work[0];
|
|
fs->work = &work[0];
|
|
fs->lu_work = &work[SPIFFS_CFG_LOG_PAGE_SZ(fs)];
|
|
fs->lu_work = &work[SPIFFS_CFG_LOG_PAGE_SZ(fs)];
|
|
- c_memset(fd_space, 0, fd_space_size);
|
|
|
|
- // align fd_space pointer to pointer size byte boundary, below is safe
|
|
|
|
|
|
+ memset(fd_space, 0, fd_space_size);
|
|
|
|
+ // align fd_space pointer to pointer size byte boundary
|
|
u8_t ptr_size = sizeof(void*);
|
|
u8_t ptr_size = sizeof(void*);
|
|
-#pragma GCC diagnostic push
|
|
|
|
-#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
|
|
|
|
- u8_t addr_lsb = ((u8_t)fd_space) & (ptr_size-1);
|
|
|
|
-#pragma GCC diagnostic pop
|
|
|
|
|
|
+ u8_t addr_lsb = ((u8_t)(intptr_t)fd_space) & (ptr_size-1);
|
|
if (addr_lsb) {
|
|
if (addr_lsb) {
|
|
fd_space += (ptr_size-addr_lsb);
|
|
fd_space += (ptr_size-addr_lsb);
|
|
fd_space_size -= (ptr_size-addr_lsb);
|
|
fd_space_size -= (ptr_size-addr_lsb);
|
|
@@ -75,11 +99,8 @@ s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work,
|
|
fs->fd_space = fd_space;
|
|
fs->fd_space = fd_space;
|
|
fs->fd_count = (fd_space_size/sizeof(spiffs_fd));
|
|
fs->fd_count = (fd_space_size/sizeof(spiffs_fd));
|
|
|
|
|
|
- // align cache pointer to 4 byte boundary, below is safe
|
|
|
|
-#pragma GCC diagnostic push
|
|
|
|
-#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
|
|
|
|
- addr_lsb = ((u8_t)cache) & (ptr_size-1);
|
|
|
|
-#pragma GCC diagnostic pop
|
|
|
|
|
|
+ // align cache pointer to 4 byte boundary
|
|
|
|
+ addr_lsb = ((u8_t)(intptr_t)cache) & (ptr_size-1);
|
|
if (addr_lsb) {
|
|
if (addr_lsb) {
|
|
u8_t *cache_8 = (u8_t *)cache;
|
|
u8_t *cache_8 = (u8_t *)cache;
|
|
cache_8 += (ptr_size-addr_lsb);
|
|
cache_8 += (ptr_size-addr_lsb);
|
|
@@ -89,9 +110,10 @@ s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work,
|
|
if (cache_size & (ptr_size-1)) {
|
|
if (cache_size & (ptr_size-1)) {
|
|
cache_size -= (cache_size & (ptr_size-1));
|
|
cache_size -= (cache_size & (ptr_size-1));
|
|
}
|
|
}
|
|
|
|
+
|
|
#if SPIFFS_CACHE
|
|
#if SPIFFS_CACHE
|
|
fs->cache = cache;
|
|
fs->cache = cache;
|
|
- fs->cache_size = (cache_size > (config->log_page_size*32)) ? config->log_page_size*32 : cache_size;
|
|
|
|
|
|
+ fs->cache_size = (cache_size > (SPIFFS_CFG_LOG_PAGE_SZ(fs)*32)) ? SPIFFS_CFG_LOG_PAGE_SZ(fs)*32 : cache_size;
|
|
spiffs_cache_init(fs);
|
|
spiffs_cache_init(fs);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
@@ -152,43 +174,68 @@ void SPIFFS_clearerr(spiffs *fs) {
|
|
fs->err_code = SPIFFS_OK;
|
|
fs->err_code = SPIFFS_OK;
|
|
}
|
|
}
|
|
|
|
|
|
-s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode) {
|
|
|
|
|
|
+s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode) {
|
|
|
|
+#if SPIFFS_READ_ONLY
|
|
|
|
+ (void)fs; (void)path; (void)mode;
|
|
|
|
+ return SPIFFS_ERR_RO_NOT_IMPL;
|
|
|
|
+#else
|
|
(void)mode;
|
|
(void)mode;
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
|
|
+ if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
|
|
|
|
+ SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
|
|
|
|
+ }
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
spiffs_obj_id obj_id;
|
|
spiffs_obj_id obj_id;
|
|
s32_t res;
|
|
s32_t res;
|
|
|
|
|
|
- res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (u8_t *)path);
|
|
|
|
|
|
+ res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (const u8_t*)path);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
- res = spiffs_object_create(fs, obj_id, (u8_t *)path, SPIFFS_TYPE_FILE, 0);
|
|
|
|
|
|
+ res = spiffs_object_create(fs, obj_id, (const u8_t*)path, SPIFFS_TYPE_FILE, 0);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
return 0;
|
|
return 0;
|
|
|
|
+#endif // SPIFFS_READ_ONLY
|
|
}
|
|
}
|
|
|
|
|
|
-spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode mode) {
|
|
|
|
|
|
+spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode) {
|
|
(void)mode;
|
|
(void)mode;
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
|
|
+ if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
|
|
|
|
+ SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
|
|
|
|
+ }
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
|
|
|
|
spiffs_fd *fd;
|
|
spiffs_fd *fd;
|
|
spiffs_page_ix pix;
|
|
spiffs_page_ix pix;
|
|
|
|
|
|
|
|
+#if SPIFFS_READ_ONLY
|
|
|
|
+ // not valid flags in read only mode
|
|
|
|
+ flags &= ~SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_TRUNC;
|
|
|
|
+#endif // SPIFFS_READ_ONLY
|
|
|
|
+
|
|
s32_t res = spiffs_fd_find_new(fs, &fd);
|
|
s32_t res = spiffs_fd_find_new(fs, &fd);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
- res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)path, &pix);
|
|
|
|
- if ((flags & SPIFFS_CREAT) == 0) {
|
|
|
|
|
|
+ res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix);
|
|
|
|
+ if ((flags & SPIFFS_O_CREAT) == 0) {
|
|
if (res < SPIFFS_OK) {
|
|
if (res < SPIFFS_OK) {
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
}
|
|
}
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
}
|
|
}
|
|
|
|
|
|
- if ((flags & SPIFFS_CREAT) && res == SPIFFS_ERR_NOT_FOUND) {
|
|
|
|
|
|
+ if (res == SPIFFS_OK &&
|
|
|
|
+ (flags & (SPIFFS_O_CREAT | SPIFFS_O_EXCL)) == (SPIFFS_O_CREAT | SPIFFS_O_EXCL)) {
|
|
|
|
+ // creat and excl and file exists - fail
|
|
|
|
+ res = SPIFFS_ERR_FILE_EXISTS;
|
|
|
|
+ spiffs_fd_return(fs, fd->file_nbr);
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if ((flags & SPIFFS_O_CREAT) && res == SPIFFS_ERR_NOT_FOUND) {
|
|
|
|
+#if !SPIFFS_READ_ONLY
|
|
spiffs_obj_id obj_id;
|
|
spiffs_obj_id obj_id;
|
|
// no need to enter conflicting name here, already looked for it above
|
|
// no need to enter conflicting name here, already looked for it above
|
|
res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, 0);
|
|
res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, 0);
|
|
@@ -196,12 +243,13 @@ spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
}
|
|
}
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
- res = spiffs_object_create(fs, obj_id, (u8_t*)path, SPIFFS_TYPE_FILE, &pix);
|
|
|
|
|
|
+ res = spiffs_object_create(fs, obj_id, (const u8_t*)path, SPIFFS_TYPE_FILE, &pix);
|
|
if (res < SPIFFS_OK) {
|
|
if (res < SPIFFS_OK) {
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
}
|
|
}
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
- flags &= ~SPIFFS_TRUNC;
|
|
|
|
|
|
+ flags &= ~SPIFFS_O_TRUNC;
|
|
|
|
+#endif // !SPIFFS_READ_ONLY
|
|
} else {
|
|
} else {
|
|
if (res < SPIFFS_OK) {
|
|
if (res < SPIFFS_OK) {
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
@@ -213,19 +261,21 @@ spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
}
|
|
}
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
- if (flags & SPIFFS_TRUNC) {
|
|
|
|
|
|
+#if !SPIFFS_READ_ONLY
|
|
|
|
+ if (flags & SPIFFS_O_TRUNC) {
|
|
res = spiffs_object_truncate(fd, 0, 0);
|
|
res = spiffs_object_truncate(fd, 0, 0);
|
|
if (res < SPIFFS_OK) {
|
|
if (res < SPIFFS_OK) {
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
}
|
|
}
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
}
|
|
}
|
|
|
|
+#endif // !SPIFFS_READ_ONLY
|
|
|
|
|
|
fd->fdoffset = 0;
|
|
fd->fdoffset = 0;
|
|
|
|
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
|
|
|
|
- return fd->file_nbr;
|
|
|
|
|
|
+ return SPIFFS_FH_OFFS(fs, fd->file_nbr);
|
|
}
|
|
}
|
|
|
|
|
|
spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode) {
|
|
spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode) {
|
|
@@ -243,19 +293,67 @@ spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_fl
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
}
|
|
}
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
- if (flags & SPIFFS_TRUNC) {
|
|
|
|
|
|
+#if !SPIFFS_READ_ONLY
|
|
|
|
+ if (flags & SPIFFS_O_TRUNC) {
|
|
|
|
+ res = spiffs_object_truncate(fd, 0, 0);
|
|
|
|
+ if (res < SPIFFS_OK) {
|
|
|
|
+ spiffs_fd_return(fs, fd->file_nbr);
|
|
|
|
+ }
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
+ }
|
|
|
|
+#endif // !SPIFFS_READ_ONLY
|
|
|
|
+
|
|
|
|
+ fd->fdoffset = 0;
|
|
|
|
+
|
|
|
|
+ SPIFFS_UNLOCK(fs);
|
|
|
|
+
|
|
|
|
+ return SPIFFS_FH_OFFS(fs, fd->file_nbr);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+spiffs_file SPIFFS_open_by_page(spiffs *fs, spiffs_page_ix page_ix, spiffs_flags flags, spiffs_mode mode) {
|
|
|
|
+ SPIFFS_API_CHECK_CFG(fs);
|
|
|
|
+ SPIFFS_API_CHECK_MOUNT(fs);
|
|
|
|
+ SPIFFS_LOCK(fs);
|
|
|
|
+
|
|
|
|
+ spiffs_fd *fd;
|
|
|
|
+
|
|
|
|
+ s32_t res = spiffs_fd_find_new(fs, &fd);
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
+
|
|
|
|
+ if (SPIFFS_IS_LOOKUP_PAGE(fs, page_ix)) {
|
|
|
|
+ res = SPIFFS_ERR_NOT_A_FILE;
|
|
|
|
+ spiffs_fd_return(fs, fd->file_nbr);
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ res = spiffs_object_open_by_page(fs, page_ix, fd, flags, mode);
|
|
|
|
+ if (res == SPIFFS_ERR_IS_FREE ||
|
|
|
|
+ res == SPIFFS_ERR_DELETED ||
|
|
|
|
+ res == SPIFFS_ERR_NOT_FINALIZED ||
|
|
|
|
+ res == SPIFFS_ERR_NOT_INDEX ||
|
|
|
|
+ res == SPIFFS_ERR_INDEX_SPAN_MISMATCH) {
|
|
|
|
+ res = SPIFFS_ERR_NOT_A_FILE;
|
|
|
|
+ }
|
|
|
|
+ if (res < SPIFFS_OK) {
|
|
|
|
+ spiffs_fd_return(fs, fd->file_nbr);
|
|
|
|
+ }
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
+
|
|
|
|
+#if !SPIFFS_READ_ONLY
|
|
|
|
+ if (flags & SPIFFS_O_TRUNC) {
|
|
res = spiffs_object_truncate(fd, 0, 0);
|
|
res = spiffs_object_truncate(fd, 0, 0);
|
|
if (res < SPIFFS_OK) {
|
|
if (res < SPIFFS_OK) {
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
}
|
|
}
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
}
|
|
}
|
|
|
|
+#endif // !SPIFFS_READ_ONLY
|
|
|
|
|
|
fd->fdoffset = 0;
|
|
fd->fdoffset = 0;
|
|
|
|
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
|
|
|
|
- return fd->file_nbr;
|
|
|
|
|
|
+ return SPIFFS_FH_OFFS(fs, fd->file_nbr);
|
|
}
|
|
}
|
|
|
|
|
|
s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
@@ -266,14 +364,21 @@ s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
spiffs_fd *fd;
|
|
spiffs_fd *fd;
|
|
s32_t res;
|
|
s32_t res;
|
|
|
|
|
|
|
|
+ fh = SPIFFS_FH_UNOFFS(fs, fh);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
- if ((fd->flags & SPIFFS_RDONLY) == 0) {
|
|
|
|
|
|
+ if ((fd->flags & SPIFFS_O_RDONLY) == 0) {
|
|
res = SPIFFS_ERR_NOT_READABLE;
|
|
res = SPIFFS_ERR_NOT_READABLE;
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if (fd->size == SPIFFS_UNDEFINED_LEN && len > 0) {
|
|
|
|
+ // special case for zero sized files
|
|
|
|
+ res = SPIFFS_ERR_END_OF_OBJECT;
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
+ }
|
|
|
|
+
|
|
#if SPIFFS_CACHE_WR
|
|
#if SPIFFS_CACHE_WR
|
|
spiffs_fflush_cache(fs, fh);
|
|
spiffs_fflush_cache(fs, fh);
|
|
#endif
|
|
#endif
|
|
@@ -305,6 +410,7 @@ s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
return len;
|
|
return len;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if !SPIFFS_READ_ONLY
|
|
static s32_t spiffs_hydro_write(spiffs *fs, spiffs_fd *fd, void *buf, u32_t offset, s32_t len) {
|
|
static s32_t spiffs_hydro_write(spiffs *fs, spiffs_fd *fd, void *buf, u32_t offset, s32_t len) {
|
|
(void)fs;
|
|
(void)fs;
|
|
s32_t res = SPIFFS_OK;
|
|
s32_t res = SPIFFS_OK;
|
|
@@ -326,8 +432,13 @@ static s32_t spiffs_hydro_write(spiffs *fs, spiffs_fd *fd, void *buf, u32_t offs
|
|
return len;
|
|
return len;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+#endif // !SPIFFS_READ_ONLY
|
|
|
|
|
|
s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
|
|
+#if SPIFFS_READ_ONLY
|
|
|
|
+ (void)fs; (void)fh; (void)buf; (void)len;
|
|
|
|
+ return SPIFFS_ERR_RO_NOT_IMPL;
|
|
|
|
+#else
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
@@ -336,14 +447,19 @@ s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
s32_t res;
|
|
s32_t res;
|
|
u32_t offset;
|
|
u32_t offset;
|
|
|
|
|
|
|
|
+ fh = SPIFFS_FH_UNOFFS(fs, fh);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
- if ((fd->flags & SPIFFS_WRONLY) == 0) {
|
|
|
|
|
|
+ if ((fd->flags & SPIFFS_O_WRONLY) == 0) {
|
|
res = SPIFFS_ERR_NOT_WRITABLE;
|
|
res = SPIFFS_ERR_NOT_WRITABLE;
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if ((fd->flags & SPIFFS_O_APPEND)) {
|
|
|
|
+ fd->fdoffset = fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size;
|
|
|
|
+ }
|
|
|
|
+
|
|
offset = fd->fdoffset;
|
|
offset = fd->fdoffset;
|
|
|
|
|
|
#if SPIFFS_CACHE_WR
|
|
#if SPIFFS_CACHE_WR
|
|
@@ -352,7 +468,7 @@ s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);
|
|
fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
- if (fd->flags & SPIFFS_APPEND) {
|
|
|
|
|
|
+ if (fd->flags & SPIFFS_O_APPEND) {
|
|
if (fd->size == SPIFFS_UNDEFINED_LEN) {
|
|
if (fd->size == SPIFFS_UNDEFINED_LEN) {
|
|
offset = 0;
|
|
offset = 0;
|
|
} else {
|
|
} else {
|
|
@@ -366,7 +482,7 @@ s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
}
|
|
}
|
|
|
|
|
|
#if SPIFFS_CACHE_WR
|
|
#if SPIFFS_CACHE_WR
|
|
- if ((fd->flags & SPIFFS_DIRECT) == 0) {
|
|
|
|
|
|
+ if ((fd->flags & SPIFFS_O_DIRECT) == 0) {
|
|
if (len < (s32_t)SPIFFS_CFG_LOG_PAGE_SZ(fs)) {
|
|
if (len < (s32_t)SPIFFS_CFG_LOG_PAGE_SZ(fs)) {
|
|
// small write, try to cache it
|
|
// small write, try to cache it
|
|
u8_t alloc_cpage = 1;
|
|
u8_t alloc_cpage = 1;
|
|
@@ -383,7 +499,7 @@ s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
|
|
spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
|
|
fd->cache_page->offset, fd->cache_page->size);
|
|
fd->cache_page->offset, fd->cache_page->size);
|
|
spiffs_cache_fd_release(fs, fd->cache_page);
|
|
spiffs_cache_fd_release(fs, fd->cache_page);
|
|
- SPIFFS_API_CHECK_RES(fs, res);
|
|
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
} else {
|
|
} else {
|
|
// writing within cache
|
|
// writing within cache
|
|
alloc_cpage = 0;
|
|
alloc_cpage = 0;
|
|
@@ -407,14 +523,14 @@ s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
offset, offset_in_cpage, len);
|
|
offset, offset_in_cpage, len);
|
|
spiffs_cache *cache = spiffs_get_cache(fs);
|
|
spiffs_cache *cache = spiffs_get_cache(fs);
|
|
u8_t *cpage_data = spiffs_get_cache_page(fs, cache, fd->cache_page->ix);
|
|
u8_t *cpage_data = spiffs_get_cache_page(fs, cache, fd->cache_page->ix);
|
|
- c_memcpy(&cpage_data[offset_in_cpage], buf, len);
|
|
|
|
|
|
+ memcpy(&cpage_data[offset_in_cpage], buf, len);
|
|
fd->cache_page->size = MAX(fd->cache_page->size, offset_in_cpage + len);
|
|
fd->cache_page->size = MAX(fd->cache_page->size, offset_in_cpage + len);
|
|
fd->fdoffset += len;
|
|
fd->fdoffset += len;
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
return len;
|
|
return len;
|
|
} else {
|
|
} else {
|
|
res = spiffs_hydro_write(fs, fd, buf, offset, len);
|
|
res = spiffs_hydro_write(fs, fd, buf, offset, len);
|
|
- SPIFFS_API_CHECK_RES(fs, res);
|
|
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
fd->fdoffset += len;
|
|
fd->fdoffset += len;
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
return res;
|
|
return res;
|
|
@@ -429,21 +545,22 @@ s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
|
|
spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
|
|
spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
|
|
fd->cache_page->offset, fd->cache_page->size);
|
|
fd->cache_page->offset, fd->cache_page->size);
|
|
spiffs_cache_fd_release(fs, fd->cache_page);
|
|
spiffs_cache_fd_release(fs, fd->cache_page);
|
|
- SPIFFS_API_CHECK_RES(fs, res);
|
|
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
res = spiffs_hydro_write(fs, fd, buf, offset, len);
|
|
res = spiffs_hydro_write(fs, fd, buf, offset, len);
|
|
- SPIFFS_API_CHECK_RES(fs, res);
|
|
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
|
|
res = spiffs_hydro_write(fs, fd, buf, offset, len);
|
|
res = spiffs_hydro_write(fs, fd, buf, offset, len);
|
|
- SPIFFS_API_CHECK_RES(fs, res);
|
|
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
fd->fdoffset += len;
|
|
fd->fdoffset += len;
|
|
|
|
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
|
|
|
|
return res;
|
|
return res;
|
|
|
|
+#endif // SPIFFS_READ_ONLY
|
|
}
|
|
}
|
|
|
|
|
|
s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) {
|
|
s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) {
|
|
@@ -453,8 +570,9 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) {
|
|
|
|
|
|
spiffs_fd *fd;
|
|
spiffs_fd *fd;
|
|
s32_t res;
|
|
s32_t res;
|
|
|
|
+ fh = SPIFFS_FH_UNOFFS(fs, fh);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
- SPIFFS_API_CHECK_RES(fs, res);
|
|
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
#if SPIFFS_CACHE_WR
|
|
#if SPIFFS_CACHE_WR
|
|
spiffs_fflush_cache(fs, fh);
|
|
spiffs_fflush_cache(fs, fh);
|
|
@@ -469,7 +587,7 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
- if (offs > (s32_t)fd->size) {
|
|
|
|
|
|
+ if ((offs > (s32_t)fd->size) && (SPIFFS_UNDEFINED_LEN != fd->size)) {
|
|
res = SPIFFS_ERR_END_OF_OBJECT;
|
|
res = SPIFFS_ERR_END_OF_OBJECT;
|
|
}
|
|
}
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
@@ -491,9 +609,16 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) {
|
|
return offs;
|
|
return offs;
|
|
}
|
|
}
|
|
|
|
|
|
-s32_t SPIFFS_remove(spiffs *fs, char *path) {
|
|
|
|
|
|
+s32_t SPIFFS_remove(spiffs *fs, const char *path) {
|
|
|
|
+#if SPIFFS_READ_ONLY
|
|
|
|
+ (void)fs; (void)path;
|
|
|
|
+ return SPIFFS_ERR_RO_NOT_IMPL;
|
|
|
|
+#else
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
|
|
+ if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
|
|
|
|
+ SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
|
|
|
|
+ }
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
|
|
|
|
spiffs_fd *fd;
|
|
spiffs_fd *fd;
|
|
@@ -503,7 +628,7 @@ s32_t SPIFFS_remove(spiffs *fs, char *path) {
|
|
res = spiffs_fd_find_new(fs, &fd);
|
|
res = spiffs_fd_find_new(fs, &fd);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
- res = spiffs_object_find_object_index_header_by_name(fs, (u8_t *)path, &pix);
|
|
|
|
|
|
+ res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix);
|
|
if (res != SPIFFS_OK) {
|
|
if (res != SPIFFS_OK) {
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
}
|
|
}
|
|
@@ -523,19 +648,25 @@ s32_t SPIFFS_remove(spiffs *fs, char *path) {
|
|
|
|
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
return 0;
|
|
return 0;
|
|
|
|
+#endif // SPIFFS_READ_ONLY
|
|
}
|
|
}
|
|
|
|
|
|
s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) {
|
|
s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) {
|
|
|
|
+#if SPIFFS_READ_ONLY
|
|
|
|
+ (void)fs; (void)fh;
|
|
|
|
+ return SPIFFS_ERR_RO_NOT_IMPL;
|
|
|
|
+#else
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
|
|
|
|
spiffs_fd *fd;
|
|
spiffs_fd *fd;
|
|
s32_t res;
|
|
s32_t res;
|
|
|
|
+ fh = SPIFFS_FH_UNOFFS(fs, fh);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
- if ((fd->flags & SPIFFS_WRONLY) == 0) {
|
|
|
|
|
|
+ if ((fd->flags & SPIFFS_O_WRONLY) == 0) {
|
|
res = SPIFFS_ERR_NOT_WRITABLE;
|
|
res = SPIFFS_ERR_NOT_WRITABLE;
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
}
|
|
}
|
|
@@ -551,9 +682,11 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) {
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
|
|
+#endif // SPIFFS_READ_ONLY
|
|
}
|
|
}
|
|
|
|
|
|
static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spiffs_stat *s) {
|
|
static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spiffs_stat *s) {
|
|
|
|
+ (void)fh;
|
|
spiffs_page_object_ix_header objix_hdr;
|
|
spiffs_page_object_ix_header objix_hdr;
|
|
spiffs_obj_id obj_id;
|
|
spiffs_obj_id obj_id;
|
|
s32_t res =_spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ, fh,
|
|
s32_t res =_spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ, fh,
|
|
@@ -566,23 +699,27 @@ static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spi
|
|
obj_id_addr, sizeof(spiffs_obj_id), (u8_t *)&obj_id);
|
|
obj_id_addr, sizeof(spiffs_obj_id), (u8_t *)&obj_id);
|
|
SPIFFS_API_CHECK_RES(fs, res);
|
|
SPIFFS_API_CHECK_RES(fs, res);
|
|
|
|
|
|
- s->obj_id = obj_id;
|
|
|
|
|
|
+ s->obj_id = obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;
|
|
s->type = objix_hdr.type;
|
|
s->type = objix_hdr.type;
|
|
s->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;
|
|
s->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;
|
|
|
|
+ s->pix = pix;
|
|
strncpy((char *)s->name, (char *)objix_hdr.name, SPIFFS_OBJ_NAME_LEN);
|
|
strncpy((char *)s->name, (char *)objix_hdr.name, SPIFFS_OBJ_NAME_LEN);
|
|
|
|
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
|
|
-s32_t SPIFFS_stat(spiffs *fs, char *path, spiffs_stat *s) {
|
|
|
|
|
|
+s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s) {
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
|
|
+ if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
|
|
|
|
+ SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
|
|
|
|
+ }
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
|
|
|
|
s32_t res;
|
|
s32_t res;
|
|
spiffs_page_ix pix;
|
|
spiffs_page_ix pix;
|
|
|
|
|
|
- res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)path, &pix);
|
|
|
|
|
|
+ res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
res = spiffs_stat_pix(fs, pix, 0, s);
|
|
res = spiffs_stat_pix(fs, pix, 0, s);
|
|
@@ -600,6 +737,7 @@ s32_t SPIFFS_fstat(spiffs *fs, spiffs_file fh, spiffs_stat *s) {
|
|
spiffs_fd *fd;
|
|
spiffs_fd *fd;
|
|
s32_t res;
|
|
s32_t res;
|
|
|
|
|
|
|
|
+ fh = SPIFFS_FH_UNOFFS(fs, fh);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
@@ -616,15 +754,18 @@ s32_t SPIFFS_fstat(spiffs *fs, spiffs_file fh, spiffs_stat *s) {
|
|
|
|
|
|
// Checks if there are any cached writes for the object id associated with
|
|
// Checks if there are any cached writes for the object id associated with
|
|
// given filehandle. If so, these writes are flushed.
|
|
// given filehandle. If so, these writes are flushed.
|
|
|
|
+#if SPIFFS_CACHE == 1
|
|
static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh) {
|
|
static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh) {
|
|
|
|
+ (void)fs;
|
|
|
|
+ (void)fh;
|
|
s32_t res = SPIFFS_OK;
|
|
s32_t res = SPIFFS_OK;
|
|
-#if SPIFFS_CACHE_WR
|
|
|
|
|
|
+#if !SPIFFS_READ_ONLY && SPIFFS_CACHE_WR
|
|
|
|
|
|
spiffs_fd *fd;
|
|
spiffs_fd *fd;
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
SPIFFS_API_CHECK_RES(fs, res);
|
|
SPIFFS_API_CHECK_RES(fs, res);
|
|
|
|
|
|
- if ((fd->flags & SPIFFS_DIRECT) == 0) {
|
|
|
|
|
|
+ if ((fd->flags & SPIFFS_O_DIRECT) == 0) {
|
|
if (fd->cache_page == 0) {
|
|
if (fd->cache_page == 0) {
|
|
// see if object id is associated with cache already
|
|
// see if object id is associated with cache already
|
|
fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);
|
|
fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);
|
|
@@ -645,13 +786,16 @@ static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh) {
|
|
|
|
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
+#endif
|
|
|
|
|
|
s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) {
|
|
s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) {
|
|
|
|
+ (void)fh;
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
s32_t res = SPIFFS_OK;
|
|
s32_t res = SPIFFS_OK;
|
|
-#if SPIFFS_CACHE_WR
|
|
|
|
|
|
+#if !SPIFFS_READ_ONLY && SPIFFS_CACHE_WR
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
|
|
+ fh = SPIFFS_FH_UNOFFS(fs, fh);
|
|
res = spiffs_fflush_cache(fs, fh);
|
|
res = spiffs_fflush_cache(fs, fh);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs,res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs,res);
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
@@ -660,38 +804,46 @@ s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) {
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
|
|
-void SPIFFS_close(spiffs *fs, spiffs_file fh) {
|
|
|
|
- if (!SPIFFS_CHECK_CFG((fs))) {
|
|
|
|
- (fs)->err_code = SPIFFS_ERR_NOT_CONFIGURED;
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
|
|
+s32_t SPIFFS_close(spiffs *fs, spiffs_file fh) {
|
|
|
|
+ SPIFFS_API_CHECK_CFG(fs);
|
|
|
|
+ SPIFFS_API_CHECK_MOUNT(fs);
|
|
|
|
|
|
- if (!SPIFFS_CHECK_MOUNT(fs)) {
|
|
|
|
- fs->err_code = SPIFFS_ERR_NOT_MOUNTED;
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
|
|
+ s32_t res = SPIFFS_OK;
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
|
|
|
|
|
|
+ fh = SPIFFS_FH_UNOFFS(fs, fh);
|
|
#if SPIFFS_CACHE
|
|
#if SPIFFS_CACHE
|
|
- spiffs_fflush_cache(fs, fh);
|
|
|
|
|
|
+ res = spiffs_fflush_cache(fs, fh);
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
#endif
|
|
#endif
|
|
- spiffs_fd_return(fs, fh);
|
|
|
|
|
|
+ res = spiffs_fd_return(fs, fh);
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
|
|
+
|
|
|
|
+ return res;
|
|
}
|
|
}
|
|
|
|
|
|
-s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) {
|
|
|
|
|
|
+s32_t SPIFFS_rename(spiffs *fs, const char *old_path, const char *new_path) {
|
|
|
|
+#if SPIFFS_READ_ONLY
|
|
|
|
+ (void)fs; (void)old_path; (void)new_path;
|
|
|
|
+ return SPIFFS_ERR_RO_NOT_IMPL;
|
|
|
|
+#else
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
|
|
+ if (strlen(new_path) > SPIFFS_OBJ_NAME_LEN - 1 ||
|
|
|
|
+ strlen(old_path) > SPIFFS_OBJ_NAME_LEN - 1) {
|
|
|
|
+ SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
|
|
|
|
+ }
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
|
|
|
|
spiffs_page_ix pix_old, pix_dummy;
|
|
spiffs_page_ix pix_old, pix_dummy;
|
|
spiffs_fd *fd;
|
|
spiffs_fd *fd;
|
|
|
|
|
|
- s32_t res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)old, &pix_old);
|
|
|
|
|
|
+ s32_t res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)old_path, &pix_old);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
- res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)new, &pix_dummy);
|
|
|
|
|
|
+ res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)new_path, &pix_dummy);
|
|
if (res == SPIFFS_ERR_NOT_FOUND) {
|
|
if (res == SPIFFS_ERR_NOT_FOUND) {
|
|
res = SPIFFS_OK;
|
|
res = SPIFFS_OK;
|
|
} else if (res == SPIFFS_OK) {
|
|
} else if (res == SPIFFS_OK) {
|
|
@@ -708,7 +860,7 @@ s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) {
|
|
}
|
|
}
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
- res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, (u8_t*)new,
|
|
|
|
|
|
+ res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, (const u8_t*)new_path,
|
|
0, &pix_dummy);
|
|
0, &pix_dummy);
|
|
|
|
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
spiffs_fd_return(fs, fd->file_nbr);
|
|
@@ -718,9 +870,10 @@ s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) {
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
|
|
|
|
return res;
|
|
return res;
|
|
|
|
+#endif // SPIFFS_READ_ONLY
|
|
}
|
|
}
|
|
|
|
|
|
-spiffs_DIR *SPIFFS_opendir(spiffs *fs, char *name, spiffs_DIR *d) {
|
|
|
|
|
|
+spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d) {
|
|
(void)name;
|
|
(void)name;
|
|
|
|
|
|
if (!SPIFFS_CHECK_CFG((fs))) {
|
|
if (!SPIFFS_CHECK_CFG((fs))) {
|
|
@@ -744,9 +897,9 @@ static s32_t spiffs_read_dir_v(
|
|
spiffs_obj_id obj_id,
|
|
spiffs_obj_id obj_id,
|
|
spiffs_block_ix bix,
|
|
spiffs_block_ix bix,
|
|
int ix_entry,
|
|
int ix_entry,
|
|
- u32_t user_data,
|
|
|
|
- void *user_p) {
|
|
|
|
- (void)user_data;
|
|
|
|
|
|
+ const void *user_const_p,
|
|
|
|
+ void *user_var_p) {
|
|
|
|
+ (void)user_const_p;
|
|
s32_t res;
|
|
s32_t res;
|
|
spiffs_page_object_ix_header objix_hdr;
|
|
spiffs_page_object_ix_header objix_hdr;
|
|
if (obj_id == SPIFFS_OBJ_ID_FREE || obj_id == SPIFFS_OBJ_ID_DELETED ||
|
|
if (obj_id == SPIFFS_OBJ_ID_FREE || obj_id == SPIFFS_OBJ_ID_DELETED ||
|
|
@@ -762,10 +915,9 @@ static s32_t spiffs_read_dir_v(
|
|
objix_hdr.p_hdr.span_ix == 0 &&
|
|
objix_hdr.p_hdr.span_ix == 0 &&
|
|
(objix_hdr.p_hdr.flags& (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) ==
|
|
(objix_hdr.p_hdr.flags& (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) ==
|
|
(SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) {
|
|
(SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) {
|
|
- struct spiffs_dirent *e = (struct spiffs_dirent *)user_p;
|
|
|
|
|
|
+ struct spiffs_dirent *e = (struct spiffs_dirent*)user_var_p;
|
|
e->obj_id = obj_id;
|
|
e->obj_id = obj_id;
|
|
- strncpy((char *)e->name, (char *)objix_hdr.name, SPIFFS_OBJ_NAME_LEN);
|
|
|
|
-
|
|
|
|
|
|
+ strcpy((char *)e->name, (char *)objix_hdr.name);
|
|
e->type = objix_hdr.type;
|
|
e->type = objix_hdr.type;
|
|
e->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;
|
|
e->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;
|
|
e->pix = pix;
|
|
e->pix = pix;
|
|
@@ -780,7 +932,7 @@ struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e) {
|
|
d->fs->err_code = SPIFFS_ERR_NOT_MOUNTED;
|
|
d->fs->err_code = SPIFFS_ERR_NOT_MOUNTED;
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
- SPIFFS_LOCK(fs);
|
|
|
|
|
|
+ SPIFFS_LOCK(d->fs);
|
|
|
|
|
|
spiffs_block_ix bix;
|
|
spiffs_block_ix bix;
|
|
int entry;
|
|
int entry;
|
|
@@ -804,7 +956,7 @@ struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e) {
|
|
} else {
|
|
} else {
|
|
d->fs->err_code = res;
|
|
d->fs->err_code = res;
|
|
}
|
|
}
|
|
- SPIFFS_UNLOCK(fs);
|
|
|
|
|
|
+ SPIFFS_UNLOCK(d->fs);
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -815,6 +967,10 @@ s32_t SPIFFS_closedir(spiffs_DIR *d) {
|
|
}
|
|
}
|
|
|
|
|
|
s32_t SPIFFS_check(spiffs *fs) {
|
|
s32_t SPIFFS_check(spiffs *fs) {
|
|
|
|
+#if SPIFFS_READ_ONLY
|
|
|
|
+ (void)fs;
|
|
|
|
+ return SPIFFS_ERR_RO_NOT_IMPL;
|
|
|
|
+#else
|
|
s32_t res;
|
|
s32_t res;
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
@@ -830,6 +986,7 @@ s32_t SPIFFS_check(spiffs *fs) {
|
|
|
|
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
return res;
|
|
return res;
|
|
|
|
+#endif // SPIFFS_READ_ONLY
|
|
}
|
|
}
|
|
|
|
|
|
s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used) {
|
|
s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used) {
|
|
@@ -856,91 +1013,96 @@ s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used) {
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
|
|
-s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) {
|
|
|
|
|
|
+s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages) {
|
|
|
|
+#if SPIFFS_READ_ONLY
|
|
|
|
+ (void)fs; (void)max_free_pages;
|
|
|
|
+ return SPIFFS_ERR_RO_NOT_IMPL;
|
|
|
|
+#else
|
|
|
|
+ s32_t res;
|
|
|
|
+ SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
|
|
|
|
- spiffs_fd *fd;
|
|
|
|
- s32_t res;
|
|
|
|
- res = spiffs_fd_get(fs, fh, &fd);
|
|
|
|
- SPIFFS_API_CHECK_RES(fs, res);
|
|
|
|
-
|
|
|
|
-#if SPIFFS_CACHE_WR
|
|
|
|
- spiffs_fflush_cache(fs, fh);
|
|
|
|
-#endif
|
|
|
|
-
|
|
|
|
- res = fd->fdoffset;
|
|
|
|
|
|
+ res = spiffs_gc_quick(fs, max_free_pages);
|
|
|
|
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
- return res;
|
|
|
|
|
|
+ return 0;
|
|
|
|
+#endif // SPIFFS_READ_ONLY
|
|
}
|
|
}
|
|
|
|
|
|
-s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh) {
|
|
|
|
- SPIFFS_API_CHECK_MOUNT(fs);
|
|
|
|
- SPIFFS_LOCK(fs);
|
|
|
|
|
|
|
|
- spiffs_fd *fd;
|
|
|
|
|
|
+s32_t SPIFFS_gc(spiffs *fs, u32_t size) {
|
|
|
|
+#if SPIFFS_READ_ONLY
|
|
|
|
+ (void)fs; (void)size;
|
|
|
|
+ return SPIFFS_ERR_RO_NOT_IMPL;
|
|
|
|
+#else
|
|
s32_t res;
|
|
s32_t res;
|
|
- res = spiffs_fd_get(fs, fh, &fd);
|
|
|
|
- SPIFFS_API_CHECK_RES(fs, res);
|
|
|
|
-
|
|
|
|
-#if SPIFFS_CACHE_WR
|
|
|
|
- spiffs_fflush_cache(fs, fh);
|
|
|
|
-#endif
|
|
|
|
|
|
+ SPIFFS_API_CHECK_CFG(fs);
|
|
|
|
+ SPIFFS_API_CHECK_MOUNT(fs);
|
|
|
|
+ SPIFFS_LOCK(fs);
|
|
|
|
|
|
- res = (fd->fdoffset == fd->size);
|
|
|
|
|
|
+ res = spiffs_gc_check(fs, size);
|
|
|
|
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
- return res;
|
|
|
|
|
|
+ return 0;
|
|
|
|
+#endif // SPIFFS_READ_ONLY
|
|
}
|
|
}
|
|
|
|
|
|
-s32_t SPIFFS_size(spiffs *fs, spiffs_file fh) {
|
|
|
|
|
|
+s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh) {
|
|
|
|
+ s32_t res;
|
|
|
|
+ SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
|
|
|
|
|
|
+ fh = SPIFFS_FH_UNOFFS(fs, fh);
|
|
|
|
+
|
|
spiffs_fd *fd;
|
|
spiffs_fd *fd;
|
|
- s32_t res;
|
|
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
res = spiffs_fd_get(fs, fh, &fd);
|
|
- SPIFFS_API_CHECK_RES(fs, res);
|
|
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
#if SPIFFS_CACHE_WR
|
|
#if SPIFFS_CACHE_WR
|
|
- spiffs_fflush_cache(fs, fh);
|
|
|
|
|
|
+ res = spiffs_fflush_cache(fs, fh);
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
- res = fd->size;
|
|
|
|
|
|
+ res = (fd->fdoffset >= (fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size));
|
|
|
|
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
|
|
-s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages) {
|
|
|
|
|
|
+s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) {
|
|
s32_t res;
|
|
s32_t res;
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_CFG(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_API_CHECK_MOUNT(fs);
|
|
SPIFFS_LOCK(fs);
|
|
SPIFFS_LOCK(fs);
|
|
|
|
|
|
- res = spiffs_gc_quick(fs, max_free_pages);
|
|
|
|
|
|
+ fh = SPIFFS_FH_UNOFFS(fs, fh);
|
|
|
|
|
|
|
|
+ spiffs_fd *fd;
|
|
|
|
+ res = spiffs_fd_get(fs, fh, &fd);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
- SPIFFS_UNLOCK(fs);
|
|
|
|
- return 0;
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
+#if SPIFFS_CACHE_WR
|
|
|
|
+ res = spiffs_fflush_cache(fs, fh);
|
|
|
|
+ SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
+#endif
|
|
|
|
|
|
-s32_t SPIFFS_gc(spiffs *fs, u32_t size) {
|
|
|
|
- s32_t res;
|
|
|
|
- SPIFFS_API_CHECK_CFG(fs);
|
|
|
|
- SPIFFS_API_CHECK_MOUNT(fs);
|
|
|
|
- SPIFFS_LOCK(fs);
|
|
|
|
|
|
+ res = fd->fdoffset;
|
|
|
|
|
|
- res = spiffs_gc_check(fs, size);
|
|
|
|
|
|
+ SPIFFS_UNLOCK(fs);
|
|
|
|
+ return res;
|
|
|
|
+}
|
|
|
|
|
|
- SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
|
|
|
|
|
|
+s32_t SPIFFS_set_file_callback_func(spiffs *fs, spiffs_file_callback cb_func) {
|
|
|
|
+ SPIFFS_LOCK(fs);
|
|
|
|
+ fs->file_cb_f = cb_func;
|
|
SPIFFS_UNLOCK(fs);
|
|
SPIFFS_UNLOCK(fs);
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
#if SPIFFS_TEST_VISUALISATION
|
|
#if SPIFFS_TEST_VISUALISATION
|
|
s32_t SPIFFS_vis(spiffs *fs) {
|
|
s32_t SPIFFS_vis(spiffs *fs) {
|
|
s32_t res = SPIFFS_OK;
|
|
s32_t res = SPIFFS_OK;
|
|
@@ -1008,11 +1170,10 @@ s32_t SPIFFS_vis(spiffs *fs) {
|
|
spiffs_printf("free_blocks: %i\n", fs->free_blocks);
|
|
spiffs_printf("free_blocks: %i\n", fs->free_blocks);
|
|
spiffs_printf("page_alloc: %i\n", fs->stats_p_allocated);
|
|
spiffs_printf("page_alloc: %i\n", fs->stats_p_allocated);
|
|
spiffs_printf("page_delet: %i\n", fs->stats_p_deleted);
|
|
spiffs_printf("page_delet: %i\n", fs->stats_p_deleted);
|
|
|
|
+ SPIFFS_UNLOCK(fs);
|
|
u32_t total, used;
|
|
u32_t total, used;
|
|
SPIFFS_info(fs, &total, &used);
|
|
SPIFFS_info(fs, &total, &used);
|
|
spiffs_printf("used: %i of %i\n", used, total);
|
|
spiffs_printf("used: %i of %i\n", used, total);
|
|
-
|
|
|
|
- SPIFFS_UNLOCK(fs);
|
|
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|