amos 1541be4f99 Add 2 MEDIUM tasks from batch 28: Mynewt FCB2 reverse alignment, SPIFFS append
After a top-up, batch 28 commit b7fd210 gave Mynewt FCB2 reverse
alignment (2 of 3 valid attempts passed) and SPIFFS append error
progress (3 of 4) as MEDIUM. Each task comes from the commit Qwen
measured, its digest matches, and it is still present at the batch
branch head.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 00:30:58 +08:00

2 lines
215 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{"LICENSE": "The MIT License (MIT)\n\nCopyright (c) 2013-2016 Peter Andersson (pelleplutt1976<at>gmail.com)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n", "Makefile": "CC ?= gcc\nCFLAGS ?= -std=c99 -O1 -Wall -Wextra\nCPPFLAGS += -Isrc -Isrc/default -Iport\nSOURCES = src/spiffs_hydrogen.c src/spiffs_nucleus.c src/spiffs_cache.c src/spiffs_gc.c src/spiffs_check.c port/flash.c\n.PHONY: all smoke clean\nall: smoke\nsmoke: smoke-bin\n\t./smoke-bin\nsmoke-bin: $(SOURCES) smoke.c\n\t$(CC) $(CPPFLAGS) $(CFLAGS) $^ -pthread -o $@\nclean:\n\trm -f smoke-bin\n", "README.md": "# Flash 日志存储工作区\n\n代码包含完整的 SPIFFS 软件文件系统和 Linux 内存 flash 移植。应用接口与交付行为见 `docs/api-contract.md`,完整声明见 `src/spiffs.h`;配置项见 `src/default/spiffs_config.h`。\n\n执行 `make smoke` 编译并运行正常路径的存储健康检查。构建只需 GCC、Make 和 POSIX 线程库,不需板卡或网络。可在应用里通过 `port_start(page_size, cache_bytes)` 建立独立的空 flash 实例,通过公开 SPIFFS API 进行文件操作。\n", "docs/api-contract.md": "# 应用所用 SPIFFS 接口\n\n本工作区使用 SPIFFS 的文件、缓存、分配、回收与 NOR flash 层。以下条款整理自随附头文件和默认移植配置,使用文件系统的公开接口描述应用需要的行为。\n\nR01. 配置给出 flash 容量、擦除块、逻辑块和逻辑页大小;配置完成后,未挂载的文件系统可以格式化,随后挂载使用。\nR02. SPIFFS_CREAT 在文件不存在时创建文件;SPIFFS_RDWR 允许读写;关闭会完成待写内容并关闭句柄。\nR03. SPIFFS_write 返回写入字节数,出现错误时返回负值;SPIFFS_read 返回读出的字节数,读取内容来自此前成功写入的文件数据。\nR04. 本应用在相邻的追加写入之间通过 SPIFFS_tell、SPIFFS_eof 或 SPIFFS_fflush 提交前次缓存;以 SPIFFS_APPEND 打开的句柄每次写入均接在文件末尾,先前的定位不改变追加目的地。\nR05. SPIFFS_tell 返回当前读写位置;成功写入后的位置在本次写入数据之后,包含尚在缓存中的成功写入。\nR06. SPIFFS_eof 在当前读写位置到达文件末尾时返回真,在末尾之前返回假;未写入的空文件,其初始位置在末尾。\nR07. SPIFFS_lseek 按 SPIFFS_SEEK_SET、SPIFFS_SEEK_CUR 或 SPIFFS_SEEK_END 定位,分别相对开头、当前位置或文件末尾,成功时返回结果位置。\nR08. SPIFFS_fflush 完成句柄的待写内容,成功时返回 SPIFFS_OK;完成缓存写入和重新打开文件均保留成功写入的数据。\nR09. 未设置 SPIFFS_APPEND 的写入从当前位置开始,可以修改已有内容;写入范围没有到达末尾时,后续数据仍保留。\nR10. SPIFFS_DIRECT 禁止该句柄的写缓存;SPIFFS_CACHE 和 SPIFFS_CACHE_WR 可在编译时关闭,文件读写语义保持一致。\nR11. 已关闭句柄的写入或定位,以及只读句柄的写入,返回负的错误结果。\nR12. 一次 API 调用因句柄或底层 I/O 错误返回后,应用仍能独立调用其他公开 API;错误不阻止后续的容量查询或其他文件的正常读写。\nR13. SPIFFS_info 成功返回 SPIFFS_OK,给出文件系统可用总字节数和已用字节数的估计;已用字节数在总字节数范围内。\nR14. SPIFFS_LOCK 和 SPIFFS_UNLOCK 用于 API 层的互斥。此移植使用 POSIX 互斥量;移植层只提供进入、退出和系统错误报告,不参与文件位置或 I/O 错误处理。\n\n`port/flash.c` 提供内存 NOR:擦除得到 0xff,编程只能清除位。`port_fault_after(n)` 可令后续第 n+1 次物理写返回负的 I/O 错误,并自动取消该一次性故障;负值取消注入。它不改变 RAM 或文件系统元数据。不同逻辑页大小、缓存容量与编译开关均可使用;物理容量为 128 KiB,擦除块为 4096 字节。`make smoke` 是正常路径读写自检。\n", "port/flash.c": "#define _POSIX_C_SOURCE 200809L\n#include <pthread.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include \"flash.h\"\n/* Memory-backed NOR device: erase sets bytes to FF, programming clears bits. */\n#define CAPACITY (128u * 1024u)\nstatic unsigned char flash[CAPACITY];\nstatic unsigned char work[2048], descriptors[4096], cache[8192];\nstatic int writes_left = -1;\nstatic pthread_mutex_t mutex;\nspiffs port_fs;\nvoid port_lock(void) {\n int r = pthread_mutex_lock(&mutex);\n if (r) { printf(\"OS_WAIT %d\\n\", r); fflush(stdout); exit(80); }\n}\nvoid port_unlock(void) {\n int r = pthread_mutex_unlock(&mutex);\n if (r) { printf(\"OS_RELEASE %d\\n\", r); fflush(stdout); exit(81); }\n}\nvoid port_fault_after(int n) { writes_left = n; }\nstatic s32_t flash_read(u32_t a,u32_t n,u8_t *p) {\n if(a > CAPACITY || n > CAPACITY-a) return -20002;\n memcpy(p,flash+a,n); return SPIFFS_OK;\n}\nstatic s32_t flash_write(u32_t a,u32_t n,u8_t *p) {\n if(a > CAPACITY || n > CAPACITY-a) return -20002;\n if(writes_left == 0) { writes_left=-1;return -20001; }\n if(writes_left > 0) writes_left--;\n for(u32_t i=0;i<n;i++) flash[a+i] &= p[i];\n return SPIFFS_OK;\n}\nstatic s32_t flash_erase(u32_t a,u32_t n) {\n if(a > CAPACITY || n > CAPACITY-a || a%4096 || n%4096) return -20002;\n memset(flash+a,255,n);return SPIFFS_OK;\n}\nint port_start(unsigned page,unsigned cache_bytes) {\n spiffs_config cfg;\n pthread_mutexattr_t attr;\n pthread_mutexattr_init(&attr);\n pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK);\n pthread_mutex_init(&mutex,&attr);\n pthread_mutexattr_destroy(&attr);\n memset(flash,255,sizeof flash);memset(&port_fs,0,sizeof port_fs);\n memset(&cfg,0,sizeof cfg);\n cfg.phys_size=CAPACITY;cfg.phys_addr=0;cfg.phys_erase_block=4096;\n cfg.log_block_size=4096;cfg.log_page_size=page;\n cfg.hal_read_f=flash_read;cfg.hal_write_f=flash_write;cfg.hal_erase_f=flash_erase;\n if(page<128 || page>1024 || cache_bytes>sizeof cache) return -20002;\n int r=SPIFFS_mount(&port_fs,&cfg,work,descriptors,sizeof descriptors,\n cache_bytes?cache:NULL,cache_bytes,NULL);\n if(r==SPIFFS_OK) SPIFFS_unmount(&port_fs);\n r=SPIFFS_format(&port_fs);\n if(r!=SPIFFS_OK)return r;\n return SPIFFS_mount(&port_fs,&cfg,work,descriptors,sizeof descriptors,\n cache_bytes?cache:NULL,cache_bytes,NULL);\n}\n", "port/flash.h": "#ifndef MEMORY_FLASH_H\n#define MEMORY_FLASH_H\n#include \"spiffs.h\"\nextern spiffs port_fs;\nint port_start(unsigned page, unsigned cache_bytes);\nvoid port_fault_after(int write_calls);\n#endif\n", "port/params_test.h": "#ifndef PORT_PARAMS_H\n#define PORT_PARAMS_H\n#include <stdint.h>\n#include <stddef.h>\ntypedef uint8_t u8_t;\ntypedef int8_t s8_t;\ntypedef uint16_t u16_t;\ntypedef int16_t s16_t;\ntypedef uint32_t u32_t;\ntypedef int32_t s32_t;\ntypedef uint64_t u64_t;\ntypedef int64_t s64_t;\nvoid port_lock(void);\nvoid port_unlock(void);\n#define SPIFFS_LOCK(fs) port_lock()\n#define SPIFFS_UNLOCK(fs) port_unlock()\n#endif\n", "smoke.c": "#include <stdio.h>\n#include <string.h>\n#include \"flash.h\"\nint main(void) {\n if(port_start(256,4096))return 1;\n spiffs_file f=SPIFFS_open(&port_fs,\"health\",SPIFFS_CREAT|SPIFFS_RDWR|SPIFFS_DIRECT,0);\n char text[]=\"storage health\",readback[32]={0};\n if(f<0 || SPIFFS_write(&port_fs,f,text,strlen(text))!=(int)strlen(text))return 2;\n if(SPIFFS_lseek(&port_fs,f,0,SPIFFS_SEEK_SET)!=0)return 3;\n if(SPIFFS_read(&port_fs,f,readback,strlen(text))!=(int)strlen(text))return 4;\n if(strcmp(text,readback) || SPIFFS_close(&port_fs,f)!=0)return 5;\n puts(\"storage health OK\");return 0;\n}\n", "src/default/spiffs_config.h": "/*\n * spiffs_config.h\n *\n * Created on: Jul 3, 2013\n * Author: petera\n */\n\n#ifndef SPIFFS_CONFIG_H_\n#define SPIFFS_CONFIG_H_\n\n// ----------- 8< ------------\n// Following includes are for the linux test build of spiffs\n// These may/should/must be removed/altered/replaced in your target\n#include \"params_test.h\"\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <stddef.h>\n// ----------- >8 ------------\n\n// compile time switches\n\n// Set generic spiffs debug output call.\n#ifndef SPIFFS_DBG\n#define SPIFFS_DBG(...) //printf(__VA_ARGS__)\n#endif\n// Set spiffs debug output call for garbage collecting.\n#ifndef SPIFFS_GC_DBG\n#define SPIFFS_GC_DBG(...) //printf(__VA_ARGS__)\n#endif\n// Set spiffs debug output call for caching.\n#ifndef SPIFFS_CACHE_DBG\n#define SPIFFS_CACHE_DBG(...) //printf(__VA_ARGS__)\n#endif\n// Set spiffs debug output call for system consistency checks.\n#ifndef SPIFFS_CHECK_DBG\n#define SPIFFS_CHECK_DBG(...) //printf(__VA_ARGS__)\n#endif\n\n// Enable/disable API functions to determine exact number of bytes\n// for filedescriptor and cache buffers. Once decided for a configuration,\n// this can be disabled to reduce flash.\n#ifndef SPIFFS_BUFFER_HELP\n#define SPIFFS_BUFFER_HELP 0\n#endif\n\n// Enables/disable memory read caching of nucleus file system operations.\n// If enabled, memory area must be provided for cache in SPIFFS_mount.\n#ifndef SPIFFS_CACHE\n#define SPIFFS_CACHE 1\n#endif\n#if SPIFFS_CACHE\n// Enables memory write caching for file descriptors in hydrogen\n#ifndef SPIFFS_CACHE_WR\n#define SPIFFS_CACHE_WR 1\n#endif\n\n// Enable/disable statistics on caching. Debug/test purpose only.\n#ifndef SPIFFS_CACHE_STATS\n#define SPIFFS_CACHE_STATS 1\n#endif\n#endif\n\n// Always check header of each accessed page to ensure consistent state.\n// If enabled it will increase number of reads, will increase flash.\n#ifndef SPIFFS_PAGE_CHECK\n#define SPIFFS_PAGE_CHECK 1\n#endif\n\n// Define maximum number of gc runs to perform to reach desired free pages.\n#ifndef SPIFFS_GC_MAX_RUNS\n#define SPIFFS_GC_MAX_RUNS 5\n#endif\n\n// Enable/disable statistics on gc. Debug/test purpose only.\n#ifndef SPIFFS_GC_STATS\n#define SPIFFS_GC_STATS 1\n#endif\n\n// Garbage collecting examines all pages in a block which and sums up\n// to a block score. Deleted pages normally gives positive score and\n// used pages normally gives a negative score (as these must be moved).\n// To have a fair wear-leveling, the erase age is also included in score,\n// whose factor normally is the most positive.\n// The larger the score, the more likely it is that the block will\n// picked for garbage collection.\n\n// Garbage collecting heuristics - weight used for deleted pages.\n#ifndef SPIFFS_GC_HEUR_W_DELET\n#define SPIFFS_GC_HEUR_W_DELET (5)\n#endif\n// Garbage collecting heuristics - weight used for used pages.\n#ifndef SPIFFS_GC_HEUR_W_USED\n#define SPIFFS_GC_HEUR_W_USED (-1)\n#endif\n// Garbage collecting heuristics - weight used for time between\n// last erased and erase of this block.\n#ifndef SPIFFS_GC_HEUR_W_ERASE_AGE\n#define SPIFFS_GC_HEUR_W_ERASE_AGE (50)\n#endif\n\n// Object name maximum length.\n#ifndef SPIFFS_OBJ_NAME_LEN\n#define SPIFFS_OBJ_NAME_LEN (32)\n#endif\n\n// Size of buffer allocated on stack used when copying data.\n// Lower value generates more read/writes. No meaning having it bigger\n// than logical page size.\n#ifndef SPIFFS_COPY_BUFFER_STACK\n#define SPIFFS_COPY_BUFFER_STACK (64)\n#endif\n\n// Enable this to have an identifiable spiffs filesystem. This will look for\n// a magic in all sectors to determine if this is a valid spiffs system or\n// not on mount point. If not, SPIFFS_format must be called prior to mounting\n// again.\n#ifndef SPIFFS_USE_MAGIC\n#define SPIFFS_USE_MAGIC (0)\n#endif\n\n#if SPIFFS_USE_MAGIC\n// Only valid when SPIFFS_USE_MAGIC is enabled. If SPIFFS_USE_MAGIC_LENGTH is\n// enabled, the magic will also be dependent on the length of the filesystem.\n// For example, a filesystem configured and formatted for 4 megabytes will not\n// be accepted for mounting with a configuration defining the filesystem as 2\n// megabytes.\n#ifndef SPIFFS_USE_MAGIC_LENGTH\n#define SPIFFS_USE_MAGIC_LENGTH (0)\n#endif\n#endif\n\n// SPIFFS_LOCK and SPIFFS_UNLOCK protects spiffs from reentrancy on api level\n// These should be defined on a multithreaded system\n\n// define this to enter a mutex if you're running on a multithreaded system\n#ifndef SPIFFS_LOCK\n#define SPIFFS_LOCK(fs)\n#endif\n// define this to exit a mutex if you're running on a multithreaded system\n#ifndef SPIFFS_UNLOCK\n#define SPIFFS_UNLOCK(fs)\n#endif\n\n// Enable if only one spiffs instance with constant configuration will exist\n// on the target. This will reduce calculations, flash and memory accesses.\n// Parts of configuration must be defined below instead of at time of mount.\n#ifndef SPIFFS_SINGLETON\n#define SPIFFS_SINGLETON 0\n#endif\n\n#if SPIFFS_SINGLETON\n// Instead of giving parameters in config struct, singleton build must\n// give parameters in defines below.\n#ifndef SPIFFS_CFG_PHYS_SZ\n#define SPIFFS_CFG_PHYS_SZ(ignore) (1024*1024*2)\n#endif\n#ifndef SPIFFS_CFG_PHYS_ERASE_SZ\n#define SPIFFS_CFG_PHYS_ERASE_SZ(ignore) (65536)\n#endif\n#ifndef SPIFFS_CFG_PHYS_ADDR\n#define SPIFFS_CFG_PHYS_ADDR(ignore) (0)\n#endif\n#ifndef SPIFFS_CFG_LOG_PAGE_SZ\n#define SPIFFS_CFG_LOG_PAGE_SZ(ignore) (256)\n#endif\n#ifndef SPIFFS_CFG_LOG_BLOCK_SZ\n#define SPIFFS_CFG_LOG_BLOCK_SZ(ignore) (65536)\n#endif\n#endif\n\n// Enable this if your target needs aligned data for index tables\n#ifndef SPIFFS_ALIGNED_OBJECT_INDEX_TABLES\n#define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES 0\n#endif\n\n// Enable this if you want the HAL callbacks to be called with the spiffs struct\n#ifndef SPIFFS_HAL_CALLBACK_EXTRA\n#define SPIFFS_HAL_CALLBACK_EXTRA 0\n#endif\n\n// Enable this if you want to add an integer offset to all file handles\n// (spiffs_file). This is useful if running multiple instances of spiffs on\n// same target, in order to recognise to what spiffs instance a file handle\n// belongs.\n// NB: This adds config field fh_ix_offset in the configuration struct when\n// mounting, which must be defined.\n#ifndef SPIFFS_FILEHDL_OFFSET\n#define SPIFFS_FILEHDL_OFFSET 0\n#endif\n\n// Enable this to compile a read only version of spiffs.\n// This will reduce binary size of spiffs. All code comprising modification\n// of the file system will not be compiled. Some config will be ignored.\n// HAL functions for erasing and writing to spi-flash may be null. Cache\n// can be disabled for even further binary size reduction (and ram savings).\n// Functions modifying the fs will return SPIFFS_ERR_RO_NOT_IMPL.\n// If the file system cannot be mounted due to aborted erase operation and\n// SPIFFS_USE_MAGIC is enabled, SPIFFS_ERR_RO_ABORTED_OPERATION will be\n// returned.\n// Might be useful for e.g. bootloaders and such.\n#ifndef SPIFFS_READ_ONLY\n#define SPIFFS_READ_ONLY 0\n#endif\n\n// Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function\n// in the api. This function will visualize all filesystem using given printf\n// function.\n#ifndef SPIFFS_TEST_VISUALISATION\n#define SPIFFS_TEST_VISUALISATION 1\n#endif\n#if SPIFFS_TEST_VISUALISATION\n#ifndef spiffs_printf\n#define spiffs_printf(...) printf(__VA_ARGS__)\n#endif\n// spiffs_printf argument for a free page\n#ifndef SPIFFS_TEST_VIS_FREE_STR\n#define SPIFFS_TEST_VIS_FREE_STR \"_\"\n#endif\n// spiffs_printf argument for a deleted page\n#ifndef SPIFFS_TEST_VIS_DELE_STR\n#define SPIFFS_TEST_VIS_DELE_STR \"/\"\n#endif\n// spiffs_printf argument for an index page for given object id\n#ifndef SPIFFS_TEST_VIS_INDX_STR\n#define SPIFFS_TEST_VIS_INDX_STR(id) \"i\"\n#endif\n// spiffs_printf argument for a data page for given object id\n#ifndef SPIFFS_TEST_VIS_DATA_STR\n#define SPIFFS_TEST_VIS_DATA_STR(id) \"d\"\n#endif\n#endif\n\n// Types depending on configuration such as the amount of flash bytes\n// given to spiffs file system in total (spiffs_file_system_size),\n// the logical block size (log_block_size), and the logical page size\n// (log_page_size)\n\n// Block index type. Make sure the size of this type can hold\n// the highest number of all blocks - i.e. spiffs_file_system_size / log_block_size\ntypedef u16_t spiffs_block_ix;\n// Page index type. Make sure the size of this type can hold\n// the highest page number of all pages - i.e. spiffs_file_system_size / log_page_size\ntypedef u16_t spiffs_page_ix;\n// Object id type - most significant bit is reserved for index flag. Make sure the\n// size of this type can hold the highest object id on a full system,\n// i.e. 2 + (spiffs_file_system_size / (2*log_page_size))*2\ntypedef u16_t spiffs_obj_id;\n// Object span index type. Make sure the size of this type can\n// hold the largest possible span index on the system -\n// i.e. (spiffs_file_system_size / log_page_size) - 1\ntypedef u16_t spiffs_span_ix;\n\n#endif /* SPIFFS_CONFIG_H_ */\n", "src/spiffs.h": "/*\n * spiffs.h\n *\n * Created on: May 26, 2013\n * Author: petera\n */\n\n#ifndef SPIFFS_H_\n#define SPIFFS_H_\n#if defined(__cplusplus)\nextern \"C\" {\n#endif\n\n#include \"spiffs_config.h\"\n\n#define SPIFFS_OK 0\n#define SPIFFS_ERR_NOT_MOUNTED -10000\n#define SPIFFS_ERR_FULL -10001\n#define SPIFFS_ERR_NOT_FOUND -10002\n#define SPIFFS_ERR_END_OF_OBJECT -10003\n#define SPIFFS_ERR_DELETED -10004\n#define SPIFFS_ERR_NOT_FINALIZED -10005\n#define SPIFFS_ERR_NOT_INDEX -10006\n#define SPIFFS_ERR_OUT_OF_FILE_DESCS -10007\n#define SPIFFS_ERR_FILE_CLOSED -10008\n#define SPIFFS_ERR_FILE_DELETED -10009\n#define SPIFFS_ERR_BAD_DESCRIPTOR -10010\n#define SPIFFS_ERR_IS_INDEX -10011\n#define SPIFFS_ERR_IS_FREE -10012\n#define SPIFFS_ERR_INDEX_SPAN_MISMATCH -10013\n#define SPIFFS_ERR_DATA_SPAN_MISMATCH -10014\n#define SPIFFS_ERR_INDEX_REF_FREE -10015\n#define SPIFFS_ERR_INDEX_REF_LU -10016\n#define SPIFFS_ERR_INDEX_REF_INVALID -10017\n#define SPIFFS_ERR_INDEX_FREE -10018\n#define SPIFFS_ERR_INDEX_LU -10019\n#define SPIFFS_ERR_INDEX_INVALID -10020\n#define SPIFFS_ERR_NOT_WRITABLE -10021\n#define SPIFFS_ERR_NOT_READABLE -10022\n#define SPIFFS_ERR_CONFLICTING_NAME -10023\n#define SPIFFS_ERR_NOT_CONFIGURED -10024\n\n#define SPIFFS_ERR_NOT_A_FS -10025\n#define SPIFFS_ERR_MOUNTED -10026\n#define SPIFFS_ERR_ERASE_FAIL -10027\n#define SPIFFS_ERR_MAGIC_NOT_POSSIBLE -10028\n\n#define SPIFFS_ERR_NO_DELETED_BLOCKS -10029\n\n#define SPIFFS_ERR_FILE_EXISTS -10030\n\n#define SPIFFS_ERR_NOT_A_FILE -10031\n#define SPIFFS_ERR_RO_NOT_IMPL -10032\n#define SPIFFS_ERR_RO_ABORTED_OPERATION -10033\n#define SPIFFS_ERR_PROBE_TOO_FEW_BLOCKS -10034\n#define SPIFFS_ERR_PROBE_NOT_A_FS -10035\n#define SPIFFS_ERR_INTERNAL -10050\n\n#define SPIFFS_ERR_TEST -10100\n\n\n// spiffs file descriptor index type. must be signed\ntypedef s16_t spiffs_file;\n// spiffs file descriptor flags\ntypedef u16_t spiffs_flags;\n// spiffs file mode\ntypedef u16_t spiffs_mode;\n// object type\ntypedef u8_t spiffs_obj_type;\n\nstruct spiffs_t;\n\n#if SPIFFS_HAL_CALLBACK_EXTRA\n\n/* spi read call function type */\ntypedef s32_t (*spiffs_read)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *dst);\n/* spi write call function type */\ntypedef s32_t (*spiffs_write)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *src);\n/* spi erase call function type */\ntypedef s32_t (*spiffs_erase)(struct spiffs_t *fs, u32_t addr, u32_t size);\n\n#else // SPIFFS_HAL_CALLBACK_EXTRA\n\n/* spi read call function type */\ntypedef s32_t (*spiffs_read)(u32_t addr, u32_t size, u8_t *dst);\n/* spi write call function type */\ntypedef s32_t (*spiffs_write)(u32_t addr, u32_t size, u8_t *src);\n/* spi erase call function type */\ntypedef s32_t (*spiffs_erase)(u32_t addr, u32_t size);\n#endif // SPIFFS_HAL_CALLBACK_EXTRA\n\n/* file system check callback report operation */\ntypedef enum {\n SPIFFS_CHECK_LOOKUP = 0,\n SPIFFS_CHECK_INDEX,\n SPIFFS_CHECK_PAGE\n} spiffs_check_type;\n\n/* file system check callback report type */\ntypedef enum {\n SPIFFS_CHECK_PROGRESS = 0,\n SPIFFS_CHECK_ERROR,\n SPIFFS_CHECK_FIX_INDEX,\n SPIFFS_CHECK_FIX_LOOKUP,\n SPIFFS_CHECK_DELETE_ORPHANED_INDEX,\n SPIFFS_CHECK_DELETE_PAGE,\n SPIFFS_CHECK_DELETE_BAD_FILE,\n} spiffs_check_report;\n\n/* file system check callback function */\n#if SPIFFS_HAL_CALLBACK_EXTRA\ntypedef void (*spiffs_check_callback)(struct spiffs_t *fs, spiffs_check_type type, spiffs_check_report report,\n u32_t arg1, u32_t arg2);\n#else // SPIFFS_HAL_CALLBACK_EXTRA\ntypedef void (*spiffs_check_callback)(spiffs_check_type type, spiffs_check_report report,\n u32_t arg1, u32_t arg2);\n#endif // SPIFFS_HAL_CALLBACK_EXTRA\n\n/* file system listener callback operation */\ntypedef enum {\n /* the file has been created */\n SPIFFS_CB_CREATED = 0,\n /* the file has been updated or moved to another page */\n SPIFFS_CB_UPDATED,\n /* the file has been deleted */\n SPIFFS_CB_DELETED,\n} spiffs_fileop_type;\n\n/* file system listener callback function */\ntypedef void (*spiffs_file_callback)(struct spiffs_t *fs, spiffs_fileop_type op, spiffs_obj_id obj_id, spiffs_page_ix pix);\n\n#ifndef SPIFFS_DBG\n#define SPIFFS_DBG(...) \\\n print(__VA_ARGS__)\n#endif\n#ifndef SPIFFS_GC_DBG\n#define SPIFFS_GC_DBG(...) printf(__VA_ARGS__)\n#endif\n#ifndef SPIFFS_CACHE_DBG\n#define SPIFFS_CACHE_DBG(...) printf(__VA_ARGS__)\n#endif\n#ifndef SPIFFS_CHECK_DBG\n#define SPIFFS_CHECK_DBG(...) printf(__VA_ARGS__)\n#endif\n\n/* Any write to the filehandle is appended to end of the file */\n#define SPIFFS_APPEND (1<<0)\n/* If the opened file exists, it will be truncated to zero length before opened */\n#define SPIFFS_TRUNC (1<<1)\n/* If the opened file does not exist, it will be created before opened */\n#define SPIFFS_CREAT (1<<2)\n/* The opened file may only be read */\n#define SPIFFS_RDONLY (1<<3)\n/* The opened file may only be writted */\n#define SPIFFS_WRONLY (1<<4)\n/* The opened file may be both read and writted */\n#define SPIFFS_RDWR (SPIFFS_RDONLY | SPIFFS_WRONLY)\n/* Any writes to the filehandle will never be cached */\n#define SPIFFS_DIRECT (1<<5)\n/* If SPIFFS_CREAT and SPIFFS_EXCL are set, SPIFFS_open() shall fail if the file exists */\n#define SPIFFS_EXCL (1<<6)\n\n#define SPIFFS_SEEK_SET (0)\n#define SPIFFS_SEEK_CUR (1)\n#define SPIFFS_SEEK_END (2)\n\n#define SPIFFS_TYPE_FILE (1)\n#define SPIFFS_TYPE_DIR (2)\n#define SPIFFS_TYPE_HARD_LINK (3)\n#define SPIFFS_TYPE_SOFT_LINK (4)\n\n#ifndef SPIFFS_LOCK\n#define SPIFFS_LOCK(fs)\n#endif\n\n#ifndef SPIFFS_UNLOCK\n#define SPIFFS_UNLOCK(fs)\n#endif\n\n// phys structs\n\n// spiffs spi configuration struct\ntypedef struct {\n // physical read function\n spiffs_read hal_read_f;\n // physical write function\n spiffs_write hal_write_f;\n // physical erase function\n spiffs_erase hal_erase_f;\n#if SPIFFS_SINGLETON == 0\n // physical size of the spi flash\n u32_t phys_size;\n // physical offset in spi flash used for spiffs,\n // must be on block boundary\n u32_t phys_addr;\n // physical size when erasing a block\n u32_t phys_erase_block;\n\n // logical size of a block, must be on physical\n // block size boundary and must never be less than\n // a physical block\n u32_t log_block_size;\n // logical size of a page, must be at least\n // log_block_size / 8\n u32_t log_page_size;\n\n#endif\n#if SPIFFS_FILEHDL_OFFSET\n // an integer offset added to each file handle\n u16_t fh_ix_offset;\n#endif\n} spiffs_config;\n\ntypedef struct spiffs_t {\n // file system configuration\n spiffs_config cfg;\n // number of logical blocks\n u32_t block_count;\n\n // cursor for free blocks, block index\n spiffs_block_ix free_cursor_block_ix;\n // cursor for free blocks, entry index\n int free_cursor_obj_lu_entry;\n // cursor when searching, block index\n spiffs_block_ix cursor_block_ix;\n // cursor when searching, entry index\n int cursor_obj_lu_entry;\n\n // primary work buffer, size of a logical page\n u8_t *lu_work;\n // secondary work buffer, size of a logical page\n u8_t *work;\n // file descriptor memory area\n u8_t *fd_space;\n // available file descriptors\n u32_t fd_count;\n\n // last error\n s32_t err_code;\n\n // current number of free blocks\n u32_t free_blocks;\n // current number of busy pages\n u32_t stats_p_allocated;\n // current number of deleted pages\n u32_t stats_p_deleted;\n // flag indicating that garbage collector is cleaning\n u8_t cleaning;\n // max erase count amongst all blocks\n spiffs_obj_id max_erase_count;\n\n#if SPIFFS_GC_STATS\n u32_t stats_gc_runs;\n#endif\n\n#if SPIFFS_CACHE\n // cache memory\n void *cache;\n // cache size\n u32_t cache_size;\n#if SPIFFS_CACHE_STATS\n u32_t cache_hits;\n u32_t cache_misses;\n#endif\n#endif\n\n // check callback function\n spiffs_check_callback check_cb_f;\n // file callback function\n spiffs_file_callback file_cb_f;\n // mounted flag\n u8_t mounted;\n // user data\n void *user_data;\n // config magic\n u32_t config_magic;\n} spiffs;\n\n/* spiffs file status struct */\ntypedef struct {\n spiffs_obj_id obj_id;\n u32_t size;\n spiffs_obj_type type;\n spiffs_page_ix pix;\n u8_t name[SPIFFS_OBJ_NAME_LEN];\n} spiffs_stat;\n\nstruct spiffs_dirent {\n spiffs_obj_id obj_id;\n u8_t name[SPIFFS_OBJ_NAME_LEN];\n spiffs_obj_type type;\n u32_t size;\n spiffs_page_ix pix;\n};\n\ntypedef struct {\n spiffs *fs;\n spiffs_block_ix block;\n int entry;\n} spiffs_DIR;\n\n// functions\n\n#if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0\n/**\n * Special function. This takes a spiffs config struct and returns the number\n * of blocks this file system was formatted with. This function relies on\n * that following info is set correctly in given config struct:\n *\n * phys_addr, log_page_size, and log_block_size.\n *\n * Also, hal_read_f must be set in the config struct.\n *\n * One must be sure of the correct page size and that the physical address is\n * correct in the probed file system when calling this function. It is not\n * checked if the phys_addr actually points to the start of the file system,\n * so one might get a false positive if entering a phys_addr somewhere in the\n * middle of the file system at block boundary. In addition, it is not checked\n * if the page size is actually correct. If it is not, weird file system sizes\n * will be returned.\n *\n * If this function detects a file system it returns the assumed file system\n * size, which can be used to set the phys_size.\n *\n * Otherwise, it returns an error indicating why it is not regarded as a file\n * system.\n *\n * Note: this function is not protected with SPIFFS_LOCK and SPIFFS_UNLOCK\n * macros. It returns the error code directly, instead of as read by\n * SPIFFS_errno.\n *\n * @param config essential parts of the physical and logical\n * configuration of the file system.\n */\ns32_t SPIFFS_probe_fs(spiffs_config *config);\n#endif // SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0\n\n/**\n * Initializes the file system dynamic parameters and mounts the filesystem.\n * If SPIFFS_USE_MAGIC is enabled the mounting may fail with SPIFFS_ERR_NOT_A_FS\n * if the flash does not contain a recognizable file system.\n * In this case, SPIFFS_format must be called prior to remounting.\n * @param fs the file system struct\n * @param config the physical and logical configuration of the file system\n * @param work a memory work buffer comprising 2*config->log_page_size\n * bytes used throughout all file system operations\n * @param fd_space memory for file descriptors\n * @param fd_space_size memory size of file descriptors\n * @param cache memory for cache, may be null\n * @param cache_size memory size of cache\n * @param check_cb_f callback function for reporting during consistency checks\n */\ns32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work,\n u8_t *fd_space, u32_t fd_space_size,\n void *cache, u32_t cache_size,\n spiffs_check_callback check_cb_f);\n\n/**\n * Unmounts the file system. All file handles will be flushed of any\n * cached writes and closed.\n * @param fs the file system struct\n */\nvoid SPIFFS_unmount(spiffs *fs);\n\n/**\n * Creates a new file.\n * @param fs the file system struct\n * @param path the path of the new file\n * @param mode ignored, for posix compliance\n */\ns32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode);\n\n/**\n * Opens/creates a file.\n * @param fs the file system struct\n * @param path the path of the new file\n * @param flags the flags for the open command, can be combinations of\n * SPIFFS_APPEND, SPIFFS_TRUNC, SPIFFS_CREAT, SPIFFS_RD_ONLY,\n * SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT\n * @param mode ignored, for posix compliance\n */\nspiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode);\n\n/**\n * Opens a file by given dir entry.\n * Optimization purposes, when traversing a file system with SPIFFS_readdir\n * a normal SPIFFS_open would need to traverse the filesystem again to find\n * the file, whilst SPIFFS_open_by_dirent already knows where the file resides.\n * @param fs the file system struct\n * @param e the dir entry to the file\n * @param flags the flags for the open command, can be combinations of\n * SPIFFS_APPEND, SPIFFS_TRUNC, SPIFFS_CREAT, SPIFFS_RD_ONLY,\n * SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT.\n * SPIFFS_CREAT will have no effect in this case.\n * @param mode ignored, for posix compliance\n */\nspiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode);\n\n/**\n * Opens a file by given page index.\n * Optimization purposes, opens a file by directly pointing to the page\n * index in the spi flash.\n * If the page index does not point to a file header SPIFFS_ERR_NOT_A_FILE\n * is returned.\n * @param fs the file system struct\n * @param page_ix the page index\n * @param flags the flags for the open command, can be combinations of\n * SPIFFS_APPEND, SPIFFS_TRUNC, SPIFFS_CREAT, SPIFFS_RD_ONLY,\n * SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT.\n * SPIFFS_CREAT will have no effect in this case.\n * @param mode ignored, for posix compliance\n */\nspiffs_file SPIFFS_open_by_page(spiffs *fs, spiffs_page_ix page_ix, spiffs_flags flags, spiffs_mode mode);\n\n/**\n * Reads from given filehandle.\n * @param fs the file system struct\n * @param fh the filehandle\n * @param buf where to put read data\n * @param len how much to read\n * @returns number of bytes read, or -1 if error\n */\ns32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len);\n\n/**\n * Writes to given filehandle.\n * @param fs the file system struct\n * @param fh the filehandle\n * @param buf the data to write\n * @param len how much to write\n * @returns number of bytes written, or -1 if error\n */\ns32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len);\n\n/**\n * Moves the read/write file offset. Resulting offset is returned or negative if error.\n * lseek(fs, fd, 0, SPIFFS_SEEK_CUR) will thus return current offset.\n * @param fs the file system struct\n * @param fh the filehandle\n * @param offs how much/where to move the offset\n * @param whence if SPIFFS_SEEK_SET, the file offset shall be set to offset bytes\n * if SPIFFS_SEEK_CUR, the file offset shall be set to its current location plus offset\n * if SPIFFS_SEEK_END, the file offset shall be set to the size of the file plus offse, which should be negative\n */\ns32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence);\n\n/**\n * Removes a file by path\n * @param fs the file system struct\n * @param path the path of the file to remove\n */\ns32_t SPIFFS_remove(spiffs *fs, const char *path);\n\n/**\n * Removes a file by filehandle\n * @param fs the file system struct\n * @param fh the filehandle of the file to remove\n */\ns32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh);\n\n/**\n * Gets file status by path\n * @param fs the file system struct\n * @param path the path of the file to stat\n * @param s the stat struct to populate\n */\ns32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s);\n\n/**\n * Gets file status by filehandle\n * @param fs the file system struct\n * @param fh the filehandle of the file to stat\n * @param s the stat struct to populate\n */\ns32_t SPIFFS_fstat(spiffs *fs, spiffs_file fh, spiffs_stat *s);\n\n/**\n * Flushes all pending write operations from cache for given file\n * @param fs the file system struct\n * @param fh the filehandle of the file to flush\n */\ns32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh);\n\n/**\n * Closes a filehandle. If there are pending write operations, these are finalized before closing.\n * @param fs the file system struct\n * @param fh the filehandle of the file to close\n */\ns32_t SPIFFS_close(spiffs *fs, spiffs_file fh);\n\n/**\n * Renames a file\n * @param fs the file system struct\n * @param old path of file to rename\n * @param newPath new path of file\n */\ns32_t SPIFFS_rename(spiffs *fs, const char *old, const char *newPath);\n\n/**\n * Returns last error of last file operation.\n * @param fs the file system struct\n */\ns32_t SPIFFS_errno(spiffs *fs);\n\n/**\n * Clears last error.\n * @param fs the file system struct\n */\nvoid SPIFFS_clearerr(spiffs *fs);\n\n/**\n * Opens a directory stream corresponding to the given name.\n * The stream is positioned at the first entry in the directory.\n * On hydrogen builds the name argument is ignored as hydrogen builds always correspond\n * to a flat file structure - no directories.\n * @param fs the file system struct\n * @param name the name of the directory\n * @param d pointer the directory stream to be populated\n */\nspiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d);\n\n/**\n * Closes a directory stream\n * @param d the directory stream to close\n */\ns32_t SPIFFS_closedir(spiffs_DIR *d);\n\n/**\n * Reads a directory into given spifs_dirent struct.\n * @param d pointer to the directory stream\n * @param e the dirent struct to be populated\n * @returns null if error or end of stream, else given dirent is returned\n */\nstruct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e);\n\n/**\n * Runs a consistency check on given filesystem.\n * @param fs the file system struct\n */\ns32_t SPIFFS_check(spiffs *fs);\n\n/**\n * Returns number of total bytes available and number of used bytes.\n * This is an estimation, and depends on if there a many files with little\n * data or few files with much data.\n * NB: If used number of bytes exceeds total bytes, a SPIFFS_check should\n * run. This indicates a power loss in midst of things. In worst case\n * (repeated powerlosses in mending or gc) you might have to delete some files.\n *\n * @param fs the file system struct\n * @param total total number of bytes in filesystem\n * @param used used number of bytes in filesystem\n */\ns32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used);\n\n/**\n * Formats the entire file system. All data will be lost.\n * The filesystem must not be mounted when calling this.\n *\n * NB: formatting is awkward. Due to backwards compatibility, SPIFFS_mount\n * MUST be called prior to formatting in order to configure the filesystem.\n * If SPIFFS_mount succeeds, SPIFFS_unmount must be called before calling\n * SPIFFS_format.\n * If SPIFFS_mount fails, SPIFFS_format can be called directly without calling\n * SPIFFS_unmount first.\n *\n * @param fs the file system struct\n */\ns32_t SPIFFS_format(spiffs *fs);\n\n/**\n * Returns nonzero if spiffs is mounted, or zero if unmounted.\n * @param fs the file system struct\n */\nu8_t SPIFFS_mounted(spiffs *fs);\n\n/**\n * Tries to find a block where most or all pages are deleted, and erase that\n * block if found. Does not care for wear levelling. Will not move pages\n * around.\n * If parameter max_free_pages are set to 0, only blocks with only deleted\n * pages will be selected.\n *\n * NB: the garbage collector is automatically called when spiffs needs free\n * pages. The reason for this function is to give possibility to do background\n * tidying when user knows the system is idle.\n *\n * Use with care.\n *\n * Setting max_free_pages to anything larger than zero will eventually wear\n * flash more as a block containing free pages can be erased.\n *\n * Will set err_no to SPIFFS_OK if a block was found and erased,\n * SPIFFS_ERR_NO_DELETED_BLOCK if no matching block was found,\n * or other error.\n *\n * @param fs the file system struct\n * @param max_free_pages maximum number allowed free pages in block\n */\ns32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages);\n\n/**\n * Will try to make room for given amount of bytes in the filesystem by moving\n * pages and erasing blocks.\n * If it is physically impossible, err_no will be set to SPIFFS_ERR_FULL. If\n * there already is this amount (or more) of free space, SPIFFS_gc will\n * silently return. It is recommended to call SPIFFS_info before invoking\n * this method in order to determine what amount of bytes to give.\n *\n * NB: the garbage collector is automatically called when spiffs needs free\n * pages. The reason for this function is to give possibility to do background\n * tidying when user knows the system is idle.\n *\n * Use with care.\n *\n * @param fs the file system struct\n * @param size amount of bytes that should be freed\n */\ns32_t SPIFFS_gc(spiffs *fs, u32_t size);\n\n/**\n * Check if EOF reached.\n * @param fs the file system struct\n * @param fh the filehandle of the file to check\n */\ns32_t SPIFFS_eof(spiffs *fs, spiffs_file fh);\n\n/**\n * Get position in file.\n * @param fs the file system struct\n * @param fh the filehandle of the file to check\n */\ns32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);\n\n/**\n * Registers a callback function that keeps track on operations on file\n * headers. Do note, that this callback is called from within internal spiffs\n * mechanisms. Any operations on the actual file system being callbacked from\n * in this callback will mess things up for sure - do not do this.\n * This can be used to track where files are and move around during garbage\n * collection, which in turn can be used to build location tables in ram.\n * Used in conjuction with SPIFFS_open_by_page this may improve performance\n * when opening a lot of files.\n * Must be invoked after mount.\n *\n * @param fs the file system struct\n * @param cb_func the callback on file operations\n */\ns32_t SPIFFS_set_file_callback_func(spiffs *fs, spiffs_file_callback cb_func);\n\n#if SPIFFS_TEST_VISUALISATION\n/**\n * Prints out a visualization of the filesystem.\n * @param fs the file system struct\n */\ns32_t SPIFFS_vis(spiffs *fs);\n#endif\n\n#if SPIFFS_BUFFER_HELP\n/**\n * Returns number of bytes needed for the filedescriptor buffer given\n * amount of file descriptors.\n */\nu32_t SPIFFS_buffer_bytes_for_filedescs(spiffs *fs, u32_t num_descs);\n\n#if SPIFFS_CACHE\n/**\n * Returns number of bytes needed for the cache buffer given\n * amount of cache pages.\n */\nu32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages);\n#endif\n#endif\n\n#if SPIFFS_CACHE\n#endif\n#if defined(__cplusplus)\n}\n#endif\n\n#endif /* SPIFFS_H_ */\n", "src/spiffs_cache.c": "/*\n * spiffs_cache.c\n *\n * Created on: Jun 23, 2013\n * Author: petera\n */\n\n#include \"spiffs.h\"\n#include \"spiffs_nucleus.h\"\n\n#if SPIFFS_CACHE\n\n// returns cached page for give page index, or null if no such cached page\nstatic spiffs_cache_page *spiffs_cache_page_get(spiffs *fs, spiffs_page_ix pix) {\n spiffs_cache *cache = spiffs_get_cache(fs);\n if ((cache->cpage_use_map & cache->cpage_use_mask) == 0) return 0;\n int i;\n for (i = 0; i < cache->cpage_count; i++) {\n spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);\n if ((cache->cpage_use_map & (1<<i)) &&\n (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 &&\n cp->pix == pix ) {\n SPIFFS_CACHE_DBG(\"CACHE_GET: have cache page %i for %04x\\n\", i, pix);\n cp->last_access = cache->last_access;\n return cp;\n }\n }\n //SPIFFS_CACHE_DBG(\"CACHE_GET: no cache for %04x\\n\", pix);\n return 0;\n}\n\n// frees cached page\nstatic s32_t spiffs_cache_page_free(spiffs *fs, int ix, u8_t write_back) {\n s32_t res = SPIFFS_OK;\n spiffs_cache *cache = spiffs_get_cache(fs);\n spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, ix);\n if (cache->cpage_use_map & (1<<ix)) {\n if (write_back &&\n (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 &&\n (cp->flags & SPIFFS_CACHE_FLAG_DIRTY)) {\n u8_t *mem = spiffs_get_cache_page(fs, cache, ix);\n res = SPIFFS_HAL_WRITE(fs, SPIFFS_PAGE_TO_PADDR(fs, cp->pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), mem);\n }\n\n cp->flags = 0;\n cache->cpage_use_map &= ~(1 << ix);\n\n if (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) {\n SPIFFS_CACHE_DBG(\"CACHE_FREE: free cache page %i objid %04x\\n\", ix, cp->obj_id);\n } else {\n SPIFFS_CACHE_DBG(\"CACHE_FREE: free cache page %i pix %04x\\n\", ix, cp->pix);\n }\n }\n\n return res;\n}\n\n// removes the oldest accessed cached page\nstatic s32_t spiffs_cache_page_remove_oldest(spiffs *fs, u8_t flag_mask, u8_t flags) {\n s32_t res = SPIFFS_OK;\n spiffs_cache *cache = spiffs_get_cache(fs);\n\n if ((cache->cpage_use_map & cache->cpage_use_mask) != cache->cpage_use_mask) {\n // at least one free cpage\n return SPIFFS_OK;\n }\n\n // all busy, scan thru all to find the cpage which has oldest access\n int i;\n int cand_ix = -1;\n u32_t oldest_val = 0;\n for (i = 0; i < cache->cpage_count; i++) {\n spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);\n if ((cache->last_access - cp->last_access) > oldest_val &&\n (cp->flags & flag_mask) == flags) {\n oldest_val = cache->last_access - cp->last_access;\n cand_ix = i;\n }\n }\n\n if (cand_ix >= 0) {\n res = spiffs_cache_page_free(fs, cand_ix, 1);\n }\n\n return res;\n}\n\n// allocates a new cached page and returns it, or null if all cache pages are busy\nstatic spiffs_cache_page *spiffs_cache_page_allocate(spiffs *fs) {\n spiffs_cache *cache = spiffs_get_cache(fs);\n if (cache->cpage_use_map == 0xffffffff) {\n // out of cache memory\n return 0;\n }\n int i;\n for (i = 0; i < cache->cpage_count; i++) {\n if ((cache->cpage_use_map & (1<<i)) == 0) {\n spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);\n cache->cpage_use_map |= (1<<i);\n cp->last_access = cache->last_access;\n SPIFFS_CACHE_DBG(\"CACHE_ALLO: allocated cache page %i\\n\", i);\n return cp;\n }\n }\n // out of cache entries\n return 0;\n}\n\n// drops the cache page for give page index\nvoid spiffs_cache_drop_page(spiffs *fs, spiffs_page_ix pix) {\n spiffs_cache_page *cp = spiffs_cache_page_get(fs, pix);\n if (cp) {\n spiffs_cache_page_free(fs, cp->ix, 0);\n }\n}\n\n// ------------------------------\n\n// reads from spi flash or the cache\ns32_t spiffs_phys_rd(\n spiffs *fs,\n u8_t op,\n spiffs_file fh,\n u32_t addr,\n u32_t len,\n u8_t *dst) {\n (void)fh;\n s32_t res = SPIFFS_OK;\n spiffs_cache *cache = spiffs_get_cache(fs);\n spiffs_cache_page *cp = spiffs_cache_page_get(fs, SPIFFS_PADDR_TO_PAGE(fs, addr));\n cache->last_access++;\n if (cp) {\n#if SPIFFS_CACHE_STATS\n fs->cache_hits++;\n#endif\n cp->last_access = cache->last_access;\n } else {\n if ((op & SPIFFS_OP_TYPE_MASK) == SPIFFS_OP_T_OBJ_LU2) {\n // for second layer lookup functions, we do not cache in order to prevent shredding\n return SPIFFS_HAL_READ(fs, addr, len, dst);\n }\n#if SPIFFS_CACHE_STATS\n fs->cache_misses++;\n#endif\n res = spiffs_cache_page_remove_oldest(fs, SPIFFS_CACHE_FLAG_TYPE_WR, 0);\n cp = spiffs_cache_page_allocate(fs);\n if (cp) {\n cp->flags = SPIFFS_CACHE_FLAG_WRTHRU;\n cp->pix = SPIFFS_PADDR_TO_PAGE(fs, addr);\n }\n s32_t res2 = SPIFFS_HAL_READ(fs,\n addr - SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr),\n SPIFFS_CFG_LOG_PAGE_SZ(fs),\n spiffs_get_cache_page(fs, cache, cp->ix));\n if (res2 != SPIFFS_OK) {\n res = res2;\n }\n }\n u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix);\n memcpy(dst, &mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], len);\n return res;\n}\n\n// writes to spi flash and/or the cache\ns32_t spiffs_phys_wr(\n spiffs *fs,\n u8_t op,\n spiffs_file fh,\n u32_t addr,\n u32_t len,\n u8_t *src) {\n (void)fh;\n spiffs_page_ix pix = SPIFFS_PADDR_TO_PAGE(fs, addr);\n spiffs_cache *cache = spiffs_get_cache(fs);\n spiffs_cache_page *cp = spiffs_cache_page_get(fs, pix);\n\n if (cp && (op & SPIFFS_OP_COM_MASK) != SPIFFS_OP_C_WRTHRU) {\n // have a cache page\n // copy in data to cache page\n\n if ((op & SPIFFS_OP_COM_MASK) == SPIFFS_OP_C_DELE &&\n (op & SPIFFS_OP_TYPE_MASK) != SPIFFS_OP_T_OBJ_LU) {\n // page is being deleted, wipe from cache - unless it is a lookup page\n spiffs_cache_page_free(fs, cp->ix, 0);\n return SPIFFS_HAL_WRITE(fs, addr, len, src);\n }\n\n u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix);\n memcpy(&mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], src, len);\n\n cache->last_access++;\n cp->last_access = cache->last_access;\n\n if (cp->flags & SPIFFS_CACHE_FLAG_WRTHRU) {\n // page is being updated, no write-cache, just pass thru\n return SPIFFS_HAL_WRITE(fs, addr, len, src);\n } else {\n return SPIFFS_OK;\n }\n } else {\n // no cache page, no write cache - just write thru\n return SPIFFS_HAL_WRITE(fs, addr, len, src);\n }\n}\n\n#if SPIFFS_CACHE_WR\n// returns the cache page that this fd refers, or null if no cache page\nspiffs_cache_page *spiffs_cache_page_get_by_fd(spiffs *fs, spiffs_fd *fd) {\n spiffs_cache *cache = spiffs_get_cache(fs);\n\n if ((cache->cpage_use_map & cache->cpage_use_mask) == 0) {\n // all cpages free, no cpage cannot be assigned to obj_id\n return 0;\n }\n\n int i;\n for (i = 0; i < cache->cpage_count; i++) {\n spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);\n if ((cache->cpage_use_map & (1<<i)) &&\n (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) &&\n cp->obj_id == fd->obj_id) {\n return cp;\n }\n }\n\n return 0;\n}\n\n// allocates a new cache page and refers this to given fd - flushes an old cache\n// page if all cache is busy\nspiffs_cache_page *spiffs_cache_page_allocate_by_fd(spiffs *fs, spiffs_fd *fd) {\n // before this function is called, it is ensured that there is no already existing\n // cache page with same object id\n spiffs_cache_page_remove_oldest(fs, SPIFFS_CACHE_FLAG_TYPE_WR, 0);\n spiffs_cache_page *cp = spiffs_cache_page_allocate(fs);\n if (cp == 0) {\n // could not get cache page\n return 0;\n }\n\n cp->flags = SPIFFS_CACHE_FLAG_TYPE_WR;\n cp->obj_id = fd->obj_id;\n fd->cache_page = cp;\n return cp;\n}\n\n// unrefers all fds that this cache page refers to and releases the cache page\nvoid spiffs_cache_fd_release(spiffs *fs, spiffs_cache_page *cp) {\n if (cp == 0) return;\n u32_t i;\n spiffs_fd *fds = (spiffs_fd *)fs->fd_space;\n for (i = 0; i < fs->fd_count; i++) {\n spiffs_fd *cur_fd = &fds[i];\n if (cur_fd->file_nbr != 0 && cur_fd->cache_page == cp) {\n cur_fd->cache_page = 0;\n }\n }\n spiffs_cache_page_free(fs, cp->ix, 0);\n\n cp->obj_id = 0;\n}\n\n#endif\n\n// initializes the cache\nvoid spiffs_cache_init(spiffs *fs) {\n if (fs->cache == 0) return;\n u32_t sz = fs->cache_size;\n u32_t cache_mask = 0;\n int i;\n int cache_entries =\n (sz - sizeof(spiffs_cache)) / (SPIFFS_CACHE_PAGE_SIZE(fs));\n if (cache_entries <= 0) return;\n\n for (i = 0; i < cache_entries; i++) {\n cache_mask <<= 1;\n cache_mask |= 1;\n }\n\n spiffs_cache cache;\n memset(&cache, 0, sizeof(spiffs_cache));\n cache.cpage_count = cache_entries;\n cache.cpages = (u8_t *)((u8_t *)fs->cache + sizeof(spiffs_cache));\n\n cache.cpage_use_map = 0xffffffff;\n cache.cpage_use_mask = cache_mask;\n memcpy(fs->cache, &cache, sizeof(spiffs_cache));\n\n spiffs_cache *c = spiffs_get_cache(fs);\n\n memset(c->cpages, 0, c->cpage_count * SPIFFS_CACHE_PAGE_SIZE(fs));\n\n c->cpage_use_map &= ~(c->cpage_use_mask);\n for (i = 0; i < cache.cpage_count; i++) {\n spiffs_get_cache_page_hdr(fs, c, i)->ix = i;\n }\n}\n\n#endif // SPIFFS_CACHE\n", "src/spiffs_check.c": "/*\n * spiffs_check.c\n *\n * Contains functionality for checking file system consistency\n * and mending problems.\n * Three levels of consistency checks are implemented:\n *\n * Look up consistency\n * Checks if indices in lookup pages are coherent with page headers\n * Object index consistency\n * Checks if there are any orphaned object indices (missing object index headers).\n * If an object index is found but not its header, the object index is deleted.\n * This is critical for the following page consistency check.\n * Page consistency\n * Checks for pages that ought to be indexed, ought not to be indexed, are multiple indexed\n *\n *\n * Created on: Jul 7, 2013\n * Author: petera\n */\n\n\n#include \"spiffs.h\"\n#include \"spiffs_nucleus.h\"\n\n#if !SPIFFS_READ_ONLY\n\n#if SPIFFS_HAL_CALLBACK_EXTRA\n#define CHECK_CB(_fs, _type, _rep, _arg1, _arg2) \\\n do { \\\n if ((_fs)->check_cb_f) (_fs)->check_cb_f((_fs), (_type), (_rep), (_arg1), (_arg2)); \\\n } while (0)\n#else\n#define CHECK_CB(_fs, _type, _rep, _arg1, _arg2) \\\n do { \\\n if ((_fs)->check_cb_f) (_fs)->check_cb_f((_type), (_rep), (_arg1), (_arg2)); \\\n } while (0)\n#endif\n\n//---------------------------------------\n// Look up consistency\n\n// searches in the object indices and returns the referenced page index given\n// the object id and the data span index\n// destroys fs->lu_work\nstatic s32_t spiffs_object_get_data_page_index_reference(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_span_ix data_spix,\n spiffs_page_ix *pix,\n spiffs_page_ix *objix_pix) {\n s32_t res;\n\n // calculate object index span index for given data page span index\n spiffs_span_ix objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);\n\n // find obj index for obj id and span index\n res = spiffs_obj_lu_find_id_and_span(fs, obj_id | SPIFFS_OBJ_ID_IX_FLAG, objix_spix, 0, objix_pix);\n SPIFFS_CHECK_RES(res);\n\n // load obj index entry\n u32_t addr = SPIFFS_PAGE_TO_PADDR(fs, *objix_pix);\n if (objix_spix == 0) {\n // get referenced page from object index header\n addr += sizeof(spiffs_page_object_ix_header) + data_spix * sizeof(spiffs_page_ix);\n } else {\n // get referenced page from object index\n addr += sizeof(spiffs_page_object_ix) + SPIFFS_OBJ_IX_ENTRY(fs, data_spix) * sizeof(spiffs_page_ix);\n }\n\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ, 0, addr, sizeof(spiffs_page_ix), (u8_t *)pix);\n\n return res;\n}\n\n// copies page contents to a new page\nstatic s32_t spiffs_rewrite_page(spiffs *fs, spiffs_page_ix cur_pix, spiffs_page_header *p_hdr, spiffs_page_ix *new_pix) {\n s32_t res;\n res = spiffs_page_allocate_data(fs, p_hdr->obj_id, p_hdr, 0,0,0,0, new_pix);\n SPIFFS_CHECK_RES(res);\n res = spiffs_phys_cpy(fs, 0,\n SPIFFS_PAGE_TO_PADDR(fs, *new_pix) + sizeof(spiffs_page_header),\n SPIFFS_PAGE_TO_PADDR(fs, cur_pix) + sizeof(spiffs_page_header),\n SPIFFS_DATA_PAGE_SIZE(fs));\n SPIFFS_CHECK_RES(res);\n return res;\n}\n\n// rewrites the object index for given object id and replaces the\n// data page index to a new page index\nstatic s32_t spiffs_rewrite_index(spiffs *fs, spiffs_obj_id obj_id, spiffs_span_ix data_spix, spiffs_page_ix new_data_pix, spiffs_page_ix objix_pix) {\n s32_t res;\n spiffs_block_ix bix;\n int entry;\n spiffs_page_ix free_pix;\n obj_id |= SPIFFS_OBJ_ID_IX_FLAG;\n\n // find free entry\n res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);\n SPIFFS_CHECK_RES(res);\n free_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);\n\n // calculate object index span index for given data page span index\n spiffs_span_ix objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);\n if (objix_spix == 0) {\n // calc index in index header\n entry = data_spix;\n } else {\n // calc entry in index\n entry = SPIFFS_OBJ_IX_ENTRY(fs, data_spix);\n\n }\n // load index\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);\n SPIFFS_CHECK_RES(res);\n spiffs_page_header *objix_p_hdr = (spiffs_page_header *)fs->lu_work;\n\n // be ultra safe, double check header against provided data\n if (objix_p_hdr->obj_id != obj_id) {\n spiffs_page_delete(fs, free_pix);\n return SPIFFS_ERR_CHECK_OBJ_ID_MISM;\n }\n if (objix_p_hdr->span_ix != objix_spix) {\n spiffs_page_delete(fs, free_pix);\n return SPIFFS_ERR_CHECK_SPIX_MISM;\n }\n if ((objix_p_hdr->flags & (SPIFFS_PH_FLAG_USED | SPIFFS_PH_FLAG_IXDELE | SPIFFS_PH_FLAG_INDEX |\n SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_DELET)) !=\n (SPIFFS_PH_FLAG_IXDELE | SPIFFS_PH_FLAG_DELET)) {\n spiffs_page_delete(fs, free_pix);\n return SPIFFS_ERR_CHECK_FLAGS_BAD;\n }\n\n // rewrite in mem\n if (objix_spix == 0) {\n ((spiffs_page_ix*)((u8_t *)fs->lu_work + sizeof(spiffs_page_object_ix_header)))[data_spix] = new_data_pix;\n } else {\n ((spiffs_page_ix*)((u8_t *)fs->lu_work + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = new_data_pix;\n }\n\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n 0, SPIFFS_PAGE_TO_PADDR(fs, free_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);\n SPIFFS_CHECK_RES(res);\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,\n 0, SPIFFS_BLOCK_TO_PADDR(fs, SPIFFS_BLOCK_FOR_PAGE(fs, free_pix)) + SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, free_pix) * sizeof(spiffs_page_ix),\n sizeof(spiffs_obj_id),\n (u8_t *)&obj_id);\n SPIFFS_CHECK_RES(res);\n res = spiffs_page_delete(fs, objix_pix);\n\n return res;\n}\n\n// deletes an object just by marking object index header as deleted\nstatic s32_t spiffs_delete_obj_lazy(spiffs *fs, spiffs_obj_id obj_id) {\n spiffs_page_ix objix_hdr_pix;\n s32_t res;\n res = spiffs_obj_lu_find_id_and_span(fs, obj_id, 0, 0, &objix_hdr_pix);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n return SPIFFS_OK;\n }\n SPIFFS_CHECK_RES(res);\n u8_t flags = 0xff & ~SPIFFS_PH_FLAG_IXDELE;\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,\n 0, SPIFFS_PAGE_TO_PADDR(fs, objix_hdr_pix) + offsetof(spiffs_page_header, flags),\n sizeof(u8_t),\n (u8_t *)&flags);\n return res;\n}\n\n// validates the given look up entry\nstatic s32_t spiffs_lookup_check_validate(spiffs *fs, spiffs_obj_id lu_obj_id, spiffs_page_header *p_hdr,\n spiffs_page_ix cur_pix, spiffs_block_ix cur_block, int cur_entry, int *reload_lu) {\n (void)cur_block;\n (void)cur_entry;\n u8_t delete_page = 0;\n s32_t res = SPIFFS_OK;\n spiffs_page_ix objix_pix;\n spiffs_page_ix ref_pix;\n // check validity, take actions\n if (((lu_obj_id == SPIFFS_OBJ_ID_DELETED) && (p_hdr->flags & SPIFFS_PH_FLAG_DELET)) ||\n ((lu_obj_id == SPIFFS_OBJ_ID_FREE) && (p_hdr->flags & SPIFFS_PH_FLAG_USED) == 0)) {\n // look up entry deleted / free but used in page header\n SPIFFS_CHECK_DBG(\"LU: pix %04x deleted/free in lu but not on page\\n\", cur_pix);\n *reload_lu = 1;\n delete_page = 1;\n if (p_hdr->flags & SPIFFS_PH_FLAG_INDEX) {\n // header says data page\n // data page can be removed if not referenced by some object index\n res = spiffs_object_get_data_page_index_reference(fs, p_hdr->obj_id, p_hdr->span_ix, &ref_pix, &objix_pix);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n // no object with this id, so remove page safely\n res = SPIFFS_OK;\n } else {\n SPIFFS_CHECK_RES(res);\n if (ref_pix == cur_pix) {\n // data page referenced by object index but deleted in lu\n // copy page to new place and re-write the object index to new place\n spiffs_page_ix new_pix;\n res = spiffs_rewrite_page(fs, cur_pix, p_hdr, &new_pix);\n SPIFFS_CHECK_DBG(\"LU: FIXUP: data page not found elsewhere, rewriting %04x to new page %04x\\n\", cur_pix, new_pix);\n SPIFFS_CHECK_RES(res);\n *reload_lu = 1;\n SPIFFS_CHECK_DBG(\"LU: FIXUP: %04x rewritten to %04x, affected objix_pix %04x\\n\", cur_pix, new_pix, objix_pix);\n res = spiffs_rewrite_index(fs, p_hdr->obj_id, p_hdr->span_ix, new_pix, objix_pix);\n if (res <= _SPIFFS_ERR_CHECK_FIRST && res > _SPIFFS_ERR_CHECK_LAST) {\n // index bad also, cannot mend this file\n SPIFFS_CHECK_DBG(\"LU: FIXUP: index bad %i, cannot mend!\\n\", res);\n res = spiffs_page_delete(fs, new_pix);\n SPIFFS_CHECK_RES(res);\n res = spiffs_delete_obj_lazy(fs, p_hdr->obj_id);\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_DELETE_BAD_FILE, p_hdr->obj_id, 0);\n } else {\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_FIX_INDEX, p_hdr->obj_id, p_hdr->span_ix);\n }\n SPIFFS_CHECK_RES(res);\n }\n }\n } else {\n // header says index page\n // index page can be removed if other index with same obj_id and spanix is found\n res = spiffs_obj_lu_find_id_and_span(fs, p_hdr->obj_id | SPIFFS_OBJ_ID_IX_FLAG, p_hdr->span_ix, cur_pix, 0);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n // no such index page found, check for a data page amongst page headers\n // lu cannot be trusted\n res = spiffs_obj_lu_find_id_and_span_by_phdr(fs, p_hdr->obj_id | SPIFFS_OBJ_ID_IX_FLAG, 0, 0, 0);\n if (res == SPIFFS_OK) { // ignore other errors\n // got a data page also, assume lu corruption only, rewrite to new page\n spiffs_page_ix new_pix;\n res = spiffs_rewrite_page(fs, cur_pix, p_hdr, &new_pix);\n SPIFFS_CHECK_DBG(\"LU: FIXUP: ix page with data not found elsewhere, rewriting %04x to new page %04x\\n\", cur_pix, new_pix);\n SPIFFS_CHECK_RES(res);\n *reload_lu = 1;\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_FIX_LOOKUP, p_hdr->obj_id, p_hdr->span_ix);\n }\n } else {\n SPIFFS_CHECK_RES(res);\n }\n }\n }\n if (lu_obj_id != SPIFFS_OBJ_ID_FREE && lu_obj_id != SPIFFS_OBJ_ID_DELETED) {\n // look up entry used\n if ((p_hdr->obj_id | SPIFFS_OBJ_ID_IX_FLAG) != (lu_obj_id | SPIFFS_OBJ_ID_IX_FLAG)) {\n SPIFFS_CHECK_DBG(\"LU: pix %04x differ in obj_id lu:%04x ph:%04x\\n\", cur_pix, lu_obj_id, p_hdr->obj_id);\n delete_page = 1;\n if ((p_hdr->flags & SPIFFS_PH_FLAG_DELET) == 0 ||\n (p_hdr->flags & SPIFFS_PH_FLAG_FINAL) ||\n (p_hdr->flags & (SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_IXDELE)) == 0) {\n // page deleted or not finalized, just remove it\n } else {\n if (p_hdr->flags & SPIFFS_PH_FLAG_INDEX) {\n // if data page, check for reference to this page\n res = spiffs_object_get_data_page_index_reference(fs, p_hdr->obj_id, p_hdr->span_ix, &ref_pix, &objix_pix);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n // no object with this id, so remove page safely\n res = SPIFFS_OK;\n } else {\n SPIFFS_CHECK_RES(res);\n // if found, rewrite page with object id, update index, and delete current\n if (ref_pix == cur_pix) {\n spiffs_page_ix new_pix;\n res = spiffs_rewrite_page(fs, cur_pix, p_hdr, &new_pix);\n SPIFFS_CHECK_RES(res);\n res = spiffs_rewrite_index(fs, p_hdr->obj_id, p_hdr->span_ix, new_pix, objix_pix);\n if (res <= _SPIFFS_ERR_CHECK_FIRST && res > _SPIFFS_ERR_CHECK_LAST) {\n // index bad also, cannot mend this file\n SPIFFS_CHECK_DBG(\"LU: FIXUP: index bad %i, cannot mend!\\n\", res);\n res = spiffs_page_delete(fs, new_pix);\n SPIFFS_CHECK_RES(res);\n res = spiffs_delete_obj_lazy(fs, p_hdr->obj_id);\n *reload_lu = 1;\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_DELETE_BAD_FILE, p_hdr->obj_id, 0);\n }\n SPIFFS_CHECK_RES(res);\n }\n }\n } else {\n // else if index, check for other pages with both obj_id's and spanix\n spiffs_page_ix objix_pix_lu, objix_pix_ph;\n // see if other object index page exists for lookup obj id and span index\n res = spiffs_obj_lu_find_id_and_span(fs, lu_obj_id | SPIFFS_OBJ_ID_IX_FLAG, p_hdr->span_ix, 0, &objix_pix_lu);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n res = SPIFFS_OK;\n objix_pix_lu = 0;\n }\n SPIFFS_CHECK_RES(res);\n // see if other object index exists for page header obj id and span index\n res = spiffs_obj_lu_find_id_and_span(fs, p_hdr->obj_id | SPIFFS_OBJ_ID_IX_FLAG, p_hdr->span_ix, 0, &objix_pix_ph);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n res = SPIFFS_OK;\n objix_pix_ph = 0;\n }\n SPIFFS_CHECK_RES(res);\n // if both obj_id's found, just delete current\n if (objix_pix_ph == 0 || objix_pix_lu == 0) {\n // otherwise try finding first corresponding data pages\n spiffs_page_ix data_pix_lu, data_pix_ph;\n // see if other data page exists for look up obj id and span index\n res = spiffs_obj_lu_find_id_and_span(fs, lu_obj_id & ~SPIFFS_OBJ_ID_IX_FLAG, 0, 0, &data_pix_lu);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n res = SPIFFS_OK;\n objix_pix_lu = 0;\n }\n SPIFFS_CHECK_RES(res);\n // see if other data page exists for page header obj id and span index\n res = spiffs_obj_lu_find_id_and_span(fs, p_hdr->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG, 0, 0, &data_pix_ph);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n res = SPIFFS_OK;\n objix_pix_ph = 0;\n }\n SPIFFS_CHECK_RES(res);\n\n spiffs_page_header new_ph;\n new_ph.flags = 0xff & ~(SPIFFS_PH_FLAG_USED | SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_FINAL);\n new_ph.span_ix = p_hdr->span_ix;\n spiffs_page_ix new_pix;\n if ((objix_pix_lu && data_pix_lu && data_pix_ph && objix_pix_ph == 0) ||\n (objix_pix_lu == 0 && data_pix_ph && objix_pix_ph == 0)) {\n // got a data page for page header obj id\n // rewrite as obj_id_ph\n new_ph.obj_id = p_hdr->obj_id | SPIFFS_OBJ_ID_IX_FLAG;\n res = spiffs_rewrite_page(fs, cur_pix, &new_ph, &new_pix);\n SPIFFS_CHECK_DBG(\"LU: FIXUP: rewrite page %04x as %04x to pix %04x\\n\", cur_pix, new_ph.obj_id, new_pix);\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_FIX_LOOKUP, p_hdr->obj_id, p_hdr->span_ix);\n SPIFFS_CHECK_RES(res);\n *reload_lu = 1;\n } else if ((objix_pix_ph && data_pix_ph && data_pix_lu && objix_pix_lu == 0) ||\n (objix_pix_ph == 0 && data_pix_lu && objix_pix_lu == 0)) {\n // got a data page for look up obj id\n // rewrite as obj_id_lu\n new_ph.obj_id = lu_obj_id | SPIFFS_OBJ_ID_IX_FLAG;\n SPIFFS_CHECK_DBG(\"LU: FIXUP: rewrite page %04x as %04x\\n\", cur_pix, new_ph.obj_id);\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_FIX_LOOKUP, p_hdr->obj_id, p_hdr->span_ix);\n res = spiffs_rewrite_page(fs, cur_pix, &new_ph, &new_pix);\n SPIFFS_CHECK_RES(res);\n *reload_lu = 1;\n } else {\n // cannot safely do anything\n SPIFFS_CHECK_DBG(\"LU: FIXUP: nothing to do, just delete\\n\");\n }\n }\n }\n }\n } else if (((lu_obj_id & SPIFFS_OBJ_ID_IX_FLAG) && (p_hdr->flags & SPIFFS_PH_FLAG_INDEX)) ||\n ((lu_obj_id & SPIFFS_OBJ_ID_IX_FLAG) == 0 && (p_hdr->flags & SPIFFS_PH_FLAG_INDEX) == 0)) {\n SPIFFS_CHECK_DBG(\"LU: %04x lu/page index marking differ\\n\", cur_pix);\n spiffs_page_ix data_pix, objix_pix_d;\n // see if other data page exists for given obj id and span index\n res = spiffs_obj_lu_find_id_and_span(fs, lu_obj_id & ~SPIFFS_OBJ_ID_IX_FLAG, p_hdr->span_ix, cur_pix, &data_pix);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n res = SPIFFS_OK;\n data_pix = 0;\n }\n SPIFFS_CHECK_RES(res);\n // see if other object index exists for given obj id and span index\n res = spiffs_obj_lu_find_id_and_span(fs, lu_obj_id | SPIFFS_OBJ_ID_IX_FLAG, p_hdr->span_ix, cur_pix, &objix_pix_d);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n res = SPIFFS_OK;\n objix_pix_d = 0;\n }\n SPIFFS_CHECK_RES(res);\n\n delete_page = 1;\n // if other data page exists and object index exists, just delete page\n if (data_pix && objix_pix_d) {\n SPIFFS_CHECK_DBG(\"LU: FIXUP: other index and data page exists, simply remove\\n\");\n } else\n // if only data page exists, make this page index\n if (data_pix && objix_pix_d == 0) {\n SPIFFS_CHECK_DBG(\"LU: FIXUP: other data page exists, make this index\\n\");\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_FIX_INDEX, lu_obj_id, p_hdr->span_ix);\n spiffs_page_header new_ph;\n spiffs_page_ix new_pix;\n new_ph.flags = 0xff & ~(SPIFFS_PH_FLAG_USED | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_INDEX);\n new_ph.obj_id = lu_obj_id | SPIFFS_OBJ_ID_IX_FLAG;\n new_ph.span_ix = p_hdr->span_ix;\n res = spiffs_page_allocate_data(fs, new_ph.obj_id, &new_ph, 0, 0, 0, 1, &new_pix);\n SPIFFS_CHECK_RES(res);\n res = spiffs_phys_cpy(fs, 0, SPIFFS_PAGE_TO_PADDR(fs, new_pix) + sizeof(spiffs_page_header),\n SPIFFS_PAGE_TO_PADDR(fs, cur_pix) + sizeof(spiffs_page_header),\n SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_header));\n SPIFFS_CHECK_RES(res);\n } else\n // if only index exists, make data page\n if (data_pix == 0 && objix_pix_d) {\n SPIFFS_CHECK_DBG(\"LU: FIXUP: other index page exists, make this data\\n\");\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_FIX_LOOKUP, lu_obj_id, p_hdr->span_ix);\n spiffs_page_header new_ph;\n spiffs_page_ix new_pix;\n new_ph.flags = 0xff & ~(SPIFFS_PH_FLAG_USED | SPIFFS_PH_FLAG_FINAL);\n new_ph.obj_id = lu_obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;\n new_ph.span_ix = p_hdr->span_ix;\n res = spiffs_page_allocate_data(fs, new_ph.obj_id, &new_ph, 0, 0, 0, 1, &new_pix);\n SPIFFS_CHECK_RES(res);\n res = spiffs_phys_cpy(fs, 0, SPIFFS_PAGE_TO_PADDR(fs, new_pix) + sizeof(spiffs_page_header),\n SPIFFS_PAGE_TO_PADDR(fs, cur_pix) + sizeof(spiffs_page_header),\n SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_header));\n SPIFFS_CHECK_RES(res);\n } else {\n // if nothing exists, we cannot safely make a decision - delete\n }\n }\n else if ((p_hdr->flags & SPIFFS_PH_FLAG_DELET) == 0) {\n SPIFFS_CHECK_DBG(\"LU: pix %04x busy in lu but deleted on page\\n\", cur_pix);\n delete_page = 1;\n } else if ((p_hdr->flags & SPIFFS_PH_FLAG_FINAL)) {\n SPIFFS_CHECK_DBG(\"LU: pix %04x busy but not final\\n\", cur_pix);\n // page can be removed if not referenced by object index\n *reload_lu = 1;\n res = spiffs_object_get_data_page_index_reference(fs, lu_obj_id, p_hdr->span_ix, &ref_pix, &objix_pix);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n // no object with this id, so remove page safely\n res = SPIFFS_OK;\n delete_page = 1;\n } else {\n SPIFFS_CHECK_RES(res);\n if (ref_pix != cur_pix) {\n SPIFFS_CHECK_DBG(\"LU: FIXUP: other finalized page is referred, just delete\\n\");\n delete_page = 1;\n } else {\n // page referenced by object index but not final\n // just finalize\n SPIFFS_CHECK_DBG(\"LU: FIXUP: unfinalized page is referred, finalizing\\n\");\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_FIX_LOOKUP, p_hdr->obj_id, p_hdr->span_ix);\n u8_t flags = 0xff & ~SPIFFS_PH_FLAG_FINAL;\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n 0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix) + offsetof(spiffs_page_header, flags),\n sizeof(u8_t), (u8_t*)&flags);\n }\n }\n }\n }\n\n if (delete_page) {\n SPIFFS_CHECK_DBG(\"LU: FIXUP: deleting page %04x\\n\", cur_pix);\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_DELETE_PAGE, cur_pix, 0);\n res = spiffs_page_delete(fs, cur_pix);\n SPIFFS_CHECK_RES(res);\n }\n\n return res;\n}\n\nstatic s32_t spiffs_lookup_check_v(spiffs *fs, spiffs_obj_id obj_id, spiffs_block_ix cur_block, int cur_entry,\n const void *user_const_p, void *user_var_p) {\n (void)user_const_p;\n (void)user_var_p;\n s32_t res = SPIFFS_OK;\n spiffs_page_header p_hdr;\n spiffs_page_ix cur_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, cur_block, cur_entry);\n\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_PROGRESS,\n (cur_block * 256)/fs->block_count, 0);\n\n // load header\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix), sizeof(spiffs_page_header), (u8_t*)&p_hdr);\n SPIFFS_CHECK_RES(res);\n\n int reload_lu = 0;\n\n res = spiffs_lookup_check_validate(fs, obj_id, &p_hdr, cur_pix, cur_block, cur_entry, &reload_lu);\n SPIFFS_CHECK_RES(res);\n\n if (res == SPIFFS_OK) {\n return reload_lu ? SPIFFS_VIS_COUNTINUE_RELOAD : SPIFFS_VIS_COUNTINUE;\n }\n return res;\n}\n\n\n// Scans all object look up. For each entry, corresponding page header is checked for validity.\n// If an object index header page is found, this is also checked\ns32_t spiffs_lookup_consistency_check(spiffs *fs, u8_t check_all_objects) {\n (void)check_all_objects;\n s32_t res = SPIFFS_OK;\n\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_PROGRESS, 0, 0);\n\n res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_lookup_check_v, 0, 0, 0, 0);\n\n if (res == SPIFFS_VIS_END) {\n res = SPIFFS_OK;\n }\n\n if (res != SPIFFS_OK) {\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_ERROR, res, 0);\n }\n\n CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_PROGRESS, 256, 0);\n\n return res;\n}\n\n//---------------------------------------\n// Page consistency\n\n// Scans all pages (except lu pages), reserves 4 bits in working memory for each page\n// bit 0: 0 == FREE|DELETED, 1 == USED\n// bit 1: 0 == UNREFERENCED, 1 == REFERENCED\n// bit 2: 0 == NOT_INDEX, 1 == INDEX\n// bit 3: unused\n// A consistent file system will have only pages being\n// * x000 free, unreferenced, not index\n// * x011 used, referenced only once, not index\n// * x101 used, unreferenced, index\n// The working memory might not fit all pages so several scans might be needed\nstatic s32_t spiffs_page_consistency_check_i(spiffs *fs) {\n const u32_t bits = 4;\n const spiffs_page_ix pages_per_scan = SPIFFS_CFG_LOG_PAGE_SZ(fs) * 8 / bits;\n\n s32_t res = SPIFFS_OK;\n spiffs_page_ix pix_offset = 0;\n\n // for each range of pages fitting into work memory\n while (pix_offset < SPIFFS_PAGES_PER_BLOCK(fs) * fs->block_count) {\n // set this flag to abort all checks and rescan the page range\n u8_t restart = 0;\n memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));\n\n spiffs_block_ix cur_block = 0;\n // build consistency bitmap for id range traversing all blocks\n while (!restart && cur_block < fs->block_count) {\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_PROGRESS,\n (pix_offset*256)/(SPIFFS_PAGES_PER_BLOCK(fs) * fs->block_count) +\n ((((cur_block * pages_per_scan * 256)/ (SPIFFS_PAGES_PER_BLOCK(fs) * fs->block_count))) / fs->block_count),\n 0);\n // traverse each page except for lookup pages\n spiffs_page_ix cur_pix = SPIFFS_OBJ_LOOKUP_PAGES(fs) + SPIFFS_PAGES_PER_BLOCK(fs) * cur_block;\n while (!restart && cur_pix < SPIFFS_PAGES_PER_BLOCK(fs) * (cur_block+1)) {\n //if ((cur_pix & 0xff) == 0)\n // SPIFFS_CHECK_DBG(\"PA: processing pix %08x, block %08x of pix %08x, block %08x\\n\",\n // cur_pix, cur_block, SPIFFS_PAGES_PER_BLOCK(fs) * fs->block_count, fs->block_count);\n\n // read header\n spiffs_page_header p_hdr;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix), sizeof(spiffs_page_header), (u8_t*)&p_hdr);\n SPIFFS_CHECK_RES(res);\n\n u8_t within_range = (cur_pix >= pix_offset && cur_pix < pix_offset + pages_per_scan);\n const u32_t pix_byte_ix = (cur_pix - pix_offset) / (8/bits);\n const u8_t pix_bit_ix = (cur_pix & ((8/bits)-1)) * bits;\n\n if (within_range &&\n (p_hdr.flags & SPIFFS_PH_FLAG_DELET) && (p_hdr.flags & SPIFFS_PH_FLAG_USED) == 0) {\n // used\n fs->work[pix_byte_ix] |= (1<<(pix_bit_ix + 0));\n }\n if ((p_hdr.flags & SPIFFS_PH_FLAG_DELET) &&\n (p_hdr.flags & SPIFFS_PH_FLAG_IXDELE) &&\n (p_hdr.flags & (SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_USED)) == 0) {\n // found non-deleted index\n if (within_range) {\n fs->work[pix_byte_ix] |= (1<<(pix_bit_ix + 2));\n }\n\n // load non-deleted index\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);\n SPIFFS_CHECK_RES(res);\n\n // traverse index for referenced pages\n spiffs_page_ix *object_page_index;\n spiffs_page_header *objix_p_hdr = (spiffs_page_header *)fs->lu_work;\n\n int entries;\n int i;\n spiffs_span_ix data_spix_offset;\n if (p_hdr.span_ix == 0) {\n // object header page index\n entries = SPIFFS_OBJ_HDR_IX_LEN(fs);\n data_spix_offset = 0;\n object_page_index = (spiffs_page_ix *)((u8_t *)fs->lu_work + sizeof(spiffs_page_object_ix_header));\n } else {\n // object page index\n entries = SPIFFS_OBJ_IX_LEN(fs);\n data_spix_offset = SPIFFS_OBJ_HDR_IX_LEN(fs) + SPIFFS_OBJ_IX_LEN(fs) * (p_hdr.span_ix - 1);\n object_page_index = (spiffs_page_ix *)((u8_t *)fs->lu_work + sizeof(spiffs_page_object_ix));\n }\n\n // for all entries in index\n for (i = 0; !restart && i < entries; i++) {\n spiffs_page_ix rpix = object_page_index[i];\n u8_t rpix_within_range = rpix >= pix_offset && rpix < pix_offset + pages_per_scan;\n\n if ((rpix != (spiffs_page_ix)-1 && rpix > SPIFFS_MAX_PAGES(fs))\n || (rpix_within_range && SPIFFS_IS_LOOKUP_PAGE(fs, rpix))) {\n\n // bad reference\n SPIFFS_CHECK_DBG(\"PA: pix %04x bad pix / LU referenced from page %04x\\n\",\n rpix, cur_pix);\n // check for data page elsewhere\n spiffs_page_ix data_pix;\n res = spiffs_obj_lu_find_id_and_span(fs, objix_p_hdr->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,\n data_spix_offset + i, 0, &data_pix);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n res = SPIFFS_OK;\n data_pix = 0;\n }\n SPIFFS_CHECK_RES(res);\n if (data_pix == 0) {\n // if not, allocate free page\n spiffs_page_header new_ph;\n new_ph.flags = 0xff & ~(SPIFFS_PH_FLAG_USED | SPIFFS_PH_FLAG_FINAL);\n new_ph.obj_id = objix_p_hdr->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;\n new_ph.span_ix = data_spix_offset + i;\n res = spiffs_page_allocate_data(fs, new_ph.obj_id, &new_ph, 0, 0, 0, 1, &data_pix);\n SPIFFS_CHECK_RES(res);\n SPIFFS_CHECK_DBG(\"PA: FIXUP: found no existing data page, created new @ %04x\\n\", data_pix);\n }\n // remap index\n SPIFFS_CHECK_DBG(\"PA: FIXUP: rewriting index pix %04x\\n\", cur_pix);\n res = spiffs_rewrite_index(fs, objix_p_hdr->obj_id | SPIFFS_OBJ_ID_IX_FLAG,\n data_spix_offset + i, data_pix, cur_pix);\n if (res <= _SPIFFS_ERR_CHECK_FIRST && res > _SPIFFS_ERR_CHECK_LAST) {\n // index bad also, cannot mend this file\n SPIFFS_CHECK_DBG(\"PA: FIXUP: index bad %i, cannot mend - delete object\\n\", res);\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_DELETE_BAD_FILE, objix_p_hdr->obj_id, 0);\n // delete file\n res = spiffs_page_delete(fs, cur_pix);\n } else {\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_FIX_INDEX, objix_p_hdr->obj_id, objix_p_hdr->span_ix);\n }\n SPIFFS_CHECK_RES(res);\n restart = 1;\n\n } else if (rpix_within_range) {\n\n // valid reference\n // read referenced page header\n spiffs_page_header rp_hdr;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, rpix), sizeof(spiffs_page_header), (u8_t*)&rp_hdr);\n SPIFFS_CHECK_RES(res);\n\n // cross reference page header check\n if (rp_hdr.obj_id != (p_hdr.obj_id & ~SPIFFS_OBJ_ID_IX_FLAG) ||\n rp_hdr.span_ix != data_spix_offset + i ||\n (rp_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_USED)) !=\n (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_INDEX)) {\n SPIFFS_CHECK_DBG(\"PA: pix %04x has inconsistent page header ix id/span:%04x/%04x, ref id/span:%04x/%04x flags:%02x\\n\",\n rpix, p_hdr.obj_id & ~SPIFFS_OBJ_ID_IX_FLAG, data_spix_offset + i,\n rp_hdr.obj_id, rp_hdr.span_ix, rp_hdr.flags);\n // try finding correct page\n spiffs_page_ix data_pix;\n res = spiffs_obj_lu_find_id_and_span(fs, p_hdr.obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,\n data_spix_offset + i, rpix, &data_pix);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n res = SPIFFS_OK;\n data_pix = 0;\n }\n SPIFFS_CHECK_RES(res);\n if (data_pix == 0) {\n // not found, this index is badly borked\n SPIFFS_CHECK_DBG(\"PA: FIXUP: index bad, delete object id %04x\\n\", p_hdr.obj_id);\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_DELETE_BAD_FILE, p_hdr.obj_id, 0);\n res = spiffs_delete_obj_lazy(fs, p_hdr.obj_id);\n SPIFFS_CHECK_RES(res);\n break;\n } else {\n // found it, so rewrite index\n SPIFFS_CHECK_DBG(\"PA: FIXUP: found correct data pix %04x, rewrite ix pix %04x id %04x\\n\",\n data_pix, cur_pix, p_hdr.obj_id);\n res = spiffs_rewrite_index(fs, p_hdr.obj_id, data_spix_offset + i, data_pix, cur_pix);\n if (res <= _SPIFFS_ERR_CHECK_FIRST && res > _SPIFFS_ERR_CHECK_LAST) {\n // index bad also, cannot mend this file\n SPIFFS_CHECK_DBG(\"PA: FIXUP: index bad %i, cannot mend!\\n\", res);\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_DELETE_BAD_FILE, p_hdr.obj_id, 0);\n res = spiffs_delete_obj_lazy(fs, p_hdr.obj_id);\n } else {\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_FIX_INDEX, p_hdr.obj_id, p_hdr.span_ix);\n }\n SPIFFS_CHECK_RES(res);\n restart = 1;\n }\n }\n else {\n // mark rpix as referenced\n const u32_t rpix_byte_ix = (rpix - pix_offset) / (8/bits);\n const u8_t rpix_bit_ix = (rpix & ((8/bits)-1)) * bits;\n if (fs->work[rpix_byte_ix] & (1<<(rpix_bit_ix + 1))) {\n SPIFFS_CHECK_DBG(\"PA: pix %04x multiple referenced from page %04x\\n\",\n rpix, cur_pix);\n // Here, we should have fixed all broken references - getting this means there\n // must be multiple files with same object id. Only solution is to delete\n // the object which is referring to this page\n SPIFFS_CHECK_DBG(\"PA: FIXUP: removing object %04x and page %04x\\n\",\n p_hdr.obj_id, cur_pix);\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_DELETE_BAD_FILE, p_hdr.obj_id, 0);\n res = spiffs_delete_obj_lazy(fs, p_hdr.obj_id);\n SPIFFS_CHECK_RES(res);\n // extra precaution, delete this page also\n res = spiffs_page_delete(fs, cur_pix);\n SPIFFS_CHECK_RES(res);\n restart = 1;\n }\n fs->work[rpix_byte_ix] |= (1<<(rpix_bit_ix + 1));\n }\n }\n } // for all index entries\n } // found index\n\n // next page\n cur_pix++;\n }\n // next block\n cur_block++;\n }\n // check consistency bitmap\n if (!restart) {\n spiffs_page_ix objix_pix;\n spiffs_page_ix rpix;\n\n u32_t byte_ix;\n u8_t bit_ix;\n for (byte_ix = 0; !restart && byte_ix < SPIFFS_CFG_LOG_PAGE_SZ(fs); byte_ix++) {\n for (bit_ix = 0; !restart && bit_ix < 8/bits; bit_ix ++) {\n u8_t bitmask = (fs->work[byte_ix] >> (bit_ix * bits)) & 0x7;\n spiffs_page_ix cur_pix = pix_offset + byte_ix * (8/bits) + bit_ix;\n\n // 000 ok - free, unreferenced, not index\n\n if (bitmask == 0x1) {\n\n // 001\n SPIFFS_CHECK_DBG(\"PA: pix %04x USED, UNREFERENCED, not index\\n\", cur_pix);\n\n u8_t rewrite_ix_to_this = 0;\n u8_t delete_page = 0;\n // check corresponding object index entry\n spiffs_page_header p_hdr;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix), sizeof(spiffs_page_header), (u8_t*)&p_hdr);\n SPIFFS_CHECK_RES(res);\n\n res = spiffs_object_get_data_page_index_reference(fs, p_hdr.obj_id, p_hdr.span_ix,\n &rpix, &objix_pix);\n if (res == SPIFFS_OK) {\n if (((rpix == (spiffs_page_ix)-1 || rpix > SPIFFS_MAX_PAGES(fs)) || (SPIFFS_IS_LOOKUP_PAGE(fs, rpix)))) {\n // pointing to a bad page altogether, rewrite index to this\n rewrite_ix_to_this = 1;\n SPIFFS_CHECK_DBG(\"PA: corresponding ref is bad: %04x, rewrite to this %04x\\n\", rpix, cur_pix);\n } else {\n // pointing to something else, check what\n spiffs_page_header rp_hdr;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, rpix), sizeof(spiffs_page_header), (u8_t*)&rp_hdr);\n SPIFFS_CHECK_RES(res);\n if (((p_hdr.obj_id & ~SPIFFS_OBJ_ID_IX_FLAG) == rp_hdr.obj_id) &&\n ((rp_hdr.flags & (SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED | SPIFFS_PH_FLAG_FINAL)) ==\n (SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_DELET))) {\n // pointing to something else valid, just delete this page then\n SPIFFS_CHECK_DBG(\"PA: corresponding ref is good but different: %04x, delete this %04x\\n\", rpix, cur_pix);\n delete_page = 1;\n } else {\n // pointing to something weird, update index to point to this page instead\n if (rpix != cur_pix) {\n SPIFFS_CHECK_DBG(\"PA: corresponding ref is weird: %04x %s%s%s%s, rewrite this %04x\\n\", rpix,\n (rp_hdr.flags & SPIFFS_PH_FLAG_INDEX) ? \"\" : \"INDEX \",\n (rp_hdr.flags & SPIFFS_PH_FLAG_DELET) ? \"\" : \"DELETED \",\n (rp_hdr.flags & SPIFFS_PH_FLAG_USED) ? \"NOTUSED \" : \"\",\n (rp_hdr.flags & SPIFFS_PH_FLAG_FINAL) ? \"NOTFINAL \" : \"\",\n cur_pix);\n rewrite_ix_to_this = 1;\n } else {\n // should not happen, destined for fubar\n }\n }\n }\n } else if (res == SPIFFS_ERR_NOT_FOUND) {\n SPIFFS_CHECK_DBG(\"PA: corresponding ref not found, delete %04x\\n\", cur_pix);\n delete_page = 1;\n res = SPIFFS_OK;\n }\n\n if (rewrite_ix_to_this) {\n // if pointing to invalid page, redirect index to this page\n SPIFFS_CHECK_DBG(\"PA: FIXUP: rewrite index id %04x data spix %04x to point to this pix: %04x\\n\",\n p_hdr.obj_id, p_hdr.span_ix, cur_pix);\n res = spiffs_rewrite_index(fs, p_hdr.obj_id, p_hdr.span_ix, cur_pix, objix_pix);\n if (res <= _SPIFFS_ERR_CHECK_FIRST && res > _SPIFFS_ERR_CHECK_LAST) {\n // index bad also, cannot mend this file\n SPIFFS_CHECK_DBG(\"PA: FIXUP: index bad %i, cannot mend!\\n\", res);\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_DELETE_BAD_FILE, p_hdr.obj_id, 0);\n res = spiffs_page_delete(fs, cur_pix);\n SPIFFS_CHECK_RES(res);\n res = spiffs_delete_obj_lazy(fs, p_hdr.obj_id);\n } else {\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_FIX_INDEX, p_hdr.obj_id, p_hdr.span_ix);\n }\n SPIFFS_CHECK_RES(res);\n restart = 1;\n continue;\n } else if (delete_page) {\n SPIFFS_CHECK_DBG(\"PA: FIXUP: deleting page %04x\\n\", cur_pix);\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_DELETE_PAGE, cur_pix, 0);\n res = spiffs_page_delete(fs, cur_pix);\n }\n SPIFFS_CHECK_RES(res);\n }\n if (bitmask == 0x2) {\n\n // 010\n SPIFFS_CHECK_DBG(\"PA: pix %04x FREE, REFERENCED, not index\\n\", cur_pix);\n\n // no op, this should be taken care of when checking valid references\n }\n\n // 011 ok - busy, referenced, not index\n\n if (bitmask == 0x4) {\n\n // 100\n SPIFFS_CHECK_DBG(\"PA: pix %04x FREE, unreferenced, INDEX\\n\", cur_pix);\n\n // this should never happen, major fubar\n }\n\n // 101 ok - busy, unreferenced, index\n\n if (bitmask == 0x6) {\n\n // 110\n SPIFFS_CHECK_DBG(\"PA: pix %04x FREE, REFERENCED, INDEX\\n\", cur_pix);\n\n // no op, this should be taken care of when checking valid references\n }\n if (bitmask == 0x7) {\n\n // 111\n SPIFFS_CHECK_DBG(\"PA: pix %04x USED, REFERENCED, INDEX\\n\", cur_pix);\n\n // no op, this should be taken care of when checking valid references\n }\n }\n }\n }\n\n SPIFFS_CHECK_DBG(\"PA: processed %04x, restart %i\\n\", pix_offset, restart);\n // next page range\n if (!restart) {\n pix_offset += pages_per_scan;\n }\n } // while page range not reached end\n return res;\n}\n\n// Checks consistency amongst all pages and fixes irregularities\ns32_t spiffs_page_consistency_check(spiffs *fs) {\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_PROGRESS, 0, 0);\n s32_t res = spiffs_page_consistency_check_i(fs);\n if (res != SPIFFS_OK) {\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_ERROR, res, 0);\n }\n CHECK_CB(fs, SPIFFS_CHECK_PAGE, SPIFFS_CHECK_PROGRESS, 256, 0);\n return res;\n}\n\n//---------------------------------------\n// Object index consistency\n\n// searches for given object id in temporary object id index,\n// returns the index or -1\nstatic int spiffs_object_index_search(spiffs *fs, spiffs_obj_id obj_id) {\n u32_t i;\n spiffs_obj_id *obj_table = (spiffs_obj_id *)fs->work;\n obj_id &= ~SPIFFS_OBJ_ID_IX_FLAG;\n for (i = 0; i < SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id); i++) {\n if ((obj_table[i] & ~SPIFFS_OBJ_ID_IX_FLAG) == obj_id) {\n return i;\n }\n }\n return -1;\n}\n\nstatic s32_t spiffs_object_index_consistency_check_v(spiffs *fs, spiffs_obj_id obj_id, spiffs_block_ix cur_block,\n int cur_entry, const void *user_const_p, void *user_var_p) {\n (void)user_const_p;\n s32_t res_c = SPIFFS_VIS_COUNTINUE;\n s32_t res = SPIFFS_OK;\n u32_t *log_ix = (u32_t*)user_var_p;\n spiffs_obj_id *obj_table = (spiffs_obj_id *)fs->work;\n\n CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_PROGRESS,\n (cur_block * 256)/fs->block_count, 0);\n\n if (obj_id != SPIFFS_OBJ_ID_FREE && obj_id != SPIFFS_OBJ_ID_DELETED && (obj_id & SPIFFS_OBJ_ID_IX_FLAG)) {\n spiffs_page_header p_hdr;\n spiffs_page_ix cur_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, cur_block, cur_entry);\n\n // load header\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix), sizeof(spiffs_page_header), (u8_t*)&p_hdr);\n SPIFFS_CHECK_RES(res);\n\n if (p_hdr.span_ix == 0 &&\n (p_hdr.flags & (SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) ==\n (SPIFFS_PH_FLAG_DELET)) {\n SPIFFS_CHECK_DBG(\"IX: pix %04x, obj id:%04x spix:%04x header not fully deleted - deleting\\n\",\n cur_pix, obj_id, p_hdr.span_ix);\n CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_DELETE_PAGE, cur_pix, obj_id);\n res = spiffs_page_delete(fs, cur_pix);\n SPIFFS_CHECK_RES(res);\n return res_c;\n }\n\n if ((p_hdr.flags & (SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) ==\n (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) {\n return res_c;\n }\n\n if (p_hdr.span_ix == 0) {\n // objix header page, register objid as reachable\n int r = spiffs_object_index_search(fs, obj_id);\n if (r == -1) {\n // not registered, do it\n obj_table[*log_ix] = obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;\n (*log_ix)++;\n if (*log_ix >= SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id)) {\n *log_ix = 0;\n }\n }\n } else { // span index\n // objix page, see if header can be found\n int r = spiffs_object_index_search(fs, obj_id);\n u8_t delete = 0;\n if (r == -1) {\n // not in temporary index, try finding it\n spiffs_page_ix objix_hdr_pix;\n res = spiffs_obj_lu_find_id_and_span(fs, obj_id | SPIFFS_OBJ_ID_IX_FLAG, 0, 0, &objix_hdr_pix);\n res_c = SPIFFS_VIS_COUNTINUE_RELOAD;\n if (res == SPIFFS_OK) {\n // found, register as reachable\n obj_table[*log_ix] = obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;\n } else if (res == SPIFFS_ERR_NOT_FOUND) {\n // not found, register as unreachable\n delete = 1;\n obj_table[*log_ix] = obj_id | SPIFFS_OBJ_ID_IX_FLAG;\n } else {\n SPIFFS_CHECK_RES(res);\n }\n (*log_ix)++;\n if (*log_ix >= SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id)) {\n *log_ix = 0;\n }\n } else {\n // in temporary index, check reachable flag\n if ((obj_table[r] & SPIFFS_OBJ_ID_IX_FLAG)) {\n // registered as unreachable\n delete = 1;\n }\n }\n\n if (delete) {\n SPIFFS_CHECK_DBG(\"IX: FIXUP: pix %04x, obj id:%04x spix:%04x is orphan index - deleting\\n\",\n cur_pix, obj_id, p_hdr.span_ix);\n CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_DELETE_ORPHANED_INDEX, cur_pix, obj_id);\n res = spiffs_page_delete(fs, cur_pix);\n SPIFFS_CHECK_RES(res);\n }\n } // span index\n } // valid object index id\n\n return res_c;\n}\n\n// Removes orphaned and partially deleted index pages.\n// Scans for index pages. When an index page is found, corresponding index header is searched for.\n// If no such page exists, the index page cannot be reached as no index header exists and must be\n// deleted.\ns32_t spiffs_object_index_consistency_check(spiffs *fs) {\n s32_t res = SPIFFS_OK;\n // impl note:\n // fs->work is used for a temporary object index memory, listing found object ids and\n // indicating whether they can be reached or not. Acting as a fifo if object ids cannot fit.\n // In the temporary object index memory, SPIFFS_OBJ_ID_IX_FLAG bit is used to indicate\n // a reachable/unreachable object id.\n memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));\n u32_t obj_id_log_ix = 0;\n CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_PROGRESS, 0, 0);\n res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_object_index_consistency_check_v, &obj_id_log_ix,\n 0, 0, 0);\n if (res == SPIFFS_VIS_END) {\n res = SPIFFS_OK;\n }\n if (res != SPIFFS_OK) {\n CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_ERROR, res, 0);\n }\n CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_PROGRESS, 256, 0);\n return res;\n}\n\n#endif // !SPIFFS_READ_ONLY\n", "src/spiffs_gc.c": "#include \"spiffs.h\"\n#include \"spiffs_nucleus.h\"\n\n#if !SPIFFS_READ_ONLY\n\n// Erases a logical block and updates the erase counter.\n// If cache is enabled, all pages that might be cached in this block\n// is dropped.\nstatic s32_t spiffs_gc_erase_block(\n spiffs *fs,\n spiffs_block_ix bix) {\n s32_t res;\n\n SPIFFS_GC_DBG(\"gc: erase block %i\\n\", bix);\n res = spiffs_erase_block(fs, bix);\n SPIFFS_CHECK_RES(res);\n\n#if SPIFFS_CACHE\n {\n u32_t i;\n for (i = 0; i < SPIFFS_PAGES_PER_BLOCK(fs); i++) {\n spiffs_cache_drop_page(fs, SPIFFS_PAGE_FOR_BLOCK(fs, bix) + i);\n }\n }\n#endif\n return res;\n}\n\n// Searches for blocks where all entries are deleted - if one is found,\n// the block is erased. Compared to the non-quick gc, the quick one ensures\n// that no updates are needed on existing objects on pages that are erased.\ns32_t spiffs_gc_quick(\n spiffs *fs, u16_t max_free_pages) {\n s32_t res = SPIFFS_OK;\n u32_t blocks = fs->block_count;\n spiffs_block_ix cur_block = 0;\n u32_t cur_block_addr = 0;\n int cur_entry = 0;\n spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work;\n\n SPIFFS_GC_DBG(\"gc_quick: running\\n\");\n#if SPIFFS_GC_STATS\n fs->stats_gc_runs++;\n#endif\n\n int entries_per_page = (SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id));\n\n // find fully deleted blocks\n // check each block\n while (res == SPIFFS_OK && blocks--) {\n u16_t deleted_pages_in_block = 0;\n u16_t free_pages_in_block = 0;\n\n int obj_lookup_page = 0;\n // check each object lookup page\n while (res == SPIFFS_OK && obj_lookup_page < (int)SPIFFS_OBJ_LOOKUP_PAGES(fs)) {\n int entry_offset = obj_lookup_page * entries_per_page;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,\n 0, cur_block_addr + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);\n // check each entry\n while (res == SPIFFS_OK &&\n cur_entry - entry_offset < entries_per_page &&\n cur_entry < (int)(SPIFFS_PAGES_PER_BLOCK(fs)-SPIFFS_OBJ_LOOKUP_PAGES(fs))) {\n spiffs_obj_id obj_id = obj_lu_buf[cur_entry-entry_offset];\n if (obj_id == SPIFFS_OBJ_ID_DELETED) {\n deleted_pages_in_block++;\n } else if (obj_id == SPIFFS_OBJ_ID_FREE) {\n // kill scan, go for next block\n free_pages_in_block++;\n if (free_pages_in_block > max_free_pages) {\n obj_lookup_page = SPIFFS_OBJ_LOOKUP_PAGES(fs);\n res = 1; // kill object lu loop\n break;\n }\n } else {\n // kill scan, go for next block\n obj_lookup_page = SPIFFS_OBJ_LOOKUP_PAGES(fs);\n res = 1; // kill object lu loop\n break;\n }\n cur_entry++;\n } // per entry\n obj_lookup_page++;\n } // per object lookup page\n if (res == 1) res = SPIFFS_OK;\n\n if (res == SPIFFS_OK &&\n deleted_pages_in_block + free_pages_in_block == SPIFFS_PAGES_PER_BLOCK(fs)-SPIFFS_OBJ_LOOKUP_PAGES(fs) &&\n free_pages_in_block <= max_free_pages) {\n // found a fully deleted block\n fs->stats_p_deleted -= deleted_pages_in_block;\n res = spiffs_gc_erase_block(fs, cur_block);\n return res;\n }\n\n cur_entry = 0;\n cur_block++;\n cur_block_addr += SPIFFS_CFG_LOG_BLOCK_SZ(fs);\n } // per block\n\n if (res == SPIFFS_OK) {\n res = SPIFFS_ERR_NO_DELETED_BLOCKS;\n }\n return res;\n}\n\n// Checks if garbage collecting is necessary. If so a candidate block is found,\n// cleansed and erased\ns32_t spiffs_gc_check(\n spiffs *fs,\n u32_t len) {\n s32_t res;\n s32_t free_pages =\n (SPIFFS_PAGES_PER_BLOCK(fs) - SPIFFS_OBJ_LOOKUP_PAGES(fs)) * (fs->block_count-2)\n - fs->stats_p_allocated - fs->stats_p_deleted;\n int tries = 0;\n\n if (fs->free_blocks > 3 &&\n (s32_t)len < free_pages * (s32_t)SPIFFS_DATA_PAGE_SIZE(fs)) {\n return SPIFFS_OK;\n }\n\n u32_t needed_pages = (len + SPIFFS_DATA_PAGE_SIZE(fs) - 1) / SPIFFS_DATA_PAGE_SIZE(fs);\n// if (fs->free_blocks <= 2 && (s32_t)needed_pages > free_pages) {\n// SPIFFS_GC_DBG(\"gc: full freeblk:%i needed:%i free:%i dele:%i\\n\", fs->free_blocks, needed_pages, free_pages, fs->stats_p_deleted);\n// return SPIFFS_ERR_FULL;\n// }\n if ((s32_t)needed_pages > (s32_t)(free_pages + fs->stats_p_deleted)) {\n SPIFFS_GC_DBG(\"gc_check: full freeblk:%i needed:%i free:%i dele:%i\\n\", fs->free_blocks, needed_pages, free_pages, fs->stats_p_deleted);\n return SPIFFS_ERR_FULL;\n }\n\n do {\n SPIFFS_GC_DBG(\"\\ngc_check #%i: run gc free_blocks:%i pfree:%i pallo:%i pdele:%i [%i] len:%i of %i\\n\",\n tries,\n fs->free_blocks, free_pages, fs->stats_p_allocated, fs->stats_p_deleted, (free_pages+fs->stats_p_allocated+fs->stats_p_deleted),\n len, free_pages*SPIFFS_DATA_PAGE_SIZE(fs));\n\n spiffs_block_ix *cands;\n int count;\n spiffs_block_ix cand;\n s32_t prev_free_pages = free_pages;\n // if the fs is crammed, ignore block age when selecting candidate - kind of a bad state\n res = spiffs_gc_find_candidate(fs, &cands, &count, free_pages <= 0);\n SPIFFS_CHECK_RES(res);\n if (count == 0) {\n SPIFFS_GC_DBG(\"gc_check: no candidates, return\\n\");\n return (s32_t)needed_pages < free_pages ? SPIFFS_OK : SPIFFS_ERR_FULL;\n }\n#if SPIFFS_GC_STATS\n fs->stats_gc_runs++;\n#endif\n cand = cands[0];\n fs->cleaning = 1;\n //printf(\"gcing: cleaning block %i\\n\", cand);\n res = spiffs_gc_clean(fs, cand);\n fs->cleaning = 0;\n if (res < 0) {\n SPIFFS_GC_DBG(\"gc_check: cleaning block %i, result %i\\n\", cand, res);\n } else {\n SPIFFS_GC_DBG(\"gc_check: cleaning block %i, result %i\\n\", cand, res);\n }\n SPIFFS_CHECK_RES(res);\n\n res = spiffs_gc_erase_page_stats(fs, cand);\n SPIFFS_CHECK_RES(res);\n\n res = spiffs_gc_erase_block(fs, cand);\n SPIFFS_CHECK_RES(res);\n\n free_pages =\n (SPIFFS_PAGES_PER_BLOCK(fs) - SPIFFS_OBJ_LOOKUP_PAGES(fs)) * (fs->block_count - 2)\n - fs->stats_p_allocated - fs->stats_p_deleted;\n\n if (prev_free_pages <= 0 && prev_free_pages == free_pages) {\n // abort early to reduce wear, at least tried once\n SPIFFS_GC_DBG(\"gc_check: early abort, no result on gc when fs crammed\\n\");\n break;\n }\n\n } while (++tries < SPIFFS_GC_MAX_RUNS && (fs->free_blocks <= 2 ||\n (s32_t)len > free_pages*(s32_t)SPIFFS_DATA_PAGE_SIZE(fs)));\n\n free_pages =\n (SPIFFS_PAGES_PER_BLOCK(fs) - SPIFFS_OBJ_LOOKUP_PAGES(fs)) * (fs->block_count - 2)\n - fs->stats_p_allocated - fs->stats_p_deleted;\n if ((s32_t)len > free_pages*(s32_t)SPIFFS_DATA_PAGE_SIZE(fs)) {\n res = SPIFFS_ERR_FULL;\n }\n\n SPIFFS_GC_DBG(\"gc_check: finished, %i dirty, blocks %i free, %i pages free, %i tries, res %i\\n\",\n fs->stats_p_allocated + fs->stats_p_deleted,\n fs->free_blocks, free_pages, tries, res);\n\n return res;\n}\n\n// Updates page statistics for a block that is about to be erased\ns32_t spiffs_gc_erase_page_stats(\n spiffs *fs,\n spiffs_block_ix bix) {\n s32_t res = SPIFFS_OK;\n int obj_lookup_page = 0;\n int entries_per_page = (SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id));\n spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work;\n int cur_entry = 0;\n u32_t dele = 0;\n u32_t allo = 0;\n\n // check each object lookup page\n while (res == SPIFFS_OK && obj_lookup_page < (int)SPIFFS_OBJ_LOOKUP_PAGES(fs)) {\n int entry_offset = obj_lookup_page * entries_per_page;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,\n 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);\n // check each entry\n while (res == SPIFFS_OK &&\n cur_entry - entry_offset < entries_per_page && cur_entry < (int)(SPIFFS_PAGES_PER_BLOCK(fs)-SPIFFS_OBJ_LOOKUP_PAGES(fs))) {\n spiffs_obj_id obj_id = obj_lu_buf[cur_entry-entry_offset];\n if (obj_id == SPIFFS_OBJ_ID_FREE) {\n } else if (obj_id == SPIFFS_OBJ_ID_DELETED) {\n dele++;\n } else {\n allo++;\n }\n cur_entry++;\n } // per entry\n obj_lookup_page++;\n } // per object lookup page\n SPIFFS_GC_DBG(\"gc_check: wipe pallo:%i pdele:%i\\n\", allo, dele);\n fs->stats_p_allocated -= allo;\n fs->stats_p_deleted -= dele;\n return res;\n}\n\n// Finds block candidates to erase\ns32_t spiffs_gc_find_candidate(\n spiffs *fs,\n spiffs_block_ix **block_candidates,\n int *candidate_count,\n char fs_crammed) {\n s32_t res = SPIFFS_OK;\n u32_t blocks = fs->block_count;\n spiffs_block_ix cur_block = 0;\n u32_t cur_block_addr = 0;\n spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work;\n int cur_entry = 0;\n\n // using fs->work area as sorted candidate memory, (spiffs_block_ix)cand_bix/(s32_t)score\n int max_candidates = MIN(fs->block_count, (SPIFFS_CFG_LOG_PAGE_SZ(fs)-8)/(sizeof(spiffs_block_ix) + sizeof(s32_t)));\n *candidate_count = 0;\n memset(fs->work, 0xff, SPIFFS_CFG_LOG_PAGE_SZ(fs));\n\n // divide up work area into block indices and scores\n spiffs_block_ix *cand_blocks = (spiffs_block_ix *)fs->work;\n s32_t *cand_scores = (s32_t *)(fs->work + max_candidates * sizeof(spiffs_block_ix));\n\n // align cand_scores on s32_t boundary\n#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wpointer-to-int-cast\"\n cand_scores = (s32_t*)(((ptrdiff_t)cand_scores + sizeof(ptrdiff_t) - 1) & ~(sizeof(ptrdiff_t) - 1));\n#pragma GCC diagnostic pop\n\n *block_candidates = cand_blocks;\n\n int entries_per_page = (SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id));\n\n // check each block\n while (res == SPIFFS_OK && blocks--) {\n u16_t deleted_pages_in_block = 0;\n u16_t used_pages_in_block = 0;\n\n int obj_lookup_page = 0;\n // check each object lookup page\n while (res == SPIFFS_OK && obj_lookup_page < (int)SPIFFS_OBJ_LOOKUP_PAGES(fs)) {\n int entry_offset = obj_lookup_page * entries_per_page;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,\n 0, cur_block_addr + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);\n // check each entry\n while (res == SPIFFS_OK &&\n cur_entry - entry_offset < entries_per_page &&\n cur_entry < (int)(SPIFFS_PAGES_PER_BLOCK(fs)-SPIFFS_OBJ_LOOKUP_PAGES(fs))) {\n spiffs_obj_id obj_id = obj_lu_buf[cur_entry-entry_offset];\n if (obj_id == SPIFFS_OBJ_ID_FREE) {\n // when a free entry is encountered, scan logic ensures that all following entries are free also\n res = 1; // kill object lu loop\n break;\n } else if (obj_id == SPIFFS_OBJ_ID_DELETED) {\n deleted_pages_in_block++;\n } else {\n used_pages_in_block++;\n }\n cur_entry++;\n } // per entry\n obj_lookup_page++;\n } // per object lookup page\n if (res == 1) res = SPIFFS_OK;\n\n // calculate score and insert into candidate table\n // stoneage sort, but probably not so many blocks\n if (res == SPIFFS_OK && deleted_pages_in_block > 0) {\n // read erase count\n spiffs_obj_id erase_count;\n res = _spiffs_rd(fs, SPIFFS_OP_C_READ | SPIFFS_OP_T_OBJ_LU2, 0,\n SPIFFS_ERASE_COUNT_PADDR(fs, cur_block),\n sizeof(spiffs_obj_id), (u8_t *)&erase_count);\n SPIFFS_CHECK_RES(res);\n\n spiffs_obj_id erase_age;\n if (fs->max_erase_count > erase_count) {\n erase_age = fs->max_erase_count - erase_count;\n } else {\n erase_age = SPIFFS_OBJ_ID_FREE - (erase_count - fs->max_erase_count);\n }\n\n s32_t score =\n deleted_pages_in_block * SPIFFS_GC_HEUR_W_DELET +\n used_pages_in_block * SPIFFS_GC_HEUR_W_USED +\n erase_age * (fs_crammed ? 0 : SPIFFS_GC_HEUR_W_ERASE_AGE);\n int cand_ix = 0;\n SPIFFS_GC_DBG(\"gc_check: bix:%i del:%i use:%i score:%i\\n\", cur_block, deleted_pages_in_block, used_pages_in_block, score);\n while (cand_ix < max_candidates) {\n if (cand_blocks[cand_ix] == (spiffs_block_ix)-1) {\n cand_blocks[cand_ix] = cur_block;\n cand_scores[cand_ix] = score;\n break;\n } else if (cand_scores[cand_ix] < score) {\n int reorder_cand_ix = max_candidates - 2;\n while (reorder_cand_ix >= cand_ix) {\n cand_blocks[reorder_cand_ix + 1] = cand_blocks[reorder_cand_ix];\n cand_scores[reorder_cand_ix + 1] = cand_scores[reorder_cand_ix];\n reorder_cand_ix--;\n }\n cand_blocks[cand_ix] = cur_block;\n cand_scores[cand_ix] = score;\n break;\n }\n cand_ix++;\n }\n (*candidate_count)++;\n }\n\n cur_entry = 0;\n cur_block++;\n cur_block_addr += SPIFFS_CFG_LOG_BLOCK_SZ(fs);\n } // per block\n\n return res;\n}\n\ntypedef enum {\n FIND_OBJ_DATA,\n MOVE_OBJ_DATA,\n MOVE_OBJ_IX,\n FINISHED\n} spiffs_gc_clean_state;\n\ntypedef struct {\n spiffs_gc_clean_state state;\n spiffs_obj_id cur_obj_id;\n spiffs_span_ix cur_objix_spix;\n spiffs_page_ix cur_objix_pix;\n int stored_scan_entry_index;\n u8_t obj_id_found;\n} spiffs_gc;\n\n// Empties given block by moving all data into free pages of another block\n// Strategy:\n// loop:\n// scan object lookup for object data pages\n// for first found id, check spix and load corresponding object index page to memory\n// push object scan lookup entry index\n// rescan object lookup, find data pages with same id and referenced by same object index\n// move data page, update object index in memory\n// when reached end of lookup, store updated object index\n// pop object scan lookup entry index\n// repeat loop until end of object lookup\n// scan object lookup again for remaining object index pages, move to new page in other block\n//\ns32_t spiffs_gc_clean(spiffs *fs, spiffs_block_ix bix) {\n s32_t res = SPIFFS_OK;\n int entries_per_page = (SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id));\n int cur_entry = 0;\n spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work;\n spiffs_gc gc;\n spiffs_page_ix cur_pix = 0;\n spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;\n spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;\n\n SPIFFS_GC_DBG(\"gc_clean: cleaning block %i\\n\", bix);\n\n memset(&gc, 0, sizeof(spiffs_gc));\n gc.state = FIND_OBJ_DATA;\n\n if (fs->free_cursor_block_ix == bix) {\n // move free cursor to next block, cannot use free pages from the block we want to clean\n fs->free_cursor_block_ix = (bix+1)%fs->block_count;\n fs->free_cursor_obj_lu_entry = 0;\n SPIFFS_GC_DBG(\"gc_clean: move free cursor to block %i\\n\", fs->free_cursor_block_ix);\n }\n\n while (res == SPIFFS_OK && gc.state != FINISHED) {\n SPIFFS_GC_DBG(\"gc_clean: state = %i entry:%i\\n\", gc.state, cur_entry);\n gc.obj_id_found = 0;\n\n // scan through lookup pages\n int obj_lookup_page = cur_entry / entries_per_page;\n u8_t scan = 1;\n // check each object lookup page\n while (scan && res == SPIFFS_OK && obj_lookup_page < (int)SPIFFS_OBJ_LOOKUP_PAGES(fs)) {\n int entry_offset = obj_lookup_page * entries_per_page;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,\n 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page),\n SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);\n // check each entry\n while (scan && res == SPIFFS_OK &&\n cur_entry - entry_offset < entries_per_page && cur_entry < (int)(SPIFFS_PAGES_PER_BLOCK(fs)-SPIFFS_OBJ_LOOKUP_PAGES(fs))) {\n spiffs_obj_id obj_id = obj_lu_buf[cur_entry-entry_offset];\n cur_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, cur_entry);\n\n // act upon object id depending on gc state\n switch (gc.state) {\n case FIND_OBJ_DATA:\n if (obj_id != SPIFFS_OBJ_ID_DELETED && obj_id != SPIFFS_OBJ_ID_FREE &&\n ((obj_id & SPIFFS_OBJ_ID_IX_FLAG) == 0)) {\n SPIFFS_GC_DBG(\"gc_clean: FIND_DATA state:%i - found obj id %04x\\n\", gc.state, obj_id);\n gc.obj_id_found = 1;\n gc.cur_obj_id = obj_id;\n scan = 0;\n }\n break;\n case MOVE_OBJ_DATA:\n if (obj_id == gc.cur_obj_id) {\n spiffs_page_header p_hdr;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix), sizeof(spiffs_page_header), (u8_t*)&p_hdr);\n SPIFFS_CHECK_RES(res);\n SPIFFS_GC_DBG(\"gc_clean: MOVE_DATA found data page %04x:%04x @ %04x\\n\", gc.cur_obj_id, p_hdr.span_ix, cur_pix);\n if (SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, p_hdr.span_ix) != gc.cur_objix_spix) {\n SPIFFS_GC_DBG(\"gc_clean: MOVE_DATA no objix spix match, take in another run\\n\");\n } else {\n spiffs_page_ix new_data_pix;\n if (p_hdr.flags & SPIFFS_PH_FLAG_DELET) {\n // move page\n res = spiffs_page_move(fs, 0, 0, obj_id, &p_hdr, cur_pix, &new_data_pix);\n SPIFFS_GC_DBG(\"gc_clean: MOVE_DATA move objix %04x:%04x page %04x to %04x\\n\", gc.cur_obj_id, p_hdr.span_ix, cur_pix, new_data_pix);\n SPIFFS_CHECK_RES(res);\n // move wipes obj_lu, reload it\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,\n 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page),\n SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);\n SPIFFS_CHECK_RES(res);\n } else {\n // page is deleted but not deleted in lookup, scrap it\n SPIFFS_GC_DBG(\"gc_clean: MOVE_DATA wipe objix %04x:%04x page %04x\\n\", obj_id, p_hdr.span_ix, cur_pix);\n res = spiffs_page_delete(fs, cur_pix);\n SPIFFS_CHECK_RES(res);\n new_data_pix = SPIFFS_OBJ_ID_FREE;\n }\n // update memory representation of object index page with new data page\n if (gc.cur_objix_spix == 0) {\n // update object index header page\n ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[p_hdr.span_ix] = new_data_pix;\n SPIFFS_GC_DBG(\"gc_clean: MOVE_DATA wrote page %04x to objix_hdr entry %02x in mem\\n\", new_data_pix, SPIFFS_OBJ_IX_ENTRY(fs, p_hdr.span_ix));\n } else {\n // update object index page\n ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, p_hdr.span_ix)] = new_data_pix;\n SPIFFS_GC_DBG(\"gc_clean: MOVE_DATA wrote page %04x to objix entry %02x in mem\\n\", new_data_pix, SPIFFS_OBJ_IX_ENTRY(fs, p_hdr.span_ix));\n }\n }\n }\n break;\n case MOVE_OBJ_IX:\n if (obj_id != SPIFFS_OBJ_ID_DELETED && obj_id != SPIFFS_OBJ_ID_FREE &&\n (obj_id & SPIFFS_OBJ_ID_IX_FLAG)) {\n // found an index object id\n spiffs_page_header p_hdr;\n spiffs_page_ix new_pix;\n // load header\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix), sizeof(spiffs_page_header), (u8_t*)&p_hdr);\n SPIFFS_CHECK_RES(res);\n if (p_hdr.flags & SPIFFS_PH_FLAG_DELET) {\n // move page\n res = spiffs_page_move(fs, 0, 0, obj_id, &p_hdr, cur_pix, &new_pix);\n SPIFFS_GC_DBG(\"gc_clean: MOVE_OBJIX move objix %04x:%04x page %04x to %04x\\n\", obj_id, p_hdr.span_ix, cur_pix, new_pix);\n SPIFFS_CHECK_RES(res);\n spiffs_cb_object_event(fs, 0, SPIFFS_EV_IX_UPD, obj_id, p_hdr.span_ix, new_pix, 0);\n // move wipes obj_lu, reload it\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,\n 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page),\n SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);\n SPIFFS_CHECK_RES(res);\n } else {\n // page is deleted but not deleted in lookup, scrap it\n SPIFFS_GC_DBG(\"gc_clean: MOVE_OBJIX wipe objix %04x:%04x page %04x\\n\", obj_id, p_hdr.span_ix, cur_pix);\n res = spiffs_page_delete(fs, cur_pix);\n if (res == SPIFFS_OK) {\n spiffs_cb_object_event(fs, 0, SPIFFS_EV_IX_DEL, obj_id, p_hdr.span_ix, cur_pix, 0);\n }\n }\n SPIFFS_CHECK_RES(res);\n }\n break;\n default:\n scan = 0;\n break;\n }\n cur_entry++;\n } // per entry\n obj_lookup_page++;\n } // per object lookup page\n\n if (res != SPIFFS_OK) break;\n\n // state finalization and switch\n switch (gc.state) {\n case FIND_OBJ_DATA:\n if (gc.obj_id_found) {\n // find out corresponding obj ix page and load it to memory\n spiffs_page_header p_hdr;\n spiffs_page_ix objix_pix;\n gc.stored_scan_entry_index = cur_entry;\n cur_entry = 0;\n gc.state = MOVE_OBJ_DATA;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix), sizeof(spiffs_page_header), (u8_t*)&p_hdr);\n SPIFFS_CHECK_RES(res);\n gc.cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, p_hdr.span_ix);\n SPIFFS_GC_DBG(\"gc_clean: FIND_DATA find objix span_ix:%04x\\n\", gc.cur_objix_spix);\n res = spiffs_obj_lu_find_id_and_span(fs, gc.cur_obj_id | SPIFFS_OBJ_ID_IX_FLAG, gc.cur_objix_spix, 0, &objix_pix);\n SPIFFS_CHECK_RES(res);\n SPIFFS_GC_DBG(\"gc_clean: FIND_DATA found object index at page %04x\\n\", objix_pix);\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res);\n SPIFFS_VALIDATE_OBJIX(objix->p_hdr, gc.cur_obj_id | SPIFFS_OBJ_ID_IX_FLAG, gc.cur_objix_spix);\n gc.cur_objix_pix = objix_pix;\n } else {\n gc.state = MOVE_OBJ_IX;\n cur_entry = 0; // restart entry scan index\n }\n break;\n case MOVE_OBJ_DATA: {\n // store modified objix (hdr) page\n spiffs_page_ix new_objix_pix;\n gc.state = FIND_OBJ_DATA;\n cur_entry = gc.stored_scan_entry_index;\n if (gc.cur_objix_spix == 0) {\n // store object index header page\n res = spiffs_object_update_index_hdr(fs, 0, gc.cur_obj_id | SPIFFS_OBJ_ID_IX_FLAG, gc.cur_objix_pix, fs->work, 0, 0, &new_objix_pix);\n SPIFFS_GC_DBG(\"gc_clean: MOVE_DATA store modified objix_hdr page, %04x:%04x\\n\", new_objix_pix, 0);\n SPIFFS_CHECK_RES(res);\n } else {\n // store object index page\n res = spiffs_page_move(fs, 0, fs->work, gc.cur_obj_id | SPIFFS_OBJ_ID_IX_FLAG, 0, gc.cur_objix_pix, &new_objix_pix);\n SPIFFS_GC_DBG(\"gc_clean: MOVE_DATA store modified objix page, %04x:%04x\\n\", new_objix_pix, objix->p_hdr.span_ix);\n SPIFFS_CHECK_RES(res);\n spiffs_cb_object_event(fs, 0, SPIFFS_EV_IX_UPD, gc.cur_obj_id, objix->p_hdr.span_ix, new_objix_pix, 0);\n }\n }\n break;\n case MOVE_OBJ_IX:\n gc.state = FINISHED;\n break;\n default:\n cur_entry = 0;\n break;\n }\n SPIFFS_GC_DBG(\"gc_clean: state-> %i\\n\", gc.state);\n } // while state != FINISHED\n\n\n return res;\n}\n\n#endif // !SPIFFS_READ_ONLY\n", "src/spiffs_nucleus.c": "#include \"spiffs.h\"\n#include \"spiffs_nucleus.h\"\n\nstatic s32_t spiffs_page_data_check(spiffs *fs, spiffs_fd *fd, spiffs_page_ix pix, spiffs_span_ix spix) {\n s32_t res = SPIFFS_OK;\n if (pix == (spiffs_page_ix)-1) {\n // referring to page 0xffff...., bad object index\n return SPIFFS_ERR_INDEX_REF_FREE;\n }\n if (pix % SPIFFS_PAGES_PER_BLOCK(fs) < SPIFFS_OBJ_LOOKUP_PAGES(fs)) {\n // referring to an object lookup page, bad object index\n return SPIFFS_ERR_INDEX_REF_LU;\n }\n if (pix > SPIFFS_MAX_PAGES(fs)) {\n // referring to a bad page\n return SPIFFS_ERR_INDEX_REF_INVALID;\n }\n#if SPIFFS_PAGE_CHECK\n spiffs_page_header ph;\n res = _spiffs_rd(\n fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_READ,\n fd->file_nbr,\n SPIFFS_PAGE_TO_PADDR(fs, pix),\n sizeof(spiffs_page_header),\n (u8_t *)&ph);\n SPIFFS_CHECK_RES(res);\n SPIFFS_VALIDATE_DATA(ph, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG, spix);\n#endif\n return res;\n}\n\n#if !SPIFFS_READ_ONLY\nstatic s32_t spiffs_page_index_check(spiffs *fs, spiffs_fd *fd, spiffs_page_ix pix, spiffs_span_ix spix) {\n s32_t res = SPIFFS_OK;\n if (pix == (spiffs_page_ix)-1) {\n // referring to page 0xffff...., bad object index\n return SPIFFS_ERR_INDEX_FREE;\n }\n if (pix % SPIFFS_PAGES_PER_BLOCK(fs) < SPIFFS_OBJ_LOOKUP_PAGES(fs)) {\n // referring to an object lookup page, bad object index\n return SPIFFS_ERR_INDEX_LU;\n }\n if (pix > SPIFFS_MAX_PAGES(fs)) {\n // referring to a bad page\n return SPIFFS_ERR_INDEX_INVALID;\n }\n#if SPIFFS_PAGE_CHECK\n spiffs_page_header ph;\n res = _spiffs_rd(\n fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,\n fd->file_nbr,\n SPIFFS_PAGE_TO_PADDR(fs, pix),\n sizeof(spiffs_page_header),\n (u8_t *)&ph);\n SPIFFS_CHECK_RES(res);\n SPIFFS_VALIDATE_OBJIX(ph, fd->obj_id, spix);\n#endif\n return res;\n}\n#endif // !SPIFFS_READ_ONLY\n\n#if !SPIFFS_CACHE\n\ns32_t spiffs_phys_rd(\n spiffs *fs,\n u32_t addr,\n u32_t len,\n u8_t *dst) {\n return SPIFFS_HAL_READ(fs, addr, len, dst);\n}\n\ns32_t spiffs_phys_wr(\n spiffs *fs,\n u32_t addr,\n u32_t len,\n u8_t *src) {\n return SPIFFS_HAL_WRITE(fs, addr, len, src);\n}\n\n#endif\n\n#if !SPIFFS_READ_ONLY\ns32_t spiffs_phys_cpy(\n spiffs *fs,\n spiffs_file fh,\n u32_t dst,\n u32_t src,\n u32_t len) {\n (void)fh;\n s32_t res;\n u8_t b[SPIFFS_COPY_BUFFER_STACK];\n while (len > 0) {\n u32_t chunk_size = MIN(SPIFFS_COPY_BUFFER_STACK, len);\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_MOVS, fh, src, chunk_size, b);\n SPIFFS_CHECK_RES(res);\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_MOVD, fh, dst, chunk_size, b);\n SPIFFS_CHECK_RES(res);\n len -= chunk_size;\n src += chunk_size;\n dst += chunk_size;\n }\n return SPIFFS_OK;\n}\n#endif // !SPIFFS_READ_ONLY\n\n// Find object lookup entry containing given id with visitor.\n// Iterate over object lookup pages in each block until a given object id entry is found.\n// When found, the visitor function is called with block index, entry index and user data.\n// If visitor returns SPIFFS_VIS_CONTINUE, the search goes on. Otherwise, the search will be\n// ended and visitor's return code is returned to caller.\n// If no visitor is given (0) the search returns on first entry with matching object id.\n// If no match is found in all look up, SPIFFS_VIS_END is returned.\n// @param fs the file system\n// @param starting_block the starting block to start search in\n// @param starting_lu_entry the look up index entry to start search in\n// @param flags ored combination of SPIFFS_VIS_CHECK_ID, SPIFFS_VIS_CHECK_PH,\n// SPIFFS_VIS_NO_WRAP\n// @param obj_id argument object id\n// @param v visitor callback function\n// @param user_const_p any const pointer, passed to the callback visitor function\n// @param user_var_p any pointer, passed to the callback visitor function\n// @param block_ix reported block index where match was found\n// @param lu_entry reported look up index where match was found\ns32_t spiffs_obj_lu_find_entry_visitor(\n spiffs *fs,\n spiffs_block_ix starting_block,\n int starting_lu_entry,\n u8_t flags,\n spiffs_obj_id obj_id,\n spiffs_visitor_f v,\n const void *user_const_p,\n void *user_var_p,\n spiffs_block_ix *block_ix,\n int *lu_entry) {\n s32_t res = SPIFFS_OK;\n s32_t entry_count = fs->block_count * SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs);\n spiffs_block_ix cur_block = starting_block;\n u32_t cur_block_addr = starting_block * SPIFFS_CFG_LOG_BLOCK_SZ(fs);\n\n spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work;\n int cur_entry = starting_lu_entry;\n int entries_per_page = (SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id));\n\n // wrap initial\n if (cur_entry >= (int)SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs) - 1) {\n cur_entry = 0;\n cur_block++;\n cur_block_addr = cur_block * SPIFFS_CFG_LOG_BLOCK_SZ(fs);\n if (cur_block >= fs->block_count) {\n if (flags & SPIFFS_VIS_NO_WRAP) {\n return SPIFFS_VIS_END;\n } else {\n // block wrap\n cur_block = 0;\n cur_block_addr = 0;\n }\n }\n }\n\n // check each block\n while (res == SPIFFS_OK && entry_count > 0) {\n int obj_lookup_page = cur_entry / entries_per_page;\n // check each object lookup page\n while (res == SPIFFS_OK && obj_lookup_page < (int)SPIFFS_OBJ_LOOKUP_PAGES(fs)) {\n int entry_offset = obj_lookup_page * entries_per_page;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,\n 0, cur_block_addr + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);\n // check each entry\n while (res == SPIFFS_OK &&\n cur_entry - entry_offset < entries_per_page && // for non-last obj lookup pages\n cur_entry < (int)SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs)) // for last obj lookup page\n {\n if ((flags & SPIFFS_VIS_CHECK_ID) == 0 || obj_lu_buf[cur_entry-entry_offset] == obj_id) {\n if (block_ix) *block_ix = cur_block;\n if (lu_entry) *lu_entry = cur_entry;\n if (v) {\n res = v(\n fs,\n (flags & SPIFFS_VIS_CHECK_PH) ? obj_id : obj_lu_buf[cur_entry-entry_offset],\n cur_block,\n cur_entry,\n user_const_p,\n user_var_p);\n if (res == SPIFFS_VIS_COUNTINUE || res == SPIFFS_VIS_COUNTINUE_RELOAD) {\n if (res == SPIFFS_VIS_COUNTINUE_RELOAD) {\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,\n 0, cur_block_addr + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);\n SPIFFS_CHECK_RES(res);\n }\n res = SPIFFS_OK;\n cur_entry++;\n entry_count--;\n continue;\n } else {\n return res;\n }\n } else {\n return SPIFFS_OK;\n }\n }\n entry_count--;\n cur_entry++;\n } // per entry\n obj_lookup_page++;\n } // per object lookup page\n cur_entry = 0;\n cur_block++;\n cur_block_addr += SPIFFS_CFG_LOG_BLOCK_SZ(fs);\n if (cur_block >= fs->block_count) {\n if (flags & SPIFFS_VIS_NO_WRAP) {\n return SPIFFS_VIS_END;\n } else {\n // block wrap\n cur_block = 0;\n cur_block_addr = 0;\n }\n }\n } // per block\n\n SPIFFS_CHECK_RES(res);\n\n return SPIFFS_VIS_END;\n}\n\n#if !SPIFFS_READ_ONLY\ns32_t spiffs_erase_block(\n spiffs *fs,\n spiffs_block_ix bix) {\n s32_t res;\n u32_t addr = SPIFFS_BLOCK_TO_PADDR(fs, bix);\n s32_t size = SPIFFS_CFG_LOG_BLOCK_SZ(fs);\n\n // here we ignore res, just try erasing the block\n while (size > 0) {\n SPIFFS_DBG(\"erase %08x:%08x\\n\", addr, SPIFFS_CFG_PHYS_ERASE_SZ(fs));\n SPIFFS_HAL_ERASE(fs, addr, SPIFFS_CFG_PHYS_ERASE_SZ(fs));\n\n addr += SPIFFS_CFG_PHYS_ERASE_SZ(fs);\n size -= SPIFFS_CFG_PHYS_ERASE_SZ(fs);\n }\n fs->free_blocks++;\n\n // register erase count for this block\n res = _spiffs_wr(fs, SPIFFS_OP_C_WRTHRU | SPIFFS_OP_T_OBJ_LU2, 0,\n SPIFFS_ERASE_COUNT_PADDR(fs, bix),\n sizeof(spiffs_obj_id), (u8_t *)&fs->max_erase_count);\n SPIFFS_CHECK_RES(res);\n\n#if SPIFFS_USE_MAGIC\n // finally, write magic\n spiffs_obj_id magic = SPIFFS_MAGIC(fs, bix);\n res = _spiffs_wr(fs, SPIFFS_OP_C_WRTHRU | SPIFFS_OP_T_OBJ_LU2, 0,\n SPIFFS_MAGIC_PADDR(fs, bix),\n sizeof(spiffs_obj_id), (u8_t *)&magic);\n SPIFFS_CHECK_RES(res);\n#endif\n\n fs->max_erase_count++;\n if (fs->max_erase_count == SPIFFS_OBJ_ID_IX_FLAG) {\n fs->max_erase_count = 0;\n }\n\n return res;\n}\n#endif // !SPIFFS_READ_ONLY\n\n#if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0\ns32_t spiffs_probe(\n spiffs_config *cfg) {\n s32_t res;\n u32_t paddr;\n spiffs dummy_fs; // create a dummy fs struct just to be able to use macros\n memcpy(&dummy_fs.cfg, cfg, sizeof(spiffs_config));\n dummy_fs.block_count = 0;\n\n // Read three magics, as one block may be in an aborted erase state.\n // At least two of these must contain magic and be in decreasing order.\n spiffs_obj_id magic[3];\n spiffs_obj_id bix_count[3];\n\n spiffs_block_ix bix;\n for (bix = 0; bix < 3; bix++) {\n paddr = SPIFFS_MAGIC_PADDR(&dummy_fs, bix);\n#if SPIFFS_HAL_CALLBACK_EXTRA\n // not any proper fs to report here, so callback with null\n // (cross fingers that no-one gets angry)\n res = cfg->hal_read_f((void *)0, paddr, sizeof(spiffs_obj_id), (u8_t *)&magic[bix]);\n#else\n res = cfg->hal_read_f(paddr, sizeof(spiffs_obj_id), (u8_t *)&magic[bix]);\n#endif\n bix_count[bix] = magic[bix] ^ SPIFFS_MAGIC(&dummy_fs, 0);\n SPIFFS_CHECK_RES(res);\n }\n\n // check that we have sane number of blocks\n if (bix_count[0] < 3) return SPIFFS_ERR_PROBE_TOO_FEW_BLOCKS;\n // check that the order is correct, take aborted erases in calculation\n // first block aborted erase\n if (magic[0] == (spiffs_obj_id)(-1) && bix_count[1] - bix_count[2] == 1) {\n return (bix_count[1]+1) * cfg->log_block_size;\n }\n // second block aborted erase\n if (magic[1] == (spiffs_obj_id)(-1) && bix_count[0] - bix_count[2] == 2) {\n return bix_count[0] * cfg->log_block_size;\n }\n // third block aborted erase\n if (magic[2] == (spiffs_obj_id)(-1) && bix_count[0] - bix_count[1] == 1) {\n return bix_count[0] * cfg->log_block_size;\n }\n // no block has aborted erase\n if (bix_count[0] - bix_count[1] == 1 && bix_count[1] - bix_count[2] == 1) {\n return bix_count[0] * cfg->log_block_size;\n }\n\n return SPIFFS_ERR_PROBE_NOT_A_FS;\n}\n#endif // SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0\n\n\nstatic s32_t spiffs_obj_lu_scan_v(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_block_ix bix,\n int ix_entry,\n const void *user_const_p,\n void *user_var_p) {\n (void)bix;\n (void)user_const_p;\n (void)user_var_p;\n if (obj_id == SPIFFS_OBJ_ID_FREE) {\n if (ix_entry == 0) {\n fs->free_blocks++;\n // todo optimize further, return SPIFFS_NEXT_BLOCK\n }\n } else if (obj_id == SPIFFS_OBJ_ID_DELETED) {\n fs->stats_p_deleted++;\n } else {\n fs->stats_p_allocated++;\n }\n\n return SPIFFS_VIS_COUNTINUE;\n}\n\n\n// Scans thru all obj lu and counts free, deleted and used pages\n// Find the maximum block erase count\n// Checks magic if enabled\ns32_t spiffs_obj_lu_scan(\n spiffs *fs) {\n s32_t res;\n spiffs_block_ix bix;\n int entry;\n#if SPIFFS_USE_MAGIC\n spiffs_block_ix unerased_bix = (spiffs_block_ix)-1;\n#endif\n\n // find out erase count\n // if enabled, check magic\n bix = 0;\n spiffs_obj_id erase_count_final;\n spiffs_obj_id erase_count_min = SPIFFS_OBJ_ID_FREE;\n spiffs_obj_id erase_count_max = 0;\n while (bix < fs->block_count) {\n#if SPIFFS_USE_MAGIC\n spiffs_obj_id magic;\n res = _spiffs_rd(fs,\n SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_MAGIC_PADDR(fs, bix) ,\n sizeof(spiffs_obj_id), (u8_t *)&magic);\n\n SPIFFS_CHECK_RES(res);\n if (magic != SPIFFS_MAGIC(fs, bix)) {\n if (unerased_bix == (spiffs_block_ix)-1) {\n // allow one unerased block as it might be powered down during an erase\n unerased_bix = bix;\n } else {\n // more than one unerased block, bail out\n SPIFFS_CHECK_RES(SPIFFS_ERR_NOT_A_FS);\n }\n }\n#endif\n spiffs_obj_id erase_count;\n res = _spiffs_rd(fs,\n SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_ERASE_COUNT_PADDR(fs, bix) ,\n sizeof(spiffs_obj_id), (u8_t *)&erase_count);\n SPIFFS_CHECK_RES(res);\n if (erase_count != SPIFFS_OBJ_ID_FREE) {\n erase_count_min = MIN(erase_count_min, erase_count);\n erase_count_max = MAX(erase_count_max, erase_count);\n }\n bix++;\n }\n\n if (erase_count_min == 0 && erase_count_max == SPIFFS_OBJ_ID_FREE) {\n // clean system, set counter to zero\n erase_count_final = 0;\n } else if (erase_count_max - erase_count_min > (SPIFFS_OBJ_ID_FREE)/2) {\n // wrap, take min\n erase_count_final = erase_count_min+1;\n } else {\n erase_count_final = erase_count_max+1;\n }\n\n fs->max_erase_count = erase_count_final;\n\n#if SPIFFS_USE_MAGIC\n if (unerased_bix != (spiffs_block_ix)-1) {\n // found one unerased block, remedy\n SPIFFS_DBG(\"mount: erase block %i\\n\", bix);\n#if SPIFFS_READ_ONLY\n res = SPIFFS_ERR_RO_ABORTED_OPERATION;\n#else\n res = spiffs_erase_block(fs, unerased_bix);\n#endif // SPIFFS_READ_ONLY\n SPIFFS_CHECK_RES(res);\n }\n#endif\n\n // count blocks\n\n fs->free_blocks = 0;\n fs->stats_p_allocated = 0;\n fs->stats_p_deleted = 0;\n\n res = spiffs_obj_lu_find_entry_visitor(fs,\n 0,\n 0,\n 0,\n 0,\n spiffs_obj_lu_scan_v,\n 0,\n 0,\n &bix,\n &entry);\n\n if (res == SPIFFS_VIS_END) {\n res = SPIFFS_OK;\n }\n\n SPIFFS_CHECK_RES(res);\n\n return res;\n}\n\n#if !SPIFFS_READ_ONLY\n// Find free object lookup entry\n// Iterate over object lookup pages in each block until a free object id entry is found\ns32_t spiffs_obj_lu_find_free(\n spiffs *fs,\n spiffs_block_ix starting_block,\n int starting_lu_entry,\n spiffs_block_ix *block_ix,\n int *lu_entry) {\n s32_t res;\n if (!fs->cleaning && fs->free_blocks < 2) {\n res = spiffs_gc_quick(fs, 0);\n if (res == SPIFFS_ERR_NO_DELETED_BLOCKS) {\n res = SPIFFS_OK;\n }\n SPIFFS_CHECK_RES(res);\n if (fs->free_blocks < 2) {\n return SPIFFS_ERR_FULL;\n }\n }\n res = spiffs_obj_lu_find_id(fs, starting_block, starting_lu_entry,\n SPIFFS_OBJ_ID_FREE, block_ix, lu_entry);\n if (res == SPIFFS_OK) {\n fs->free_cursor_block_ix = *block_ix;\n fs->free_cursor_obj_lu_entry = *lu_entry;\n if (*lu_entry == 0) {\n fs->free_blocks--;\n }\n }\n if (res == SPIFFS_ERR_FULL) {\n SPIFFS_DBG(\"fs full\\n\");\n }\n\n return res;\n}\n#endif // !SPIFFS_READ_ONLY\n\n// Find object lookup entry containing given id\n// Iterate over object lookup pages in each block until a given object id entry is found\ns32_t spiffs_obj_lu_find_id(\n spiffs *fs,\n spiffs_block_ix starting_block,\n int starting_lu_entry,\n spiffs_obj_id obj_id,\n spiffs_block_ix *block_ix,\n int *lu_entry) {\n s32_t res = spiffs_obj_lu_find_entry_visitor(\n fs, starting_block, starting_lu_entry, SPIFFS_VIS_CHECK_ID, obj_id, 0, 0, 0, block_ix, lu_entry);\n if (res == SPIFFS_VIS_END) {\n res = SPIFFS_ERR_NOT_FOUND;\n }\n return res;\n}\n\n\nstatic s32_t spiffs_obj_lu_find_id_and_span_v(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_block_ix bix,\n int ix_entry,\n const void *user_const_p,\n void *user_var_p) {\n s32_t res;\n spiffs_page_header ph;\n spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);\n res = _spiffs_rd(fs, 0, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_header), (u8_t *)&ph);\n SPIFFS_CHECK_RES(res);\n if (ph.obj_id == obj_id &&\n ph.span_ix == *((spiffs_span_ix*)user_var_p) &&\n (ph.flags & (SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED)) == SPIFFS_PH_FLAG_DELET &&\n !((obj_id & SPIFFS_OBJ_ID_IX_FLAG) && (ph.flags & SPIFFS_PH_FLAG_IXDELE) == 0 && ph.span_ix == 0) &&\n (user_const_p == 0 || *((const spiffs_page_ix*)user_const_p) != pix)) {\n return SPIFFS_OK;\n } else {\n return SPIFFS_VIS_COUNTINUE;\n }\n}\n\n// Find object lookup entry containing given id and span index\n// Iterate over object lookup pages in each block until a given object id entry is found\ns32_t spiffs_obj_lu_find_id_and_span(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_span_ix spix,\n spiffs_page_ix exclusion_pix,\n spiffs_page_ix *pix) {\n s32_t res;\n spiffs_block_ix bix;\n int entry;\n\n res = spiffs_obj_lu_find_entry_visitor(fs,\n fs->cursor_block_ix,\n fs->cursor_obj_lu_entry,\n SPIFFS_VIS_CHECK_ID,\n obj_id,\n spiffs_obj_lu_find_id_and_span_v,\n exclusion_pix ? &exclusion_pix : 0,\n &spix,\n &bix,\n &entry);\n\n if (res == SPIFFS_VIS_END) {\n res = SPIFFS_ERR_NOT_FOUND;\n }\n\n SPIFFS_CHECK_RES(res);\n\n if (pix) {\n *pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);\n }\n\n fs->cursor_block_ix = bix;\n fs->cursor_obj_lu_entry = entry;\n\n return res;\n}\n\n// Find object lookup entry containing given id and span index in page headers only\n// Iterate over object lookup pages in each block until a given object id entry is found\ns32_t spiffs_obj_lu_find_id_and_span_by_phdr(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_span_ix spix,\n spiffs_page_ix exclusion_pix,\n spiffs_page_ix *pix) {\n s32_t res;\n spiffs_block_ix bix;\n int entry;\n\n res = spiffs_obj_lu_find_entry_visitor(fs,\n fs->cursor_block_ix,\n fs->cursor_obj_lu_entry,\n SPIFFS_VIS_CHECK_PH,\n obj_id,\n spiffs_obj_lu_find_id_and_span_v,\n exclusion_pix ? &exclusion_pix : 0,\n &spix,\n &bix,\n &entry);\n\n if (res == SPIFFS_VIS_END) {\n res = SPIFFS_ERR_NOT_FOUND;\n }\n\n SPIFFS_CHECK_RES(res);\n\n if (pix) {\n *pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);\n }\n\n fs->cursor_block_ix = bix;\n fs->cursor_obj_lu_entry = entry;\n\n return res;\n}\n\n#if !SPIFFS_READ_ONLY\n// Allocates a free defined page with given obj_id\n// Occupies object lookup entry and page\n// data may be NULL; where only page header is stored, len and page_offs is ignored\ns32_t spiffs_page_allocate_data(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_page_header *ph,\n u8_t *data,\n u32_t len,\n u32_t page_offs,\n u8_t finalize,\n spiffs_page_ix *pix) {\n s32_t res = SPIFFS_OK;\n spiffs_block_ix bix;\n int entry;\n\n // find free entry\n res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);\n SPIFFS_CHECK_RES(res);\n\n // occupy page in object lookup\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,\n 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + entry * sizeof(spiffs_obj_id), sizeof(spiffs_obj_id), (u8_t*)&obj_id);\n SPIFFS_CHECK_RES(res);\n\n fs->stats_p_allocated++;\n\n // write page header\n ph->flags &= ~SPIFFS_PH_FLAG_USED;\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry), sizeof(spiffs_page_header), (u8_t*)ph);\n SPIFFS_CHECK_RES(res);\n\n // write page data\n if (data) {\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n 0,SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry) + sizeof(spiffs_page_header) + page_offs, len, data);\n SPIFFS_CHECK_RES(res);\n }\n\n // finalize header if necessary\n if (finalize && (ph->flags & SPIFFS_PH_FLAG_FINAL)) {\n ph->flags &= ~SPIFFS_PH_FLAG_FINAL;\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry) + offsetof(spiffs_page_header, flags),\n sizeof(u8_t),\n (u8_t *)&ph->flags);\n SPIFFS_CHECK_RES(res);\n }\n\n // return written page\n if (pix) {\n *pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);\n }\n\n return res;\n}\n#endif // !SPIFFS_READ_ONLY\n\n#if !SPIFFS_READ_ONLY\n// Moves a page from src to a free page and finalizes it. Updates page index. Page data is given in param page.\n// If page data is null, provided header is used for metainfo and page data is physically copied.\ns32_t spiffs_page_move(\n spiffs *fs,\n spiffs_file fh,\n u8_t *page_data,\n spiffs_obj_id obj_id,\n spiffs_page_header *page_hdr,\n spiffs_page_ix src_pix,\n spiffs_page_ix *dst_pix) {\n s32_t res;\n u8_t was_final = 0;\n spiffs_page_header *p_hdr;\n spiffs_block_ix bix;\n int entry;\n spiffs_page_ix free_pix;\n\n // find free entry\n res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);\n SPIFFS_CHECK_RES(res);\n free_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);\n\n if (dst_pix) *dst_pix = free_pix;\n\n p_hdr = page_data ? (spiffs_page_header *)page_data : page_hdr;\n if (page_data) {\n // got page data\n was_final = (p_hdr->flags & SPIFFS_PH_FLAG_FINAL) == 0;\n // write unfinalized page\n p_hdr->flags |= SPIFFS_PH_FLAG_FINAL;\n p_hdr->flags &= ~SPIFFS_PH_FLAG_USED;\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n 0, SPIFFS_PAGE_TO_PADDR(fs, free_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), page_data);\n } else {\n // copy page data\n res = spiffs_phys_cpy(fs, fh, SPIFFS_PAGE_TO_PADDR(fs, free_pix), SPIFFS_PAGE_TO_PADDR(fs, src_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs));\n }\n SPIFFS_CHECK_RES(res);\n\n // mark entry in destination object lookup\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,\n 0, SPIFFS_BLOCK_TO_PADDR(fs, SPIFFS_BLOCK_FOR_PAGE(fs, free_pix)) + SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, free_pix) * sizeof(spiffs_page_ix),\n sizeof(spiffs_obj_id),\n (u8_t *)&obj_id);\n SPIFFS_CHECK_RES(res);\n\n fs->stats_p_allocated++;\n\n if (was_final) {\n // mark finalized in destination page\n p_hdr->flags &= ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_USED);\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n fh,\n SPIFFS_PAGE_TO_PADDR(fs, free_pix) + offsetof(spiffs_page_header, flags),\n sizeof(u8_t),\n (u8_t *)&p_hdr->flags);\n SPIFFS_CHECK_RES(res);\n }\n // mark source deleted\n res = spiffs_page_delete(fs, src_pix);\n return res;\n}\n#endif // !SPIFFS_READ_ONLY\n\n#if !SPIFFS_READ_ONLY\n// Deletes a page and removes it from object lookup.\ns32_t spiffs_page_delete(\n spiffs *fs,\n spiffs_page_ix pix) {\n s32_t res;\n spiffs_page_header hdr;\n hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED);\n // mark deleted entry in source object lookup\n spiffs_obj_id d_obj_id = SPIFFS_OBJ_ID_DELETED;\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_DELE,\n 0,\n SPIFFS_BLOCK_TO_PADDR(fs, SPIFFS_BLOCK_FOR_PAGE(fs, pix)) + SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, pix) * sizeof(spiffs_page_ix),\n sizeof(spiffs_obj_id),\n (u8_t *)&d_obj_id);\n SPIFFS_CHECK_RES(res);\n\n fs->stats_p_deleted++;\n fs->stats_p_allocated--;\n\n // mark deleted in source page\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_DELE,\n 0,\n SPIFFS_PAGE_TO_PADDR(fs, pix) + offsetof(spiffs_page_header, flags),\n sizeof(u8_t),\n (u8_t *)&hdr.flags);\n\n return res;\n}\n#endif // !SPIFFS_READ_ONLY\n\n#if !SPIFFS_READ_ONLY\n// Create an object index header page with empty index and undefined length\ns32_t spiffs_object_create(\n spiffs *fs,\n spiffs_obj_id obj_id,\n const u8_t name[SPIFFS_OBJ_NAME_LEN],\n spiffs_obj_type type,\n spiffs_page_ix *objix_hdr_pix) {\n s32_t res = SPIFFS_OK;\n spiffs_block_ix bix;\n spiffs_page_object_ix_header oix_hdr;\n int entry;\n\n res = spiffs_gc_check(fs, SPIFFS_DATA_PAGE_SIZE(fs));\n SPIFFS_CHECK_RES(res);\n\n obj_id |= SPIFFS_OBJ_ID_IX_FLAG;\n\n // find free entry\n res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);\n SPIFFS_CHECK_RES(res);\n SPIFFS_DBG(\"create: found free page @ %04x bix:%i entry:%i\\n\", SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry), bix, entry);\n\n // occupy page in object lookup\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,\n 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + entry * sizeof(spiffs_obj_id), sizeof(spiffs_obj_id), (u8_t*)&obj_id);\n SPIFFS_CHECK_RES(res);\n\n fs->stats_p_allocated++;\n\n // write empty object index page\n oix_hdr.p_hdr.obj_id = obj_id;\n oix_hdr.p_hdr.span_ix = 0;\n oix_hdr.p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_USED);\n oix_hdr.type = type;\n oix_hdr.size = SPIFFS_UNDEFINED_LEN; // keep ones so we can update later without wasting this page\n strncpy((char*)&oix_hdr.name, (const char*)name, SPIFFS_OBJ_NAME_LEN);\n\n\n // update page\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry), sizeof(spiffs_page_object_ix_header), (u8_t*)&oix_hdr);\n\n SPIFFS_CHECK_RES(res);\n spiffs_cb_object_event(fs, 0, SPIFFS_EV_IX_NEW, obj_id, 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry), SPIFFS_UNDEFINED_LEN);\n\n if (objix_hdr_pix) {\n *objix_hdr_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);\n }\n\n return res;\n}\n#endif // !SPIFFS_READ_ONLY\n\n#if !SPIFFS_READ_ONLY\n// update object index header with any combination of name/size/index\n// new_objix_hdr_data may be null, if so the object index header page is loaded\n// name may be null, if so name is not changed\n// size may be null, if so size is not changed\ns32_t spiffs_object_update_index_hdr(\n spiffs *fs,\n spiffs_fd *fd,\n spiffs_obj_id obj_id,\n spiffs_page_ix objix_hdr_pix,\n u8_t *new_objix_hdr_data,\n const u8_t name[SPIFFS_OBJ_NAME_LEN],\n u32_t size,\n spiffs_page_ix *new_pix) {\n s32_t res = SPIFFS_OK;\n spiffs_page_object_ix_header *objix_hdr;\n spiffs_page_ix new_objix_hdr_pix;\n\n obj_id |= SPIFFS_OBJ_ID_IX_FLAG;\n\n if (new_objix_hdr_data) {\n // object index header page already given to us, no need to load it\n objix_hdr = (spiffs_page_object_ix_header *)new_objix_hdr_data;\n } else {\n // read object index header page\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, objix_hdr_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res);\n objix_hdr = (spiffs_page_object_ix_header *)fs->work;\n }\n\n SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, obj_id, 0);\n\n // change name\n if (name) {\n strncpy((char*)objix_hdr->name, (const char*)name, SPIFFS_OBJ_NAME_LEN);\n }\n if (size) {\n objix_hdr->size = size;\n }\n\n // move and update page\n res = spiffs_page_move(fs, fd == 0 ? 0 : fd->file_nbr, (u8_t*)objix_hdr, obj_id, 0, objix_hdr_pix, &new_objix_hdr_pix);\n\n if (res == SPIFFS_OK) {\n if (new_pix) {\n *new_pix = new_objix_hdr_pix;\n }\n // callback on object index update\n spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, obj_id, objix_hdr->p_hdr.span_ix, new_objix_hdr_pix, objix_hdr->size);\n if (fd) fd->objix_hdr_pix = new_objix_hdr_pix; // if this is not in the registered cluster\n }\n\n return res;\n}\n#endif // !SPIFFS_READ_ONLY\n\nvoid spiffs_cb_object_event(\n spiffs *fs,\n spiffs_fd *fd,\n int ev,\n spiffs_obj_id obj_id_raw,\n spiffs_span_ix spix,\n spiffs_page_ix new_pix,\n u32_t new_size) {\n (void)fd;\n // update index caches in all file descriptors\n spiffs_obj_id obj_id = obj_id_raw & ~SPIFFS_OBJ_ID_IX_FLAG;\n u32_t i;\n spiffs_fd *fds = (spiffs_fd *)fs->fd_space;\n for (i = 0; i < fs->fd_count; i++) {\n spiffs_fd *cur_fd = &fds[i];\n if (cur_fd->file_nbr == 0 || (cur_fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG) != obj_id) continue;\n if (spix == 0) {\n if (ev == SPIFFS_EV_IX_NEW || ev == SPIFFS_EV_IX_UPD) {\n SPIFFS_DBG(\" callback: setting fd %i:%04x objix_hdr_pix to %04x, size:%i\\n\", cur_fd->file_nbr, cur_fd->obj_id, new_pix, new_size);\n cur_fd->objix_hdr_pix = new_pix;\n if (new_size != 0) {\n cur_fd->size = new_size;\n }\n } else if (ev == SPIFFS_EV_IX_DEL) {\n cur_fd->file_nbr = 0;\n cur_fd->obj_id = SPIFFS_OBJ_ID_DELETED;\n }\n }\n if (cur_fd->cursor_objix_spix == spix) {\n if (ev == SPIFFS_EV_IX_NEW || ev == SPIFFS_EV_IX_UPD) {\n SPIFFS_DBG(\" callback: setting fd %i:%04x span:%04x objix_pix to %04x\\n\", cur_fd->file_nbr, cur_fd->obj_id, spix, new_pix);\n cur_fd->cursor_objix_pix = new_pix;\n } else {\n cur_fd->cursor_objix_pix = 0;\n }\n }\n }\n\n // callback to user if object index header\n if (fs->file_cb_f && spix == 0 && (obj_id_raw & SPIFFS_OBJ_ID_IX_FLAG)) {\n spiffs_fileop_type op;\n if (ev == SPIFFS_EV_IX_NEW) {\n op = SPIFFS_CB_CREATED;\n } else if (ev == SPIFFS_EV_IX_UPD) {\n op = SPIFFS_CB_UPDATED;\n } else if (ev == SPIFFS_EV_IX_DEL) {\n op = SPIFFS_CB_DELETED;\n } else {\n SPIFFS_DBG(\" callback: WARNING unknown callback event %02x\\n\", ev);\n return; // bail out\n }\n fs->file_cb_f(fs, op, obj_id, new_pix);\n }\n}\n\n// Open object by id\ns32_t spiffs_object_open_by_id(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_fd *fd,\n spiffs_flags flags,\n spiffs_mode mode) {\n s32_t res = SPIFFS_OK;\n spiffs_page_ix pix;\n\n res = spiffs_obj_lu_find_id_and_span(fs, obj_id | SPIFFS_OBJ_ID_IX_FLAG, 0, 0, &pix);\n SPIFFS_CHECK_RES(res);\n\n res = spiffs_object_open_by_page(fs, pix, fd, flags, mode);\n\n return res;\n}\n\n// Open object by page index\ns32_t spiffs_object_open_by_page(\n spiffs *fs,\n spiffs_page_ix pix,\n spiffs_fd *fd,\n spiffs_flags flags,\n spiffs_mode mode) {\n (void)mode;\n s32_t res = SPIFFS_OK;\n spiffs_page_object_ix_header oix_hdr;\n spiffs_obj_id obj_id;\n\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&oix_hdr);\n SPIFFS_CHECK_RES(res);\n\n spiffs_block_ix bix = SPIFFS_BLOCK_FOR_PAGE(fs, pix);\n int entry = SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, pix);\n\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,\n 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + entry * sizeof(spiffs_obj_id), sizeof(spiffs_obj_id), (u8_t *)&obj_id);\n\n fd->fs = fs;\n fd->objix_hdr_pix = pix;\n fd->size = oix_hdr.size;\n fd->offset = 0;\n fd->cursor_objix_pix = pix;\n fd->cursor_objix_spix = 0;\n fd->obj_id = obj_id;\n fd->flags = flags;\n\n SPIFFS_VALIDATE_OBJIX(oix_hdr.p_hdr, fd->obj_id, 0);\n\n SPIFFS_DBG(\"open: fd %i is obj id %04x\\n\", fd->file_nbr, fd->obj_id);\n\n return res;\n}\n\n#if !SPIFFS_READ_ONLY\n// Append to object\n// keep current object index (header) page in fs->work buffer\ns32_t spiffs_object_append(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) {\n spiffs *fs = fd->fs;\n s32_t res = SPIFFS_OK;\n u32_t written = 0;\n\n SPIFFS_DBG(\"append: %i bytes @ offs %i of size %i\\n\", len, offset, fd->size);\n\n if (offset > fd->size) {\n SPIFFS_DBG(\"append: offset reversed to size\\n\");\n offset = fd->size;\n }\n\n res = spiffs_gc_check(fs, len + SPIFFS_DATA_PAGE_SIZE(fs)); // add an extra page of data worth for meta\n if (res != SPIFFS_OK) {\n SPIFFS_DBG(\"append: gc check fail %i\\n\", res);\n }\n SPIFFS_CHECK_RES(res);\n\n spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;\n spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;\n spiffs_page_header p_hdr;\n\n spiffs_span_ix cur_objix_spix = 0;\n spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;\n spiffs_page_ix cur_objix_pix = fd->objix_hdr_pix;\n spiffs_page_ix new_objix_hdr_page;\n\n spiffs_span_ix data_spix = offset / SPIFFS_DATA_PAGE_SIZE(fs);\n spiffs_page_ix data_page;\n u32_t page_offs = offset % SPIFFS_DATA_PAGE_SIZE(fs);\n\n // write all data\n while (res == SPIFFS_OK && written < len) {\n // calculate object index page span index\n cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);\n\n // handle storing and loading of object indices\n if (cur_objix_spix != prev_objix_spix) {\n // new object index page\n // within this clause we return directly if something fails, object index mess-up\n if (written > 0) {\n // store previous object index page, unless first pass\n SPIFFS_DBG(\"append: %04x store objix %04x:%04x, written %i\\n\", fd->obj_id,\n cur_objix_pix, prev_objix_spix, written);\n if (prev_objix_spix == 0) {\n // this is an update to object index header page\n objix_hdr->size = offset+written;\n if (offset == 0) {\n // was an empty object, update same page (size was 0xffffffff)\n res = spiffs_page_index_check(fs, fd, cur_objix_pix, 0);\n SPIFFS_CHECK_RES(res);\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res);\n } else {\n // was a nonempty object, update to new page\n res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,\n fd->objix_hdr_pix, fs->work, 0, offset+written, &new_objix_hdr_page);\n SPIFFS_CHECK_RES(res);\n SPIFFS_DBG(\"append: %04x store new objix_hdr, %04x:%04x, written %i\\n\", fd->obj_id,\n new_objix_hdr_page, 0, written);\n }\n } else {\n // this is an update to an object index page\n res = spiffs_page_index_check(fs, fd, cur_objix_pix, prev_objix_spix);\n SPIFFS_CHECK_RES(res);\n\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res);\n spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD,fd->obj_id, objix->p_hdr.span_ix, cur_objix_pix, 0);\n // update length in object index header page\n res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,\n fd->objix_hdr_pix, 0, 0, offset+written, &new_objix_hdr_page);\n SPIFFS_CHECK_RES(res);\n SPIFFS_DBG(\"append: %04x store new size I %i in objix_hdr, %04x:%04x, written %i\\n\", fd->obj_id,\n offset+written, new_objix_hdr_page, 0, written);\n }\n fd->size = offset+written;\n fd->offset = offset+written;\n }\n\n // create or load new object index page\n if (cur_objix_spix == 0) {\n // load object index header page, must always exist\n SPIFFS_DBG(\"append: %04x load objixhdr page %04x:%04x\\n\", fd->obj_id, cur_objix_pix, cur_objix_spix);\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res);\n SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);\n } else {\n spiffs_span_ix len_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, (fd->size-1)/SPIFFS_DATA_PAGE_SIZE(fs));\n // on subsequent passes, create a new object index page\n if (written > 0 || cur_objix_spix > len_objix_spix) {\n p_hdr.obj_id = fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG;\n p_hdr.span_ix = cur_objix_spix;\n p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_INDEX);\n res = spiffs_page_allocate_data(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG,\n &p_hdr, 0, 0, 0, 1, &cur_objix_pix);\n SPIFFS_CHECK_RES(res);\n spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_NEW, fd->obj_id, cur_objix_spix, cur_objix_pix, 0);\n // quick \"load\" of new object index page\n memset(fs->work, 0xff, SPIFFS_CFG_LOG_PAGE_SZ(fs));\n memcpy(fs->work, &p_hdr, sizeof(spiffs_page_header));\n SPIFFS_DBG(\"append: %04x create objix page, %04x:%04x, written %i\\n\", fd->obj_id\n , cur_objix_pix, cur_objix_spix, written);\n } else {\n // on first pass, we load existing object index page\n spiffs_page_ix pix;\n SPIFFS_DBG(\"append: %04x find objix span_ix:%04x\\n\", fd->obj_id, cur_objix_spix);\n if (fd->cursor_objix_spix == cur_objix_spix) {\n pix = fd->cursor_objix_pix;\n } else {\n res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &pix);\n SPIFFS_CHECK_RES(res);\n }\n SPIFFS_DBG(\"append: %04x found object index at page %04x [fd size %i]\\n\", fd->obj_id, pix, fd->size);\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res);\n SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);\n cur_objix_pix = pix;\n }\n fd->cursor_objix_pix = cur_objix_pix;\n fd->cursor_objix_spix = cur_objix_spix;\n fd->offset = offset+written;\n fd->size = offset+written;\n }\n prev_objix_spix = cur_objix_spix;\n }\n\n // write data\n u32_t to_write = MIN(len-written, SPIFFS_DATA_PAGE_SIZE(fs) - page_offs);\n if (page_offs == 0) {\n // at beginning of a page, allocate and write a new page of data\n p_hdr.obj_id = fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;\n p_hdr.span_ix = data_spix;\n p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL); // finalize immediately\n res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,\n &p_hdr, &data[written], to_write, page_offs, 1, &data_page);\n SPIFFS_DBG(\"append: %04x store new data page, %04x:%04x offset:%i, len %i, written %i\\n\", fd->obj_id,\n data_page, data_spix, page_offs, to_write, written);\n } else {\n // append to existing page, fill out free data in existing page\n if (cur_objix_spix == 0) {\n // get data page from object index header page\n data_page = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];\n } else {\n // get data page from object index page\n data_page = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];\n }\n\n res = spiffs_page_data_check(fs, fd, data_page, data_spix);\n SPIFFS_CHECK_RES(res);\n\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, data_page) + sizeof(spiffs_page_header) + page_offs, to_write, &data[written]);\n SPIFFS_DBG(\"append: %04x store to existing data page, %04x:%04x offset:%i, len %i, written %i\\n\", fd->obj_id\n , data_page, data_spix, page_offs, to_write, written);\n }\n\n if (res != SPIFFS_OK) break;\n\n // update memory representation of object index page with new data page\n if (cur_objix_spix == 0) {\n // update object index header page\n ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = data_page;\n SPIFFS_DBG(\"append: %04x wrote page %04x to objix_hdr entry %02x in mem\\n\", fd->obj_id\n , data_page, data_spix);\n objix_hdr->size = offset+written;\n } else {\n // update object index page\n ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = data_page;\n SPIFFS_DBG(\"append: %04x wrote page %04x to objix entry %02x in mem\\n\", fd->obj_id\n , data_page, SPIFFS_OBJ_IX_ENTRY(fs, data_spix));\n }\n\n // update internals\n page_offs = 0;\n data_spix++;\n written += to_write;\n } // while all data\n\n fd->size = offset+written;\n fd->offset = offset+written;\n fd->cursor_objix_pix = cur_objix_pix;\n fd->cursor_objix_spix = cur_objix_spix;\n\n // finalize updated object indices\n s32_t res2 = SPIFFS_OK;\n if (cur_objix_spix != 0) {\n // wrote beyond object index header page\n // write last modified object index page, unless object header index page\n SPIFFS_DBG(\"append: %04x store objix page, %04x:%04x, written %i\\n\", fd->obj_id,\n cur_objix_pix, cur_objix_spix, written);\n\n res2 = spiffs_page_index_check(fs, fd, cur_objix_pix, cur_objix_spix);\n SPIFFS_CHECK_RES(res2);\n\n res2 = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res2);\n spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, cur_objix_pix, 0);\n\n // update size in object header index page\n res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,\n fd->objix_hdr_pix, 0, 0, offset+written, &new_objix_hdr_page);\n SPIFFS_DBG(\"append: %04x store new size II %i in objix_hdr, %04x:%04x, written %i, res %i\\n\", fd->obj_id\n , offset+written, new_objix_hdr_page, 0, written, res2);\n SPIFFS_CHECK_RES(res2);\n } else {\n // wrote within object index header page\n if (offset == 0) {\n // wrote to empty object - simply update size and write whole page\n objix_hdr->size = offset+written;\n SPIFFS_DBG(\"append: %04x store fresh objix_hdr page, %04x:%04x, written %i\\n\", fd->obj_id\n , cur_objix_pix, cur_objix_spix, written);\n\n res2 = spiffs_page_index_check(fs, fd, cur_objix_pix, cur_objix_spix);\n SPIFFS_CHECK_RES(res2);\n\n res2 = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res2);\n // callback on object index update\n spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, fd->obj_id, objix_hdr->p_hdr.span_ix, cur_objix_pix, objix_hdr->size);\n } else {\n // modifying object index header page, update size and make new copy\n res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,\n fd->objix_hdr_pix, fs->work, 0, offset+written, &new_objix_hdr_page);\n SPIFFS_DBG(\"append: %04x store modified objix_hdr page, %04x:%04x, written %i\\n\", fd->obj_id\n , new_objix_hdr_page, 0, written);\n SPIFFS_CHECK_RES(res2);\n }\n }\n\n return res;\n} // spiffs_object_append\n#endif // !SPIFFS_READ_ONLY\n\n#if !SPIFFS_READ_ONLY\n// Modify object\n// keep current object index (header) page in fs->work buffer\ns32_t spiffs_object_modify(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) {\n spiffs *fs = fd->fs;\n s32_t res = SPIFFS_OK;\n u32_t written = 0;\n\n res = spiffs_gc_check(fs, len + SPIFFS_DATA_PAGE_SIZE(fs));\n SPIFFS_CHECK_RES(res);\n\n spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;\n spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;\n spiffs_page_header p_hdr;\n\n spiffs_span_ix cur_objix_spix = 0;\n spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;\n spiffs_page_ix cur_objix_pix = fd->objix_hdr_pix;\n spiffs_page_ix new_objix_hdr_pix;\n\n spiffs_span_ix data_spix = offset / SPIFFS_DATA_PAGE_SIZE(fs);\n spiffs_page_ix data_pix;\n u32_t page_offs = offset % SPIFFS_DATA_PAGE_SIZE(fs);\n\n\n // write all data\n while (res == SPIFFS_OK && written < len) {\n // calculate object index page span index\n cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);\n\n // handle storing and loading of object indices\n if (cur_objix_spix != prev_objix_spix) {\n // new object index page\n // within this clause we return directly if something fails, object index mess-up\n if (written > 0) {\n // store previous object index (header) page, unless first pass\n if (prev_objix_spix == 0) {\n // store previous object index header page\n res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,\n fd->objix_hdr_pix, fs->work, 0, 0, &new_objix_hdr_pix);\n SPIFFS_DBG(\"modify: store modified objix_hdr page, %04x:%04x, written %i\\n\", new_objix_hdr_pix, 0, written);\n SPIFFS_CHECK_RES(res);\n } else {\n // store new version of previous object index page\n spiffs_page_ix new_objix_pix;\n\n res = spiffs_page_index_check(fs, fd, cur_objix_pix, prev_objix_spix);\n SPIFFS_CHECK_RES(res);\n\n res = spiffs_page_move(fs, fd->file_nbr, (u8_t*)objix, fd->obj_id, 0, cur_objix_pix, &new_objix_pix);\n SPIFFS_DBG(\"modify: store previous modified objix page, %04x:%04x, written %i\\n\", new_objix_pix, objix->p_hdr.span_ix, written);\n SPIFFS_CHECK_RES(res);\n spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, new_objix_pix, 0);\n }\n }\n\n // load next object index page\n if (cur_objix_spix == 0) {\n // load object index header page, must exist\n SPIFFS_DBG(\"modify: load objixhdr page %04x:%04x\\n\", cur_objix_pix, cur_objix_spix);\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res);\n SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);\n } else {\n // load existing object index page on first pass\n spiffs_page_ix pix;\n SPIFFS_DBG(\"modify: find objix span_ix:%04x\\n\", cur_objix_spix);\n if (fd->cursor_objix_spix == cur_objix_spix) {\n pix = fd->cursor_objix_pix;\n } else {\n res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &pix);\n SPIFFS_CHECK_RES(res);\n }\n SPIFFS_DBG(\"modify: found object index at page %04x\\n\", pix);\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res);\n SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);\n cur_objix_pix = pix;\n }\n fd->cursor_objix_pix = cur_objix_pix;\n fd->cursor_objix_spix = cur_objix_spix;\n fd->offset = offset+written;\n prev_objix_spix = cur_objix_spix;\n }\n\n // write partial data\n u32_t to_write = MIN(len-written, SPIFFS_DATA_PAGE_SIZE(fs) - page_offs);\n spiffs_page_ix orig_data_pix;\n if (cur_objix_spix == 0) {\n // get data page from object index header page\n orig_data_pix = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];\n } else {\n // get data page from object index page\n orig_data_pix = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];\n }\n\n p_hdr.obj_id = fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;\n p_hdr.span_ix = data_spix;\n p_hdr.flags = 0xff;\n if (page_offs == 0 && to_write == SPIFFS_DATA_PAGE_SIZE(fs)) {\n // a full page, allocate and write a new page of data\n res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,\n &p_hdr, &data[written], to_write, page_offs, 1, &data_pix);\n SPIFFS_DBG(\"modify: store new data page, %04x:%04x offset:%i, len %i, written %i\\n\", data_pix, data_spix, page_offs, to_write, written);\n } else {\n // write to existing page, allocate new and copy unmodified data\n\n res = spiffs_page_data_check(fs, fd, orig_data_pix, data_spix);\n SPIFFS_CHECK_RES(res);\n\n res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,\n &p_hdr, 0, 0, 0, 0, &data_pix);\n if (res != SPIFFS_OK) break;\n\n // copy unmodified data\n if (page_offs > 0) {\n // before modification\n res = spiffs_phys_cpy(fs, fd->file_nbr,\n SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header),\n SPIFFS_PAGE_TO_PADDR(fs, orig_data_pix) + sizeof(spiffs_page_header),\n page_offs);\n if (res != SPIFFS_OK) break;\n }\n if (page_offs + to_write < SPIFFS_DATA_PAGE_SIZE(fs)) {\n // after modification\n res = spiffs_phys_cpy(fs, fd->file_nbr,\n SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + page_offs + to_write,\n SPIFFS_PAGE_TO_PADDR(fs, orig_data_pix) + sizeof(spiffs_page_header) + page_offs + to_write,\n SPIFFS_DATA_PAGE_SIZE(fs) - (page_offs + to_write));\n if (res != SPIFFS_OK) break;\n }\n\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n fd->file_nbr,\n SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + page_offs, to_write, &data[written]);\n if (res != SPIFFS_OK) break;\n p_hdr.flags &= ~SPIFFS_PH_FLAG_FINAL;\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n fd->file_nbr,\n SPIFFS_PAGE_TO_PADDR(fs, data_pix) + offsetof(spiffs_page_header, flags),\n sizeof(u8_t),\n (u8_t *)&p_hdr.flags);\n if (res != SPIFFS_OK) break;\n\n SPIFFS_DBG(\"modify: store to existing data page, src:%04x, dst:%04x:%04x offset:%i, len %i, written %i\\n\", orig_data_pix, data_pix, data_spix, page_offs, to_write, written);\n }\n\n // delete original data page\n res = spiffs_page_delete(fs, orig_data_pix);\n if (res != SPIFFS_OK) break;\n // update memory representation of object index page with new data page\n if (cur_objix_spix == 0) {\n // update object index header page\n ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = data_pix;\n SPIFFS_DBG(\"modify: wrote page %04x to objix_hdr entry %02x in mem\\n\", data_pix, data_spix);\n } else {\n // update object index page\n ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = data_pix;\n SPIFFS_DBG(\"modify: wrote page %04x to objix entry %02x in mem\\n\", data_pix, SPIFFS_OBJ_IX_ENTRY(fs, data_spix));\n }\n\n // update internals\n page_offs = 0;\n data_spix++;\n written += to_write;\n } // while all data\n\n fd->offset = offset+written;\n fd->cursor_objix_pix = cur_objix_pix;\n fd->cursor_objix_spix = cur_objix_spix;\n\n // finalize updated object indices\n s32_t res2 = SPIFFS_OK;\n if (cur_objix_spix != 0) {\n // wrote beyond object index header page\n // write last modified object index page\n // move and update page\n spiffs_page_ix new_objix_pix;\n\n res2 = spiffs_page_index_check(fs, fd, cur_objix_pix, cur_objix_spix);\n SPIFFS_CHECK_RES(res2);\n\n res2 = spiffs_page_move(fs, fd->file_nbr, (u8_t*)objix, fd->obj_id, 0, cur_objix_pix, &new_objix_pix);\n SPIFFS_DBG(\"modify: store modified objix page, %04x:%04x, written %i\\n\", new_objix_pix, cur_objix_spix, written);\n fd->cursor_objix_pix = new_objix_pix;\n fd->cursor_objix_spix = cur_objix_spix;\n SPIFFS_CHECK_RES(res2);\n spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, new_objix_pix, 0);\n\n } else {\n // wrote within object index header page\n res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,\n fd->objix_hdr_pix, fs->work, 0, 0, &new_objix_hdr_pix);\n SPIFFS_DBG(\"modify: store modified objix_hdr page, %04x:%04x, written %i\\n\", new_objix_hdr_pix, 0, written);\n SPIFFS_CHECK_RES(res2);\n }\n\n return res;\n} // spiffs_object_modify\n#endif // !SPIFFS_READ_ONLY\n\nstatic s32_t spiffs_object_find_object_index_header_by_name_v(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_block_ix bix,\n int ix_entry,\n const void *user_const_p,\n void *user_var_p) {\n (void)user_var_p;\n s32_t res;\n spiffs_page_object_ix_header objix_hdr;\n spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);\n if (obj_id == SPIFFS_OBJ_ID_FREE || obj_id == SPIFFS_OBJ_ID_DELETED ||\n (obj_id & SPIFFS_OBJ_ID_IX_FLAG) == 0) {\n return SPIFFS_VIS_COUNTINUE;\n }\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);\n SPIFFS_CHECK_RES(res);\n if (objix_hdr.p_hdr.span_ix == 0 &&\n (objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) ==\n (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) {\n if (strcmp((const char*)user_const_p, (char*)objix_hdr.name) == 0) {\n return SPIFFS_OK;\n }\n }\n\n return SPIFFS_VIS_COUNTINUE;\n}\n\n// Finds object index header page by name\ns32_t spiffs_object_find_object_index_header_by_name(\n spiffs *fs,\n const u8_t name[SPIFFS_OBJ_NAME_LEN],\n spiffs_page_ix *pix) {\n s32_t res;\n spiffs_block_ix bix;\n int entry;\n\n res = spiffs_obj_lu_find_entry_visitor(fs,\n fs->cursor_block_ix,\n fs->cursor_obj_lu_entry,\n 0,\n 0,\n spiffs_object_find_object_index_header_by_name_v,\n name,\n 0,\n &bix,\n &entry);\n\n if (res == SPIFFS_VIS_END) {\n res = SPIFFS_ERR_NOT_FOUND;\n }\n SPIFFS_CHECK_RES(res);\n\n if (pix) {\n *pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);\n }\n\n fs->cursor_block_ix = bix;\n fs->cursor_obj_lu_entry = entry;\n\n return res;\n}\n\n#if !SPIFFS_READ_ONLY\n// Truncates object to new size. If new size is null, object may be removed totally\ns32_t spiffs_object_truncate(\n spiffs_fd *fd,\n u32_t new_size,\n u8_t remove) {\n s32_t res = SPIFFS_OK;\n spiffs *fs = fd->fs;\n\n // need 2 pages if not removing: object index page + possibly chopped data page\n res = spiffs_gc_check(fs, remove ? 0 : SPIFFS_DATA_PAGE_SIZE(fs) * 2);\n SPIFFS_CHECK_RES(res);\n\n spiffs_page_ix objix_pix = fd->objix_hdr_pix;\n spiffs_span_ix data_spix = (fd->size > 0 ? fd->size-1 : 0) / SPIFFS_DATA_PAGE_SIZE(fs);\n u32_t cur_size = fd->size == (u32_t)SPIFFS_UNDEFINED_LEN ? 0 : fd->size ;\n spiffs_span_ix cur_objix_spix = 0;\n spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;\n spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;\n spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;\n spiffs_page_ix data_pix;\n spiffs_page_ix new_objix_hdr_pix;\n\n // before truncating, check if object is to be fully removed and mark this\n if (remove && new_size == 0) {\n u8_t flags = ~( SPIFFS_PH_FLAG_USED | SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE);\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, fd->objix_hdr_pix) + offsetof(spiffs_page_header, flags),\n sizeof(u8_t),\n (u8_t *)&flags);\n SPIFFS_CHECK_RES(res);\n }\n\n // delete from end of object until desired len is reached\n while (cur_size > new_size) {\n cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);\n\n // put object index for current data span index in work buffer\n if (prev_objix_spix != cur_objix_spix) {\n if (prev_objix_spix != (spiffs_span_ix)-1) {\n // remove previous object index page\n SPIFFS_DBG(\"truncate: delete objix page %04x:%04x\\n\", objix_pix, prev_objix_spix);\n\n res = spiffs_page_index_check(fs, fd, objix_pix, prev_objix_spix);\n SPIFFS_CHECK_RES(res);\n\n res = spiffs_page_delete(fs, objix_pix);\n SPIFFS_CHECK_RES(res);\n spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_DEL, fd->obj_id, objix->p_hdr.span_ix, objix_pix, 0);\n if (prev_objix_spix > 0) {\n // update object index header page\n SPIFFS_DBG(\"truncate: update objix hdr page %04x:%04x to size %i\\n\", fd->objix_hdr_pix, prev_objix_spix, cur_size);\n res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,\n fd->objix_hdr_pix, 0, 0, cur_size, &new_objix_hdr_pix);\n SPIFFS_CHECK_RES(res);\n fd->size = cur_size;\n }\n }\n // load current object index (header) page\n if (cur_objix_spix == 0) {\n objix_pix = fd->objix_hdr_pix;\n } else {\n res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &objix_pix);\n SPIFFS_CHECK_RES(res);\n }\n\n SPIFFS_DBG(\"truncate: load objix page %04x:%04x for data spix:%04x\\n\", objix_pix, cur_objix_spix, data_spix);\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res);\n SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);\n fd->cursor_objix_pix = objix_pix;\n fd->cursor_objix_spix = cur_objix_spix;\n fd->offset = cur_size;\n\n prev_objix_spix = cur_objix_spix;\n }\n\n if (cur_objix_spix == 0) {\n // get data page from object index header page\n data_pix = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];\n ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = SPIFFS_OBJ_ID_FREE;\n } else {\n // get data page from object index page\n data_pix = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];\n ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = SPIFFS_OBJ_ID_FREE;\n }\n\n SPIFFS_DBG(\"truncate: got data pix %04x\\n\", data_pix);\n\n if (new_size == 0 || remove || cur_size - new_size >= SPIFFS_DATA_PAGE_SIZE(fs)) {\n // delete full data page\n res = spiffs_page_data_check(fs, fd, data_pix, data_spix);\n if (res != SPIFFS_ERR_DELETED && res != SPIFFS_OK && res != SPIFFS_ERR_INDEX_REF_FREE) {\n SPIFFS_DBG(\"truncate: err validating data pix %i\\n\", res);\n break;\n }\n\n if (res == SPIFFS_OK) {\n res = spiffs_page_delete(fs, data_pix);\n if (res != SPIFFS_OK) {\n SPIFFS_DBG(\"truncate: err deleting data pix %i\\n\", res);\n break;\n }\n } else if (res == SPIFFS_ERR_DELETED || res == SPIFFS_ERR_INDEX_REF_FREE) {\n res = SPIFFS_OK;\n }\n\n // update current size\n if (cur_size % SPIFFS_DATA_PAGE_SIZE(fs) == 0) {\n cur_size -= SPIFFS_DATA_PAGE_SIZE(fs);\n } else {\n cur_size -= cur_size % SPIFFS_DATA_PAGE_SIZE(fs);\n }\n fd->size = cur_size;\n fd->offset = cur_size;\n SPIFFS_DBG(\"truncate: delete data page %04x for data spix:%04x, cur_size:%i\\n\", data_pix, data_spix, cur_size);\n } else {\n // delete last page, partially\n spiffs_page_header p_hdr;\n spiffs_page_ix new_data_pix;\n u32_t bytes_to_remove = SPIFFS_DATA_PAGE_SIZE(fs) - (new_size % SPIFFS_DATA_PAGE_SIZE(fs));\n SPIFFS_DBG(\"truncate: delete %i bytes from data page %04x for data spix:%04x, cur_size:%i\\n\", bytes_to_remove, data_pix, data_spix, cur_size);\n\n res = spiffs_page_data_check(fs, fd, data_pix, data_spix);\n if (res != SPIFFS_OK) break;\n\n p_hdr.obj_id = fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;\n p_hdr.span_ix = data_spix;\n p_hdr.flags = 0xff;\n // allocate new page and copy unmodified data\n res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,\n &p_hdr, 0, 0, 0, 0, &new_data_pix);\n if (res != SPIFFS_OK) break;\n res = spiffs_phys_cpy(fs, 0,\n SPIFFS_PAGE_TO_PADDR(fs, new_data_pix) + sizeof(spiffs_page_header),\n SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header),\n SPIFFS_DATA_PAGE_SIZE(fs) - bytes_to_remove);\n if (res != SPIFFS_OK) break;\n // delete original data page\n res = spiffs_page_delete(fs, data_pix);\n if (res != SPIFFS_OK) break;\n p_hdr.flags &= ~SPIFFS_PH_FLAG_FINAL;\n res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,\n fd->file_nbr,\n SPIFFS_PAGE_TO_PADDR(fs, new_data_pix) + offsetof(spiffs_page_header, flags),\n sizeof(u8_t),\n (u8_t *)&p_hdr.flags);\n if (res != SPIFFS_OK) break;\n\n // update memory representation of object index page with new data page\n if (cur_objix_spix == 0) {\n // update object index header page\n ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = new_data_pix;\n SPIFFS_DBG(\"truncate: wrote page %04x to objix_hdr entry %02x in mem\\n\", new_data_pix, SPIFFS_OBJ_IX_ENTRY(fs, data_spix));\n } else {\n // update object index page\n ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = new_data_pix;\n SPIFFS_DBG(\"truncate: wrote page %04x to objix entry %02x in mem\\n\", new_data_pix, SPIFFS_OBJ_IX_ENTRY(fs, data_spix));\n }\n cur_size = new_size;\n fd->size = new_size;\n fd->offset = cur_size;\n break;\n }\n data_spix--;\n } // while all data\n\n // update object indices\n if (cur_objix_spix == 0) {\n // update object index header page\n if (cur_size == 0) {\n if (remove) {\n // remove object altogether\n SPIFFS_DBG(\"truncate: remove object index header page %04x\\n\", objix_pix);\n\n res = spiffs_page_index_check(fs, fd, objix_pix, 0);\n SPIFFS_CHECK_RES(res);\n\n res = spiffs_page_delete(fs, objix_pix);\n SPIFFS_CHECK_RES(res);\n spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_DEL, fd->obj_id, 0, objix_pix, 0);\n } else {\n // make uninitialized object\n SPIFFS_DBG(\"truncate: reset objix_hdr page %04x\\n\", objix_pix);\n memset(fs->work + sizeof(spiffs_page_object_ix_header), 0xff,\n SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_object_ix_header));\n res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,\n objix_pix, fs->work, 0, SPIFFS_UNDEFINED_LEN, &new_objix_hdr_pix);\n SPIFFS_CHECK_RES(res);\n }\n } else {\n // update object index header page\n SPIFFS_DBG(\"truncate: update object index header page with indices and size\\n\");\n res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,\n objix_pix, fs->work, 0, cur_size, &new_objix_hdr_pix);\n SPIFFS_CHECK_RES(res);\n }\n } else {\n // update both current object index page and object index header page\n spiffs_page_ix new_objix_pix;\n\n res = spiffs_page_index_check(fs, fd, objix_pix, cur_objix_spix);\n SPIFFS_CHECK_RES(res);\n\n // move and update object index page\n res = spiffs_page_move(fs, fd->file_nbr, (u8_t*)objix_hdr, fd->obj_id, 0, objix_pix, &new_objix_pix);\n SPIFFS_CHECK_RES(res);\n spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, new_objix_pix, 0);\n SPIFFS_DBG(\"truncate: store modified objix page, %04x:%04x\\n\", new_objix_pix, cur_objix_spix);\n fd->cursor_objix_pix = new_objix_pix;\n fd->cursor_objix_spix = cur_objix_spix;\n fd->offset = cur_size;\n // update object index header page with new size\n res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,\n fd->objix_hdr_pix, 0, 0, cur_size, &new_objix_hdr_pix);\n SPIFFS_CHECK_RES(res);\n }\n fd->size = cur_size;\n\n return res;\n} // spiffs_object_truncate\n#endif // !SPIFFS_READ_ONLY\n\ns32_t spiffs_object_read(\n spiffs_fd *fd,\n u32_t offset,\n u32_t len,\n u8_t *dst) {\n s32_t res = SPIFFS_OK;\n spiffs *fs = fd->fs;\n spiffs_page_ix objix_pix;\n spiffs_page_ix data_pix;\n spiffs_span_ix data_spix = offset / SPIFFS_DATA_PAGE_SIZE(fs);\n u32_t cur_offset = offset;\n spiffs_span_ix cur_objix_spix;\n spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;\n spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;\n spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;\n\n while (cur_offset < offset + len) {\n cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);\n if (prev_objix_spix != cur_objix_spix) {\n // load current object index (header) page\n if (cur_objix_spix == 0) {\n objix_pix = fd->objix_hdr_pix;\n } else {\n SPIFFS_DBG(\"read: find objix %04x:%04x\\n\", fd->obj_id, cur_objix_spix);\n res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &objix_pix);\n SPIFFS_CHECK_RES(res);\n }\n SPIFFS_DBG(\"read: load objix page %04x:%04x for data spix:%04x\\n\", objix_pix, cur_objix_spix, data_spix);\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,\n fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);\n SPIFFS_CHECK_RES(res);\n SPIFFS_VALIDATE_OBJIX(objix->p_hdr, fd->obj_id, cur_objix_spix);\n\n fd->offset = cur_offset;\n fd->cursor_objix_pix = objix_pix;\n fd->cursor_objix_spix = cur_objix_spix;\n\n prev_objix_spix = cur_objix_spix;\n }\n\n if (cur_objix_spix == 0) {\n // get data page from object index header page\n data_pix = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];\n } else {\n // get data page from object index page\n data_pix = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];\n }\n\n // all remaining data\n u32_t len_to_read = offset + len - cur_offset;\n // remaining data in page\n len_to_read = MIN(len_to_read, SPIFFS_DATA_PAGE_SIZE(fs) - (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs)));\n // remaining data in file\n len_to_read = MIN(len_to_read, fd->size);\n SPIFFS_DBG(\"read: offset:%i rd:%i data spix:%04x is data_pix:%04x addr:%08x\\n\", cur_offset, len_to_read, data_spix, data_pix,\n SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs)));\n if (len_to_read <= 0) {\n res = SPIFFS_ERR_END_OF_OBJECT;\n break;\n }\n res = spiffs_page_data_check(fs, fd, data_pix, data_spix);\n SPIFFS_CHECK_RES(res);\n res = _spiffs_rd(\n fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_READ,\n fd->file_nbr,\n SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs)),\n len_to_read,\n dst);\n SPIFFS_CHECK_RES(res);\n dst += len_to_read;\n cur_offset += len_to_read;\n fd->offset = cur_offset;\n data_spix++;\n }\n\n return res;\n}\n\n#if !SPIFFS_READ_ONLY\ntypedef struct {\n spiffs_obj_id min_obj_id;\n spiffs_obj_id max_obj_id;\n u32_t compaction;\n const u8_t *conflicting_name;\n} spiffs_free_obj_id_state;\n\nstatic s32_t spiffs_obj_lu_find_free_obj_id_bitmap_v(spiffs *fs, spiffs_obj_id id, spiffs_block_ix bix, int ix_entry,\n const void *user_const_p, void *user_var_p) {\n if (id != SPIFFS_OBJ_ID_FREE && id != SPIFFS_OBJ_ID_DELETED) {\n spiffs_obj_id min_obj_id = *((spiffs_obj_id*)user_var_p);\n const u8_t *conflicting_name = (const u8_t*)user_const_p;\n\n // if conflicting name parameter is given, also check if this name is found in object index hdrs\n if (conflicting_name && (id & SPIFFS_OBJ_ID_IX_FLAG)) {\n spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);\n int res;\n spiffs_page_object_ix_header objix_hdr;\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);\n SPIFFS_CHECK_RES(res);\n if (objix_hdr.p_hdr.span_ix == 0 &&\n (objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) ==\n (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) {\n if (strcmp((const char*)user_const_p, (char*)objix_hdr.name) == 0) {\n return SPIFFS_ERR_CONFLICTING_NAME;\n }\n }\n }\n\n id &= ~SPIFFS_OBJ_ID_IX_FLAG;\n u32_t bit_ix = (id-min_obj_id) & 7;\n int byte_ix = (id-min_obj_id) >> 3;\n if (byte_ix >= 0 && (u32_t)byte_ix < SPIFFS_CFG_LOG_PAGE_SZ(fs)) {\n fs->work[byte_ix] |= (1<<bit_ix);\n }\n }\n return SPIFFS_VIS_COUNTINUE;\n}\n\nstatic s32_t spiffs_obj_lu_find_free_obj_id_compact_v(spiffs *fs, spiffs_obj_id id, spiffs_block_ix bix, int ix_entry,\n const void *user_const_p, void *user_var_p) {\n (void)user_var_p;\n if (id != SPIFFS_OBJ_ID_FREE && id != SPIFFS_OBJ_ID_DELETED && (id & SPIFFS_OBJ_ID_IX_FLAG)) {\n s32_t res;\n const spiffs_free_obj_id_state *state = (const spiffs_free_obj_id_state*)user_const_p;\n spiffs_page_object_ix_header objix_hdr;\n\n res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,\n 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, ix_entry), sizeof(spiffs_page_object_ix_header), (u8_t*)&objix_hdr);\n if (res == SPIFFS_OK && objix_hdr.p_hdr.span_ix == 0 &&\n ((objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_DELET)) ==\n (SPIFFS_PH_FLAG_DELET))) {\n // ok object look up entry\n if (state->conflicting_name && strcmp((const char *)state->conflicting_name, (char *)objix_hdr.name) == 0) {\n return SPIFFS_ERR_CONFLICTING_NAME;\n }\n\n id &= ~SPIFFS_OBJ_ID_IX_FLAG;\n if (id >= state->min_obj_id && id <= state->max_obj_id) {\n u8_t *map = (u8_t *)fs->work;\n int ix = (id - state->min_obj_id) / state->compaction;\n //SPIFFS_DBG(\"free_obj_id: add ix %i for id %04x min:%04x max%04x comp:%i\\n\", ix, id, state->min_obj_id, state->max_obj_id, state->compaction);\n map[ix]++;\n }\n }\n }\n return SPIFFS_VIS_COUNTINUE;\n}\n\n// Scans thru all object lookup for object index header pages. If total possible number of\n// object ids cannot fit into a work buffer, these are grouped. When a group containing free\n// object ids is found, the object lu is again scanned for object ids within group and bitmasked.\n// Finally, the bitmask is searched for a free id\ns32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, const u8_t *conflicting_name) {\n s32_t res = SPIFFS_OK;\n u32_t max_objects = (fs->block_count * SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs)) / 2;\n spiffs_free_obj_id_state state;\n spiffs_obj_id free_obj_id = SPIFFS_OBJ_ID_FREE;\n state.min_obj_id = 1;\n state.max_obj_id = max_objects + 1;\n if (state.max_obj_id & SPIFFS_OBJ_ID_IX_FLAG) {\n state.max_obj_id = ((spiffs_obj_id)-1) & ~SPIFFS_OBJ_ID_IX_FLAG;\n }\n state.compaction = 0;\n state.conflicting_name = conflicting_name;\n while (res == SPIFFS_OK && free_obj_id == SPIFFS_OBJ_ID_FREE) {\n if (state.max_obj_id - state.min_obj_id <= (spiffs_obj_id)SPIFFS_CFG_LOG_PAGE_SZ(fs)*8) {\n // possible to represent in bitmap\n u32_t i, j;\n SPIFFS_DBG(\"free_obj_id: BITM min:%04x max:%04x\\n\", state.min_obj_id, state.max_obj_id);\n\n memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));\n res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_bitmap_v,\n conflicting_name, &state.min_obj_id, 0, 0);\n if (res == SPIFFS_VIS_END) res = SPIFFS_OK;\n SPIFFS_CHECK_RES(res);\n // traverse bitmask until found free obj_id\n for (i = 0; i < SPIFFS_CFG_LOG_PAGE_SZ(fs); i++) {\n u8_t mask = fs->work[i];\n if (mask == 0xff) {\n continue;\n }\n for (j = 0; j < 8; j++) {\n if ((mask & (1<<j)) == 0) {\n *obj_id = (i<<3)+j+state.min_obj_id;\n return SPIFFS_OK;\n }\n }\n }\n return SPIFFS_ERR_FULL;\n } else {\n // not possible to represent all ids in range in a bitmap, compact and count\n if (state.compaction != 0) {\n // select element in compacted table, decrease range and recompact\n u32_t i, min_i = 0;\n u8_t *map = (u8_t *)fs->work;\n u8_t min_count = 0xff;\n\n for (i = 0; i < SPIFFS_CFG_LOG_PAGE_SZ(fs)/sizeof(u8_t); i++) {\n if (map[i] < min_count) {\n min_count = map[i];\n min_i = i;\n if (min_count == 0) {\n break;\n }\n }\n }\n\n if (min_count == state.compaction) {\n // there are no free objids!\n SPIFFS_DBG(\"free_obj_id: compacted table is full\\n\");\n return SPIFFS_ERR_FULL;\n }\n\n SPIFFS_DBG(\"free_obj_id: COMP select index:%i min_count:%i min:%04x max:%04x compact:%i\\n\", min_i, min_count, state.min_obj_id, state.max_obj_id, state.compaction);\n\n if (min_count == 0) {\n // no id in this range, skip compacting and use directly\n *obj_id = min_i * state.compaction + state.min_obj_id;\n return SPIFFS_OK;\n } else {\n SPIFFS_DBG(\"free_obj_id: COMP SEL chunk:%04x min:%04x -> %04x\\n\", state.compaction, state.min_obj_id, state.min_obj_id + min_i * state.compaction);\n state.min_obj_id += min_i * state.compaction;\n state.max_obj_id = state.min_obj_id + state.compaction;\n // decrease compaction\n }\n if ((state.max_obj_id - state.min_obj_id <= (spiffs_obj_id)SPIFFS_CFG_LOG_PAGE_SZ(fs)*8)) {\n // no need for compacting, use bitmap\n continue;\n }\n }\n // in a work memory of log_page_size bytes, we may fit in log_page_size ids\n // todo what if compaction is > 255 - then we cannot fit it in a byte\n state.compaction = (state.max_obj_id-state.min_obj_id) / ((SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(u8_t)));\n SPIFFS_DBG(\"free_obj_id: COMP min:%04x max:%04x compact:%i\\n\", state.min_obj_id, state.max_obj_id, state.compaction);\n\n memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));\n res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_compact_v, &state, 0, 0, 0);\n if (res == SPIFFS_VIS_END) res = SPIFFS_OK;\n SPIFFS_CHECK_RES(res);\n state.conflicting_name = 0; // searched for conflicting name once, no need to do it again\n }\n }\n\n return res;\n}\n#endif // !SPIFFS_READ_ONLY\n\ns32_t spiffs_fd_find_new(spiffs *fs, spiffs_fd **fd) {\n u32_t i;\n spiffs_fd *fds = (spiffs_fd *)fs->fd_space;\n for (i = 0; i < fs->fd_count; i++) {\n spiffs_fd *cur_fd = &fds[i];\n if (cur_fd->file_nbr == 0) {\n cur_fd->file_nbr = i+1;\n *fd = cur_fd;\n return SPIFFS_OK;\n }\n }\n return SPIFFS_ERR_OUT_OF_FILE_DESCS;\n}\n\ns32_t spiffs_fd_return(spiffs *fs, spiffs_file f) {\n if (f <= 0 || f > (s16_t)fs->fd_count) {\n return SPIFFS_ERR_BAD_DESCRIPTOR;\n }\n spiffs_fd *fds = (spiffs_fd *)fs->fd_space;\n spiffs_fd *fd = &fds[f-1];\n if (fd->file_nbr == 0) {\n return SPIFFS_ERR_FILE_CLOSED;\n }\n fd->file_nbr = 0;\n return SPIFFS_OK;\n}\n\ns32_t spiffs_fd_get(spiffs *fs, spiffs_file f, spiffs_fd **fd) {\n if (f <= 0 || f > (s16_t)fs->fd_count) {\n return SPIFFS_ERR_BAD_DESCRIPTOR;\n }\n spiffs_fd *fds = (spiffs_fd *)fs->fd_space;\n *fd = &fds[f-1];\n if ((*fd)->file_nbr == 0) {\n return SPIFFS_ERR_FILE_CLOSED;\n }\n return SPIFFS_OK;\n}\n", "src/spiffs_nucleus.h": "/*\n * spiffs_nucleus.h\n *\n * Created on: Jun 15, 2013\n * Author: petera\n */\n\n/* SPIFFS layout\n *\n * spiffs is designed for following spi flash characteristics:\n * - only big areas of data (blocks) can be erased\n * - erasing resets all bits in a block to ones\n * - writing pulls ones to zeroes\n * - zeroes cannot be pulled to ones, without erase\n * - wear leveling\n *\n * spiffs is also meant to be run on embedded, memory constraint devices.\n *\n * Entire area is divided in blocks. Entire area is also divided in pages.\n * Each block contains same number of pages. A page cannot be erased, but a\n * block can be erased.\n *\n * Entire area must be block_size * x\n * page_size must be block_size / (2^y) where y > 2\n *\n * ex: area = 1024*1024 bytes, block size = 65536 bytes, page size = 256 bytes\n *\n * BLOCK 0 PAGE 0 object lookup 1\n * PAGE 1 object lookup 2\n * ...\n * PAGE n-1 object lookup n\n * PAGE n object data 1\n * PAGE n+1 object data 2\n * ...\n * PAGE n+m-1 object data m\n *\n * BLOCK 1 PAGE n+m object lookup 1\n * PAGE n+m+1 object lookup 2\n * ...\n * PAGE 2n+m-1 object lookup n\n * PAGE 2n+m object data 1\n * PAGE 2n+m object data 2\n * ...\n * PAGE 2n+2m-1 object data m\n * ...\n *\n * n is number of object lookup pages, which is number of pages needed to index all pages\n * in a block by object id\n * : block_size / page_size * sizeof(obj_id) / page_size\n * m is number data pages, which is number of pages in block minus number of lookup pages\n * : block_size / page_size - block_size / page_size * sizeof(obj_id) / page_size\n * thus, n+m is total number of pages in a block\n * : block_size / page_size\n *\n * ex: n = 65536/256*2/256 = 2, m = 65536/256 - 2 = 254 => n+m = 65536/256 = 256\n *\n * Object lookup pages contain object id entries. Each entry represent the corresponding\n * data page.\n * Assuming a 16 bit object id, an object id being 0xffff represents a free page.\n * An object id being 0x0000 represents a deleted page.\n *\n * ex: page 0 : lookup : 0008 0001 0aaa ffff ffff ffff ffff ffff ..\n * page 1 : lookup : ffff ffff ffff ffff ffff ffff ffff ffff ..\n * page 2 : data : data for object id 0008\n * page 3 : data : data for object id 0001\n * page 4 : data : data for object id 0aaa\n * ...\n *\n *\n * Object data pages can be either object index pages or object content.\n * All object data pages contains a data page header, containing object id and span index.\n * The span index denotes the object page ordering amongst data pages with same object id.\n * This applies to both object index pages (when index spans more than one page of entries),\n * and object data pages.\n * An object index page contains page entries pointing to object content page. The entry index\n * in a object index page correlates to the span index in the actual object data page.\n * The first object index page (span index 0) is called object index header page, and also\n * contains object flags (directory/file), size, object name etc.\n *\n * ex:\n * BLOCK 1\n * PAGE 256: objectl lookup page 1\n * [*123] [ 123] [ 123] [ 123]\n * [ 123] [*123] [ 123] [ 123]\n * [free] [free] [free] [free] ...\n * PAGE 257: objectl lookup page 2\n * [free] [free] [free] [free] ...\n * PAGE 258: object index page (header)\n * obj.id:0123 span.ix:0000 flags:INDEX\n * size:1600 name:ex.txt type:file\n * [259] [260] [261] [262]\n * PAGE 259: object data page\n * obj.id:0123 span.ix:0000 flags:DATA\n * PAGE 260: object data page\n * obj.id:0123 span.ix:0001 flags:DATA\n * PAGE 261: object data page\n * obj.id:0123 span.ix:0002 flags:DATA\n * PAGE 262: object data page\n * obj.id:0123 span.ix:0003 flags:DATA\n * PAGE 263: object index page\n * obj.id:0123 span.ix:0001 flags:INDEX\n * [264] [265] [fre] [fre]\n * [fre] [fre] [fre] [fre]\n * PAGE 264: object data page\n * obj.id:0123 span.ix:0004 flags:DATA\n * PAGE 265: object data page\n * obj.id:0123 span.ix:0005 flags:DATA\n *\n */\n#ifndef SPIFFS_NUCLEUS_H_\n#define SPIFFS_NUCLEUS_H_\n\n#define _SPIFFS_ERR_CHECK_FIRST (SPIFFS_ERR_INTERNAL - 1)\n#define SPIFFS_ERR_CHECK_OBJ_ID_MISM (SPIFFS_ERR_INTERNAL - 1)\n#define SPIFFS_ERR_CHECK_SPIX_MISM (SPIFFS_ERR_INTERNAL - 2)\n#define SPIFFS_ERR_CHECK_FLAGS_BAD (SPIFFS_ERR_INTERNAL - 3)\n#define _SPIFFS_ERR_CHECK_LAST (SPIFFS_ERR_INTERNAL - 4)\n\n#define SPIFFS_VIS_COUNTINUE (SPIFFS_ERR_INTERNAL - 20)\n#define SPIFFS_VIS_COUNTINUE_RELOAD (SPIFFS_ERR_INTERNAL - 21)\n#define SPIFFS_VIS_END (SPIFFS_ERR_INTERNAL - 22)\n\n#define SPIFFS_EV_IX_UPD 0\n#define SPIFFS_EV_IX_NEW 1\n#define SPIFFS_EV_IX_DEL 2\n\n#define SPIFFS_OBJ_ID_IX_FLAG ((spiffs_obj_id)(1<<(8*sizeof(spiffs_obj_id)-1)))\n\n#define SPIFFS_UNDEFINED_LEN (u32_t)(-1)\n\n#define SPIFFS_OBJ_ID_DELETED ((spiffs_obj_id)0)\n#define SPIFFS_OBJ_ID_FREE ((spiffs_obj_id)-1)\n\n#if SPIFFS_USE_MAGIC\n#if !SPIFFS_USE_MAGIC_LENGTH\n#define SPIFFS_MAGIC(fs, bix) \\\n ((spiffs_obj_id)(0x20140529 ^ SPIFFS_CFG_LOG_PAGE_SZ(fs)))\n#else // SPIFFS_USE_MAGIC_LENGTH\n#define SPIFFS_MAGIC(fs, bix) \\\n ((spiffs_obj_id)(0x20140529 ^ SPIFFS_CFG_LOG_PAGE_SZ(fs) ^ ((fs)->block_count - (bix))))\n#endif // SPIFFS_USE_MAGIC_LENGTH\n#endif // SPIFFS_USE_MAGIC\n\n#define SPIFFS_CONFIG_MAGIC (0x20090315)\n\n#if SPIFFS_SINGLETON == 0\n#define SPIFFS_CFG_LOG_PAGE_SZ(fs) \\\n ((fs)->cfg.log_page_size)\n#define SPIFFS_CFG_LOG_BLOCK_SZ(fs) \\\n ((fs)->cfg.log_block_size)\n#define SPIFFS_CFG_PHYS_SZ(fs) \\\n ((fs)->cfg.phys_size)\n#define SPIFFS_CFG_PHYS_ERASE_SZ(fs) \\\n ((fs)->cfg.phys_erase_block)\n#define SPIFFS_CFG_PHYS_ADDR(fs) \\\n ((fs)->cfg.phys_addr)\n#endif\n\n// total number of pages\n#define SPIFFS_MAX_PAGES(fs) \\\n ( SPIFFS_CFG_PHYS_SZ(fs)/SPIFFS_CFG_LOG_PAGE_SZ(fs) )\n// total number of pages per block, including object lookup pages\n#define SPIFFS_PAGES_PER_BLOCK(fs) \\\n ( SPIFFS_CFG_LOG_BLOCK_SZ(fs)/SPIFFS_CFG_LOG_PAGE_SZ(fs) )\n// number of object lookup pages per block\n#define SPIFFS_OBJ_LOOKUP_PAGES(fs) \\\n (MAX(1, (SPIFFS_PAGES_PER_BLOCK(fs) * sizeof(spiffs_obj_id)) / SPIFFS_CFG_LOG_PAGE_SZ(fs)) )\n// checks if page index belongs to object lookup\n#define SPIFFS_IS_LOOKUP_PAGE(fs,pix) \\\n (((pix) % SPIFFS_PAGES_PER_BLOCK(fs)) < SPIFFS_OBJ_LOOKUP_PAGES(fs))\n// number of object lookup entries in all object lookup pages\n#define SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs) \\\n (SPIFFS_PAGES_PER_BLOCK(fs)-SPIFFS_OBJ_LOOKUP_PAGES(fs))\n// converts a block to physical address\n#define SPIFFS_BLOCK_TO_PADDR(fs, block) \\\n ( SPIFFS_CFG_PHYS_ADDR(fs) + (block)* SPIFFS_CFG_LOG_BLOCK_SZ(fs) )\n// converts a object lookup entry to page index\n#define SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, block, entry) \\\n ((block)*SPIFFS_PAGES_PER_BLOCK(fs) + (SPIFFS_OBJ_LOOKUP_PAGES(fs) + entry))\n// converts a object lookup entry to physical address of corresponding page\n#define SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, block, entry) \\\n (SPIFFS_BLOCK_TO_PADDR(fs, block) + (SPIFFS_OBJ_LOOKUP_PAGES(fs) + entry) * SPIFFS_CFG_LOG_PAGE_SZ(fs) )\n// converts a page to physical address\n#define SPIFFS_PAGE_TO_PADDR(fs, page) \\\n ( SPIFFS_CFG_PHYS_ADDR(fs) + (page) * SPIFFS_CFG_LOG_PAGE_SZ(fs) )\n// converts a physical address to page\n#define SPIFFS_PADDR_TO_PAGE(fs, addr) \\\n ( ((addr) - SPIFFS_CFG_PHYS_ADDR(fs)) / SPIFFS_CFG_LOG_PAGE_SZ(fs) )\n// gives index in page for a physical address\n#define SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr) \\\n ( ((addr) - SPIFFS_CFG_PHYS_ADDR(fs)) % SPIFFS_CFG_LOG_PAGE_SZ(fs) )\n// returns containing block for given page\n#define SPIFFS_BLOCK_FOR_PAGE(fs, page) \\\n ( (page) / SPIFFS_PAGES_PER_BLOCK(fs) )\n// returns starting page for block\n#define SPIFFS_PAGE_FOR_BLOCK(fs, block) \\\n ( (block) * SPIFFS_PAGES_PER_BLOCK(fs) )\n// converts page to entry in object lookup page\n#define SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, page) \\\n ( (page) % SPIFFS_PAGES_PER_BLOCK(fs) - SPIFFS_OBJ_LOOKUP_PAGES(fs) )\n// returns data size in a data page\n#define SPIFFS_DATA_PAGE_SIZE(fs) \\\n ( SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_header) )\n// returns physical address for block's erase count,\n// always in the physical last entry of the last object lookup page\n#define SPIFFS_ERASE_COUNT_PADDR(fs, bix) \\\n ( SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_OBJ_LOOKUP_PAGES(fs) * SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_obj_id) )\n// returns physical address for block's magic,\n// always in the physical second last entry of the last object lookup page\n#define SPIFFS_MAGIC_PADDR(fs, bix) \\\n ( SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_OBJ_LOOKUP_PAGES(fs) * SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_obj_id)*2 )\n// checks if there is any room for magic in the object luts\n#define SPIFFS_CHECK_MAGIC_POSSIBLE(fs) \\\n ( (SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs) % (SPIFFS_CFG_LOG_PAGE_SZ(fs)/sizeof(spiffs_obj_id))) * sizeof(spiffs_obj_id) \\\n <= (SPIFFS_CFG_LOG_PAGE_SZ(fs)-sizeof(spiffs_obj_id)*2) )\n\n// define helpers object\n\n// entries in an object header page index\n#define SPIFFS_OBJ_HDR_IX_LEN(fs) \\\n ((SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_object_ix_header))/sizeof(spiffs_page_ix))\n// entries in an object page index\n#define SPIFFS_OBJ_IX_LEN(fs) \\\n ((SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_object_ix))/sizeof(spiffs_page_ix))\n// object index entry for given data span index\n#define SPIFFS_OBJ_IX_ENTRY(fs, spix) \\\n ((spix) < SPIFFS_OBJ_HDR_IX_LEN(fs) ? (spix) : (((spix)-SPIFFS_OBJ_HDR_IX_LEN(fs))%SPIFFS_OBJ_IX_LEN(fs)))\n// object index span index number for given data span index or entry\n#define SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, spix) \\\n ((spix) < SPIFFS_OBJ_HDR_IX_LEN(fs) ? 0 : (1+((spix)-SPIFFS_OBJ_HDR_IX_LEN(fs))/SPIFFS_OBJ_IX_LEN(fs)))\n\n\n#define SPIFFS_OP_T_OBJ_LU (0<<0)\n#define SPIFFS_OP_T_OBJ_LU2 (1<<0)\n#define SPIFFS_OP_T_OBJ_IX (2<<0)\n#define SPIFFS_OP_T_OBJ_DA (3<<0)\n#define SPIFFS_OP_C_DELE (0<<2)\n#define SPIFFS_OP_C_UPDT (1<<2)\n#define SPIFFS_OP_C_MOVS (2<<2)\n#define SPIFFS_OP_C_MOVD (3<<2)\n#define SPIFFS_OP_C_FLSH (4<<2)\n#define SPIFFS_OP_C_READ (5<<2)\n#define SPIFFS_OP_C_WRTHRU (6<<2)\n\n#define SPIFFS_OP_TYPE_MASK (3<<0)\n#define SPIFFS_OP_COM_MASK (7<<2)\n\n\n// if 0, this page is written to, else clean\n#define SPIFFS_PH_FLAG_USED (1<<0)\n// if 0, writing is finalized, else under modification\n#define SPIFFS_PH_FLAG_FINAL (1<<1)\n// if 0, this is an index page, else a data page\n#define SPIFFS_PH_FLAG_INDEX (1<<2)\n// if 0, page is deleted, else valid\n#define SPIFFS_PH_FLAG_DELET (1<<7)\n// if 0, this index header is being deleted\n#define SPIFFS_PH_FLAG_IXDELE (1<<6)\n\n\n#define SPIFFS_CHECK_MOUNT(fs) \\\n ((fs)->mounted != 0)\n\n#define SPIFFS_CHECK_CFG(fs) \\\n ((fs)->config_magic == SPIFFS_CONFIG_MAGIC)\n\n#define SPIFFS_CHECK_RES(res) \\\n do { \\\n if ((res) < SPIFFS_OK) return (res); \\\n } while (0);\n\n#define SPIFFS_API_CHECK_MOUNT(fs) \\\n if (!SPIFFS_CHECK_MOUNT((fs))) { \\\n (fs)->err_code = SPIFFS_ERR_NOT_MOUNTED; \\\n return -1; \\\n }\n\n#define SPIFFS_API_CHECK_CFG(fs) \\\n if (!SPIFFS_CHECK_CFG((fs))) { \\\n (fs)->err_code = SPIFFS_ERR_NOT_CONFIGURED; \\\n return -1; \\\n }\n\n#define SPIFFS_API_CHECK_RES(fs, res) \\\n if ((res) < SPIFFS_OK) { \\\n (fs)->err_code = (res); \\\n return -1; \\\n }\n\n#define SPIFFS_API_CHECK_RES_UNLOCK(fs, res) \\\n if ((res) < SPIFFS_OK) { \\\n (fs)->err_code = (res); \\\n SPIFFS_UNLOCK(fs); \\\n return -1; \\\n }\n\n#define SPIFFS_VALIDATE_OBJIX(ph, objid, spix) \\\n if (((ph).flags & SPIFFS_PH_FLAG_USED) != 0) return SPIFFS_ERR_IS_FREE; \\\n if (((ph).flags & SPIFFS_PH_FLAG_DELET) == 0) return SPIFFS_ERR_DELETED; \\\n if (((ph).flags & SPIFFS_PH_FLAG_FINAL) != 0) return SPIFFS_ERR_NOT_FINALIZED; \\\n if (((ph).flags & SPIFFS_PH_FLAG_INDEX) != 0) return SPIFFS_ERR_NOT_INDEX; \\\n if (((objid) & SPIFFS_OBJ_ID_IX_FLAG) == 0) return SPIFFS_ERR_NOT_INDEX; \\\n if ((ph).span_ix != (spix)) return SPIFFS_ERR_INDEX_SPAN_MISMATCH;\n //if ((spix) == 0 && ((ph).flags & SPIFFS_PH_FLAG_IXDELE) == 0) return SPIFFS_ERR_DELETED;\n\n#define SPIFFS_VALIDATE_DATA(ph, objid, spix) \\\n if (((ph).flags & SPIFFS_PH_FLAG_USED) != 0) return SPIFFS_ERR_IS_FREE; \\\n if (((ph).flags & SPIFFS_PH_FLAG_DELET) == 0) return SPIFFS_ERR_DELETED; \\\n if (((ph).flags & SPIFFS_PH_FLAG_FINAL) != 0) return SPIFFS_ERR_NOT_FINALIZED; \\\n if (((ph).flags & SPIFFS_PH_FLAG_INDEX) == 0) return SPIFFS_ERR_IS_INDEX; \\\n if ((objid) & SPIFFS_OBJ_ID_IX_FLAG) return SPIFFS_ERR_IS_INDEX; \\\n if ((ph).span_ix != (spix)) return SPIFFS_ERR_DATA_SPAN_MISMATCH;\n\n\n// check id\n#define SPIFFS_VIS_CHECK_ID (1<<0)\n// report argument object id to visitor - else object lookup id is reported\n#define SPIFFS_VIS_CHECK_PH (1<<1)\n// stop searching at end of all look up pages\n#define SPIFFS_VIS_NO_WRAP (1<<2)\n\n#if SPIFFS_HAL_CALLBACK_EXTRA\n\n#define SPIFFS_HAL_WRITE(_fs, _paddr, _len, _src) \\\n (_fs)->cfg.hal_write_f((_fs), (_paddr), (_len), (_src))\n#define SPIFFS_HAL_READ(_fs, _paddr, _len, _dst) \\\n (_fs)->cfg.hal_read_f((_fs), (_paddr), (_len), (_dst))\n#define SPIFFS_HAL_ERASE(_fs, _paddr, _len) \\\n (_fs)->cfg.hal_erase_f((_fs), (_paddr), (_len))\n\n#else // SPIFFS_HAL_CALLBACK_EXTRA\n\n#define SPIFFS_HAL_WRITE(_fs, _paddr, _len, _src) \\\n (_fs)->cfg.hal_write_f((_paddr), (_len), (_src))\n#define SPIFFS_HAL_READ(_fs, _paddr, _len, _dst) \\\n (_fs)->cfg.hal_read_f((_paddr), (_len), (_dst))\n#define SPIFFS_HAL_ERASE(_fs, _paddr, _len) \\\n (_fs)->cfg.hal_erase_f((_paddr), (_len))\n\n#endif // SPIFFS_HAL_CALLBACK_EXTRA\n\n#if SPIFFS_CACHE\n\n#define SPIFFS_CACHE_FLAG_DIRTY (1<<0)\n#define SPIFFS_CACHE_FLAG_WRTHRU (1<<1)\n#define SPIFFS_CACHE_FLAG_OBJLU (1<<2)\n#define SPIFFS_CACHE_FLAG_OBJIX (1<<3)\n#define SPIFFS_CACHE_FLAG_DATA (1<<4)\n#define SPIFFS_CACHE_FLAG_TYPE_WR (1<<7)\n\n#define SPIFFS_CACHE_PAGE_SIZE(fs) \\\n (sizeof(spiffs_cache_page) + SPIFFS_CFG_LOG_PAGE_SZ(fs))\n\n#define spiffs_get_cache(fs) \\\n ((spiffs_cache *)((fs)->cache))\n\n#define spiffs_get_cache_page_hdr(fs, c, ix) \\\n ((spiffs_cache_page *)(&((c)->cpages[(ix) * SPIFFS_CACHE_PAGE_SIZE(fs)])))\n\n#define spiffs_get_cache_page(fs, c, ix) \\\n ((u8_t *)(&((c)->cpages[(ix) * SPIFFS_CACHE_PAGE_SIZE(fs)])) + sizeof(spiffs_cache_page))\n\n// cache page struct\ntypedef struct {\n // cache flags\n u8_t flags;\n // cache page index\n u8_t ix;\n // last access of this cache page\n u32_t last_access;\n union {\n // type read cache\n struct {\n // read cache page index\n spiffs_page_ix pix;\n };\n#if SPIFFS_CACHE_WR\n // type write cache\n struct {\n // write cache\n spiffs_obj_id obj_id;\n // offset in cache page\n u32_t offset;\n // size of cache page\n u16_t size;\n };\n#endif\n };\n} spiffs_cache_page;\n\n// cache struct\ntypedef struct {\n u8_t cpage_count;\n u32_t last_access;\n u32_t cpage_use_map;\n u32_t cpage_use_mask;\n u8_t *cpages;\n} spiffs_cache;\n\n#endif\n\n\n// spiffs nucleus file descriptor\ntypedef struct {\n // the filesystem of this descriptor\n spiffs *fs;\n // number of file descriptor - if 0, the file descriptor is closed\n spiffs_file file_nbr;\n // object id - if SPIFFS_OBJ_ID_ERASED, the file was deleted\n spiffs_obj_id obj_id;\n // size of the file\n u32_t size;\n // cached object index header page index\n spiffs_page_ix objix_hdr_pix;\n // cached offset object index page index\n spiffs_page_ix cursor_objix_pix;\n // cached offset object index span index\n spiffs_span_ix cursor_objix_spix;\n // current absolute offset\n u32_t offset;\n // current file descriptor offset\n u32_t fdoffset;\n // fd flags\n spiffs_flags flags;\n#if SPIFFS_CACHE_WR\n spiffs_cache_page *cache_page;\n#endif\n} spiffs_fd;\n\n\n// object structs\n\n// page header, part of each page except object lookup pages\n// NB: this is always aligned when the data page is an object index,\n// as in this case struct spiffs_page_object_ix is used\ntypedef struct __attribute(( packed )) {\n // object id\n spiffs_obj_id obj_id;\n // object span index\n spiffs_span_ix span_ix;\n // flags\n u8_t flags;\n} spiffs_page_header;\n\n// object index header page header\ntypedef struct __attribute(( packed ))\n#if SPIFFS_ALIGNED_OBJECT_INDEX_TABLES\n __attribute(( aligned(sizeof(spiffs_page_ix)) ))\n#endif\n{\n // common page header\n spiffs_page_header p_hdr;\n // alignment\n u8_t _align[4 - ((sizeof(spiffs_page_header)&3)==0 ? 4 : (sizeof(spiffs_page_header)&3))];\n // size of object\n u32_t size;\n // type of object\n spiffs_obj_type type;\n // name of object\n u8_t name[SPIFFS_OBJ_NAME_LEN];\n} spiffs_page_object_ix_header;\n\n// object index page header\ntypedef struct __attribute(( packed )) {\n spiffs_page_header p_hdr;\n u8_t _align[4 - ((sizeof(spiffs_page_header)&3)==0 ? 4 : (sizeof(spiffs_page_header)&3))];\n} spiffs_page_object_ix;\n\n// callback func for object lookup visitor\ntypedef s32_t (*spiffs_visitor_f)(spiffs *fs, spiffs_obj_id id, spiffs_block_ix bix, int ix_entry,\n const void *user_const_p, void *user_var_p);\n\n\n#if SPIFFS_CACHE\n#define _spiffs_rd(fs, op, fh, addr, len, dst) \\\n spiffs_phys_rd((fs), (op), (fh), (addr), (len), (dst))\n#define _spiffs_wr(fs, op, fh, addr, len, src) \\\n spiffs_phys_wr((fs), (op), (fh), (addr), (len), (src))\n#else\n#define _spiffs_rd(fs, op, fh, addr, len, dst) \\\n spiffs_phys_rd((fs), (addr), (len), (dst))\n#define _spiffs_wr(fs, op, fh, addr, len, src) \\\n spiffs_phys_wr((fs), (addr), (len), (src))\n#endif\n\n#ifndef MIN\n#define MIN(a,b) ((a) < (b) ? (a) : (b))\n#endif\n#ifndef MAX\n#define MAX(a,b) ((a) > (b) ? (a) : (b))\n#endif\n\n// ---------------\n\ns32_t spiffs_phys_rd(\n spiffs *fs,\n#if SPIFFS_CACHE\n u8_t op,\n spiffs_file fh,\n#endif\n u32_t addr,\n u32_t len,\n u8_t *dst);\n\ns32_t spiffs_phys_wr(\n spiffs *fs,\n#if SPIFFS_CACHE\n u8_t op,\n spiffs_file fh,\n#endif\n u32_t addr,\n u32_t len,\n u8_t *src);\n\ns32_t spiffs_phys_cpy(\n spiffs *fs,\n spiffs_file fh,\n u32_t dst,\n u32_t src,\n u32_t len);\n\ns32_t spiffs_phys_count_free_blocks(\n spiffs *fs);\n\ns32_t spiffs_obj_lu_find_entry_visitor(\n spiffs *fs,\n spiffs_block_ix starting_block,\n int starting_lu_entry,\n u8_t flags,\n spiffs_obj_id obj_id,\n spiffs_visitor_f v,\n const void *user_const_p,\n void *user_var_p,\n spiffs_block_ix *block_ix,\n int *lu_entry);\n\ns32_t spiffs_erase_block(\n spiffs *fs,\n spiffs_block_ix bix);\n\n#if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH\ns32_t spiffs_probe(\n spiffs_config *cfg);\n#endif // SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH\n\n// ---------------\n\ns32_t spiffs_obj_lu_scan(\n spiffs *fs);\n\ns32_t spiffs_obj_lu_find_free_obj_id(\n spiffs *fs,\n spiffs_obj_id *obj_id,\n const u8_t *conflicting_name);\n\ns32_t spiffs_obj_lu_find_free(\n spiffs *fs,\n spiffs_block_ix starting_block,\n int starting_lu_entry,\n spiffs_block_ix *block_ix,\n int *lu_entry);\n\ns32_t spiffs_obj_lu_find_id(\n spiffs *fs,\n spiffs_block_ix starting_block,\n int starting_lu_entry,\n spiffs_obj_id obj_id,\n spiffs_block_ix *block_ix,\n int *lu_entry);\n\ns32_t spiffs_obj_lu_find_id_and_span(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_span_ix spix,\n spiffs_page_ix exclusion_pix,\n spiffs_page_ix *pix);\n\ns32_t spiffs_obj_lu_find_id_and_span_by_phdr(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_span_ix spix,\n spiffs_page_ix exclusion_pix,\n spiffs_page_ix *pix);\n\n// ---------------\n\ns32_t spiffs_page_allocate_data(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_page_header *ph,\n u8_t *data,\n u32_t len,\n u32_t page_offs,\n u8_t finalize,\n spiffs_page_ix *pix);\n\ns32_t spiffs_page_move(\n spiffs *fs,\n spiffs_file fh,\n u8_t *page_data,\n spiffs_obj_id obj_id,\n spiffs_page_header *page_hdr,\n spiffs_page_ix src_pix,\n spiffs_page_ix *dst_pix);\n\ns32_t spiffs_page_delete(\n spiffs *fs,\n spiffs_page_ix pix);\n\n// ---------------\n\ns32_t spiffs_object_create(\n spiffs *fs,\n spiffs_obj_id obj_id,\n const u8_t name[SPIFFS_OBJ_NAME_LEN],\n spiffs_obj_type type,\n spiffs_page_ix *objix_hdr_pix);\n\ns32_t spiffs_object_update_index_hdr(\n spiffs *fs,\n spiffs_fd *fd,\n spiffs_obj_id obj_id,\n spiffs_page_ix objix_hdr_pix,\n u8_t *new_objix_hdr_data,\n const u8_t name[SPIFFS_OBJ_NAME_LEN],\n u32_t size,\n spiffs_page_ix *new_pix);\n\nvoid spiffs_cb_object_event(\n spiffs *fs,\n spiffs_fd *fd,\n int ev,\n spiffs_obj_id obj_id,\n spiffs_span_ix spix,\n spiffs_page_ix new_pix,\n u32_t new_size);\n\ns32_t spiffs_object_open_by_id(\n spiffs *fs,\n spiffs_obj_id obj_id,\n spiffs_fd *f,\n spiffs_flags flags,\n spiffs_mode mode);\n\ns32_t spiffs_object_open_by_page(\n spiffs *fs,\n spiffs_page_ix pix,\n spiffs_fd *f,\n spiffs_flags flags,\n spiffs_mode mode);\n\ns32_t spiffs_object_append(\n spiffs_fd *fd,\n u32_t offset,\n u8_t *data,\n u32_t len);\n\ns32_t spiffs_object_modify(\n spiffs_fd *fd,\n u32_t offset,\n u8_t *data,\n u32_t len);\n\ns32_t spiffs_object_read(\n spiffs_fd *fd,\n u32_t offset,\n u32_t len,\n u8_t *dst);\n\ns32_t spiffs_object_truncate(\n spiffs_fd *fd,\n u32_t new_len,\n u8_t remove_object);\n\ns32_t spiffs_object_find_object_index_header_by_name(\n spiffs *fs,\n const u8_t name[SPIFFS_OBJ_NAME_LEN],\n spiffs_page_ix *pix);\n\n// ---------------\n\ns32_t spiffs_gc_check(\n spiffs *fs,\n u32_t len);\n\ns32_t spiffs_gc_erase_page_stats(\n spiffs *fs,\n spiffs_block_ix bix);\n\ns32_t spiffs_gc_find_candidate(\n spiffs *fs,\n spiffs_block_ix **block_candidate,\n int *candidate_count,\n char fs_crammed);\n\ns32_t spiffs_gc_clean(\n spiffs *fs,\n spiffs_block_ix bix);\n\ns32_t spiffs_gc_quick(\n spiffs *fs, u16_t max_free_pages);\n\n// ---------------\n\ns32_t spiffs_fd_find_new(\n spiffs *fs,\n spiffs_fd **fd);\n\ns32_t spiffs_fd_return(\n spiffs *fs,\n spiffs_file f);\n\ns32_t spiffs_fd_get(\n spiffs *fs,\n spiffs_file f,\n spiffs_fd **fd);\n\n#if SPIFFS_CACHE\nvoid spiffs_cache_init(\n spiffs *fs);\n\nvoid spiffs_cache_drop_page(\n spiffs *fs,\n spiffs_page_ix pix);\n\n#if SPIFFS_CACHE_WR\nspiffs_cache_page *spiffs_cache_page_allocate_by_fd(\n spiffs *fs,\n spiffs_fd *fd);\n\nvoid spiffs_cache_fd_release(\n spiffs *fs,\n spiffs_cache_page *cp);\n\nspiffs_cache_page *spiffs_cache_page_get_by_fd(\n spiffs *fs,\n spiffs_fd *fd);\n#endif\n#endif\n\ns32_t spiffs_lookup_consistency_check(\n spiffs *fs,\n u8_t check_all_objects);\n\ns32_t spiffs_page_consistency_check(\n spiffs *fs);\n\ns32_t spiffs_object_index_consistency_check(\n spiffs *fs);\n\n#endif /* SPIFFS_NUCLEUS_H_ */\n"}