Skip to content

Commit

Permalink
Merge pull request #10 from pgollor/spiffs-0.3.3
Browse files Browse the repository at this point in the history
update to Spiffs 0.3.3
  • Loading branch information
igrr committed Oct 28, 2015
2 parents a0160df + 6e41f36 commit 0cba6e2
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 104 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ Tool to build and unpack [SPIFFS](https://github.com/pellepl/spiffs) images.

```
./mkspiffs {-c <pack_dir>|-u <dest_dir>|-l|-i} [-b <number>] [-p <number>] [-s
<number>] [--] [--version] [-h] <image_file>
mkspiffs {-c <pack_dir>|-u <dest_dir>|-l|-i} [-d <0-5>] [-b <number>]
[-p <number>] [-s <number>] [--] [--version] [-h]
<image_file>
Where:
Where:
-c <pack_dir>, --create <pack_dir>
(OR required) create spiffs image from a directory
Expand All @@ -26,6 +27,9 @@ Where:
(OR required) visualize spiffs image
-d <0-5>, --debug <0-5>
Debug level. 0 means no debug output.
-b <number>, --block <number>
fs block size, in bytes
Expand Down Expand Up @@ -71,6 +75,7 @@ MIT

## To do

- Error handling
- Determine the image size automatically when opening a file
- Code cleanup
- [ ] Add more denug output and print spiffs debug output
- [ ] Error handling
- [ ] Determine the image size automatically when opening a file
- [ ] Code cleanup
34 changes: 34 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ static s32_t api_spiffs_erase(u32_t addr, u32_t size){
}


int g_debugLevel = 0;


//implementation

int spiffsTryMount(){
Expand Down Expand Up @@ -113,20 +116,40 @@ int addFile(char* name, const char* path) {

spiffs_file dst = SPIFFS_open(&s_fs, name, SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);

// read file size
fseek(src, 0, SEEK_END);
size_t size = ftell(src);
fseek(src, 0, SEEK_SET);

if (g_debugLevel > 0) {
std::cout << "file size: " << size << std::endl;
}

size_t left = size;
uint8_t data_byte;
while (left > 0){
if (1 != fread(&data_byte, 1, 1, src)) {
std::cerr << "fread error!" << std::endl;

fclose(src);
SPIFFS_close(&s_fs, dst);
return 1;
}
int res = SPIFFS_write(&s_fs, dst, &data_byte, 1);
if (res < 0) {
std::cerr << "SPIFFS_write error(" << s_fs.err_code << "): ";

if (s_fs.err_code == SPIFFS_ERR_FULL) {
std::cerr << "File system is full." << std::endl;
} else {
std::cerr << "unknown";
}
std::cerr << std::endl;

if (g_debugLevel > 0) {
std::cout << "data left: " << left << std::endl;
}

fclose(src);
SPIFFS_close(&s_fs, dst);
return 1;
Expand All @@ -136,6 +159,7 @@ int addFile(char* name, const char* path) {

SPIFFS_close(&s_fs, dst);
fclose(src);

return 0;
}

Expand Down Expand Up @@ -191,6 +215,9 @@ int addFiles(const char* dirname, const char* subPath) {
if (addFile((char*)filepath.c_str(), fullpath.c_str()) != 0) {
std::cerr << "error adding file!" << std::endl;
error = true;
if (g_debugLevel > 0) {
std::cout << std::endl;
}
break;
}
} // end while
Expand Down Expand Up @@ -485,15 +512,22 @@ void processArgs(int argc, const char** argv) {
TCLAP::ValueArg<int> imageSizeArg( "s", "size", "fs image size, in bytes", false, 0x10000, "number" );
TCLAP::ValueArg<int> pageSizeArg( "p", "page", "fs page size, in bytes", false, 256, "number" );
TCLAP::ValueArg<int> blockSizeArg( "b", "block", "fs block size, in bytes", false, 4096, "number" );
TCLAP::ValueArg<int> debugArg( "d", "debug", "Debug level. 0 means no debug output.", false, 0, "0-5" );

cmd.add( imageSizeArg );
cmd.add( pageSizeArg );
cmd.add( blockSizeArg );
cmd.add(debugArg);
std::vector<TCLAP::Arg*> args = {&packArg, &unpackArg, &listArg, &visualizeArg};
cmd.xorAdd( args );
cmd.add( outNameArg );
cmd.parse( argc, argv );

if (debugArg.getValue() > 0) {
std::cout << "Debug output enabled" << std::endl;
g_debugLevel = debugArg.getValue();
}

if (packArg.isSet()) {
s_dirName = packArg.getValue();
s_action = ACTION_PACK;
Expand Down
38 changes: 28 additions & 10 deletions spiffs/spiffs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Author: petera
*/



#ifndef SPIFFS_H_
#define SPIFFS_H_
#if defined(__cplusplus)
Expand Down Expand Up @@ -186,6 +184,11 @@ typedef struct {
// logical size of a page, must be at least
// log_block_size / 8
u32_t log_page_size;

#endif
#if SPIFFS_FILEHDL_OFFSET
// an integer offset added to each file handle
u16_t fh_ix_offset;
#endif
} spiffs_config;

Expand Down Expand Up @@ -310,7 +313,7 @@ void SPIFFS_unmount(spiffs *fs);
* @param path the path of the new file
* @param mode ignored, for posix compliance
*/
s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode);
s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode);

/**
* Opens/creates a file.
Expand All @@ -321,7 +324,7 @@ s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode);
* SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT
* @param mode ignored, for posix compliance
*/
spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode mode);
spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode);


/**
Expand Down Expand Up @@ -360,13 +363,14 @@ s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len);
s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len);

/**
* Moves the read/write file offset
* Moves the read/write file offset. Resulting offset is returned or negative if error.
* lseek(fs, fd, 0, SPIFFS_SEEK_CUR) will thus return current offset.
* @param fs the file system struct
* @param fh the filehandle
* @param offs how much/where to move the offset
* @param whence if SPIFFS_SEEK_SET, the file offset shall be set to offset bytes
* if SPIFFS_SEEK_CUR, the file offset shall be set to its current location plus offset
* if SPIFFS_SEEK_END, the file offset shall be set to the size of the file plus offset
* if SPIFFS_SEEK_END, the file offset shall be set to the size of the file plus offse, which should be negative
*/
s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence);

Expand All @@ -375,7 +379,7 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence);
* @param fs the file system struct
* @param path the path of the file to remove
*/
s32_t SPIFFS_remove(spiffs *fs, char *path);
s32_t SPIFFS_remove(spiffs *fs, const char *path);

/**
* Removes a file by filehandle
Expand All @@ -390,7 +394,7 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh);
* @param path the path of the file to stat
* @param s the stat struct to populate
*/
s32_t SPIFFS_stat(spiffs *fs, char *path, spiffs_stat *s);
s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s);

/**
* Gets file status by filehandle
Expand Down Expand Up @@ -420,7 +424,7 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh);
* @param old path of file to rename
* @param newPath new path of file
*/
s32_t SPIFFS_rename(spiffs *fs, char *old, char *newPath);
s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *newPath);

/**
* Returns last error of last file operation.
Expand All @@ -443,7 +447,7 @@ void SPIFFS_clearerr(spiffs *fs);
* @param name the name of the directory
* @param d pointer the directory stream to be populated
*/
spiffs_DIR *SPIFFS_opendir(spiffs *fs, char *name, spiffs_DIR *d);
spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d);

/**
* Closes a directory stream
Expand Down Expand Up @@ -544,6 +548,20 @@ s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages);
*/
s32_t SPIFFS_gc(spiffs *fs, u32_t size);

/**
* Check if EOF reached.
* @param fs the file system struct
* @param fh the filehandle of the file to check
*/
s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh);

/**
* Get position in file.
* @param fs the file system struct
* @param fh the filehandle of the file to check
*/
s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);

#if SPIFFS_TEST_VISUALISATION
/**
* Prints out a visualization of the filesystem.
Expand Down
16 changes: 8 additions & 8 deletions spiffs/spiffs_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,9 @@ static s32_t spiffs_lookup_check_validate(spiffs *fs, spiffs_obj_id lu_obj_id, s
}

static s32_t spiffs_lookup_check_v(spiffs *fs, spiffs_obj_id obj_id, spiffs_block_ix cur_block, int cur_entry,
u32_t user_data, void *user_p) {
(void)user_data;
(void)user_p;
const void *user_const_p, void *user_var_p) {
(void)user_const_p;
(void)user_var_p;
s32_t res = SPIFFS_OK;
spiffs_page_header p_hdr;
spiffs_page_ix cur_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, cur_block, cur_entry);
Expand Down Expand Up @@ -873,11 +873,11 @@ static int spiffs_object_index_search(spiffs *fs, spiffs_obj_id obj_id) {
}

static s32_t spiffs_object_index_consistency_check_v(spiffs *fs, spiffs_obj_id obj_id, spiffs_block_ix cur_block,
int cur_entry, u32_t user_data, void *user_p) {
(void)user_data;
int cur_entry, const void *user_const_p, void *user_var_p) {
(void)user_const_p;
s32_t res_c = SPIFFS_VIS_COUNTINUE;
s32_t res = SPIFFS_OK;
u32_t *log_ix = (u32_t *)user_p;
u32_t *log_ix = (u32_t*)user_var_p;
spiffs_obj_id *obj_table = (spiffs_obj_id *)fs->work;

CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_PROGRESS,
Expand Down Expand Up @@ -977,8 +977,8 @@ s32_t spiffs_object_index_consistency_check(spiffs *fs) {
memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));
u32_t obj_id_log_ix = 0;
CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_PROGRESS, 0, 0);
res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_object_index_consistency_check_v, 0, &obj_id_log_ix,
0, 0);
res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_object_index_consistency_check_v, &obj_id_log_ix,
0, 0, 0);
if (res == SPIFFS_VIS_END) {
res = SPIFFS_OK;
}
Expand Down
29 changes: 22 additions & 7 deletions spiffs/spiffs_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ typedef uint8_t u8_t;
// compile time switches

// Set generic spiffs debug output call.
#ifndef SPIFFS_DGB
#ifndef SPIFFS_DBG
#define SPIFFS_DBG(...) //printf(__VA_ARGS__)
#endif
// Set spiffs debug output call for garbage collecting.
#ifndef SPIFFS_GC_DGB
#ifndef SPIFFS_GC_DBG
#define SPIFFS_GC_DBG(...) //printf(__VA_ARGS__)
#endif
// Set spiffs debug output call for caching.
#ifndef SPIFFS_CACHE_DGB
#ifndef SPIFFS_CACHE_DBG
#define SPIFFS_CACHE_DBG(...) //printf(__VA_ARGS__)
#endif
// Set spiffs debug output call for system consistency checks.
#ifndef SPIFFS_CHECK_DGB
#ifndef SPIFFS_CHECK_DBG
#define SPIFFS_CHECK_DBG(...) //printf(__VA_ARGS__)
#endif

Expand Down Expand Up @@ -142,11 +142,11 @@ typedef uint8_t u8_t;
// SPIFFS_LOCK and SPIFFS_UNLOCK protects spiffs from reentrancy on api level
// These should be defined on a multithreaded system

// define this to entering a mutex if you're running on a multithreaded system
// define this to enter a mutex if you're running on a multithreaded system
#ifndef SPIFFS_LOCK
#define SPIFFS_LOCK(fs)
#endif
// define this to exiting a mutex if you're running on a multithreaded system
// define this to exit a mutex if you're running on a multithreaded system
#ifndef SPIFFS_UNLOCK
#define SPIFFS_UNLOCK(fs)
#endif
Expand Down Expand Up @@ -184,7 +184,22 @@ typedef uint8_t u8_t;
#define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES 1
#endif

// Set SPFIFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function
// Enable this if you want the HAL callbacks to be called with the spiffs struct
#ifndef SPIFFS_HAL_CALLBACK_EXTRA
#define SPIFFS_HAL_CALLBACK_EXTRA 0
#endif

// Enable this if you want to add an integer offset to all file handles
// (spiffs_file). This is useful if running multiple instances of spiffs on
// same target, in order to recognise to what spiffs instance a file handle
// belongs.
// NB: This adds config field fh_ix_offset in the configuration struct when
// mounting, which must be defined.
#ifndef SPIFFS_FILEHDL_OFFSET
#define SPIFFS_FILEHDL_OFFSET 0
#endif

// Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function
// in the api. This function will visualize all filesystem using given printf
// function.
#ifndef SPIFFS_TEST_VISUALISATION
Expand Down
2 changes: 1 addition & 1 deletion spiffs/spiffs_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ s32_t spiffs_gc_quick(
int cur_entry = 0;
spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work;

SPIFFS_GC_DBG("gc_quick: running\n", cur_block);
SPIFFS_GC_DBG("gc_quick: running\n");
#if SPIFFS_GC_STATS
fs->stats_gc_runs++;
#endif
Expand Down
Loading

0 comments on commit 0cba6e2

Please sign in to comment.