Skip to content

Commit 1103e1b

Browse files
committed
spiffs: fix esp_spiffs_format not working if partition is not mounted
Fixes #1547.
1 parent 7b32aaa commit 1103e1b

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

components/spiffs/esp_spiffs.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,12 @@ esp_err_t esp_spiffs_info(const char* partition_label, size_t *total_bytes, size
357357

358358
esp_err_t esp_spiffs_format(const char* partition_label)
359359
{
360-
bool mount_on_success = false;
360+
bool partition_was_mounted = false;
361361
int index;
362+
/* If the partition is not mounted, need to create SPIFFS structures
363+
* and mount the partition, unmount, format, delete SPIFFS structures.
364+
* See SPIFFS wiki for the reason why.
365+
*/
362366
esp_err_t err = esp_spiffs_by_label(partition_label, &index);
363367
if (err != ESP_OK) {
364368
esp_vfs_spiffs_conf_t conf = {
@@ -371,23 +375,28 @@ esp_err_t esp_spiffs_format(const char* partition_label)
371375
return err;
372376
}
373377
err = esp_spiffs_by_label(partition_label, &index);
374-
if (err != ESP_OK) {
375-
return err;
376-
}
377-
esp_spiffs_free(&_efs[index]);
378-
return ESP_OK;
378+
assert(err == ESP_OK && "failed to get index of the partition just mounted");
379379
} else if (SPIFFS_mounted(_efs[index]->fs)) {
380-
SPIFFS_unmount(_efs[index]->fs);
381-
mount_on_success = true;
380+
partition_was_mounted = true;
382381
}
382+
383+
SPIFFS_unmount(_efs[index]->fs);
384+
383385
s32_t res = SPIFFS_format(_efs[index]->fs);
384386
if (res != SPIFFS_OK) {
385387
ESP_LOGE(TAG, "format failed, %i", SPIFFS_errno(_efs[index]->fs));
386388
SPIFFS_clearerr(_efs[index]->fs);
389+
/* If the partition was previously mounted, but format failed, don't
390+
* try to mount the partition back (it will probably fail). On the
391+
* other hand, if it was not mounted, need to clean up.
392+
*/
393+
if (!partition_was_mounted) {
394+
esp_spiffs_free(&_efs[index]);
395+
}
387396
return ESP_FAIL;
388397
}
389398

390-
if (mount_on_success) {
399+
if (partition_was_mounted) {
391400
res = SPIFFS_mount(_efs[index]->fs, &_efs[index]->cfg, _efs[index]->work,
392401
_efs[index]->fds, _efs[index]->fds_sz, _efs[index]->cache,
393402
_efs[index]->cache_sz, spiffs_api_check);
@@ -396,6 +405,8 @@ esp_err_t esp_spiffs_format(const char* partition_label)
396405
SPIFFS_clearerr(_efs[index]->fs);
397406
return ESP_FAIL;
398407
}
408+
} else {
409+
esp_spiffs_free(&_efs[index]);
399410
}
400411
return ESP_OK;
401412
}

components/spiffs/test/test_spiffs.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ static void test_teardown()
407407
TEST_ESP_OK(esp_vfs_spiffs_unregister(spiffs_test_partition_label));
408408
}
409409

410-
TEST_CASE("can format partition", "[spiffs]")
410+
TEST_CASE("can initialize SPIFFS in erased partition", "[spiffs]")
411411
{
412412
const esp_partition_t* part = get_test_data_partition();
413413
TEST_ASSERT_NOT_NULL(part);
@@ -420,6 +420,44 @@ TEST_CASE("can format partition", "[spiffs]")
420420
test_teardown();
421421
}
422422

423+
TEST_CASE("can format mounted partition", "[spiffs]")
424+
{
425+
// Mount SPIFFS, create file, format, check that the file does not exist.
426+
const esp_partition_t* part = get_test_data_partition();
427+
TEST_ASSERT_NOT_NULL(part);
428+
test_setup();
429+
const char* filename = "/spiffs/hello.txt";
430+
test_spiffs_create_file_with_text(filename, spiffs_test_hello_str);
431+
esp_spiffs_format(part->label);
432+
FILE* f = fopen(filename, "r");
433+
TEST_ASSERT_NULL(f);
434+
test_teardown();
435+
}
436+
437+
TEST_CASE("can format unmounted partition", "[spiffs]")
438+
{
439+
// Mount SPIFFS, create file, unmount. Format. Mount again, check that
440+
// the file does not exist.
441+
const esp_partition_t* part = get_test_data_partition();
442+
TEST_ASSERT_NOT_NULL(part);
443+
test_setup();
444+
const char* filename = "/spiffs/hello.txt";
445+
test_spiffs_create_file_with_text(filename, spiffs_test_hello_str);
446+
test_teardown();
447+
esp_spiffs_format(part->label);
448+
// Don't use test_setup here, need to mount without formatting
449+
esp_vfs_spiffs_conf_t conf = {
450+
.base_path = "/spiffs",
451+
.partition_label = spiffs_test_partition_label,
452+
.max_files = 5,
453+
.format_if_mount_failed = false
454+
};
455+
TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
456+
FILE* f = fopen(filename, "r");
457+
TEST_ASSERT_NULL(f);
458+
test_teardown();
459+
}
460+
423461
TEST_CASE("can create and write file", "[spiffs]")
424462
{
425463
test_setup();

0 commit comments

Comments
 (0)