amos 28b63db6ff Add 3 HARD tasks from batch 28: SPIFFS delete cache, full-delete GC, GC truncate
Qwen failed all 3 attempts of each (context limit, counted as failure).

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

13 lines
199 KiB
JSON

{
"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",
"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#include <unistd.h>\n// ----------- >8 ------------\n\n// compile time switches\n\n// Set generic spiffs debug output call.\n#ifndef SPIFFS_DBG\n#define SPIFFS_DBG(_f, ...) //printf(_f, ## __VA_ARGS__)\n#endif\n// Set spiffs debug output call for garbage collecting.\n#ifndef SPIFFS_GC_DBG\n#define SPIFFS_GC_DBG(_f, ...) //printf(_f, ## __VA_ARGS__)\n#endif\n// Set spiffs debug output call for caching.\n#ifndef SPIFFS_CACHE_DBG\n#define SPIFFS_CACHE_DBG(_f, ...) //printf(_f, ## __VA_ARGS__)\n#endif\n// Set spiffs debug output call for system consistency checks.\n#ifndef SPIFFS_CHECK_DBG\n#define SPIFFS_CHECK_DBG(_f, ...) //printf(_f, ## __VA_ARGS__)\n#endif\n// Set spiffs debug output call for all api invocations.\n#ifndef SPIFFS_API_DBG\n#define SPIFFS_API_DBG(_f, ...) //printf(_f, ## __VA_ARGS__)\n#endif\n\n\n\n// Defines spiffs debug print formatters\n// some general signed number\n#ifndef _SPIPRIi\n#define _SPIPRIi \"%d\"\n#endif\n// address\n#ifndef _SPIPRIad\n#define _SPIPRIad \"%08x\"\n#endif\n// block\n#ifndef _SPIPRIbl\n#define _SPIPRIbl \"%04x\"\n#endif\n// page\n#ifndef _SPIPRIpg\n#define _SPIPRIpg \"%04x\"\n#endif\n// span index\n#ifndef _SPIPRIsp\n#define _SPIPRIsp \"%04x\"\n#endif\n// file descriptor\n#ifndef _SPIPRIfd\n#define _SPIPRIfd \"%d\"\n#endif\n// file object id\n#ifndef _SPIPRIid\n#define _SPIPRIid \"%04x\"\n#endif\n// file flags\n#ifndef _SPIPRIfl\n#define _SPIPRIfl \"%02x\"\n#endif\n\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. Note that this length include the\n// zero-termination character, meaning maximum string of characters\n// can at most be SPIFFS_OBJ_NAME_LEN - 1.\n#ifndef SPIFFS_OBJ_NAME_LEN\n#define SPIFFS_OBJ_NAME_LEN (32)\n#endif\n\n// Maximum length of the metadata associated with an object.\n// Setting to non-zero value enables metadata-related API but also\n// changes the on-disk format, so the change is not backward-compatible.\n//\n// Do note: the meta length must never exceed\n// logical_page_size - (SPIFFS_OBJ_NAME_LEN + 64)\n//\n// This is derived from following:\n// logical_page_size - (SPIFFS_OBJ_NAME_LEN + sizeof(spiffs_page_header) +\n// spiffs_object_ix_header fields + at least some LUT entries)\n#ifndef SPIFFS_OBJ_META_LEN\n#define SPIFFS_OBJ_META_LEN (0)\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// Enable this to add a temporal file cache using the fd buffer.\n// The effects of the cache is that SPIFFS_open will find the file faster in\n// certain cases. It will make it a lot easier for spiffs to find files\n// opened frequently, reducing number of readings from the spi flash for\n// finding those files.\n// This will grow each fd by 6 bytes. If your files are opened in patterns\n// with a degree of temporal locality, the system is optimized.\n// Examples can be letting spiffs serve web content, where one file is the css.\n// The css is accessed for each html file that is opened, meaning it is\n// accessed almost every second time a file is opened. Another example could be\n// a log file that is often opened, written, and closed.\n// The size of the cache is number of given file descriptors, as it piggybacks\n// on the fd update mechanism. The cache lives in the closed file descriptors.\n// When closed, the fd know the whereabouts of the file. Instead of forgetting\n// this, the temporal cache will keep handling updates to that file even if the\n// fd is closed. If the file is opened again, the location of the file is found\n// directly. If all available descriptors become opened, all cache memory is\n// lost.\n#ifndef SPIFFS_TEMPORAL_FD_CACHE\n#define SPIFFS_TEMPORAL_FD_CACHE 1\n#endif\n\n// Temporal file cache hit score. Each time a file is opened, all cached files\n// will lose one point. If the opened file is found in cache, that entry will\n// gain SPIFFS_TEMPORAL_CACHE_HIT_SCORE points. One can experiment with this\n// value for the specific access patterns of the application. However, it must\n// be between 1 (no gain for hitting a cached entry often) and 255.\n#ifndef SPIFFS_TEMPORAL_CACHE_HIT_SCORE\n#define SPIFFS_TEMPORAL_CACHE_HIT_SCORE 4\n#endif\n\n// Enable to be able to map object indices to memory.\n// This allows for faster and more deterministic reading if cases of reading\n// large files and when changing file offset by seeking around a lot.\n// When mapping a file's index, the file system will be scanned for index pages\n// and the info will be put in memory provided by user. When reading, the\n// memory map can be looked up instead of searching for index pages on the\n// medium. This way, user can trade memory against performance.\n// Whole, parts of, or future parts not being written yet can be mapped. The\n// memory array will be owned by spiffs and updated accordingly during garbage\n// collecting or when modifying the indices. The latter is invoked by when the\n// file is modified in some way. The index buffer is tied to the file\n// descriptor.\n#ifndef SPIFFS_IX_MAP\n#define SPIFFS_IX_MAP 1\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/test/params_test.h": "/*\n * params_test.h\n *\n * Created on: May 26, 2013\n * Author: petera\n */\n\n#ifndef PARAMS_TEST_H_\n#define PARAMS_TEST_H_\n\n//////////////// TEST PARAMS ////////////////\n\n// default test total emulated spi flash size\n#define PHYS_FLASH_SIZE (16*1024*1024)\n// default test spiffs file system size\n#define SPIFFS_FLASH_SIZE (2*1024*1024)\n// default test spiffs file system offset in emulated spi flash\n#define SPIFFS_PHYS_ADDR (4*1024*1024)\n// default test sector size\n#define SECTOR_SIZE 65536\n// default test logical block size\n#define LOG_BLOCK (SECTOR_SIZE*2)\n// default test logical page size\n#define LOG_PAGE (SECTOR_SIZE/256)\n// default test number of filedescs\n#define DEFAULT_NUM_FD 16\n// default test number of cache pages\n#define DEFAULT_NUM_CACHE_PAGES 8\n\n// When testing, test bench create reference files for comparison on\n// the actual hard drive. By default, put these on ram drive for speed.\n#define TEST_PATH \"/dev/shm/spiffs/test-data/\"\n\n#define ASSERT(c, m) real_assert((c),(m), __FILE__, __LINE__);\nvoid real_assert(int c, const char *n, const char *file, int l);\n\n/////////// SPIFFS BUILD CONFIG ////////////\n\n// test using filesystem magic\n#ifndef SPIFFS_USE_MAGIC\n#define SPIFFS_USE_MAGIC 1\n#endif\n// test using filesystem magic length\n#ifndef SPIFFS_USE_MAGIC_LENGTH\n#define SPIFFS_USE_MAGIC_LENGTH 1\n#endif\n// test using extra param in callback\n#ifndef SPIFFS_HAL_CALLBACK_EXTRA\n#define SPIFFS_HAL_CALLBACK_EXTRA 1\n#endif\n// test using filehandle offset\n#ifndef SPIFFS_FILEHDL_OFFSET\n#define SPIFFS_FILEHDL_OFFSET 1\n// use this offset\n#define TEST_SPIFFS_FILEHDL_OFFSET 0x1000\n#endif\n\n#ifdef NO_TEST\n#define SPIFFS_LOCK(fs)\n#define SPIFFS_UNLOCK(fs)\n#else\nstruct spiffs_t;\nextern void test_lock(struct spiffs_t *fs);\nextern void test_unlock(struct spiffs_t *fs);\n#define SPIFFS_LOCK(fs) test_lock(fs)\n#define SPIFFS_UNLOCK(fs) test_unlock(fs)\n#endif\n\n// dbg output\n#define SPIFFS_DBG(_f, ...) //printf(\"\\x1b[32m\" _f \"\\x1b[0m\", ## __VA_ARGS__)\n#define SPIFFS_API_DBG(_f, ...) //printf(\"\\n\\x1b[1m\\x1b[7m\" _f \"\\x1b[0m\", ## __VA_ARGS__)\n#define SPIFFS_GC_DBG(_f, ...) //printf(\"\\x1b[36m\" _f \"\\x1b[0m\", ## __VA_ARGS__)\n#define SPIFFS_CACHE_DBG(_f, ...) //printf(\"\\x1b[33m\" _f \"\\x1b[0m\", ## __VA_ARGS__)\n#define SPIFFS_CHECK_DBG(_f, ...) //printf(\"\\x1b[31m\" _f \"\\x1b[0m\", ## __VA_ARGS__)\n\n// needed types\ntypedef signed int s32_t;\ntypedef unsigned int u32_t;\ntypedef signed short s16_t;\ntypedef unsigned short u16_t;\ntypedef signed char s8_t;\ntypedef unsigned char u8_t;\n\n#endif /* PARAMS_TEST_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_NAME_TOO_LONG -10036\n\n#define SPIFFS_ERR_IX_MAP_UNMAPPED -10037\n#define SPIFFS_ERR_IX_MAP_MAPPED -10038\n#define SPIFFS_ERR_IX_MAP_BAD_RANGE -10039\n\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 printf(__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#define SPIFFS_O_APPEND SPIFFS_APPEND\n/* If the opened file exists, it will be truncated to zero length before opened */\n#define SPIFFS_TRUNC (1<<1)\n#define SPIFFS_O_TRUNC SPIFFS_TRUNC\n/* If the opened file does not exist, it will be created before opened */\n#define SPIFFS_CREAT (1<<2)\n#define SPIFFS_O_CREAT SPIFFS_CREAT\n/* The opened file may only be read */\n#define SPIFFS_RDONLY (1<<3)\n#define SPIFFS_O_RDONLY SPIFFS_RDONLY\n/* The opened file may only be written */\n#define SPIFFS_WRONLY (1<<4)\n#define SPIFFS_O_WRONLY SPIFFS_WRONLY\n/* The opened file may be both read and written */\n#define SPIFFS_RDWR (SPIFFS_RDONLY | SPIFFS_WRONLY)\n#define SPIFFS_O_RDWR SPIFFS_RDWR\n/* Any writes to the filehandle will never be cached but flushed directly */\n#define SPIFFS_DIRECT (1<<5)\n#define SPIFFS_O_DIRECT SPIFFS_DIRECT\n/* If SPIFFS_O_CREAT and SPIFFS_O_EXCL are set, SPIFFS_open() shall fail if the file exists */\n#define SPIFFS_EXCL (1<<6)\n#define SPIFFS_O_EXCL SPIFFS_EXCL\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#if SPIFFS_OBJ_META_LEN\n u8_t meta[SPIFFS_OBJ_META_LEN];\n#endif\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#if SPIFFS_OBJ_META_LEN\n u8_t meta[SPIFFS_OBJ_META_LEN];\n#endif\n};\n\ntypedef struct {\n spiffs *fs;\n spiffs_block_ix block;\n int entry;\n} spiffs_DIR;\n\n#if SPIFFS_IX_MAP\n\ntypedef struct {\n // buffer with looked up data pixes\n spiffs_page_ix *map_buf;\n // precise file byte offset\n u32_t offset;\n // start data span index of lookup buffer\n spiffs_span_ix start_spix;\n // end data span index of lookup buffer\n spiffs_span_ix end_spix;\n} spiffs_ix_map;\n\n#endif\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_O_APPEND, SPIFFS_O_TRUNC, SPIFFS_O_CREAT, SPIFFS_O_RDONLY,\n * SPIFFS_O_WRONLY, SPIFFS_O_RDWR, SPIFFS_O_DIRECT, SPIFFS_O_EXCL\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#if SPIFFS_OBJ_META_LEN\n/**\n * Updates file's metadata\n * @param fs the file system struct\n * @param path path to the file\n * @param meta new metadata. must be SPIFFS_OBJ_META_LEN bytes long.\n */\ns32_t SPIFFS_update_meta(spiffs *fs, const char *name, const void *meta);\n\n/**\n * Updates file's metadata\n * @param fs the file system struct\n * @param fh file handle of the file\n * @param meta new metadata. must be SPIFFS_OBJ_META_LEN bytes long.\n */\ns32_t SPIFFS_fupdate_meta(spiffs *fs, spiffs_file fh, const void *meta);\n#endif\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_IX_MAP\n\n/**\n * Maps the first level index lookup to a given memory map.\n * This will make reading big files faster, as the memory map will be used for\n * looking up data pages instead of searching for the indices on the physical\n * medium. When mapping, all affected indicies are found and the information is\n * copied to the array.\n * Whole file or only parts of it may be mapped. The index map will cover file\n * contents from argument offset until and including arguments (offset+len).\n * It is valid to map a longer range than the current file size. The map will\n * then be populated when the file grows.\n * On garbage collections and file data page movements, the map array will be\n * automatically updated. Do not tamper with the map array, as this contains\n * the references to the data pages. Modifying it from outside will corrupt any\n * future readings using this file descriptor.\n * The map will no longer be used when the file descriptor closed or the file\n * is unmapped.\n * This can be useful to get faster and more deterministic timing when reading\n * large files, or when seeking and reading a lot within a file.\n * @param fs the file system struct\n * @param fh the file handle of the file to map\n * @param map a spiffs_ix_map struct, describing the index map\n * @param offset absolute file offset where to start the index map\n * @param len length of the mapping in actual file bytes\n * @param map_buf the array buffer for the look up data - number of required\n * elements in the array can be derived from function\n * SPIFFS_bytes_to_ix_map_entries given the length\n */\ns32_t SPIFFS_ix_map(spiffs *fs, spiffs_file fh, spiffs_ix_map *map,\n u32_t offset, u32_t len, spiffs_page_ix *map_buf);\n\n/**\n * Unmaps the index lookup from this filehandle. All future readings will\n * proceed as normal, requiring reading of the first level indices from\n * physical media.\n * The map and map buffer given in function SPIFFS_ix_map will no longer be\n * referenced by spiffs.\n * It is not strictly necessary to unmap a file before closing it, as closing\n * a file will automatically unmap it.\n * @param fs the file system struct\n * @param fh the file handle of the file to unmap\n */\ns32_t SPIFFS_ix_unmap(spiffs *fs, spiffs_file fh);\n\n/**\n * Moves the offset for the index map given in function SPIFFS_ix_map. Parts or\n * all of the map buffer will repopulated.\n * @param fs the file system struct\n * @param fh the mapped file handle of the file to remap\n * @param offset new absolute file offset where to start the index map\n */\ns32_t SPIFFS_ix_remap(spiffs *fs, spiffs_file fh, u32_t offs);\n\n/**\n * Utility function to get number of spiffs_page_ix entries a map buffer must\n * contain on order to map given amount of file data in bytes.\n * See function SPIFFS_ix_map and SPIFFS_ix_map_entries_to_bytes.\n * @param fs the file system struct\n * @param bytes number of file data bytes to map\n * @return needed number of elements in a spiffs_page_ix array needed to\n * map given amount of bytes in a file\n */\ns32_t SPIFFS_bytes_to_ix_map_entries(spiffs *fs, u32_t bytes);\n\n/**\n * Utility function to amount of file data bytes that can be mapped when\n * mapping a file with buffer having given number of spiffs_page_ix entries.\n * See function SPIFFS_ix_map and SPIFFS_bytes_to_ix_map_entries.\n * @param fs the file system struct\n * @param map_page_ix_entries number of entries in a spiffs_page_ix array\n * @return amount of file data in bytes that can be mapped given a map\n * buffer having given amount of spiffs_page_ix entries\n */\ns32_t SPIFFS_ix_map_entries_to_bytes(spiffs *fs, u32_t map_page_ix_entries);\n\n#endif // SPIFFS_IX_MAP\n\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_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// visitor result, continue searching\n#define SPIFFS_VIS_COUNTINUE (SPIFFS_ERR_INTERNAL - 20)\n// visitor result, continue searching after reloading lu buffer\n#define SPIFFS_VIS_COUNTINUE_RELOAD (SPIFFS_ERR_INTERNAL - 21)\n// visitor result, stop searching\n#define SPIFFS_VIS_END (SPIFFS_ERR_INTERNAL - 22)\n\n// updating an object index contents\n#define SPIFFS_EV_IX_UPD (0)\n// creating a new object index\n#define SPIFFS_EV_IX_NEW (1)\n// deleting an object index\n#define SPIFFS_EV_IX_DEL (2)\n// moving an object index without updating contents\n#define SPIFFS_EV_IX_MOV (3)\n// updating an object index header data only, not the table itself\n#define SPIFFS_EV_IX_UPD_HDR (4)\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// get data span index for object index span index\n#define SPIFFS_DATA_SPAN_IX_FOR_OBJ_IX_SPAN_IX(fs, spix) \\\n ( (spix) == 0 ? 0 : (SPIFFS_OBJ_HDR_IX_LEN(fs) + (((spix)-1) * SPIFFS_OBJ_IX_LEN(fs))) )\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 SPIFFS_ERR_NOT_MOUNTED; \\\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 SPIFFS_ERR_NOT_CONFIGURED; \\\n }\n\n#define SPIFFS_API_CHECK_RES(fs, res) \\\n if ((res) < SPIFFS_OK) { \\\n (fs)->err_code = (res); \\\n return (res); \\\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 (res); \\\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, only visit matching objec ids\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#if SPIFFS_TEMPORAL_FD_CACHE\n // djb2 hash of filename\n u32_t name_hash;\n // hit score (score == 0 indicates never used fd)\n u16_t score;\n#endif\n#if SPIFFS_IX_MAP\n // spiffs index map, if 0 it means unmapped\n spiffs_ix_map *ix_map;\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#if SPIFFS_OBJ_META_LEN\n // metadata. not interpreted by SPIFFS in any way.\n u8_t meta[SPIFFS_OBJ_META_LEN];\n#endif\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[],\n const u8_t meta[],\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[],\n const u8_t meta[],\n u32_t size,\n spiffs_page_ix *new_pix);\n\n#if SPIFFS_IX_MAP\n\ns32_t spiffs_populate_ix_map(\n spiffs *fs,\n spiffs_fd *fd,\n u32_t vec_entry_start,\n u32_t vec_entry_end);\n\n#endif\n\nvoid spiffs_cb_object_event(\n spiffs *fs,\n spiffs_page_object_ix *objix,\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 const char *name);\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_TEMPORAL_FD_CACHE\nvoid spiffs_fd_temporal_cache_rehash(\n spiffs *fs,\n const char *old_path,\n const char *new_path);\n#endif\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",
"src/spiffs_hydrogen.c": "/*\n * spiffs_hydrogen.c\n *\n * Created on: Jun 16, 2013\n * Author: petera\n */\n\n#include \"spiffs.h\"\n#include \"spiffs_nucleus.h\"\n\n#if SPIFFS_FILEHDL_OFFSET\n#define SPIFFS_FH_OFFS(fs, fh) ((fh) != 0 ? ((fh) + (fs)->cfg.fh_ix_offset) : 0)\n#define SPIFFS_FH_UNOFFS(fs, fh) ((fh) != 0 ? ((fh) - (fs)->cfg.fh_ix_offset) : 0)\n#else\n#define SPIFFS_FH_OFFS(fs, fh) (fh)\n#define SPIFFS_FH_UNOFFS(fs, fh) (fh)\n#endif\n\n#if SPIFFS_CACHE == 1\nstatic s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh);\n#endif\n\n#if SPIFFS_BUFFER_HELP\nu32_t SPIFFS_buffer_bytes_for_filedescs(spiffs *fs, u32_t num_descs) {\n return num_descs * sizeof(spiffs_fd);\n}\n#if SPIFFS_CACHE\nu32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages) {\n return sizeof(spiffs_cache) + num_pages * (sizeof(spiffs_cache_page) + SPIFFS_CFG_LOG_PAGE_SZ(fs));\n}\n#endif\n#endif\n\nu8_t SPIFFS_mounted(spiffs *fs) {\n return SPIFFS_CHECK_MOUNT(fs);\n}\n\ns32_t SPIFFS_format(spiffs *fs) {\n#if SPIFFS_READ_ONLY\n (void)fs;\n return SPIFFS_ERR_RO_NOT_IMPL;\n#else\n SPIFFS_API_CHECK_CFG(fs);\n if (SPIFFS_CHECK_MOUNT(fs)) {\n fs->err_code = SPIFFS_ERR_MOUNTED;\n return -1;\n }\n\n s32_t res;\n SPIFFS_LOCK(fs);\n\n spiffs_block_ix bix = 0;\n while (bix < fs->block_count) {\n fs->max_erase_count = 0;\n res = spiffs_erase_block(fs, bix);\n if (res != SPIFFS_OK) {\n res = SPIFFS_ERR_ERASE_FAIL;\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n bix++;\n }\n\n SPIFFS_UNLOCK(fs);\n\n return 0;\n#endif // SPIFFS_READ_ONLY\n}\n\n#if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0\n\ns32_t SPIFFS_probe_fs(spiffs_config *config) {\n SPIFFS_API_DBG(\"%s\\n\", __func__);\n s32_t res = spiffs_probe(config);\n return res;\n}\n\n#endif // SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0\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 SPIFFS_API_DBG(\"%s \"\n \" sz:\"_SPIPRIi \" logpgsz:\"_SPIPRIi \" logblksz:\"_SPIPRIi \" perasz:\"_SPIPRIi\n \" addr:\"_SPIPRIad\n \" fdsz:\"_SPIPRIi \" cachesz:\"_SPIPRIi\n \"\\n\",\n __func__,\n SPIFFS_CFG_PHYS_SZ(fs), SPIFFS_CFG_LOG_PAGE_SZ(fs), SPIFFS_CFG_LOG_BLOCK_SZ(fs),\n SPIFFS_CFG_PHYS_ADDR(fs),\n SPIFFS_CFG_PHYS_ERASE_SZ(fs),\n fd_space_size, cache_size);\n void *user_data;\n SPIFFS_LOCK(fs);\n user_data = fs->user_data;\n memset(fs, 0, sizeof(spiffs));\n memcpy(&fs->cfg, config, sizeof(spiffs_config));\n fs->user_data = user_data;\n fs->block_count = SPIFFS_CFG_PHYS_SZ(fs) / SPIFFS_CFG_LOG_BLOCK_SZ(fs);\n fs->work = &work[0];\n fs->lu_work = &work[SPIFFS_CFG_LOG_PAGE_SZ(fs)];\n memset(fd_space, 0, fd_space_size);\n // align fd_space pointer to pointer size byte boundary\n u8_t ptr_size = sizeof(void*);\n u8_t addr_lsb = ((u8_t)(intptr_t)fd_space) & (ptr_size-1);\n if (addr_lsb) {\n fd_space += (ptr_size-addr_lsb);\n fd_space_size -= (ptr_size-addr_lsb);\n }\n fs->fd_space = fd_space;\n fs->fd_count = (fd_space_size/sizeof(spiffs_fd));\n\n // align cache pointer to 4 byte boundary\n addr_lsb = ((u8_t)(intptr_t)cache) & (ptr_size-1);\n if (addr_lsb) {\n u8_t *cache_8 = (u8_t *)cache;\n cache_8 += (ptr_size-addr_lsb);\n cache = cache_8;\n cache_size -= (ptr_size-addr_lsb);\n }\n if (cache_size & (ptr_size-1)) {\n cache_size -= (cache_size & (ptr_size-1));\n }\n\n#if SPIFFS_CACHE\n fs->cache = cache;\n fs->cache_size = (cache_size > (SPIFFS_CFG_LOG_PAGE_SZ(fs)*32)) ? SPIFFS_CFG_LOG_PAGE_SZ(fs)*32 : cache_size;\n spiffs_cache_init(fs);\n#endif\n\n s32_t res;\n\n#if SPIFFS_USE_MAGIC\n res = SPIFFS_CHECK_MAGIC_POSSIBLE(fs) ? SPIFFS_OK : SPIFFS_ERR_MAGIC_NOT_POSSIBLE;\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n#endif\n\n fs->config_magic = SPIFFS_CONFIG_MAGIC;\n\n res = spiffs_obj_lu_scan(fs);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n SPIFFS_DBG(\"page index byte len: \"_SPIPRIi\"\\n\", (u32_t)SPIFFS_CFG_LOG_PAGE_SZ(fs));\n SPIFFS_DBG(\"object lookup pages: \"_SPIPRIi\"\\n\", (u32_t)SPIFFS_OBJ_LOOKUP_PAGES(fs));\n SPIFFS_DBG(\"page pages per block: \"_SPIPRIi\"\\n\", (u32_t)SPIFFS_PAGES_PER_BLOCK(fs));\n SPIFFS_DBG(\"page header length: \"_SPIPRIi\"\\n\", (u32_t)sizeof(spiffs_page_header));\n SPIFFS_DBG(\"object header index entries: \"_SPIPRIi\"\\n\", (u32_t)SPIFFS_OBJ_HDR_IX_LEN(fs));\n SPIFFS_DBG(\"object index entries: \"_SPIPRIi\"\\n\", (u32_t)SPIFFS_OBJ_IX_LEN(fs));\n SPIFFS_DBG(\"available file descriptors: \"_SPIPRIi\"\\n\", (u32_t)fs->fd_count);\n SPIFFS_DBG(\"free blocks: \"_SPIPRIi\"\\n\", (u32_t)fs->free_blocks);\n\n fs->check_cb_f = check_cb_f;\n\n fs->mounted = 1;\n\n SPIFFS_UNLOCK(fs);\n\n return 0;\n}\n\nvoid SPIFFS_unmount(spiffs *fs) {\n SPIFFS_API_DBG(\"%s\\n\", __func__);\n if (!SPIFFS_CHECK_CFG(fs) || !SPIFFS_CHECK_MOUNT(fs)) return;\n SPIFFS_LOCK(fs);\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#if SPIFFS_CACHE\n (void)spiffs_fflush_cache(fs, cur_fd->file_nbr);\n#endif\n spiffs_fd_return(fs, cur_fd->file_nbr);\n }\n }\n fs->mounted = 0;\n\n SPIFFS_UNLOCK(fs);\n}\n\ns32_t SPIFFS_errno(spiffs *fs) {\n return fs->err_code;\n}\n\nvoid SPIFFS_clearerr(spiffs *fs) {\n SPIFFS_API_DBG(\"%s\\n\", __func__);\n fs->err_code = SPIFFS_OK;\n}\n\ns32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode) {\n SPIFFS_API_DBG(\"%s '%s'\\n\", __func__, path);\n#if SPIFFS_READ_ONLY\n (void)fs; (void)path; (void)mode;\n return SPIFFS_ERR_RO_NOT_IMPL;\n#else\n (void)mode;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {\n SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);\n }\n SPIFFS_LOCK(fs);\n spiffs_obj_id obj_id;\n s32_t res;\n\n res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (const u8_t*)path);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n res = spiffs_object_create(fs, obj_id, (const u8_t*)path, 0, SPIFFS_TYPE_FILE, 0);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n SPIFFS_UNLOCK(fs);\n return 0;\n#endif // SPIFFS_READ_ONLY\n}\n\nspiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode) {\n SPIFFS_API_DBG(\"%s '%s' \"_SPIPRIfl \"\\n\", __func__, path, flags);\n (void)mode;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {\n SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);\n }\n SPIFFS_LOCK(fs);\n\n spiffs_fd *fd;\n spiffs_page_ix pix;\n\n#if SPIFFS_READ_ONLY\n // not valid flags in read only mode\n flags &= ~(SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_TRUNC);\n#endif // SPIFFS_READ_ONLY\n\n s32_t res = spiffs_fd_find_new(fs, &fd, path);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix);\n if ((flags & SPIFFS_O_CREAT) == 0) {\n if (res < SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n\n if (res == SPIFFS_OK &&\n (flags & (SPIFFS_O_CREAT | SPIFFS_O_EXCL)) == (SPIFFS_O_CREAT | SPIFFS_O_EXCL)) {\n // creat and excl and file exists - fail\n res = SPIFFS_ERR_FILE_EXISTS;\n spiffs_fd_return(fs, fd->file_nbr);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n\n if ((flags & SPIFFS_O_CREAT) && res == SPIFFS_ERR_NOT_FOUND) {\n#if !SPIFFS_READ_ONLY\n spiffs_obj_id obj_id;\n // no need to enter conflicting name here, already looked for it above\n res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, 0);\n if (res < SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n res = spiffs_object_create(fs, obj_id, (const u8_t*)path, 0, SPIFFS_TYPE_FILE, &pix);\n if (res < SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n flags &= ~SPIFFS_O_TRUNC;\n#endif // !SPIFFS_READ_ONLY\n } else {\n if (res < SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n res = spiffs_object_open_by_page(fs, pix, fd, flags, mode);\n if (res < SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n#if !SPIFFS_READ_ONLY\n if (flags & SPIFFS_O_TRUNC) {\n res = spiffs_object_truncate(fd, 0, 0);\n if (res < SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n#endif // !SPIFFS_READ_ONLY\n\n fd->fdoffset = 0;\n\n SPIFFS_UNLOCK(fs);\n\n return SPIFFS_FH_OFFS(fs, fd->file_nbr);\n}\n\nspiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode) {\n SPIFFS_API_DBG(\"%s '%s':\"_SPIPRIid \" \"_SPIPRIfl \"\\n\", __func__, e->name, e->obj_id, flags);\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n spiffs_fd *fd;\n\n s32_t res = spiffs_fd_find_new(fs, &fd, 0);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_object_open_by_page(fs, e->pix, fd, flags, mode);\n if (res < SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n#if !SPIFFS_READ_ONLY\n if (flags & SPIFFS_O_TRUNC) {\n res = spiffs_object_truncate(fd, 0, 0);\n if (res < SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n#endif // !SPIFFS_READ_ONLY\n\n fd->fdoffset = 0;\n\n SPIFFS_UNLOCK(fs);\n\n return SPIFFS_FH_OFFS(fs, fd->file_nbr);\n}\n\nspiffs_file SPIFFS_open_by_page(spiffs *fs, spiffs_page_ix page_ix, spiffs_flags flags, spiffs_mode mode) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIpg \" \"_SPIPRIfl \"\\n\", __func__, page_ix, flags);\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n spiffs_fd *fd;\n\n s32_t res = spiffs_fd_find_new(fs, &fd, 0);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n if (SPIFFS_IS_LOOKUP_PAGE(fs, page_ix)) {\n res = SPIFFS_ERR_NOT_A_FILE;\n spiffs_fd_return(fs, fd->file_nbr);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n\n res = spiffs_object_open_by_page(fs, page_ix, fd, flags, mode);\n if (res == SPIFFS_ERR_IS_FREE ||\n res == SPIFFS_ERR_DELETED ||\n res == SPIFFS_ERR_NOT_FINALIZED ||\n res == SPIFFS_ERR_NOT_INDEX ||\n res == SPIFFS_ERR_INDEX_SPAN_MISMATCH) {\n res = SPIFFS_ERR_NOT_A_FILE;\n }\n if (res < SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n#if !SPIFFS_READ_ONLY\n if (flags & SPIFFS_O_TRUNC) {\n res = spiffs_object_truncate(fd, 0, 0);\n if (res < SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n#endif // !SPIFFS_READ_ONLY\n\n fd->fdoffset = 0;\n\n SPIFFS_UNLOCK(fs);\n\n return SPIFFS_FH_OFFS(fs, fd->file_nbr);\n}\n\nstatic s32_t spiffs_hydro_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n spiffs_fd *fd;\n s32_t res;\n\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n if ((fd->flags & SPIFFS_O_RDONLY) == 0) {\n res = SPIFFS_ERR_NOT_READABLE;\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n\n if (fd->size == SPIFFS_UNDEFINED_LEN && len > 0) {\n // special case for zero sized files\n res = SPIFFS_ERR_END_OF_OBJECT;\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n\n#if SPIFFS_CACHE_WR\n spiffs_fflush_cache(fs, fh);\n#endif\n\n if (fd->fdoffset + len >= fd->size) {\n // reading beyond file size\n s32_t avail = fd->size - fd->fdoffset;\n if (avail <= 0) {\n SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_END_OF_OBJECT);\n }\n res = spiffs_object_read(fd, fd->fdoffset, avail, (u8_t*)buf);\n if (res == SPIFFS_ERR_END_OF_OBJECT) {\n fd->fdoffset += avail;\n SPIFFS_UNLOCK(fs);\n return avail;\n } else {\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n len = avail;\n }\n } else {\n // reading within file size\n res = spiffs_object_read(fd, fd->fdoffset, len, (u8_t*)buf);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n fd->fdoffset += len;\n\n SPIFFS_UNLOCK(fs);\n\n return len;\n}\n\ns32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \" \"_SPIPRIi \"\\n\", __func__, fh, len);\n s32_t res = spiffs_hydro_read(fs, fh, buf, len);\n if (res == SPIFFS_ERR_END_OF_OBJECT) {\n res = 0;\n }\n return res;\n}\n\n\n#if !SPIFFS_READ_ONLY\nstatic s32_t spiffs_hydro_write(spiffs *fs, spiffs_fd *fd, void *buf, u32_t offset, s32_t len) {\n (void)fs;\n s32_t res = SPIFFS_OK;\n s32_t remaining = len;\n if (fd->size != SPIFFS_UNDEFINED_LEN && offset < fd->size) {\n s32_t m_len = MIN((s32_t)(fd->size - offset), len);\n res = spiffs_object_modify(fd, offset, (u8_t *)buf, m_len);\n SPIFFS_CHECK_RES(res);\n remaining -= m_len;\n u8_t *buf_8 = (u8_t *)buf;\n buf_8 += m_len;\n buf = buf_8;\n offset += m_len;\n }\n if (remaining > 0) {\n res = spiffs_object_append(fd, offset, (u8_t *)buf, remaining);\n SPIFFS_CHECK_RES(res);\n }\n return len;\n\n}\n#endif // !SPIFFS_READ_ONLY\n\ns32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \" \"_SPIPRIi \"\\n\", __func__, fh, len);\n#if SPIFFS_READ_ONLY\n (void)fs; (void)fh; (void)buf; (void)len;\n return SPIFFS_ERR_RO_NOT_IMPL;\n#else\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n spiffs_fd *fd;\n s32_t res;\n u32_t offset;\n\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n if ((fd->flags & SPIFFS_O_WRONLY) == 0) {\n res = SPIFFS_ERR_NOT_WRITABLE;\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n\n if ((fd->flags & SPIFFS_O_APPEND)) {\n fd->fdoffset = fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size;\n }\n\n offset = fd->fdoffset;\n\n#if SPIFFS_CACHE_WR\n if (fd->cache_page == 0) {\n // see if object id is associated with cache already\n fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);\n }\n#endif\n if (fd->flags & SPIFFS_O_APPEND) {\n if (fd->size == SPIFFS_UNDEFINED_LEN) {\n offset = 0;\n } else {\n offset = fd->size;\n }\n#if SPIFFS_CACHE_WR\n if (fd->cache_page) {\n offset = MAX(offset, fd->cache_page->offset + fd->cache_page->size);\n }\n#endif\n }\n\n#if SPIFFS_CACHE_WR\n if ((fd->flags & SPIFFS_O_DIRECT) == 0) {\n if (len < (s32_t)SPIFFS_CFG_LOG_PAGE_SZ(fs)) {\n // small write, try to cache it\n u8_t alloc_cpage = 1;\n if (fd->cache_page) {\n // have a cached page for this fd already, check cache page boundaries\n if (offset < fd->cache_page->offset || // writing before cache\n offset > fd->cache_page->offset + fd->cache_page->size || // writing after cache\n offset + len > fd->cache_page->offset + SPIFFS_CFG_LOG_PAGE_SZ(fs)) // writing beyond cache page\n {\n // boundary violation, write back cache first and allocate new\n SPIFFS_CACHE_DBG(\"CACHE_WR_DUMP: dumping cache page \"_SPIPRIi\" for fd \"_SPIPRIfd\":\"_SPIPRIid\", boundary viol, offs:\"_SPIPRIi\" size:\"_SPIPRIi\"\\n\",\n fd->cache_page->ix, fd->file_nbr, fd->obj_id, fd->cache_page->offset, fd->cache_page->size);\n res = spiffs_hydro_write(fs, fd,\n spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),\n fd->cache_page->offset, fd->cache_page->size);\n spiffs_cache_fd_release(fs, fd->cache_page);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n } else {\n // writing within cache\n alloc_cpage = 0;\n }\n }\n\n if (alloc_cpage) {\n fd->cache_page = spiffs_cache_page_allocate_by_fd(fs, fd);\n if (fd->cache_page) {\n fd->cache_page->offset = offset;\n fd->cache_page->size = 0;\n SPIFFS_CACHE_DBG(\"CACHE_WR_ALLO: allocating cache page \"_SPIPRIi\" for fd \"_SPIPRIfd\":\"_SPIPRIid\"\\n\",\n fd->cache_page->ix, fd->file_nbr, fd->obj_id);\n }\n }\n\n if (fd->cache_page) {\n u32_t offset_in_cpage = offset - fd->cache_page->offset;\n SPIFFS_CACHE_DBG(\"CACHE_WR_WRITE: storing to cache page \"_SPIPRIi\" for fd \"_SPIPRIfd\":\"_SPIPRIid\", offs \"_SPIPRIi\":\"_SPIPRIi\" len \"_SPIPRIi\"\\n\",\n fd->cache_page->ix, fd->file_nbr, fd->obj_id,\n offset, offset_in_cpage, len);\n spiffs_cache *cache = spiffs_get_cache(fs);\n u8_t *cpage_data = spiffs_get_cache_page(fs, cache, fd->cache_page->ix);\n memcpy(&cpage_data[offset_in_cpage], buf, len);\n fd->cache_page->size = MAX(fd->cache_page->size, offset_in_cpage + len);\n fd->fdoffset += len;\n SPIFFS_UNLOCK(fs);\n return len;\n } else {\n res = spiffs_hydro_write(fs, fd, buf, offset, len);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n fd->fdoffset += len;\n SPIFFS_UNLOCK(fs);\n return res;\n }\n } else {\n // big write, no need to cache it - but first check if there is a cached write already\n if (fd->cache_page) {\n // write back cache first\n SPIFFS_CACHE_DBG(\"CACHE_WR_DUMP: dumping cache page \"_SPIPRIi\" for fd \"_SPIPRIfd\":\"_SPIPRIid\", big write, offs:\"_SPIPRIi\" size:\"_SPIPRIi\"\\n\",\n fd->cache_page->ix, fd->file_nbr, fd->obj_id, fd->cache_page->offset, fd->cache_page->size);\n res = spiffs_hydro_write(fs, fd,\n spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),\n fd->cache_page->offset, fd->cache_page->size);\n spiffs_cache_fd_release(fs, fd->cache_page);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n // data written below\n }\n }\n }\n#endif\n\n res = spiffs_hydro_write(fs, fd, buf, offset, len);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n fd->fdoffset += len;\n\n SPIFFS_UNLOCK(fs);\n\n return res;\n#endif // SPIFFS_READ_ONLY\n}\n\ns32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \" \"_SPIPRIi \" \"_SPIPRIi \"\\n\", __func__, fh, offs, whence);\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n spiffs_fd *fd;\n s32_t res;\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n#if SPIFFS_CACHE_WR\n spiffs_fflush_cache(fs, fh);\n#endif\n\n s32_t fileSize = fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size;\n\n switch (whence) {\n case SPIFFS_SEEK_CUR:\n offs = fd->fdoffset+offs;\n break;\n case SPIFFS_SEEK_END:\n offs = fileSize + offs;\n break;\n }\n\n if (offs > fileSize) {\n fd->fdoffset = fileSize;\n res = SPIFFS_ERR_END_OF_OBJECT;\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n spiffs_span_ix data_spix = (offs > 0 ? (offs-1) : 0) / SPIFFS_DATA_PAGE_SIZE(fs);\n spiffs_span_ix objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);\n if (fd->cursor_objix_spix != objix_spix) {\n spiffs_page_ix pix;\n res = spiffs_obj_lu_find_id_and_span(\n fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, objix_spix, 0, &pix);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n fd->cursor_objix_spix = objix_spix;\n fd->cursor_objix_pix = pix;\n }\n fd->fdoffset = offs;\n\n SPIFFS_UNLOCK(fs);\n\n return offs;\n}\n\ns32_t SPIFFS_remove(spiffs *fs, const char *path) {\n SPIFFS_API_DBG(\"%s '%s'\\n\", __func__, path);\n#if SPIFFS_READ_ONLY\n (void)fs; (void)path;\n return SPIFFS_ERR_RO_NOT_IMPL;\n#else\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {\n SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);\n }\n SPIFFS_LOCK(fs);\n\n spiffs_fd *fd;\n spiffs_page_ix pix;\n s32_t res;\n\n res = spiffs_fd_find_new(fs, &fd, 0);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix);\n if (res != SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_object_open_by_page(fs, pix, fd, 0,0);\n if (res != SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_object_truncate(fd, 0, 1);\n if (res != SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n SPIFFS_UNLOCK(fs);\n return 0;\n#endif // SPIFFS_READ_ONLY\n}\n\ns32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \"\\n\", __func__, fh);\n#if SPIFFS_READ_ONLY\n (void)fs; (void)fh;\n return SPIFFS_ERR_RO_NOT_IMPL;\n#else\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n spiffs_fd *fd;\n s32_t res;\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n if ((fd->flags & SPIFFS_O_WRONLY) == 0) {\n res = SPIFFS_ERR_NOT_WRITABLE;\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n\n#if SPIFFS_CACHE_WR\n spiffs_cache_fd_release(fs, fd->cache_page);\n#endif\n\n res = spiffs_object_truncate(fd, 0, 1);\n\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n SPIFFS_UNLOCK(fs);\n\n return 0;\n#endif // SPIFFS_READ_ONLY\n}\n\nstatic s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spiffs_stat *s) {\n (void)fh;\n spiffs_page_object_ix_header objix_hdr;\n spiffs_obj_id obj_id;\n s32_t res =_spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ, fh,\n SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);\n SPIFFS_API_CHECK_RES(fs, res);\n\n u32_t obj_id_addr = SPIFFS_BLOCK_TO_PADDR(fs, SPIFFS_BLOCK_FOR_PAGE(fs , pix)) +\n SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, pix) * sizeof(spiffs_obj_id);\n res =_spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, fh,\n obj_id_addr, sizeof(spiffs_obj_id), (u8_t *)&obj_id);\n SPIFFS_API_CHECK_RES(fs, res);\n\n s->obj_id = obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;\n s->type = objix_hdr.type;\n s->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;\n s->pix = pix;\n strncpy((char *)s->name, (char *)objix_hdr.name, SPIFFS_OBJ_NAME_LEN);\n#if SPIFFS_OBJ_META_LEN\n memcpy(s->meta, objix_hdr.meta, SPIFFS_OBJ_META_LEN);\n#endif\n\n return res;\n}\n\ns32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s) {\n SPIFFS_API_DBG(\"%s '%s'\\n\", __func__, path);\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {\n SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);\n }\n SPIFFS_LOCK(fs);\n\n s32_t res;\n spiffs_page_ix pix;\n\n res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_stat_pix(fs, pix, 0, s);\n\n SPIFFS_UNLOCK(fs);\n\n return res;\n}\n\ns32_t SPIFFS_fstat(spiffs *fs, spiffs_file fh, spiffs_stat *s) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \"\\n\", __func__, fh);\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n spiffs_fd *fd;\n s32_t res;\n\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n#if SPIFFS_CACHE_WR\n spiffs_fflush_cache(fs, fh);\n#endif\n\n res = spiffs_stat_pix(fs, fd->objix_hdr_pix, fh, s);\n\n SPIFFS_UNLOCK(fs);\n\n return res;\n}\n\n// Checks if there are any cached writes for the object id associated with\n// given filehandle. If so, these writes are flushed.\n#if SPIFFS_CACHE == 1\nstatic s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh) {\n (void)fs;\n (void)fh;\n s32_t res = SPIFFS_OK;\n#if !SPIFFS_READ_ONLY && SPIFFS_CACHE_WR\n\n spiffs_fd *fd;\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES(fs, res);\n\n if ((fd->flags & SPIFFS_O_DIRECT) == 0) {\n if (fd->cache_page == 0) {\n // see if object id is associated with cache already\n fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);\n }\n if (fd->cache_page) {\n SPIFFS_CACHE_DBG(\"CACHE_WR_DUMP: dumping cache page \"_SPIPRIi\" for fd \"_SPIPRIfd\":\"_SPIPRIid\", flush, offs:\"_SPIPRIi\" size:\"_SPIPRIi\"\\n\",\n fd->cache_page->ix, fd->file_nbr, fd->obj_id, fd->cache_page->offset, fd->cache_page->size);\n res = spiffs_hydro_write(fs, fd,\n spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),\n fd->cache_page->offset, fd->cache_page->size);\n if (res < SPIFFS_OK) {\n fs->err_code = res;\n }\n spiffs_cache_fd_release(fs, fd->cache_page);\n }\n }\n#endif\n\n return res;\n}\n#endif\n\ns32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \"\\n\", __func__, fh);\n (void)fh;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n s32_t res = SPIFFS_OK;\n#if !SPIFFS_READ_ONLY && SPIFFS_CACHE_WR\n SPIFFS_LOCK(fs);\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n res = spiffs_fflush_cache(fs, fh);\n SPIFFS_API_CHECK_RES_UNLOCK(fs,res);\n SPIFFS_UNLOCK(fs);\n#endif\n\n return res;\n}\n\ns32_t SPIFFS_close(spiffs *fs, spiffs_file fh) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \"\\n\", __func__, fh);\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n\n s32_t res = SPIFFS_OK;\n SPIFFS_LOCK(fs);\n\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n#if SPIFFS_CACHE\n res = spiffs_fflush_cache(fs, fh);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n#endif\n res = spiffs_fd_return(fs, fh);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n SPIFFS_UNLOCK(fs);\n\n return res;\n}\n\ns32_t SPIFFS_rename(spiffs *fs, const char *old_path, const char *new_path) {\n SPIFFS_API_DBG(\"%s %s %s\\n\", __func__, old_path, new_path);\n#if SPIFFS_READ_ONLY\n (void)fs; (void)old_path; (void)new_path;\n return SPIFFS_ERR_RO_NOT_IMPL;\n#else\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n if (strlen(new_path) > SPIFFS_OBJ_NAME_LEN - 1 ||\n strlen(old_path) > SPIFFS_OBJ_NAME_LEN - 1) {\n SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);\n }\n SPIFFS_LOCK(fs);\n\n spiffs_page_ix pix_old, pix_dummy;\n spiffs_fd *fd;\n\n s32_t res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)old_path, &pix_old);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)new_path, &pix_dummy);\n if (res == SPIFFS_ERR_NOT_FOUND) {\n res = SPIFFS_OK;\n } else if (res == SPIFFS_OK) {\n res = SPIFFS_ERR_CONFLICTING_NAME;\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_fd_find_new(fs, &fd, 0);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_object_open_by_page(fs, pix_old, fd, 0, 0);\n if (res != SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, (const u8_t*)new_path,\n 0, 0, &pix_dummy);\n#if SPIFFS_TEMPORAL_FD_CACHE\n if (res == SPIFFS_OK) {\n spiffs_fd_temporal_cache_rehash(fs, old_path, new_path);\n }\n#endif\n\n spiffs_fd_return(fs, fd->file_nbr);\n\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n SPIFFS_UNLOCK(fs);\n\n return res;\n#endif // SPIFFS_READ_ONLY\n}\n\n#if SPIFFS_OBJ_META_LEN\ns32_t SPIFFS_update_meta(spiffs *fs, const char *name, const void *meta) {\n#if SPIFFS_READ_ONLY\n (void)fs; (void)name; (void)meta;\n return SPIFFS_ERR_RO_NOT_IMPL;\n#else\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n spiffs_page_ix pix, pix_dummy;\n spiffs_fd *fd;\n\n s32_t res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)name, &pix);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_fd_find_new(fs, &fd, 0);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_object_open_by_page(fs, pix, fd, 0, 0);\n if (res != SPIFFS_OK) {\n spiffs_fd_return(fs, fd->file_nbr);\n }\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, 0, meta,\n 0, &pix_dummy);\n\n spiffs_fd_return(fs, fd->file_nbr);\n\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n SPIFFS_UNLOCK(fs);\n\n return res;\n#endif // SPIFFS_READ_ONLY\n}\n\ns32_t SPIFFS_fupdate_meta(spiffs *fs, spiffs_file fh, const void *meta) {\n#if SPIFFS_READ_ONLY\n (void)fs; (void)fh; (void)meta;\n return SPIFFS_ERR_RO_NOT_IMPL;\n#else\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n s32_t res;\n spiffs_fd *fd;\n spiffs_page_ix pix_dummy;\n\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n if ((fd->flags & SPIFFS_O_WRONLY) == 0) {\n res = SPIFFS_ERR_NOT_WRITABLE;\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n\n res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, 0, meta,\n 0, &pix_dummy);\n\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n SPIFFS_UNLOCK(fs);\n\n return res;\n#endif // SPIFFS_READ_ONLY\n}\n#endif // SPIFFS_OBJ_META_LEN\n\nspiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d) {\n SPIFFS_API_DBG(\"%s\\n\", __func__);\n (void)name;\n\n if (!SPIFFS_CHECK_CFG((fs))) {\n (fs)->err_code = SPIFFS_ERR_NOT_CONFIGURED;\n return 0;\n }\n\n if (!SPIFFS_CHECK_MOUNT(fs)) {\n fs->err_code = SPIFFS_ERR_NOT_MOUNTED;\n return 0;\n }\n\n d->fs = fs;\n d->block = 0;\n d->entry = 0;\n return d;\n}\n\nstatic s32_t spiffs_read_dir_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_const_p;\n s32_t res;\n spiffs_page_object_ix_header objix_hdr;\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\n spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);\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 if (res != SPIFFS_OK) return res;\n if ((obj_id & SPIFFS_OBJ_ID_IX_FLAG) &&\n 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 struct spiffs_dirent *e = (struct spiffs_dirent*)user_var_p;\n e->obj_id = obj_id;\n strcpy((char *)e->name, (char *)objix_hdr.name);\n e->type = objix_hdr.type;\n e->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;\n e->pix = pix;\n#if SPIFFS_OBJ_META_LEN\n memcpy(e->meta, objix_hdr.meta, SPIFFS_OBJ_META_LEN);\n#endif\n return SPIFFS_OK;\n }\n return SPIFFS_VIS_COUNTINUE;\n}\n\nstruct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e) {\n SPIFFS_API_DBG(\"%s\\n\", __func__);\n if (!SPIFFS_CHECK_MOUNT(d->fs)) {\n d->fs->err_code = SPIFFS_ERR_NOT_MOUNTED;\n return 0;\n }\n SPIFFS_LOCK(d->fs);\n\n spiffs_block_ix bix;\n int entry;\n s32_t res;\n struct spiffs_dirent *ret = 0;\n\n res = spiffs_obj_lu_find_entry_visitor(d->fs,\n d->block,\n d->entry,\n SPIFFS_VIS_NO_WRAP,\n 0,\n spiffs_read_dir_v,\n 0,\n e,\n &bix,\n &entry);\n if (res == SPIFFS_OK) {\n d->block = bix;\n d->entry = entry + 1;\n e->obj_id &= ~SPIFFS_OBJ_ID_IX_FLAG;\n ret = e;\n } else {\n d->fs->err_code = res;\n }\n SPIFFS_UNLOCK(d->fs);\n return ret;\n}\n\ns32_t SPIFFS_closedir(spiffs_DIR *d) {\n SPIFFS_API_DBG(\"%s\\n\", __func__);\n SPIFFS_API_CHECK_CFG(d->fs);\n SPIFFS_API_CHECK_MOUNT(d->fs);\n return 0;\n}\n\ns32_t SPIFFS_check(spiffs *fs) {\n SPIFFS_API_DBG(\"%s\\n\", __func__);\n#if SPIFFS_READ_ONLY\n (void)fs;\n return SPIFFS_ERR_RO_NOT_IMPL;\n#else\n s32_t res;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n res = spiffs_lookup_consistency_check(fs, 0);\n\n res = spiffs_object_index_consistency_check(fs);\n\n res = spiffs_page_consistency_check(fs);\n\n res = spiffs_obj_lu_scan(fs);\n\n SPIFFS_UNLOCK(fs);\n return res;\n#endif // SPIFFS_READ_ONLY\n}\n\ns32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used) {\n SPIFFS_API_DBG(\"%s\\n\", __func__);\n s32_t res = SPIFFS_OK;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n u32_t pages_per_block = SPIFFS_PAGES_PER_BLOCK(fs);\n u32_t blocks = fs->block_count;\n u32_t obj_lu_pages = SPIFFS_OBJ_LOOKUP_PAGES(fs);\n u32_t data_page_size = SPIFFS_DATA_PAGE_SIZE(fs);\n u32_t total_data_pages = (blocks - 2) * (pages_per_block - obj_lu_pages) + 1; // -2 for spare blocks, +1 for emergency page\n\n if (total) {\n *total = total_data_pages * data_page_size;\n }\n\n if (used) {\n *used = fs->stats_p_allocated * data_page_size;\n }\n\n SPIFFS_UNLOCK(fs);\n return res;\n}\n\ns32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIi \"\\n\", __func__, max_free_pages);\n#if SPIFFS_READ_ONLY\n (void)fs; (void)max_free_pages;\n return SPIFFS_ERR_RO_NOT_IMPL;\n#else\n s32_t res;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n res = spiffs_gc_quick(fs, max_free_pages);\n\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n SPIFFS_UNLOCK(fs);\n return 0;\n#endif // SPIFFS_READ_ONLY\n}\n\n\ns32_t SPIFFS_gc(spiffs *fs, u32_t size) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIi \"\\n\", __func__, size);\n#if SPIFFS_READ_ONLY\n (void)fs; (void)size;\n return SPIFFS_ERR_RO_NOT_IMPL;\n#else\n s32_t res;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n res = spiffs_gc_check(fs, size);\n\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n SPIFFS_UNLOCK(fs);\n return 0;\n#endif // SPIFFS_READ_ONLY\n}\n\ns32_t SPIFFS_eof(spiffs *fs, spiffs_file fh) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \"\\n\", __func__, fh);\n s32_t res;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n\n spiffs_fd *fd;\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n#if SPIFFS_CACHE_WR\n res = spiffs_fflush_cache(fs, fh);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n#endif\n\n res = (fd->fdoffset >= (fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size));\n\n SPIFFS_UNLOCK(fs);\n return res;\n}\n\ns32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \"\\n\", __func__, fh);\n s32_t res;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n\n spiffs_fd *fd;\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n#if SPIFFS_CACHE_WR\n res = spiffs_fflush_cache(fs, fh);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n#endif\n\n res = fd->fdoffset;\n\n SPIFFS_UNLOCK(fs);\n return res;\n}\n\ns32_t SPIFFS_set_file_callback_func(spiffs *fs, spiffs_file_callback cb_func) {\n SPIFFS_API_DBG(\"%s\\n\", __func__);\n SPIFFS_LOCK(fs);\n fs->file_cb_f = cb_func;\n SPIFFS_UNLOCK(fs);\n return 0;\n}\n\n#if SPIFFS_IX_MAP\n\ns32_t SPIFFS_ix_map(spiffs *fs, spiffs_file fh, spiffs_ix_map *map,\n u32_t offset, u32_t len, spiffs_page_ix *map_buf) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \" \"_SPIPRIi \" \"_SPIPRIi \"\\n\", __func__, fh, offset, len);\n s32_t res;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n\n spiffs_fd *fd;\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n if (fd->ix_map) {\n SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_IX_MAP_MAPPED);\n }\n\n map->map_buf = map_buf;\n map->offset = offset;\n // nb: spix range includes last\n map->start_spix = offset / SPIFFS_DATA_PAGE_SIZE(fs);\n map->end_spix = (offset + len) / SPIFFS_DATA_PAGE_SIZE(fs);\n memset(map_buf, 0, sizeof(spiffs_page_ix) * (map->end_spix - map->start_spix + 1));\n fd->ix_map = map;\n\n // scan for pixes\n res = spiffs_populate_ix_map(fs, fd, 0, map->end_spix - map->start_spix + 1);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n SPIFFS_UNLOCK(fs);\n return res;\n}\n\ns32_t SPIFFS_ix_unmap(spiffs *fs, spiffs_file fh) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \"\\n\", __func__, fh);\n s32_t res;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n\n spiffs_fd *fd;\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n if (fd->ix_map == 0) {\n SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_IX_MAP_UNMAPPED);\n }\n\n fd->ix_map = 0;\n\n SPIFFS_UNLOCK(fs);\n return res;\n}\n\ns32_t SPIFFS_ix_remap(spiffs *fs, spiffs_file fh, u32_t offset) {\n SPIFFS_API_DBG(\"%s \"_SPIPRIfd \" \"_SPIPRIi \"\\n\", __func__, fh, offset);\n s32_t res = SPIFFS_OK;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\n fh = SPIFFS_FH_UNOFFS(fs, fh);\n\n spiffs_fd *fd;\n res = spiffs_fd_get(fs, fh, &fd);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n\n if (fd->ix_map == 0) {\n SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_IX_MAP_UNMAPPED);\n }\n\n spiffs_ix_map *map = fd->ix_map;\n\n s32_t spix_diff = offset / SPIFFS_DATA_PAGE_SIZE(fs) - map->start_spix;\n map->offset = offset;\n\n // move existing pixes if within map offs\n if (spix_diff != 0) {\n // move vector\n int i;\n const s32_t vec_len = map->end_spix - map->start_spix + 1; // spix range includes last\n map->start_spix += spix_diff;\n map->end_spix += spix_diff;\n if (spix_diff >= vec_len) {\n // moving beyond range\n memset(&map->map_buf, 0, vec_len * sizeof(spiffs_page_ix));\n // populate_ix_map is inclusive\n res = spiffs_populate_ix_map(fs, fd, 0, vec_len-1);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n } else if (spix_diff > 0) {\n // diff positive\n for (i = 0; i < vec_len - spix_diff; i++) {\n map->map_buf[i] = map->map_buf[i + spix_diff];\n }\n // memset is non-inclusive\n memset(&map->map_buf[vec_len - spix_diff], 0, spix_diff * sizeof(spiffs_page_ix));\n // populate_ix_map is inclusive\n res = spiffs_populate_ix_map(fs, fd, vec_len - spix_diff, vec_len-1);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n } else {\n // diff negative\n for (i = vec_len - 1; i >= -spix_diff; i--) {\n map->map_buf[i] = map->map_buf[i + spix_diff];\n }\n // memset is non-inclusive\n memset(&map->map_buf[0], 0, -spix_diff * sizeof(spiffs_page_ix));\n // populate_ix_map is inclusive\n res = spiffs_populate_ix_map(fs, fd, 0, -spix_diff - 1);\n SPIFFS_API_CHECK_RES_UNLOCK(fs, res);\n }\n\n }\n\n SPIFFS_UNLOCK(fs);\n return res;\n}\n\ns32_t SPIFFS_bytes_to_ix_map_entries(spiffs *fs, u32_t bytes) {\n SPIFFS_API_CHECK_CFG(fs);\n // always add one extra page, the offset might change to the middle of a page\n return (bytes + SPIFFS_DATA_PAGE_SIZE(fs) ) / SPIFFS_DATA_PAGE_SIZE(fs);\n}\n\ns32_t SPIFFS_ix_map_entries_to_bytes(spiffs *fs, u32_t map_page_ix_entries) {\n SPIFFS_API_CHECK_CFG(fs);\n return map_page_ix_entries * SPIFFS_DATA_PAGE_SIZE(fs);\n}\n\n#endif // SPIFFS_IX_MAP\n\n#if SPIFFS_TEST_VISUALISATION\ns32_t SPIFFS_vis(spiffs *fs) {\n s32_t res = SPIFFS_OK;\n SPIFFS_API_CHECK_CFG(fs);\n SPIFFS_API_CHECK_MOUNT(fs);\n SPIFFS_LOCK(fs);\n\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 spiffs_block_ix bix = 0;\n\n while (bix < fs->block_count) {\n // check each object lookup page\n int obj_lookup_page = 0;\n int cur_entry = 0;\n\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 (cur_entry == 0) {\n spiffs_printf(_SPIPRIbl\" \", bix);\n } else if ((cur_entry & 0x3f) == 0) {\n spiffs_printf(\" \");\n }\n if (obj_id == SPIFFS_OBJ_ID_FREE) {\n spiffs_printf(SPIFFS_TEST_VIS_FREE_STR);\n } else if (obj_id == SPIFFS_OBJ_ID_DELETED) {\n spiffs_printf(SPIFFS_TEST_VIS_DELE_STR);\n } else if (obj_id & SPIFFS_OBJ_ID_IX_FLAG){\n spiffs_printf(SPIFFS_TEST_VIS_INDX_STR(obj_id));\n } else {\n spiffs_printf(SPIFFS_TEST_VIS_DATA_STR(obj_id));\n }\n cur_entry++;\n if ((cur_entry & 0x3f) == 0) {\n spiffs_printf(\"\\n\");\n }\n } // per entry\n obj_lookup_page++;\n } // per object lookup page\n\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, bix),\n sizeof(spiffs_obj_id), (u8_t *)&erase_count);\n SPIFFS_CHECK_RES(res);\n\n if (erase_count != (spiffs_obj_id)-1) {\n spiffs_printf(\"\\tera_cnt: \"_SPIPRIi\"\\n\", erase_count);\n } else {\n spiffs_printf(\"\\tera_cnt: N/A\\n\");\n }\n\n bix++;\n } // per block\n\n spiffs_printf(\"era_cnt_max: \"_SPIPRIi\"\\n\", fs->max_erase_count);\n spiffs_printf(\"last_errno: \"_SPIPRIi\"\\n\", fs->err_code);\n spiffs_printf(\"blocks: \"_SPIPRIi\"\\n\", fs->block_count);\n spiffs_printf(\"free_blocks: \"_SPIPRIi\"\\n\", fs->free_blocks);\n spiffs_printf(\"page_alloc: \"_SPIPRIi\"\\n\", fs->stats_p_allocated);\n spiffs_printf(\"page_delet: \"_SPIPRIi\"\\n\", fs->stats_p_deleted);\n SPIFFS_UNLOCK(fs);\n u32_t total, used;\n SPIFFS_info(fs, &total, &used);\n spiffs_printf(\"used: \"_SPIPRIi\" of \"_SPIPRIi\"\\n\", used, total);\n return res;\n}\n#endif\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 \"_SPIPRIbl\"\\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:\"_SPIPRIi\" needed:\"_SPIPRIi\" free:\"_SPIPRIi\" dele:\"_SPIPRIi\"\\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:\"_SPIPRIi\" needed:\"_SPIPRIi\" free:\"_SPIPRIi\" dele:\"_SPIPRIi\"\\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 #\"_SPIPRIi\": run gc free_blocks:\"_SPIPRIi\" pfree:\"_SPIPRIi\" pallo:\"_SPIPRIi\" pdele:\"_SPIPRIi\" [\"_SPIPRIi\"] len:\"_SPIPRIi\" of \"_SPIPRIi\"\\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, (u32_t)(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 //SPIFFS_GC_DBG(\"gcing: cleaning block \"_SPIPRIi\"\\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 \"_SPIPRIi\", result \"_SPIPRIi\"\\n\", cand, res);\n } else {\n SPIFFS_GC_DBG(\"gc_check: cleaning block \"_SPIPRIi\", result \"_SPIPRIi\"\\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, \"_SPIPRIi\" dirty, blocks \"_SPIPRIi\" free, \"_SPIPRIi\" pages free, \"_SPIPRIi\" tries, res \"_SPIPRIi\"\\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:\"_SPIPRIi\" pdele:\"_SPIPRIi\"\\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 cand_scores = (s32_t*)(((intptr_t)cand_scores + sizeof(intptr_t) - 1) & ~(sizeof(intptr_t) - 1));\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:\"_SPIPRIbl\" del:\"_SPIPRIi\" use:\"_SPIPRIi\" score:\"_SPIPRIi\"\\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 spiffs_page_ix cur_data_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 const int entries_per_page = (SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id));\n // this is the global localizer being pushed and popped\n int cur_entry = 0;\n spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work;\n spiffs_gc gc; // our stack frame/state\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 \"_SPIPRIbl\"\\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 \"_SPIPRIbl\"\\n\", fs->free_cursor_block_ix);\n }\n\n while (res == SPIFFS_OK && gc.state != FINISHED) {\n SPIFFS_GC_DBG(\"gc_clean: state = \"_SPIPRIi\" entry:\"_SPIPRIi\"\\n\", gc.state, cur_entry);\n gc.obj_id_found = 0; // reset (to no found data page)\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 object lookup 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 // find a data page\n if (obj_id != SPIFFS_OBJ_ID_DELETED && obj_id != SPIFFS_OBJ_ID_FREE &&\n ((obj_id & SPIFFS_OBJ_ID_IX_FLAG) == 0)) {\n // found a data page, stop scanning and handle in switch case below\n SPIFFS_GC_DBG(\"gc_clean: FIND_DATA state:\"_SPIPRIi\" - found obj id \"_SPIPRIid\"\\n\", gc.state, obj_id);\n gc.obj_id_found = 1;\n gc.cur_obj_id = obj_id;\n gc.cur_data_pix = cur_pix;\n scan = 0;\n }\n break;\n case MOVE_OBJ_DATA:\n // evacuate found data pages for corresponding object index we have in memory,\n // update memory representation\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 \"_SPIPRIid\":\"_SPIPRIsp\" @ \"_SPIPRIpg\"\\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 \"_SPIPRIid\":\"_SPIPRIsp\" page \"_SPIPRIpg\" to \"_SPIPRIpg\"\\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 // might seem unnecessary as we will erase this block, but\n // we might get aborted\n SPIFFS_GC_DBG(\"gc_clean: MOVE_DATA wipe objix \"_SPIPRIid\":\"_SPIPRIsp\" page \"_SPIPRIpg\"\\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 \"_SPIPRIpg\" to objix_hdr entry \"_SPIPRIsp\" in mem\\n\", new_data_pix, (spiffs_span_ix)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 \"_SPIPRIpg\" to objix entry \"_SPIPRIsp\" in mem\\n\", new_data_pix, (spiffs_span_ix)SPIFFS_OBJ_IX_ENTRY(fs, p_hdr.span_ix));\n }\n }\n }\n break;\n case MOVE_OBJ_IX:\n // find and evacuate object index pages\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 \"_SPIPRIid\":\"_SPIPRIsp\" page \"_SPIPRIpg\" to \"_SPIPRIpg\"\\n\", obj_id, p_hdr.span_ix, cur_pix, new_pix);\n SPIFFS_CHECK_RES(res);\n spiffs_cb_object_event(fs, (spiffs_page_object_ix *)&p_hdr,\n SPIFFS_EV_IX_MOV, 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 // might seem unnecessary as we will erase this block, but\n // we might get aborted\n SPIFFS_GC_DBG(\"gc_clean: MOVE_OBJIX wipe objix \"_SPIPRIid\":\"_SPIPRIsp\" page \"_SPIPRIpg\"\\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, (spiffs_page_object_ix *)0,\n 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 } // switch gc state\n cur_entry++;\n } // per entry\n obj_lookup_page++; // no need to check scan variable here, obj_lookup_page is set in start of loop\n } // per object lookup page\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 // handle found data page -\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; // push cursor\n cur_entry = 0; // restart scan from start\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:\"_SPIPRIsp\"\\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 if (res == SPIFFS_ERR_NOT_FOUND) {\n // on borked systems we might get an ERR_NOT_FOUND here -\n // this is handled by simply deleting the page as it is not referenced\n // from anywhere\n SPIFFS_GC_DBG(\"gc_clean: FIND_OBJ_DATA objix not found! Wipe page \"_SPIPRIpg\"\\n\", gc.cur_data_pix);\n res = spiffs_page_delete(fs, gc.cur_data_pix);\n SPIFFS_CHECK_RES(res);\n // then we restore states and continue scanning for data pages\n cur_entry = gc.stored_scan_entry_index; // pop cursor\n gc.state = FIND_OBJ_DATA;\n break; // done\n }\n SPIFFS_CHECK_RES(res);\n SPIFFS_GC_DBG(\"gc_clean: FIND_DATA found object index at page \"_SPIPRIpg\"\\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 // cannot allow a gc if the presumed index in fact is no index, a\n // check must run or lot of data may be lost\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 // no more data pages found, passed thru all block, start evacuating object indices\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 residing in memory now that all\n // data pages belonging to this object index and residing in the block\n // we want to evacuate\n spiffs_page_ix new_objix_pix;\n gc.state = FIND_OBJ_DATA;\n cur_entry = gc.stored_scan_entry_index; // pop cursor\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, 0, &new_objix_pix);\n SPIFFS_GC_DBG(\"gc_clean: MOVE_DATA store modified objix_hdr page, \"_SPIPRIpg\":\"_SPIPRIsp\"\\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, \"_SPIPRIpg\":\"_SPIPRIsp\"\\n\", new_objix_pix, objix->p_hdr.span_ix);\n SPIFFS_CHECK_RES(res);\n spiffs_cb_object_event(fs, (spiffs_page_object_ix *)fs->work,\n 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 // scanned thru all block, no more object indices found - our work here is done\n gc.state = FINISHED;\n break;\n default:\n cur_entry = 0;\n break;\n } // switch gc.state\n SPIFFS_GC_DBG(\"gc_clean: state-> \"_SPIPRIi\"\\n\", gc.state);\n } // while state != FINISHED\n\n\n return res;\n}\n\n#endif // !SPIFFS_READ_ONLY\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 \"_SPIPRIi\" for \"_SPIPRIpg\"\\n\", i, pix);\n cp->last_access = cache->last_access;\n return cp;\n }\n }\n //SPIFFS_CACHE_DBG(\"CACHE_GET: no cache for \"_SPIPRIpg\"\\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 SPIFFS_CACHE_DBG(\"CACHE_FREE: write cache page \"_SPIPRIi\" pix \"_SPIPRIpg\"\\n\", ix, cp->pix);\n res = SPIFFS_HAL_WRITE(fs, SPIFFS_PAGE_TO_PADDR(fs, cp->pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), mem);\n }\n\n\n if (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) {\n SPIFFS_CACHE_DBG(\"CACHE_FREE: free cache page \"_SPIPRIi\" objid \"_SPIPRIid\"\\n\", ix, cp->obj_id);\n } else {\n SPIFFS_CACHE_DBG(\"CACHE_FREE: free cache page \"_SPIPRIi\" pix \"_SPIPRIpg\"\\n\", ix, cp->pix);\n }\n cache->cpage_use_map &= ~(1 << ix);\n cp->flags = 0;\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 \"_SPIPRIi\"\\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 // we've already got one, you see\n#if SPIFFS_CACHE_STATS\n fs->cache_hits++;\n#endif\n cp->last_access = cache->last_access;\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 } 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 // this operation will always free one cache page (unless all already free),\n // the result code stems from the write operation of the possibly freed cache page\n res = spiffs_cache_page_remove_oldest(fs, SPIFFS_CACHE_FLAG_TYPE_WR, 0);\n\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 SPIFFS_CACHE_DBG(\"CACHE_ALLO: allocated cache page \"_SPIPRIi\" for pix \"_SPIPRIpg \"\\n\", cp->ix, cp->pix);\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 // honor read failure before possible write failure (bad idea?)\n res = res2;\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 } else {\n // this will never happen, last resort for sake of symmetry\n s32_t res2 = SPIFFS_HAL_READ(fs, addr, len, dst);\n if (res2 != SPIFFS_OK) {\n // honor read failure before possible write failure (bad idea?)\n res = res2;\n }\n }\n }\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 SPIFFS_CACHE_DBG(\"CACHE_ALLO: allocated cache page \"_SPIPRIi\" for fd \"_SPIPRIfd \":\"_SPIPRIid \"\\n\", cp->ix, fd->file_nbr, fd->obj_id);\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 \"_SPIPRIpg\" 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 \"_SPIPRIpg\" to new page \"_SPIPRIpg\"\\n\", cur_pix, new_pix);\n SPIFFS_CHECK_RES(res);\n *reload_lu = 1;\n SPIFFS_CHECK_DBG(\"LU: FIXUP: \"_SPIPRIpg\" rewritten to \"_SPIPRIpg\", affected objix_pix \"_SPIPRIpg\"\\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 \"_SPIPRIi\", 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 \"_SPIPRIpg\" to new page \"_SPIPRIpg\"\\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 \"_SPIPRIpg\" differ in obj_id lu:\"_SPIPRIid\" ph:\"_SPIPRIid\"\\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 \"_SPIPRIi\", 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 \"_SPIPRIpg\" as \"_SPIPRIid\" to pix \"_SPIPRIpg\"\\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 \"_SPIPRIpg\" as \"_SPIPRIid\"\\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: \"_SPIPRIpg\" 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 \"_SPIPRIpg\" 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 \"_SPIPRIpg\" 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 \"_SPIPRIpg\"\\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 \"_SPIPRIpg\", block \"_SPIPRIbl\" of pix \"_SPIPRIpg\", block \"_SPIPRIbl\"\\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 \"_SPIPRIpg\"x bad pix / LU referenced from page \"_SPIPRIpg\"\\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 @ \"_SPIPRIpg\"\\n\", data_pix);\n }\n // remap index\n SPIFFS_CHECK_DBG(\"PA: FIXUP: rewriting index pix \"_SPIPRIpg\"\\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 \"_SPIPRIi\", 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 \"_SPIPRIpg\" has inconsistent page header ix id/span:\"_SPIPRIid\"/\"_SPIPRIsp\", ref id/span:\"_SPIPRIid\"/\"_SPIPRIsp\" flags:\"_SPIPRIfl\"\\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 \"_SPIPRIid\"\\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 \"_SPIPRIpg\", rewrite ix pix \"_SPIPRIpg\" id \"_SPIPRIid\"\\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 \"_SPIPRIi\", 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 \"_SPIPRIpg\" multiple referenced from page \"_SPIPRIpg\"\\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 \"_SPIPRIid\" and page \"_SPIPRIpg\"\\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 \"_SPIPRIpg\" 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: \"_SPIPRIpg\", rewrite to this \"_SPIPRIpg\"\\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: \"_SPIPRIpg\", delete this \"_SPIPRIpg\"\\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: \"_SPIPRIpg\" %s%s%s%s, rewrite this \"_SPIPRIpg\"\\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 \"_SPIPRIpg\"\\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 \"_SPIPRIid\" data spix \"_SPIPRIsp\" to point to this pix: \"_SPIPRIpg\"\\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 \"_SPIPRIi\", 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 \"_SPIPRIpg\"\\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 \"_SPIPRIpg\" 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 \"_SPIPRIpg\" 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 \"_SPIPRIpg\" 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 \"_SPIPRIpg\" 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 \"_SPIPRIpg\", restart \"_SPIPRIi\"\\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 \"_SPIPRIpg\", obj id:\"_SPIPRIid\" spix:\"_SPIPRIsp\" 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 \"_SPIPRIpg\", obj id:\"_SPIPRIid\" spix:\"_SPIPRIsp\" 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, 0, &obj_id_log_ix,\n 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",
"observe.c": "#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include \"spiffs.h\"\n#include \"spiffs_nucleus.h\"\n\n#define FLASH_BYTES (64 * 1024)\n#define PAGE_BYTES 256\n#define BLOCK_BYTES 4096\nstatic unsigned char flash_bytes[FLASH_BYTES];\nstatic spiffs fs;\nstatic spiffs_file handles[96];\nstatic unsigned long reads, writes, erases, physical_errors;\n\n#if SPIFFS_HAL_CALLBACK_EXTRA\n#define HAL_ARGS spiffs *ignored,\n#define HAL_IGNORE (void)ignored\n#else\n#define HAL_ARGS\n#define HAL_IGNORE ((void)0)\n#endif\nstatic s32_t flash_read(HAL_ARGS u32_t addr, u32_t len, u8_t *dst) {\n HAL_IGNORE;\n reads++;\n if (addr > FLASH_BYTES || len > FLASH_BYTES - addr) {\n physical_errors++;\n return -10100;\n }\n memcpy(dst, flash_bytes + addr, len);\n return SPIFFS_OK;\n}\nstatic s32_t flash_write(HAL_ARGS u32_t addr, u32_t len, u8_t *src) {\n HAL_IGNORE;\n writes++;\n if (addr > FLASH_BYTES || len > FLASH_BYTES - addr) {\n physical_errors++;\n return -10100;\n }\n for (u32_t n = 0; n < len; n++) flash_bytes[addr+n] &= src[n];\n return SPIFFS_OK;\n}\nstatic s32_t flash_erase(HAL_ARGS u32_t addr, u32_t len) {\n HAL_IGNORE;\n erases++;\n if (addr > FLASH_BYTES || len > FLASH_BYTES - addr ||\n addr % BLOCK_BYTES || len % BLOCK_BYTES) {\n physical_errors++;\n return -10100;\n }\n memset(flash_bytes + addr, 255, len);\n return SPIFFS_OK;\n}\nstatic int nibble(char c) {\n if (c >= '0' && c <= '9') return c - '0';\n if (c >= 'a' && c <= 'f') return c - 'a' + 10;\n return -1;\n}\nint main(void) {\n printf(\"U %u %u %d\\n\", (unsigned)getuid(), (unsigned)getgid(), getgroups(0, 0));\n unsigned fd_number, cache_number;\n if (scanf(\"%u %u\", &fd_number, &cache_number) != 2 ||\n fd_number < 1 || fd_number > 64 || cache_number < 1 || cache_number > 16)\n return 80;\n memset(&fs, 0, sizeof fs);\n memset(flash_bytes, 255, sizeof flash_bytes);\n spiffs_config cfg;\n memset(&cfg, 0, sizeof cfg);\n cfg.hal_read_f = flash_read;\n cfg.hal_write_f = flash_write;\n cfg.hal_erase_f = flash_erase;\n cfg.phys_size = FLASH_BYTES;\n cfg.phys_addr = 0;\n cfg.phys_erase_block = BLOCK_BYTES;\n cfg.log_block_size = BLOCK_BYTES;\n cfg.log_page_size = PAGE_BYTES;\n#if SPIFFS_FILEHDL_OFFSET\n cfg.fh_ix_offset = TEST_SPIFFS_FILEHDL_OFFSET;\n#endif\n void *fd_mem = calloc(fd_number, sizeof(spiffs_fd));\n#if SPIFFS_CACHE\n size_t cache_size = sizeof(spiffs_cache) + cache_number *\n (sizeof(spiffs_cache_page) + PAGE_BYTES);\n void *cache_mem = calloc(1, cache_size);\n#else\n size_t cache_size = 0;\n void *cache_mem = 0;\n#endif\n u8_t work[2 * PAGE_BYTES];\n if (!fd_mem || (cache_size && !cache_mem)) return 81;\n s32_t initial = SPIFFS_mount(&fs, &cfg, work, fd_mem,\n fd_number * sizeof(spiffs_fd), cache_mem, cache_size, 0);\n if (initial == SPIFFS_OK) SPIFFS_unmount(&fs);\n s32_t format = SPIFFS_format(&fs);\n s32_t mount = SPIFFS_mount(&fs, &cfg, work, fd_mem,\n fd_number * sizeof(spiffs_fd), cache_mem, cache_size, 0);\n printf(\"M %d %d %d %u\\n\", (int)initial, (int)format, (int)mount,\n (unsigned)fs.fd_count);\n if (mount != SPIFFS_OK) return 82;\n char op[8], name[100], hex[8193];\n int tag, flags, length, result;\n unsigned char bytes[4096];\n while (scanf(\"%7s\", op) == 1) {\n if (!strcmp(op, \"O\")) {\n if (scanf(\"%d %99s %d\", &tag, name, &flags) != 3 || tag < 0 || tag >= 96) return 83;\n handles[tag] = SPIFFS_open(&fs, name, flags, 0);\n printf(\"O %d %d\\n\", tag, (int)handles[tag]);\n } else if (!strcmp(op, \"W\")) {\n if (scanf(\"%d %8192s\", &tag, hex) != 2 || tag < 0 || tag >= 96) return 84;\n size_t n = strlen(hex);\n if (n % 2 || n > 8192) return 85;\n for (size_t j = 0; j < n; j += 2) {\n int hi = nibble(hex[j]), lo = nibble(hex[j+1]);\n if (hi < 0 || lo < 0) return 86;\n bytes[j/2] = 16 * hi + lo;\n }\n result = SPIFFS_write(&fs, handles[tag], bytes, n / 2);\n printf(\"W %d %d\\n\", tag, result);\n } else if (!strcmp(op, \"R\")) {\n if (scanf(\"%d %d\", &tag, &length) != 2 || tag < 0 || tag >= 96 || length < 0 || length > 4096) return 87;\n result = SPIFFS_read(&fs, handles[tag], bytes, length);\n printf(\"R %d %d \", tag, result);\n for (int j = 0; j < result && j < length; j++) printf(\"%02x\", bytes[j]);\n putchar('\\n');\n } else if (!strcmp(op, \"C\")) {\n if (scanf(\"%d\", &tag) != 1 || tag < 0 || tag >= 96) return 88;\n result = SPIFFS_close(&fs, handles[tag]);\n printf(\"C %d %d\\n\", tag, result);\n } else if (!strcmp(op, \"D\")) {\n if (scanf(\"%99s\", name) != 1) return 89;\n result = SPIFFS_remove(&fs, name);\n printf(\"D %s %d\\n\", name, result);\n } else if (!strcmp(op, \"F\")) {\n if (scanf(\"%d\", &tag) != 1 || tag < 0 || tag >= 96) return 90;\n result = SPIFFS_fremove(&fs, handles[tag]);\n printf(\"F %d %d\\n\", tag, result);\n } else if (!strcmp(op, \"S\")) {\n if (scanf(\"%99s\", name) != 1) return 91;\n spiffs_stat st;\n memset(&st, 0, sizeof st);\n result = SPIFFS_stat(&fs, name, &st);\n printf(\"S %s %d %u\\n\", name, result, (unsigned)st.size);\n } else {\n return 92;\n }\n }\n SPIFFS_unmount(&fs);\n printf(\"P %lu %lu %lu %lu\\n\", reads, writes, erases, physical_errors);\n free(fd_mem);\n free(cache_mem);\n return 0;\n}\n"
}