Skip to content

Commit 616b14f

Browse files
committed
supervisor: Prepare for multiple files with optional contents
1 parent ebe8390 commit 616b14f

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

supervisor/shared/filesystem.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,23 @@ static void make_empty_file(FATFS *fatfs, const char *path) {
7070
f_close(&fp);
7171
}
7272

73+
#if CIRCUITPY_FULL_BUILD
74+
#define MAKE_FILE_WITH_OPTIONAL_CONTENTS(fatfs, filename, string_literal) do { \
75+
const byte buffer[] = string_literal; \
76+
make_file_with_contents(fatfs, filename, buffer, sizeof(buffer) - 1); \
77+
} while (0)
7378

74-
static void make_sample_code_file(FATFS *fatfs) {
75-
#if CIRCUITPY_FULL_BUILD
79+
static void make_file_with_contents(FATFS *fatfs, const char *filename, const byte *content, UINT size) {
7680
FIL fs;
77-
UINT char_written = 0;
78-
const byte buffer[] = "print(\"Hello World!\")\n";
7981
// Create or modify existing code.py file
80-
f_open(fatfs, &fs, "/code.py", FA_WRITE | FA_CREATE_ALWAYS);
81-
f_write(&fs, buffer, sizeof(buffer) - 1, &char_written);
82+
f_open(fatfs, &fs, filename, FA_WRITE | FA_CREATE_ALWAYS);
83+
f_write(&fs, content, size, &size);
8284
f_close(&fs);
83-
#else
84-
make_empty_file(fatfs, "/code.py");
85-
#endif
8685
}
86+
#else
87+
#define MAKE_FILE_WITH_OPTIONAL_CONTENTS(fatfs, filename, string_literal) \
88+
make_empty_file(fatfs, filename)
89+
#endif
8790

8891
// we don't make this function static because it needs a lot of stack and we
8992
// want it to be executed without using stack within main() function
@@ -141,7 +144,7 @@ bool filesystem_init(bool create_allowed, bool force_create) {
141144
make_empty_file(&vfs_fat->fatfs, "/settings.toml");
142145
#endif
143146
// make a sample code.py file
144-
make_sample_code_file(&vfs_fat->fatfs);
147+
MAKE_FILE_WITH_OPTIONAL_CONTENTS(&vfs_fat->fatfs, "/code.py", "print(\"Hello World!\")\n");
145148

146149
// create empty lib directory
147150
res = f_mkdir(&vfs_fat->fatfs, "/lib");

0 commit comments

Comments
 (0)