@@ -197,17 +197,15 @@ void listFiles() {
197
197
*
198
198
* @author Pascal Gollor (http://www.pgollor.de/cms/)
199
199
*/
200
- bool dirExists (const char * path)
201
- {
202
- DIR *d = opendir (path);
200
+ bool dirExists (const char * path) {
201
+ DIR *d = opendir (path);
203
202
204
- if (d)
205
- {
206
- closedir (d);
207
- return true ;
208
- }
203
+ if (d) {
204
+ closedir (d);
205
+ return true ;
206
+ }
209
207
210
- return false ;
208
+ return false ;
211
209
}
212
210
213
211
/* *
@@ -218,31 +216,30 @@ bool dirExists(const char* path)
218
216
*
219
217
* @author Pascal Gollor (http://www.pgollor.de/cms/)
220
218
*/
221
- bool unpackFile (spiffs_dirent *spiffsFile, const char *destPath)
222
- {
223
- u8_t buffer[spiffsFile->size ];
224
- std::string filename = (const char *)(spiffsFile->name );
219
+ bool unpackFile (spiffs_dirent *spiffsFile, const char *destPath) {
220
+ u8_t buffer[spiffsFile->size ];
221
+ std::string filename = (const char *)(spiffsFile->name );
225
222
226
- // Open file from spiffs file system.
227
- spiffs_file src = SPIFFS_open (&s_fs, (char *)(filename.c_str ()), SPIFFS_RDONLY, 0 );
223
+ // Open file from spiffs file system.
224
+ spiffs_file src = SPIFFS_open (&s_fs, (char *)(filename.c_str ()), SPIFFS_RDONLY, 0 );
228
225
229
- // read content into buffer
230
- SPIFFS_read (&s_fs, src, buffer, spiffsFile->size );
226
+ // read content into buffer
227
+ SPIFFS_read (&s_fs, src, buffer, spiffsFile->size );
231
228
232
- // Close spiffs file.
233
- SPIFFS_close (&s_fs, src);
229
+ // Close spiffs file.
230
+ SPIFFS_close (&s_fs, src);
234
231
235
- // Open file.
236
- FILE* dst = fopen (destPath, " wb" );
232
+ // Open file.
233
+ FILE* dst = fopen (destPath, " wb" );
237
234
238
- // Write content into file.
239
- fwrite (buffer, sizeof (u8_t ), sizeof (buffer), dst);
235
+ // Write content into file.
236
+ fwrite (buffer, sizeof (u8_t ), sizeof (buffer), dst);
240
237
241
- // Close file.
242
- fclose (dst);
238
+ // Close file.
239
+ fclose (dst);
243
240
244
241
245
- return true ;
242
+ return true ;
246
243
}
247
244
248
245
/* *
@@ -254,71 +251,64 @@ bool unpackFile(spiffs_dirent *spiffsFile, const char *destPath)
254
251
*
255
252
* todo: Do unpack stuff for directories.
256
253
*/
257
- bool unpackFiles (std::string sDest )
258
- {
259
- spiffs_DIR dir;
260
- spiffs_dirent ent;
261
-
262
- // Add "./" to path if is not given.
263
- if (sDest .find (" ./" ) == std::string::npos)
264
- {
265
- sDest = " ./" + sDest ;
266
- }
267
-
268
- // Check if directory exists. If it does not then try to create it with permissions 755.
269
- if (! dirExists (sDest .c_str ()))
270
- {
271
- std::cout << " Directory " << sDest << " does not exists. Try to create it." << std::endl;
272
-
273
- // Try to create directory.
274
- // platform stuff...
254
+ bool unpackFiles (std::string sDest ) {
255
+ spiffs_DIR dir;
256
+ spiffs_dirent ent;
257
+
258
+ // Add "./" to path if is not given.
259
+ if (sDest .find (" ./" ) == std::string::npos) {
260
+ sDest = " ./" + sDest ;
261
+ }
262
+
263
+ // Check if directory exists. If it does not then try to create it with permissions 755.
264
+ if (! dirExists (sDest .c_str ())) {
265
+ std::cout << " Directory " << sDest << " does not exists. Try to create it." << std::endl;
266
+
267
+ // Try to create directory.
268
+ // platform stuff...
275
269
#if defined(_WIN32)
276
- if (_mkdir (sDest .c_str ()) != 0 )
270
+ if (_mkdir (sDest .c_str ()) != 0 ) {
277
271
#else
278
- if (mkdir (sDest .c_str (), S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0 )
272
+ if (mkdir (sDest .c_str (), S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0 ) {
279
273
#endif
280
- {
281
- std::cerr << " Can not create directory!!!" << std::endl;
282
- return false ;
283
- }
284
- }
285
-
286
- // Open directory.
287
- SPIFFS_opendir (&s_fs, 0 , &dir);
288
-
289
- // Read content from directory.
290
- spiffs_dirent* it = SPIFFS_readdir (&dir, &ent);
291
- while (it)
292
- {
293
- // Check if content is a file.
294
- if ((int )(it->type ) == 1 )
295
- {
296
- std::string sDestFilePath = sDest + (const char *)(it->name );
297
-
298
- // Unpack file to destination directory.
299
- if (! unpackFile (it, sDestFilePath .c_str ()) )
300
- {
301
- std::cout << " Can not unpack " << it->name << " !" << std::endl;
302
- return false ;
303
- }
304
-
305
- // Output stuff.
306
- std::cout
307
- << it->name
308
- << ' \t '
309
- << " > " << sDestFilePath
310
- << ' \t '
311
- << " size: " << it->size << " Bytes"
312
- << std::endl;
313
- }
314
-
315
- it = SPIFFS_readdir (&dir, &ent);
316
- }
317
-
318
- // Close directory.
319
- SPIFFS_closedir (&dir);
320
-
321
- return true ;
274
+ std::cerr << " Can not create directory!!!" << std::endl;
275
+ return false ;
276
+ }
277
+ }
278
+
279
+ // Open directory.
280
+ SPIFFS_opendir (&s_fs, 0 , &dir);
281
+
282
+ // Read content from directory.
283
+ spiffs_dirent* it = SPIFFS_readdir (&dir, &ent);
284
+ while (it) {
285
+ // Check if content is a file.
286
+ if ((int )(it->type ) == 1 ) {
287
+ std::string sDestFilePath = sDest + (const char *)(it->name );
288
+
289
+ // Unpack file to destination directory.
290
+ if (! unpackFile (it, sDestFilePath .c_str ()) ) {
291
+ std::cout << " Can not unpack " << it->name << " !" << std::endl;
292
+ return false ;
293
+ }
294
+
295
+ // Output stuff.
296
+ std::cout
297
+ << it->name
298
+ << ' \t '
299
+ << " > " << sDestFilePath
300
+ << ' \t '
301
+ << " size: " << it->size << " Bytes"
302
+ << std::endl;
303
+ }
304
+
305
+ it = SPIFFS_readdir (&dir, &ent);
306
+ }
307
+
308
+ // Close directory.
309
+ SPIFFS_closedir (&dir);
310
+
311
+ return true ;
322
312
}
323
313
324
314
// Actions
@@ -348,37 +338,35 @@ int actionPack() {
348
338
*
349
339
* @author Pascal Gollor (http://www.pgollor.de/cms/)
350
340
*/
351
- int actionUnpack (void )
352
- {
353
- int ret = 0 ;
354
- s_flashmem.resize (s_imageSize, 0xff );
341
+ int actionUnpack (void ) {
342
+ int ret = 0 ;
343
+ s_flashmem.resize (s_imageSize, 0xff );
355
344
356
- // open spiffs image
357
- FILE* fdsrc = fopen (s_imageName.c_str (), " rb" );
358
- if (!fdsrc) {
359
- std::cerr << " error: failed to open image file" << std::endl;
360
- return 1 ;
361
- }
345
+ // open spiffs image
346
+ FILE* fdsrc = fopen (s_imageName.c_str (), " rb" );
347
+ if (!fdsrc) {
348
+ std::cerr << " error: failed to open image file" << std::endl;
349
+ return 1 ;
350
+ }
362
351
363
- // read content into s_flashmem
364
- fread (&s_flashmem[0 ], 4 , s_flashmem.size ()/4 , fdsrc);
352
+ // read content into s_flashmem
353
+ fread (&s_flashmem[0 ], 4 , s_flashmem.size ()/4 , fdsrc);
365
354
366
- // close fiel handle
367
- fclose (fdsrc);
355
+ // close fiel handle
356
+ fclose (fdsrc);
368
357
369
- // mount file system
370
- spiffsMount ();
358
+ // mount file system
359
+ spiffsMount ();
371
360
372
- // unpack files
373
- if (! unpackFiles (s_dirName))
374
- {
375
- ret = 1 ;
376
- }
361
+ // unpack files
362
+ if (! unpackFiles (s_dirName)) {
363
+ ret = 1 ;
364
+ }
377
365
378
- // unmount file system
379
- spiffsUnmount ();
366
+ // unmount file system
367
+ spiffsUnmount ();
380
368
381
- return ret;
369
+ return ret;
382
370
}
383
371
384
372
@@ -440,22 +428,15 @@ void processArgs(int argc, const char** argv) {
440
428
cmd.add ( outNameArg );
441
429
cmd.parse ( argc, argv );
442
430
443
- if (packArg.isSet ())
444
- {
431
+ if (packArg.isSet ()) {
445
432
s_dirName = packArg.getValue ();
446
433
s_action = ACTION_PACK;
447
- }
448
- else if (unpackArg.isSet ())
449
- {
434
+ } else if (unpackArg.isSet ()) {
450
435
s_dirName = unpackArg.getValue ();
451
436
s_action = ACTION_UNPACK;
452
- }
453
- else if (listArg.isSet ())
454
- {
437
+ } else if (listArg.isSet ()) {
455
438
s_action = ACTION_LIST;
456
- }
457
- else if (visualizeArg.isSet ())
458
- {
439
+ } else if (visualizeArg.isSet ()) {
459
440
s_action = ACTION_VISUALIZE;
460
441
}
461
442
@@ -474,8 +455,7 @@ int main(int argc, const char * argv[]) {
474
455
return 1 ;
475
456
}
476
457
477
- switch (s_action)
478
- {
458
+ switch (s_action) {
479
459
case ACTION_PACK:
480
460
return actionPack ();
481
461
break ;
0 commit comments