Skip to content

WIP - Add time to SPIFFS for 3.0.0 #6315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
6 changes: 3 additions & 3 deletions cores/esp8266/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ File Dir::openFile(const char* mode) {
return File();
}

File f(_impl->openFile(om, am), _baseFS);
auto f = File(_impl->openFile(om, am), _baseFS);
f.setTimeCallback(timeCallback);
return f;
}
Expand Down Expand Up @@ -350,7 +350,7 @@ File FS::open(const char* path, const char* mode) {
DEBUGV("FS::open: invalid mode `%s`\r\n", mode);
return File();
}
File f(_impl->open(path, om, am), this);
auto f = File(_impl->open(path, om, am), this);
f.setTimeCallback(timeCallback);
return f;
}
Expand All @@ -371,7 +371,7 @@ Dir FS::openDir(const char* path) {
return Dir();
}
DirImplPtr p = _impl->openDir(path);
Dir d(p, this);
auto d = Dir(p, this);
d.setTimeCallback(timeCallback);
return d;
}
Expand Down
13 changes: 11 additions & 2 deletions cores/esp8266/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,19 @@ class SPIFFSConfig : public FSConfig
{
public:
static constexpr uint32_t FSId = 0x53504946;
SPIFFSConfig(bool autoFormat = true) : FSConfig(FSId, autoFormat) { }
SPIFFSConfig(bool autoFormat = true, bool enableTime = true) {
_type = SPIFFSConfig::FSId;
_autoFormat = autoFormat;
_enableTime = enableTime;
}

SPIFFSConfig setEnableTime(bool val = true) {
_enableTime = val;
return *this;
}

// Inherit _type and _autoFormat
// nothing yet, enableTime TBD when SPIFFS has metadate
bool _enableTime;
};

class FS
Expand Down
15 changes: 6 additions & 9 deletions cores/esp8266/spiffs/spiffs.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,13 @@ typedef struct {
// logical size of a page, must be at least
// log_block_size / 8
u32_t log_page_size;

#endif
#if SPIFFS_FILEHDL_OFFSET
// an integer offset added to each file handle
u16_t fh_ix_offset;
#endif
// active metadata length
u32_t obj_meta_len;
} spiffs_config;

typedef struct spiffs_t {
Expand Down Expand Up @@ -300,9 +301,7 @@ typedef struct {
spiffs_obj_type type;
spiffs_page_ix pix;
u8_t name[SPIFFS_OBJ_NAME_LEN];
#if SPIFFS_OBJ_META_LEN
u8_t meta[SPIFFS_OBJ_META_LEN];
#endif
u8_t meta[SPIFFS_MAX_META]; // Set to the largest supported metadata len
} spiffs_stat;

struct spiffs_dirent {
Expand All @@ -311,9 +310,7 @@ struct spiffs_dirent {
spiffs_obj_type type;
u32_t size;
spiffs_page_ix pix;
#if SPIFFS_OBJ_META_LEN
u8_t meta[SPIFFS_OBJ_META_LEN];
#endif
u8_t meta[SPIFFS_MAX_META]; // Set to the largest supported metadata len
};

typedef struct {
Expand Down Expand Up @@ -534,7 +531,7 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh);
*/
s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *newPath);

#if SPIFFS_OBJ_META_LEN

/**
* Updates file's metadata
* @param fs the file system struct
Expand All @@ -550,7 +547,7 @@ s32_t SPIFFS_update_meta(spiffs *fs, const char *name, const void *meta);
* @param meta new metadata. must be SPIFFS_OBJ_META_LEN bytes long.
*/
s32_t SPIFFS_fupdate_meta(spiffs *fs, spiffs_file fh, const void *meta);
#endif


/**
* Returns last error of last file operation.
Expand Down
6 changes: 3 additions & 3 deletions cores/esp8266/spiffs/spiffs_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static s32_t spiffs_object_get_data_page_index_reference(
u32_t addr = SPIFFS_PAGE_TO_PADDR(fs, *objix_pix);
if (objix_spix == 0) {
// get referenced page from object index header
addr += sizeof(spiffs_page_object_ix_header) + data_spix * sizeof(spiffs_page_ix);
addr += sizeof(spiffs_page_object_ix_header)+SPIFFS_OBJ_META_LEN + data_spix * sizeof(spiffs_page_ix);
} else {
// get referenced page from object index
addr += sizeof(spiffs_page_object_ix) + SPIFFS_OBJ_IX_ENTRY(fs, data_spix) * sizeof(spiffs_page_ix);
Expand Down Expand Up @@ -136,7 +136,7 @@ static s32_t spiffs_rewrite_index(spiffs *fs, spiffs_obj_id obj_id, spiffs_span_

// rewrite in mem
if (objix_spix == 0) {
((spiffs_page_ix*)((u8_t *)fs->lu_work + sizeof(spiffs_page_object_ix_header)))[data_spix] = new_data_pix;
((spiffs_page_ix*)((u8_t *)fs->lu_work + sizeof(spiffs_page_object_ix_header)+SPIFFS_OBJ_META_LEN))[data_spix] = new_data_pix;
} else {
((spiffs_page_ix*)((u8_t *)fs->lu_work + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = new_data_pix;
}
Expand Down Expand Up @@ -574,7 +574,7 @@ static s32_t spiffs_page_consistency_check_i(spiffs *fs) {
// object header page index
entries = SPIFFS_OBJ_HDR_IX_LEN(fs);
data_spix_offset = 0;
object_page_index = (spiffs_page_ix *)((u8_t *)fs->lu_work + sizeof(spiffs_page_object_ix_header));
object_page_index = (spiffs_page_ix *)((u8_t *)fs->lu_work + sizeof(spiffs_page_object_ix_header)+SPIFFS_OBJ_META_LEN);
} else {
// object page index
entries = SPIFFS_OBJ_IX_LEN(fs);
Expand Down
15 changes: 15 additions & 0 deletions cores/esp8266/spiffs/spiffs_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ typedef uint16_t u16_t;
typedef int8_t s8_t;
typedef uint8_t u8_t;


// Enable supporting both timestamped and non-timestamped FS images by making
// the meta-len a global variable here and by changing the logic of SPIFFS.begin.
#define SPIFFS_OBJ_META_LEN (fs->cfg.obj_meta_len)
#define SPIFFS_MAX_META (4) // Maximum metadata size allowed at runtime
// Because SPIFFS reads binary images of blocks, we need to be sure to actually ensure there is space to
// keep the metadata (since 0 bytes are allocated in the struct itself). Use an anonymous union to ensure
// that there is at least MAX_META bytes after the end of the struct.
#define alloc_spiffs_page_object_ix_header(name) \
union { \
spiffs_page_object_ix_header name; \
char xxx[sizeof(spiffs_page_object_ix_header) + SPIFFS_MAX_META]; \
};


#ifndef SEEK_SET
#define SEEK_SET 0 /* set file offset to offset */
#endif
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/spiffs/spiffs_gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ s32_t spiffs_gc_clean(spiffs *fs, spiffs_block_ix bix) {
// update memory representation of object index page with new data page
if (gc.cur_objix_spix == 0) {
// update object index header page
((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[p_hdr.span_ix] = new_data_pix;
((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)+SPIFFS_OBJ_META_LEN))[p_hdr.span_ix] = new_data_pix;
SPIFFS_GC_DBG("gc_clean: MOVE_DATA wrote page " _SPIPRIpg" to objix_hdr entry " _SPIPRIsp" in mem\n", new_data_pix, (spiffs_span_ix)SPIFFS_OBJ_IX_ENTRY(fs, p_hdr.span_ix));
} else {
// update object index page
Expand Down
28 changes: 12 additions & 16 deletions cores/esp8266/spiffs/spiffs_hydrogen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,10 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) {

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;
alloc_spiffs_page_object_ix_header(objix_hdr);
spiffs_obj_id obj_id;
s32_t res =_spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ, fh,
SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header)+SPIFFS_OBJ_META_LEN, (u8_t *)&objix_hdr);
SPIFFS_API_CHECK_RES(fs, res);

u32_t obj_id_addr = SPIFFS_BLOCK_TO_PADDR(fs, SPIFFS_BLOCK_FOR_PAGE(fs , pix)) +
Expand All @@ -746,9 +746,8 @@ static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spi
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);
#if SPIFFS_OBJ_META_LEN
_SPIFFS_MEMCPY(s->meta, objix_hdr.meta, SPIFFS_OBJ_META_LEN);
#endif
if (SPIFFS_OBJ_META_LEN)
_SPIFFS_MEMCPY(s->meta, objix_hdr.meta, SPIFFS_OBJ_META_LEN);

return res;
}
Expand Down Expand Up @@ -928,7 +927,7 @@ s32_t SPIFFS_rename(spiffs *fs, const char *old_path, const char *new_path) {
#endif // SPIFFS_READ_ONLY
}

#if SPIFFS_OBJ_META_LEN

s32_t SPIFFS_update_meta(spiffs *fs, const char *name, const void *meta) {
#if SPIFFS_READ_ONLY
(void)fs; (void)name; (void)meta;
Expand All @@ -953,8 +952,7 @@ s32_t SPIFFS_update_meta(spiffs *fs, const char *name, const void *meta) {
}
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, 0, meta,
0, &pix_dummy);
res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, 0, (const u8_t*)meta, 0, &pix_dummy);

spiffs_fd_return(fs, fd->file_nbr);

Expand Down Expand Up @@ -988,8 +986,7 @@ s32_t SPIFFS_fupdate_meta(spiffs *fs, spiffs_file fh, const void *meta) {
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
}

res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, 0, meta,
0, &pix_dummy);
res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, 0, (const u8_t*)meta, 0, &pix_dummy);

SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

Expand All @@ -998,7 +995,7 @@ s32_t SPIFFS_fupdate_meta(spiffs *fs, spiffs_file fh, const void *meta) {
return res;
#endif // SPIFFS_READ_ONLY
}
#endif // SPIFFS_OBJ_META_LEN


spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d) {
SPIFFS_API_DBG("%s\n", __func__);
Expand Down Expand Up @@ -1029,15 +1026,15 @@ static s32_t spiffs_read_dir_v(
void *user_var_p) {
(void)user_const_p;
s32_t res;
spiffs_page_object_ix_header objix_hdr;
alloc_spiffs_page_object_ix_header(objix_hdr);
if (obj_id == SPIFFS_OBJ_ID_FREE || obj_id == SPIFFS_OBJ_ID_DELETED ||
(obj_id & SPIFFS_OBJ_ID_IX_FLAG) == 0) {
return SPIFFS_VIS_COUNTINUE;
}

spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);
res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header)+SPIFFS_OBJ_META_LEN, (u8_t *)&objix_hdr);
if (res != SPIFFS_OK) return res;
if ((obj_id & SPIFFS_OBJ_ID_IX_FLAG) &&
objix_hdr.p_hdr.span_ix == 0 &&
Expand All @@ -1049,9 +1046,8 @@ static s32_t spiffs_read_dir_v(
e->type = objix_hdr.type;
e->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;
e->pix = pix;
#if SPIFFS_OBJ_META_LEN
_SPIFFS_MEMCPY(e->meta, objix_hdr.meta, SPIFFS_OBJ_META_LEN);
#endif
if (SPIFFS_OBJ_META_LEN)
_SPIFFS_MEMCPY(e->meta, objix_hdr.meta, SPIFFS_OBJ_META_LEN);
return SPIFFS_OK;
}
return SPIFFS_VIS_COUNTINUE;
Expand Down
Loading