Skip to content

Commit b73250b

Browse files
committed
Merge pull request igrr#1 from ficeto/master
add slash
2 parents 415d7d9 + a958f2e commit b73250b

File tree

4 files changed

+112
-66
lines changed

4 files changed

+112
-66
lines changed

main.cpp

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,25 @@ static std::vector<uint8_t> s_spiffsFds;
3939
static std::vector<uint8_t> s_spiffsCache;
4040

4141

42-
static s32_t api_spiffs_read(u32_t addr, u32_t size, u8_t *dst)
43-
{
42+
static s32_t api_spiffs_read(u32_t addr, u32_t size, u8_t *dst){
4443
memcpy(dst, &s_flashmem[0] + addr, size);
4544
return SPIFFS_OK;
4645
}
4746

48-
static s32_t api_spiffs_write(u32_t addr, u32_t size, u8_t *src)
49-
{
47+
static s32_t api_spiffs_write(u32_t addr, u32_t size, u8_t *src){
5048
memcpy(&s_flashmem[0] + addr, src, size);
5149
return SPIFFS_OK;
5250
}
5351

54-
static s32_t api_spiffs_erase(u32_t addr, u32_t size)
55-
{
52+
static s32_t api_spiffs_erase(u32_t addr, u32_t size){
5653
memset(&s_flashmem[0] + addr, 0xff, size);
5754
return SPIFFS_OK;
5855
}
5956

60-
void checkCallback(spiffs_check_type type, spiffs_check_report report,
61-
u32_t arg1, u32_t arg2)
62-
{
63-
}
6457

65-
void spiffsMount(bool init)
66-
{
58+
//implementation
59+
60+
int spiffsTryMount(){
6761
spiffs_config cfg = {0};
6862

6963
cfg.phys_addr = 0x0000;
@@ -82,35 +76,35 @@ void spiffsMount(bool init)
8276
s_spiffsFds.resize(32 * maxOpenFiles);
8377
s_spiffsCache.resize((32 + s_pageSize) * maxOpenFiles);
8478

85-
86-
SPIFFS_mount(&s_fs, &cfg,
79+
return SPIFFS_mount(&s_fs, &cfg,
8780
&s_spiffsWorkBuf[0],
8881
&s_spiffsFds[0], s_spiffsFds.size(),
8982
&s_spiffsCache[0], s_spiffsCache.size(),
9083
NULL);
91-
92-
// debugf("mount res: %d\n", res);
93-
if (init) {
94-
spiffs_file fd = SPIFFS_open(&s_fs, (char*) "tmp", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
95-
uint8_t tmp = 0xff;
96-
SPIFFS_write(&s_fs, fd, &tmp, 1);
97-
SPIFFS_fremove(&s_fs, fd);
98-
SPIFFS_close(&s_fs, fd);
99-
} else {
100-
s_fs.check_cb_f = &checkCallback;
101-
//SPIFFS_check(&s_fs);
102-
}
10384
}
10485

105-
void spiffsUnmount()
106-
{
86+
bool spiffsMount(){
87+
if(SPIFFS_mounted(&s_fs))
88+
return true;
89+
int res = spiffsTryMount();
90+
return (res == SPIFFS_OK);
91+
}
92+
93+
bool spiffsFormat(){
94+
spiffsMount();
95+
SPIFFS_unmount(&s_fs);
96+
int formated = SPIFFS_format(&s_fs);
97+
if(formated != SPIFFS_OK)
98+
return false;
99+
return (spiffsTryMount() == SPIFFS_OK);
100+
}
101+
102+
void spiffsUnmount(){
103+
if(SPIFFS_mounted(&s_fs))
107104
SPIFFS_unmount(&s_fs);
108105
}
109106

110107
int addFile(char* name, const char* path) {
111-
const size_t write_size = 512;
112-
uint8_t write_block[write_size];
113-
114108
FILE* src = fopen(path, "rb");
115109
if (!src) {
116110
std::cerr << "error: failed to open " << path << " for reading" << std::endl;
@@ -124,30 +118,28 @@ int addFile(char* name, const char* path) {
124118
fseek(src, 0, SEEK_SET);
125119

126120
size_t left = size;
127-
while (left > 0)
128-
{
129-
size_t will_write = std::min(left, write_size);
130-
if (will_write != fread(write_block, 1, will_write, src)) {
121+
uint8_t data_byte;
122+
while (left > 0){
123+
if (1 != fread(&data_byte, 1, 1, src)) {
131124
fclose(src);
132125
SPIFFS_close(&s_fs, dst);
133126
return 1;
134127
}
135-
int res = SPIFFS_write(&s_fs, dst, write_block, write_size);
128+
int res = SPIFFS_write(&s_fs, dst, &data_byte, 1);
136129
if (res < 0) {
137130
fclose(src);
138131
SPIFFS_close(&s_fs, dst);
139132
return 1;
140133
}
141-
left -= will_write;
134+
left -= 1;
142135
}
143136

144137
SPIFFS_close(&s_fs, dst);
145138
fclose(src);
146139
return 0;
147140
}
148141

149-
int addFiles(const char* dirname)
150-
{
142+
int addFiles(const char* dirname){
151143
DIR *dir;
152144
struct dirent *ent;
153145
bool error = false;
@@ -165,8 +157,10 @@ int addFiles(const char* dirname)
165157
continue;
166158
}
167159

160+
std::string filepath = "/";
161+
filepath += ent->d_name;
168162
std::cout << ent->d_name << std::endl;
169-
if (addFile(ent->d_name, fullpath.c_str()) != 0) {
163+
if (addFile((char*)filepath.c_str(), fullpath.c_str()) != 0) {
170164
std::cerr << "error adding file!" << std::endl;
171165
error = true;
172166
break;
@@ -196,7 +190,7 @@ void listFiles() {
196190
SPIFFS_closedir(&dir);
197191
}
198192

199-
193+
// Actions
200194

201195
int actionPack() {
202196
s_flashmem.resize(s_imageSize, 0xff);
@@ -207,7 +201,7 @@ int actionPack() {
207201
return 1;
208202
}
209203

210-
spiffsMount(true);
204+
spiffsFormat();
211205
int result = addFiles(s_dirName.c_str());
212206
spiffsUnmount();
213207

@@ -229,11 +223,9 @@ int actionList() {
229223

230224
fread(&s_flashmem[0], 4, s_flashmem.size()/4, fdsrc);
231225
fclose(fdsrc);
232-
233-
spiffsMount(false);
226+
spiffsMount();
234227
listFiles();
235228
spiffsUnmount();
236-
237229
return 0;
238230
}
239231

@@ -249,7 +241,7 @@ int actionVisualize() {
249241
fread(&s_flashmem[0], 4, s_flashmem.size()/4, fdsrc);
250242
fclose(fdsrc);
251243

252-
spiffsMount(false);
244+
spiffsMount();
253245
SPIFFS_vis(&s_fs);
254246
uint32_t total, used;
255247
SPIFFS_info(&s_fs, &total, &used);
@@ -260,7 +252,6 @@ int actionVisualize() {
260252
}
261253

262254
void processArgs(int argc, const char** argv) {
263-
264255
TCLAP::CmdLine cmd("", ' ', VERSION);
265256
TCLAP::ValueArg<std::string> packArg( "c", "create", "create spiffs image from a directory", true, "", "pack_dir");
266257
TCLAP::SwitchArg listArg( "l", "list", "list files in spiffs image", false);
@@ -299,13 +290,11 @@ int main(int argc, const char * argv[]) {
299290

300291
try {
301292
processArgs(argc, argv);
302-
}
303-
catch(...) {
293+
} catch(...) {
304294
std::cerr << "Invalid arguments" << std::endl;
305295
return 1;
306296
}
307297

308-
309298
switch (s_action) {
310299
case ACTION_PACK: return actionPack();
311300
case ACTION_LIST: return actionList();

spiffs/spiffs.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,20 @@ s32_t SPIFFS_format(spiffs *fs);
473473
*/
474474
u8_t SPIFFS_mounted(spiffs *fs);
475475

476+
/**
477+
* Check if EOF reached.
478+
* @param fs the file system struct
479+
* @param fh the filehandle of the file to check
480+
*/
481+
s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh);
482+
483+
/**
484+
* Get the current position of the data pointer.
485+
* @param fs the file system struct
486+
* @param fh the filehandle of the open file
487+
*/
488+
s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);
489+
476490
#if SPIFFS_TEST_VISUALISATION
477491
/**
478492
* Prints out a visualization of the filesystem.

spiffs/spiffs_config.h

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@
1414
#include <string.h>
1515
#include <stdio.h>
1616

17-
#define IRAM_ATTR
18-
#define STORE_TYPEDEF_ATTR __attribute__((aligned(4),packed))
19-
#define STORE_ATTR __attribute__((aligned(4)))
20-
21-
#define c_memcpy memcpy
22-
#define c_printf printf
23-
#define c_memset memset
24-
2517
typedef int16_t file_t;
2618
typedef int32_t s32_t;
2719
typedef uint32_t u32_t;
@@ -47,24 +39,22 @@ typedef uint8_t u8_t;
4739
#endif
4840

4941
// compile time switches
50-
#define debugf(fmt, ...) //os_printf(fmt"\r\n", ##__VA_ARGS__)
51-
#define SYSTEM_ERROR(fmt, ...) //os_printf("ERROR: " fmt "\r\n", ##__VA_ARGS__)
5242

5343
// Set generic spiffs debug output call.
5444
#ifndef SPIFFS_DGB
55-
#define SPIFFS_DBG(...) //os_printf(__VA_ARGS__)
45+
#define SPIFFS_DBG(...) //printf(__VA_ARGS__)
5646
#endif
5747
// Set spiffs debug output call for garbage collecting.
5848
#ifndef SPIFFS_GC_DGB
59-
#define SPIFFS_GC_DBG(...) //os_printf(__VA_ARGS__)
49+
#define SPIFFS_GC_DBG(...) //printf(__VA_ARGS__)
6050
#endif
6151
// Set spiffs debug output call for caching.
6252
#ifndef SPIFFS_CACHE_DGB
63-
#define SPIFFS_CACHE_DBG(...) //os_printf(__VA_ARGS__)
53+
#define SPIFFS_CACHE_DBG(...) //printf(__VA_ARGS__)
6454
#endif
6555
// Set spiffs debug output call for system consistency checks.
6656
#ifndef SPIFFS_CHECK_DGB
67-
#define SPIFFS_CHECK_DBG(...) //os_printf(__VA_ARGS__)
57+
#define SPIFFS_CHECK_DBG(...) //printf(__VA_ARGS__)
6858
#endif
6959

7060
// Enable/disable API functions to determine exact number of bytes
@@ -99,7 +89,7 @@ typedef uint8_t u8_t;
9989

10090
// Define maximum number of gc runs to perform to reach desired free pages.
10191
#ifndef SPIFFS_GC_MAX_RUNS
102-
#define SPIFFS_GC_MAX_RUNS 3
92+
#define SPIFFS_GC_MAX_RUNS 5
10393
#endif
10494

10595
// Enable/disable statistics on gc. Debug/test purpose only.
@@ -141,6 +131,14 @@ typedef uint8_t u8_t;
141131
#define SPIFFS_COPY_BUFFER_STACK (64)
142132
#endif
143133

134+
// Enable this to have an identifiable spiffs filesystem. This will look for
135+
// a magic in all sectors to determine if this is a valid spiffs system or
136+
// not on mount point. If not, SPIFFS_format must be called prior to mounting
137+
// again.
138+
#ifndef SPIFFS_USE_MAGIC
139+
#define SPIFFS_USE_MAGIC (0)
140+
#endif
141+
144142
// SPIFFS_LOCK and SPIFFS_UNLOCK protects spiffs from reentrancy on api level
145143
// These should be defined on a multithreaded system
146144

@@ -181,6 +179,11 @@ typedef uint8_t u8_t;
181179
#endif
182180
#endif
183181

182+
// Enable this if your target needs aligned data for index tables
183+
#ifndef SPIFFS_ALIGNED_OBJECT_INDEX_TABLES
184+
#define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES 1
185+
#endif
186+
184187
// Set SPFIFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function
185188
// in the api. This function will visualize all filesystem using given printf
186189
// function.
@@ -189,7 +192,7 @@ typedef uint8_t u8_t;
189192
#endif
190193
#if SPIFFS_TEST_VISUALISATION
191194
#ifndef spiffs_printf
192-
#define spiffs_printf(...) c_printf(__VA_ARGS__)
195+
#define spiffs_printf(...) printf(__VA_ARGS__)
193196
#endif
194197
// spiffs_printf argument for a free page
195198
#ifndef SPIFFS_TEST_VIS_FREE_STR

spiffs/spiffs_hydrogen.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,46 @@ s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used) {
856856
return res;
857857
}
858858

859+
s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh) {
860+
SPIFFS_API_CHECK_CFG(fs);
861+
SPIFFS_API_CHECK_MOUNT(fs);
862+
SPIFFS_LOCK(fs);
863+
864+
spiffs_fd *fd;
865+
s32_t res;
866+
res = spiffs_fd_get(fs, fh, &fd);
867+
SPIFFS_API_CHECK_RES(fs, res);
868+
869+
#if SPIFFS_CACHE_WR
870+
spiffs_fflush_cache(fs, fh);
871+
#endif
872+
873+
res = (fd->fdoffset == fd->size);
874+
875+
SPIFFS_UNLOCK(fs);
876+
return res;
877+
}
878+
879+
s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) {
880+
SPIFFS_API_CHECK_CFG(fs);
881+
SPIFFS_API_CHECK_MOUNT(fs);
882+
SPIFFS_LOCK(fs);
883+
884+
spiffs_fd *fd;
885+
s32_t res;
886+
res = spiffs_fd_get(fs, fh, &fd);
887+
SPIFFS_API_CHECK_RES(fs, res);
888+
889+
#if SPIFFS_CACHE_WR
890+
spiffs_fflush_cache(fs, fh);
891+
#endif
892+
893+
res = fd->fdoffset;
894+
895+
SPIFFS_UNLOCK(fs);
896+
return res;
897+
}
898+
859899
#if SPIFFS_TEST_VISUALISATION
860900
s32_t SPIFFS_vis(spiffs *fs) {
861901
s32_t res = SPIFFS_OK;

0 commit comments

Comments
 (0)