Added utime() to the os librady

This commit is contained in:
Isaac Benitez 2022-09-18 18:43:27 -07:00
parent d2f28eb980
commit 5a21c30ab2
4 changed files with 42 additions and 0 deletions

View File

@ -415,6 +415,27 @@ STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);
STATIC mp_obj_t vfs_fat_utime(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t time_in) {
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
const char *path = mp_obj_str_get_str(path_in);
const int time = mp_obj_get_int(time_in);
timeutils_struct_time_t tm;
timeutils_seconds_since_epoch_to_struct_time(time, &tm);
FILINFO fno;
fno.fdate = (WORD)(((tm.tm_year - 1980) * 512U) | tm.tm_mon * 32U | tm.tm_mday);
fno.ftime = (WORD)(tm.tm_hour * 2048U | tm.tm_min * 32U | tm.tm_sec / 2U);
FRESULT res = f_utime(&self->fatfs, path, &fno);
if (res != FR_OK) {
mp_raise_OSError_fresult(res);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_utime_obj, vfs_fat_utime);
#if MICROPY_FATFS_USE_LABEL
STATIC mp_obj_t vfs_fat_getlabel(mp_obj_t self_in) {
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
@ -466,6 +487,7 @@ STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
{ MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&fat_vfs_utime_obj) },
#if MICROPY_FATFS_USE_LABEL
{ MP_ROM_QSTR(MP_QSTR_label), MP_ROM_PTR(&fat_vfs_label_obj) },
#endif

View File

@ -247,6 +247,17 @@ STATIC mp_obj_t os_urandom(mp_obj_t size_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
//| def utime(path: str, time: int) -> None:
//| """Change the timestamp of a file."""
//| ...
//|
STATIC mp_obj_t os_utime(mp_obj_t path_in, mp_obj_t time_in) {
const char *path = mp_obj_str_get_str(path_in);
common_hal_os_utime(path, time_in);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(os_utime_obj, os_utime);
STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_os) },
@ -263,6 +274,7 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&os_stat_obj) },
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&os_statvfs_obj) },
{ MP_ROM_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&os_remove_obj) }, // unlink aliases to remove
{ MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&os_utime_obj) },
{ MP_ROM_QSTR(MP_QSTR_sync), MP_ROM_PTR(&os_sync_obj) },

View File

@ -45,6 +45,7 @@ void common_hal_os_rename(const char *old_path, const char *new_path);
void common_hal_os_rmdir(const char *path);
mp_obj_t common_hal_os_stat(const char *path);
mp_obj_t common_hal_os_statvfs(const char *path);
void common_hal_os_utime(const char *path, mp_obj_t time);
// Returns true if data was correctly sourced from a true random number generator.
bool common_hal_os_urandom(uint8_t *buffer, mp_uint_t length);

View File

@ -226,3 +226,10 @@ mp_obj_t common_hal_os_statvfs(const char *path) {
}
return mp_vfs_proxy_call(vfs, MP_QSTR_statvfs, 1, &path_out);
}
void common_hal_os_utime(const char *path, mp_obj_t time) {
mp_obj_t args[2];
mp_vfs_mount_t *vfs = lookup_path(path, &args[0]);
args[1] = time;
mp_vfs_proxy_call(vfs, MP_QSTR_utime, 2, args);
}