Skip to content

Commit 0cba6e2

Browse files
committed
Merge pull request igrr#10 from pgollor/spiffs-0.3.3
update to Spiffs 0.3.3
2 parents a0160df + 6e41f36 commit 0cba6e2

File tree

9 files changed

+247
-104
lines changed

9 files changed

+247
-104
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ Tool to build and unpack [SPIFFS](https://github.com/pellepl/spiffs) images.
77

88
```
99
10-
./mkspiffs {-c <pack_dir>|-u <dest_dir>|-l|-i} [-b <number>] [-p <number>] [-s
11-
<number>] [--] [--version] [-h] <image_file>
10+
mkspiffs {-c <pack_dir>|-u <dest_dir>|-l|-i} [-d <0-5>] [-b <number>]
11+
[-p <number>] [-s <number>] [--] [--version] [-h]
12+
<image_file>
1213
1314
14-
Where:
15+
Where:
1516
1617
-c <pack_dir>, --create <pack_dir>
1718
(OR required) create spiffs image from a directory
@@ -26,6 +27,9 @@ Where:
2627
(OR required) visualize spiffs image
2728
2829
30+
-d <0-5>, --debug <0-5>
31+
Debug level. 0 means no debug output.
32+
2933
-b <number>, --block <number>
3034
fs block size, in bytes
3135
@@ -71,6 +75,7 @@ MIT
7175

7276
## To do
7377

74-
- Error handling
75-
- Determine the image size automatically when opening a file
76-
- Code cleanup
78+
- [ ] Add more denug output and print spiffs debug output
79+
- [ ] Error handling
80+
- [ ] Determine the image size automatically when opening a file
81+
- [ ] Code cleanup

main.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ static s32_t api_spiffs_erase(u32_t addr, u32_t size){
5555
}
5656

5757

58+
int g_debugLevel = 0;
59+
60+
5861
//implementation
5962

6063
int spiffsTryMount(){
@@ -113,20 +116,40 @@ int addFile(char* name, const char* path) {
113116

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

119+
// read file size
116120
fseek(src, 0, SEEK_END);
117121
size_t size = ftell(src);
118122
fseek(src, 0, SEEK_SET);
119123

124+
if (g_debugLevel > 0) {
125+
std::cout << "file size: " << size << std::endl;
126+
}
127+
120128
size_t left = size;
121129
uint8_t data_byte;
122130
while (left > 0){
123131
if (1 != fread(&data_byte, 1, 1, src)) {
132+
std::cerr << "fread error!" << std::endl;
133+
124134
fclose(src);
125135
SPIFFS_close(&s_fs, dst);
126136
return 1;
127137
}
128138
int res = SPIFFS_write(&s_fs, dst, &data_byte, 1);
129139
if (res < 0) {
140+
std::cerr << "SPIFFS_write error(" << s_fs.err_code << "): ";
141+
142+
if (s_fs.err_code == SPIFFS_ERR_FULL) {
143+
std::cerr << "File system is full." << std::endl;
144+
} else {
145+
std::cerr << "unknown";
146+
}
147+
std::cerr << std::endl;
148+
149+
if (g_debugLevel > 0) {
150+
std::cout << "data left: " << left << std::endl;
151+
}
152+
130153
fclose(src);
131154
SPIFFS_close(&s_fs, dst);
132155
return 1;
@@ -136,6 +159,7 @@ int addFile(char* name, const char* path) {
136159

137160
SPIFFS_close(&s_fs, dst);
138161
fclose(src);
162+
139163
return 0;
140164
}
141165

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

489517
cmd.add( imageSizeArg );
490518
cmd.add( pageSizeArg );
491519
cmd.add( blockSizeArg );
520+
cmd.add(debugArg);
492521
std::vector<TCLAP::Arg*> args = {&packArg, &unpackArg, &listArg, &visualizeArg};
493522
cmd.xorAdd( args );
494523
cmd.add( outNameArg );
495524
cmd.parse( argc, argv );
496525

526+
if (debugArg.getValue() > 0) {
527+
std::cout << "Debug output enabled" << std::endl;
528+
g_debugLevel = debugArg.getValue();
529+
}
530+
497531
if (packArg.isSet()) {
498532
s_dirName = packArg.getValue();
499533
s_action = ACTION_PACK;

spiffs/spiffs.h

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* Author: petera
66
*/
77

8-
9-
108
#ifndef SPIFFS_H_
119
#define SPIFFS_H_
1210
#if defined(__cplusplus)
@@ -186,6 +184,11 @@ typedef struct {
186184
// logical size of a page, must be at least
187185
// log_block_size / 8
188186
u32_t log_page_size;
187+
188+
#endif
189+
#if SPIFFS_FILEHDL_OFFSET
190+
// an integer offset added to each file handle
191+
u16_t fh_ix_offset;
189192
#endif
190193
} spiffs_config;
191194

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

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

326329

327330
/**
@@ -360,13 +363,14 @@ s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len);
360363
s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len);
361364

362365
/**
363-
* Moves the read/write file offset
366+
* Moves the read/write file offset. Resulting offset is returned or negative if error.
367+
* lseek(fs, fd, 0, SPIFFS_SEEK_CUR) will thus return current offset.
364368
* @param fs the file system struct
365369
* @param fh the filehandle
366370
* @param offs how much/where to move the offset
367371
* @param whence if SPIFFS_SEEK_SET, the file offset shall be set to offset bytes
368372
* if SPIFFS_SEEK_CUR, the file offset shall be set to its current location plus offset
369-
* if SPIFFS_SEEK_END, the file offset shall be set to the size of the file plus offset
373+
* if SPIFFS_SEEK_END, the file offset shall be set to the size of the file plus offse, which should be negative
370374
*/
371375
s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence);
372376

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

380384
/**
381385
* Removes a file by filehandle
@@ -390,7 +394,7 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh);
390394
* @param path the path of the file to stat
391395
* @param s the stat struct to populate
392396
*/
393-
s32_t SPIFFS_stat(spiffs *fs, char *path, spiffs_stat *s);
397+
s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s);
394398

395399
/**
396400
* Gets file status by filehandle
@@ -420,7 +424,7 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh);
420424
* @param old path of file to rename
421425
* @param newPath new path of file
422426
*/
423-
s32_t SPIFFS_rename(spiffs *fs, char *old, char *newPath);
427+
s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *newPath);
424428

425429
/**
426430
* Returns last error of last file operation.
@@ -443,7 +447,7 @@ void SPIFFS_clearerr(spiffs *fs);
443447
* @param name the name of the directory
444448
* @param d pointer the directory stream to be populated
445449
*/
446-
spiffs_DIR *SPIFFS_opendir(spiffs *fs, char *name, spiffs_DIR *d);
450+
spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d);
447451

448452
/**
449453
* Closes a directory stream
@@ -544,6 +548,20 @@ s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages);
544548
*/
545549
s32_t SPIFFS_gc(spiffs *fs, u32_t size);
546550

551+
/**
552+
* Check if EOF reached.
553+
* @param fs the file system struct
554+
* @param fh the filehandle of the file to check
555+
*/
556+
s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh);
557+
558+
/**
559+
* Get position in file.
560+
* @param fs the file system struct
561+
* @param fh the filehandle of the file to check
562+
*/
563+
s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);
564+
547565
#if SPIFFS_TEST_VISUALISATION
548566
/**
549567
* Prints out a visualization of the filesystem.

spiffs/spiffs_check.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,9 @@ static s32_t spiffs_lookup_check_validate(spiffs *fs, spiffs_obj_id lu_obj_id, s
440440
}
441441

442442
static s32_t spiffs_lookup_check_v(spiffs *fs, spiffs_obj_id obj_id, spiffs_block_ix cur_block, int cur_entry,
443-
u32_t user_data, void *user_p) {
444-
(void)user_data;
445-
(void)user_p;
443+
const void *user_const_p, void *user_var_p) {
444+
(void)user_const_p;
445+
(void)user_var_p;
446446
s32_t res = SPIFFS_OK;
447447
spiffs_page_header p_hdr;
448448
spiffs_page_ix cur_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, cur_block, cur_entry);
@@ -873,11 +873,11 @@ static int spiffs_object_index_search(spiffs *fs, spiffs_obj_id obj_id) {
873873
}
874874

875875
static s32_t spiffs_object_index_consistency_check_v(spiffs *fs, spiffs_obj_id obj_id, spiffs_block_ix cur_block,
876-
int cur_entry, u32_t user_data, void *user_p) {
877-
(void)user_data;
876+
int cur_entry, const void *user_const_p, void *user_var_p) {
877+
(void)user_const_p;
878878
s32_t res_c = SPIFFS_VIS_COUNTINUE;
879879
s32_t res = SPIFFS_OK;
880-
u32_t *log_ix = (u32_t *)user_p;
880+
u32_t *log_ix = (u32_t*)user_var_p;
881881
spiffs_obj_id *obj_table = (spiffs_obj_id *)fs->work;
882882

883883
CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_PROGRESS,
@@ -977,8 +977,8 @@ s32_t spiffs_object_index_consistency_check(spiffs *fs) {
977977
memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));
978978
u32_t obj_id_log_ix = 0;
979979
CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_PROGRESS, 0, 0);
980-
res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_object_index_consistency_check_v, 0, &obj_id_log_ix,
981-
0, 0);
980+
res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_object_index_consistency_check_v, &obj_id_log_ix,
981+
0, 0, 0);
982982
if (res == SPIFFS_VIS_END) {
983983
res = SPIFFS_OK;
984984
}

spiffs/spiffs_config.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ typedef uint8_t u8_t;
4141
// compile time switches
4242

4343
// Set generic spiffs debug output call.
44-
#ifndef SPIFFS_DGB
44+
#ifndef SPIFFS_DBG
4545
#define SPIFFS_DBG(...) //printf(__VA_ARGS__)
4646
#endif
4747
// Set spiffs debug output call for garbage collecting.
48-
#ifndef SPIFFS_GC_DGB
48+
#ifndef SPIFFS_GC_DBG
4949
#define SPIFFS_GC_DBG(...) //printf(__VA_ARGS__)
5050
#endif
5151
// Set spiffs debug output call for caching.
52-
#ifndef SPIFFS_CACHE_DGB
52+
#ifndef SPIFFS_CACHE_DBG
5353
#define SPIFFS_CACHE_DBG(...) //printf(__VA_ARGS__)
5454
#endif
5555
// Set spiffs debug output call for system consistency checks.
56-
#ifndef SPIFFS_CHECK_DGB
56+
#ifndef SPIFFS_CHECK_DBG
5757
#define SPIFFS_CHECK_DBG(...) //printf(__VA_ARGS__)
5858
#endif
5959

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

145-
// define this to entering a mutex if you're running on a multithreaded system
145+
// define this to enter a mutex if you're running on a multithreaded system
146146
#ifndef SPIFFS_LOCK
147147
#define SPIFFS_LOCK(fs)
148148
#endif
149-
// define this to exiting a mutex if you're running on a multithreaded system
149+
// define this to exit a mutex if you're running on a multithreaded system
150150
#ifndef SPIFFS_UNLOCK
151151
#define SPIFFS_UNLOCK(fs)
152152
#endif
@@ -184,7 +184,22 @@ typedef uint8_t u8_t;
184184
#define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES 1
185185
#endif
186186

187-
// Set SPFIFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function
187+
// Enable this if you want the HAL callbacks to be called with the spiffs struct
188+
#ifndef SPIFFS_HAL_CALLBACK_EXTRA
189+
#define SPIFFS_HAL_CALLBACK_EXTRA 0
190+
#endif
191+
192+
// Enable this if you want to add an integer offset to all file handles
193+
// (spiffs_file). This is useful if running multiple instances of spiffs on
194+
// same target, in order to recognise to what spiffs instance a file handle
195+
// belongs.
196+
// NB: This adds config field fh_ix_offset in the configuration struct when
197+
// mounting, which must be defined.
198+
#ifndef SPIFFS_FILEHDL_OFFSET
199+
#define SPIFFS_FILEHDL_OFFSET 0
200+
#endif
201+
202+
// Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function
188203
// in the api. This function will visualize all filesystem using given printf
189204
// function.
190205
#ifndef SPIFFS_TEST_VISUALISATION

spiffs/spiffs_gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ s32_t spiffs_gc_quick(
3636
int cur_entry = 0;
3737
spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work;
3838

39-
SPIFFS_GC_DBG("gc_quick: running\n", cur_block);
39+
SPIFFS_GC_DBG("gc_quick: running\n");
4040
#if SPIFFS_GC_STATS
4141
fs->stats_gc_runs++;
4242
#endif

0 commit comments

Comments
 (0)