unix/modos: stat: Accept optional "follow_symlinks" arg, default True.
Of course, this arg is positional-only. Serves as the base to implement
CPython-compatible os.lstat() call.
#define USE_STATFS 1
#endif
-STATIC mp_obj_t mod_os_stat(mp_obj_t path_in) {
+STATIC mp_obj_t mod_os_stat(size_t n_args, const mp_obj_t *args) {
+ mp_obj_t path_in = args[0];
+ bool follow_symlinks = true;
+ if (n_args > 1) {
+ follow_symlinks = mp_obj_is_true(args[1]);
+ }
struct stat sb;
const char *path = mp_obj_str_get_str(path_in);
- int res = stat(path, &sb);
+ int res = (follow_symlinks ? stat : lstat)(path, &sb);
RAISE_ERRNO(res, errno);
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
@@ -65,7 +70,7 @@ STATIC mp_obj_t mod_os_stat(mp_obj_t path_in) {
t->items[9] = MP_OBJ_NEW_SMALL_INT(sb.st_ctime);
return MP_OBJ_FROM_PTR(t);
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_stat_obj, mod_os_stat);
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_os_stat_obj, 1, 2, mod_os_stat);
#if MICROPY_PY_OS_STATVFS
