| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- [cases.test_files_simple]
- defines.INLINE_MAX = [0, -1, 8]
- code = '''
- lfs_t lfs;
- lfs_format(&lfs, cfg) => 0;
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_t file;
- lfs_file_open(&lfs, &file, "hello",
- LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
- lfs_size_t size = strlen("Hello World!")+1;
- uint8_t buffer[1024];
- strcpy((char*)buffer, "Hello World!");
- lfs_file_write(&lfs, &file, buffer, size) => size;
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, "hello", LFS_O_RDONLY) => 0;
- lfs_file_read(&lfs, &file, buffer, size) => size;
- assert(strcmp((char*)buffer, "Hello World!") == 0);
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- '''
- [cases.test_files_large]
- defines.SIZE = [32, 8192, 262144, 0, 7, 8193]
- defines.CHUNKSIZE = [31, 16, 33, 1, 1023]
- defines.INLINE_MAX = [0, -1, 8]
- code = '''
- lfs_t lfs;
- lfs_format(&lfs, cfg) => 0;
- // write
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_t file;
- lfs_file_open(&lfs, &file, "avacado",
- LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
- uint32_t prng = 1;
- uint8_t buffer[1024];
- for (lfs_size_t i = 0; i < SIZE; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
- for (lfs_size_t b = 0; b < chunk; b++) {
- buffer[b] = TEST_PRNG(&prng) & 0xff;
- }
- lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
- }
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- // read
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
- lfs_file_size(&lfs, &file) => SIZE;
- prng = 1;
- for (lfs_size_t i = 0; i < SIZE; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- '''
- [cases.test_files_rewrite]
- defines.SIZE1 = [32, 8192, 131072, 0, 7, 8193]
- defines.SIZE2 = [32, 8192, 131072, 0, 7, 8193]
- defines.CHUNKSIZE = [31, 16, 1]
- defines.INLINE_MAX = [0, -1, 8]
- code = '''
- lfs_t lfs;
- lfs_format(&lfs, cfg) => 0;
- // write
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_t file;
- uint8_t buffer[1024];
- lfs_file_open(&lfs, &file, "avacado",
- LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
- uint32_t prng = 1;
- for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
- for (lfs_size_t b = 0; b < chunk; b++) {
- buffer[b] = TEST_PRNG(&prng) & 0xff;
- }
- lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
- }
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- // read
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
- lfs_file_size(&lfs, &file) => SIZE1;
- prng = 1;
- for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- // rewrite
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, "avacado", LFS_O_WRONLY) => 0;
- prng = 2;
- for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
- for (lfs_size_t b = 0; b < chunk; b++) {
- buffer[b] = TEST_PRNG(&prng) & 0xff;
- }
- lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
- }
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- // read
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
- lfs_file_size(&lfs, &file) => lfs_max(SIZE1, SIZE2);
- prng = 2;
- for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- if (SIZE1 > SIZE2) {
- prng = 1;
- for (lfs_size_t b = 0; b < SIZE2; b++) {
- TEST_PRNG(&prng);
- }
- for (lfs_size_t i = SIZE2; i < SIZE1; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- }
- lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- '''
- [cases.test_files_append]
- defines.SIZE1 = [32, 8192, 131072, 0, 7, 8193]
- defines.SIZE2 = [32, 8192, 131072, 0, 7, 8193]
- defines.CHUNKSIZE = [31, 16, 1]
- defines.INLINE_MAX = [0, -1, 8]
- code = '''
- lfs_t lfs;
- lfs_format(&lfs, cfg) => 0;
- // write
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_t file;
- uint8_t buffer[1024];
- lfs_file_open(&lfs, &file, "avacado",
- LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
- uint32_t prng = 1;
- for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
- for (lfs_size_t b = 0; b < chunk; b++) {
- buffer[b] = TEST_PRNG(&prng) & 0xff;
- }
- lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
- }
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- // read
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
- lfs_file_size(&lfs, &file) => SIZE1;
- prng = 1;
- for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- // append
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, "avacado", LFS_O_WRONLY | LFS_O_APPEND) => 0;
- prng = 2;
- for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
- for (lfs_size_t b = 0; b < chunk; b++) {
- buffer[b] = TEST_PRNG(&prng) & 0xff;
- }
- lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
- }
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- // read
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
- lfs_file_size(&lfs, &file) => SIZE1 + SIZE2;
- prng = 1;
- for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- prng = 2;
- for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- '''
- [cases.test_files_truncate]
- defines.SIZE1 = [32, 8192, 131072, 0, 7, 8193]
- defines.SIZE2 = [32, 8192, 131072, 0, 7, 8193]
- defines.CHUNKSIZE = [31, 16, 1]
- defines.INLINE_MAX = [0, -1, 8]
- code = '''
- lfs_t lfs;
- lfs_format(&lfs, cfg) => 0;
- // write
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_t file;
- uint8_t buffer[1024];
- lfs_file_open(&lfs, &file, "avacado",
- LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
- uint32_t prng = 1;
- for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
- for (lfs_size_t b = 0; b < chunk; b++) {
- buffer[b] = TEST_PRNG(&prng) & 0xff;
- }
- lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
- }
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- // read
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
- lfs_file_size(&lfs, &file) => SIZE1;
- prng = 1;
- for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- // truncate
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, "avacado", LFS_O_WRONLY | LFS_O_TRUNC) => 0;
- prng = 2;
- for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
- for (lfs_size_t b = 0; b < chunk; b++) {
- buffer[b] = TEST_PRNG(&prng) & 0xff;
- }
- lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
- }
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- // read
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
- lfs_file_size(&lfs, &file) => SIZE2;
- prng = 2;
- for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- '''
- [cases.test_files_reentrant_write]
- defines.SIZE = [32, 0, 7, 2049]
- defines.CHUNKSIZE = [31, 16, 65]
- defines.INLINE_MAX = [0, -1, 8]
- reentrant = true
- defines.POWERLOSS_BEHAVIOR = [
- 'LFS_EMUBD_POWERLOSS_NOOP',
- 'LFS_EMUBD_POWERLOSS_OOO',
- ]
- code = '''
- lfs_t lfs;
- int err = lfs_mount(&lfs, cfg);
- if (err) {
- lfs_format(&lfs, cfg) => 0;
- lfs_mount(&lfs, cfg) => 0;
- }
- lfs_file_t file;
- uint8_t buffer[1024];
- err = lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY);
- assert(err == LFS_ERR_NOENT || err == 0);
- if (err == 0) {
- // can only be 0 (new file) or full size
- lfs_size_t size = lfs_file_size(&lfs, &file);
- assert(size == 0 || size == SIZE);
- lfs_file_close(&lfs, &file) => 0;
- }
- // write
- lfs_file_open(&lfs, &file, "avacado", LFS_O_WRONLY | LFS_O_CREAT) => 0;
- uint32_t prng = 1;
- for (lfs_size_t i = 0; i < SIZE; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
- for (lfs_size_t b = 0; b < chunk; b++) {
- buffer[b] = TEST_PRNG(&prng) & 0xff;
- }
- lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
- }
- lfs_file_close(&lfs, &file) => 0;
- // read
- lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
- lfs_file_size(&lfs, &file) => SIZE;
- prng = 1;
- for (lfs_size_t i = 0; i < SIZE; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- '''
- [cases.test_files_reentrant_write_sync]
- defines = [
- # append (O(n))
- {MODE='LFS_O_APPEND',
- SIZE=[32, 0, 7, 2049],
- CHUNKSIZE=[31, 16, 65],
- INLINE_MAX=[0, -1, 8]},
- # truncate (O(n^2))
- {MODE='LFS_O_TRUNC',
- SIZE=[32, 0, 7, 200],
- CHUNKSIZE=[31, 16, 65],
- INLINE_MAX=[0, -1, 8]},
- # rewrite (O(n^2))
- {MODE=0,
- SIZE=[32, 0, 7, 200],
- CHUNKSIZE=[31, 16, 65],
- INLINE_MAX=[0, -1, 8]},
- ]
- reentrant = true
- code = '''
- lfs_t lfs;
- int err = lfs_mount(&lfs, cfg);
- if (err) {
- lfs_format(&lfs, cfg) => 0;
- lfs_mount(&lfs, cfg) => 0;
- }
- lfs_file_t file;
- uint8_t buffer[1024];
- err = lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY);
- assert(err == LFS_ERR_NOENT || err == 0);
- if (err == 0) {
- // with syncs we could be any size, but it at least must be valid data
- lfs_size_t size = lfs_file_size(&lfs, &file);
- assert(size <= SIZE);
- uint32_t prng = 1;
- for (lfs_size_t i = 0; i < size; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, size-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- lfs_file_close(&lfs, &file) => 0;
- }
- // write
- lfs_file_open(&lfs, &file, "avacado",
- LFS_O_WRONLY | LFS_O_CREAT | MODE) => 0;
- lfs_size_t size = lfs_file_size(&lfs, &file);
- assert(size <= SIZE);
- uint32_t prng = 1;
- lfs_size_t skip = (MODE == LFS_O_APPEND) ? size : 0;
- for (lfs_size_t b = 0; b < skip; b++) {
- TEST_PRNG(&prng);
- }
- for (lfs_size_t i = skip; i < SIZE; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
- for (lfs_size_t b = 0; b < chunk; b++) {
- buffer[b] = TEST_PRNG(&prng) & 0xff;
- }
- lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
- lfs_file_sync(&lfs, &file) => 0;
- }
- lfs_file_close(&lfs, &file) => 0;
- // read
- lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
- lfs_file_size(&lfs, &file) => SIZE;
- prng = 1;
- for (lfs_size_t i = 0; i < SIZE; i += CHUNKSIZE) {
- lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
- lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
- for (lfs_size_t b = 0; b < chunk; b++) {
- assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
- }
- }
- lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- '''
- [cases.test_files_many]
- defines.N = 300
- code = '''
- lfs_t lfs;
- lfs_format(&lfs, cfg) => 0;
- // create N files of 7 bytes
- lfs_mount(&lfs, cfg) => 0;
- for (int i = 0; i < N; i++) {
- lfs_file_t file;
- char path[1024];
- sprintf(path, "file_%03d", i);
- lfs_file_open(&lfs, &file, path,
- LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
- char wbuffer[1024];
- lfs_size_t size = 7;
- sprintf(wbuffer, "Hi %03d", i);
- lfs_file_write(&lfs, &file, wbuffer, size) => size;
- lfs_file_close(&lfs, &file) => 0;
- char rbuffer[1024];
- lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
- lfs_file_read(&lfs, &file, rbuffer, size) => size;
- assert(strcmp(rbuffer, wbuffer) == 0);
- lfs_file_close(&lfs, &file) => 0;
- }
- lfs_unmount(&lfs) => 0;
- '''
- [cases.test_files_many_power_cycle]
- defines.N = 300
- code = '''
- lfs_t lfs;
- lfs_format(&lfs, cfg) => 0;
- // create N files of 7 bytes
- lfs_mount(&lfs, cfg) => 0;
- for (int i = 0; i < N; i++) {
- lfs_file_t file;
- char path[1024];
- sprintf(path, "file_%03d", i);
- lfs_file_open(&lfs, &file, path,
- LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
- char wbuffer[1024];
- lfs_size_t size = 7;
- sprintf(wbuffer, "Hi %03d", i);
- lfs_file_write(&lfs, &file, wbuffer, size) => size;
- lfs_file_close(&lfs, &file) => 0;
- lfs_unmount(&lfs) => 0;
- char rbuffer[1024];
- lfs_mount(&lfs, cfg) => 0;
- lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
- lfs_file_read(&lfs, &file, rbuffer, size) => size;
- assert(strcmp(rbuffer, wbuffer) == 0);
- lfs_file_close(&lfs, &file) => 0;
- }
- lfs_unmount(&lfs) => 0;
- '''
- [cases.test_files_many_power_loss]
- defines.N = 300
- reentrant = true
- defines.POWERLOSS_BEHAVIOR = [
- 'LFS_EMUBD_POWERLOSS_NOOP',
- 'LFS_EMUBD_POWERLOSS_OOO',
- ]
- code = '''
- lfs_t lfs;
- int err = lfs_mount(&lfs, cfg);
- if (err) {
- lfs_format(&lfs, cfg) => 0;
- lfs_mount(&lfs, cfg) => 0;
- }
- // create N files of 7 bytes
- for (int i = 0; i < N; i++) {
- lfs_file_t file;
- char path[1024];
- sprintf(path, "file_%03d", i);
- err = lfs_file_open(&lfs, &file, path, LFS_O_WRONLY | LFS_O_CREAT);
- char wbuffer[1024];
- lfs_size_t size = 7;
- sprintf(wbuffer, "Hi %03d", i);
- if ((lfs_size_t)lfs_file_size(&lfs, &file) != size) {
- lfs_file_write(&lfs, &file, wbuffer, size) => size;
- }
- lfs_file_close(&lfs, &file) => 0;
- char rbuffer[1024];
- lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
- lfs_file_read(&lfs, &file, rbuffer, size) => size;
- assert(strcmp(rbuffer, wbuffer) == 0);
- lfs_file_close(&lfs, &file) => 0;
- }
- lfs_unmount(&lfs) => 0;
- '''
|