Skip to content

Commit 8a11d69

Browse files
committed
stmhal: Ability to stat /flash and /sd.
Addresses issue adafruit#780.
1 parent 2fe2a05 commit 8a11d69

1 file changed

Lines changed: 38 additions & 7 deletions

File tree

stmhal/modos.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@
4545
static char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */
4646
#endif
4747

48+
STATIC bool sd_in_root(void) {
49+
#if MICROPY_HW_HAS_SDCARD
50+
// TODO this is not the correct logic to check for /sd
51+
return sdcard_is_present();
52+
#else
53+
return false;
54+
#endif
55+
}
56+
4857
STATIC mp_obj_t os_chdir(mp_obj_t path_in) {
4958
const char *path;
5059
path = mp_obj_str_get_str(path_in);
@@ -92,11 +101,9 @@ STATIC mp_obj_t os_listdir(uint n_args, const mp_obj_t *args) {
92101
if (path[0] == '/' && path[1] == '\0') {
93102
mp_obj_t dir_list = mp_obj_new_list(0, NULL);
94103
mp_obj_list_append(dir_list, MP_OBJ_NEW_QSTR(MP_QSTR_flash));
95-
#if MICROPY_HW_HAS_SDCARD
96-
if (sdcard_is_present()) { // TODO this is not the correct logic to check for /sd
104+
if (sd_in_root()) {
97105
mp_obj_list_append(dir_list, MP_OBJ_NEW_QSTR(MP_QSTR_sd));
98106
}
99-
#endif
100107
return dir_list;
101108
}
102109

@@ -195,6 +202,21 @@ STATIC mp_obj_t os_rmdir(mp_obj_t path_o) {
195202
}
196203
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir);
197204

205+
// Checks for path equality, ignoring trailing slashes:
206+
// path_equal(/, /) -> true
207+
// path_equal(/flash//, /flash) -> true
208+
// second argument must be in canonical form (meaning no trailing slash, unless it's just /)
209+
STATIC bool path_equal(const char *path, const char *path_canonical) {
210+
for (; *path_canonical != '\0' && *path == *path_canonical; ++path, ++path_canonical) {
211+
}
212+
if (*path_canonical != '\0') {
213+
return false;
214+
}
215+
for (; *path == '/'; ++path) {
216+
}
217+
return *path == '\0';
218+
}
219+
198220
STATIC mp_obj_t os_stat(mp_obj_t path_in) {
199221
const char *path = mp_obj_str_get_str(path_in);
200222

@@ -204,16 +226,22 @@ STATIC mp_obj_t os_stat(mp_obj_t path_in) {
204226
fno.lfsize = 0;
205227
#endif
206228

207-
if (path[0] == '/' && path[1] == '\0') {
208-
// stat root directory
229+
FRESULT res;
230+
if (path_equal(path, "/") || path_equal(path, "/flash") || path_equal(path, "/sd")) {
231+
// stat built-in directory
232+
if (path[1] == 's' && !sd_in_root()) {
233+
// no /sd directory
234+
res = FR_NO_PATH;
235+
goto error;
236+
}
209237
fno.fsize = 0;
210238
fno.fdate = 0;
211239
fno.ftime = 0;
212240
fno.fattrib = AM_DIR;
213241
} else {
214-
FRESULT res = f_stat(path, &fno);
242+
res = f_stat(path, &fno);
215243
if (res != FR_OK) {
216-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
244+
goto error;
217245
}
218246
}
219247

@@ -244,6 +272,9 @@ STATIC mp_obj_t os_stat(mp_obj_t path_in) {
244272
t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime
245273

246274
return t;
275+
276+
error:
277+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
247278
}
248279
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat);
249280

0 commit comments

Comments
 (0)