Skip to content

Commit

Permalink
Merge d51b084 into 8da40ba
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgeorge committed Jul 28, 2020
2 parents 8da40ba + d51b084 commit b192532
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
9 changes: 8 additions & 1 deletion extmod/vfs_lfs.c
Expand Up @@ -30,13 +30,14 @@

#if MICROPY_VFS && (MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2)

enum { LFS_MAKE_ARG_bdev, LFS_MAKE_ARG_readsize, LFS_MAKE_ARG_progsize, LFS_MAKE_ARG_lookahead };
enum { LFS_MAKE_ARG_bdev, LFS_MAKE_ARG_readsize, LFS_MAKE_ARG_progsize, LFS_MAKE_ARG_lookahead, LFS_MAKE_ARG_mtime };

static const mp_arg_t lfs_make_allowed_args[] = {
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_readsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} },
{ MP_QSTR_progsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} },
{ MP_QSTR_lookahead, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} },
{ MP_QSTR_mtime, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
};

#if MICROPY_VFS_LFS1
Expand Down Expand Up @@ -98,9 +99,13 @@ mp_obj_t mp_vfs_lfs1_file_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode
#define MP_TYPE_VFS_LFSx mp_type_vfs_lfs2
#define MP_TYPE_VFS_LFSx_(s) mp_type_vfs_lfs2##s

// Attribute ids for lfs2_attr.type.
#define LFS_ATTR_MTIME (1)

typedef struct _mp_obj_vfs_lfs2_t {
mp_obj_base_t base;
mp_vfs_blockdev_t blockdev;
bool enable_mtime;
vstr_t cur_dir;
struct lfs2_config config;
lfs2_t lfs;
Expand All @@ -109,8 +114,10 @@ typedef struct _mp_obj_vfs_lfs2_t {
typedef struct _mp_obj_vfs_lfs2_file_t {
mp_obj_base_t base;
mp_obj_vfs_lfs2_t *vfs;
uint32_t mtime;
lfs2_file_t file;
struct lfs2_file_config cfg;
struct lfs2_attr attrs[1];
uint8_t file_buffer[0];
} mp_obj_vfs_lfs2_file_t;

Expand Down
25 changes: 22 additions & 3 deletions extmod/vfs_lfsx.c
Expand Up @@ -34,6 +34,7 @@
#include "py/objstr.h"
#include "py/mperrno.h"
#include "extmod/vfs.h"
#include "lib/timeutils/timeutils.h"

STATIC int MP_VFS_LFSx(dev_ioctl)(const struct LFSx_API (config) * c, int cmd, int arg, bool must_return_int) {
mp_obj_t ret = mp_vfs_blockdev_ioctl(c->context, cmd, arg);
Expand Down Expand Up @@ -120,6 +121,9 @@ STATIC mp_obj_t MP_VFS_LFSx(make_new)(const mp_obj_type_t * type, size_t n_args,
self->base.type = type;
vstr_init(&self->cur_dir, 16);
vstr_add_byte(&self->cur_dir, '/');
#if LFS_BUILD_VERSION == 2
self->enable_mtime = args[LFS_MAKE_ARG_mtime].u_bool;
#endif
MP_VFS_LFSx(init_config)(self, args[LFS_MAKE_ARG_bdev].u_obj,
args[LFS_MAKE_ARG_readsize].u_int, args[LFS_MAKE_ARG_progsize].u_int, args[LFS_MAKE_ARG_lookahead].u_int);
int ret = LFSx_API(mount)(&self->lfs, &self->config);
Expand Down Expand Up @@ -352,6 +356,21 @@ STATIC mp_obj_t MP_VFS_LFSx(stat)(mp_obj_t self_in, mp_obj_t path_in) {
mp_raise_OSError(-ret);
}

uint32_t mtime = 0;
#if LFS_BUILD_VERSION == 2
lfs2_ssize_t sz = lfs2_getattr(&self->lfs, path, LFS_ATTR_MTIME, &mtime, sizeof(mtime));
if (sz == sizeof(mtime)) {
mtime = timeutils_seconds_since_2000(
1980 + ((mtime >> 25) & 0x7f),
(mtime >> 21) & 0x0f,
(mtime >> 16) & 0x1f,
(mtime >> 11) & 0x1f,
(mtime >> 5) & 0x3f,
2 * (mtime & 0x1f)
);
}
#endif

mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
t->items[0] = MP_OBJ_NEW_SMALL_INT(info.type == LFSx_MACRO(_TYPE_REG) ? MP_S_IFREG : MP_S_IFDIR); // st_mode
t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
Expand All @@ -360,9 +379,9 @@ STATIC mp_obj_t MP_VFS_LFSx(stat)(mp_obj_t self_in, mp_obj_t path_in) {
t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
t->items[6] = mp_obj_new_int_from_uint(info.size); // st_size
t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // st_atime
t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // st_mtime
t->items[9] = MP_OBJ_NEW_SMALL_INT(0); // st_ctime
t->items[7] = MP_OBJ_NEW_SMALL_INT(mtime); // st_atime
t->items[8] = MP_OBJ_NEW_SMALL_INT(mtime); // st_mtime
t->items[9] = MP_OBJ_NEW_SMALL_INT(mtime); // st_ctime

return MP_OBJ_FROM_PTR(t);
}
Expand Down
19 changes: 19 additions & 0 deletions extmod/vfs_lfsx_file.c
Expand Up @@ -32,6 +32,9 @@
#include "py/mperrno.h"
#include "extmod/vfs.h"

// TODO use something better like mp_hal_time?
uint32_t get_fattime(void);

STATIC void MP_VFS_LFSx(check_open)(MP_OBJ_VFS_LFSx_FILE * self) {
if (self->vfs == NULL) {
mp_raise_ValueError(NULL);
Expand Down Expand Up @@ -101,6 +104,17 @@ mp_obj_t MP_VFS_LFSx(file_open)(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mod
#endif
o->cfg.buffer = &o->file_buffer[0];

#if LFS_BUILD_VERSION == 2
if (self->enable_mtime) {
o->mtime = get_fattime();
o->attrs[0].type = LFS_ATTR_MTIME;
o->attrs[0].buffer = &o->mtime;
o->attrs[0].size = sizeof(o->mtime);
o->cfg.attrs = &o->attrs[0];
o->cfg.attr_count = MP_ARRAY_SIZE(o->attrs);
}
#endif

const char *path = MP_VFS_LFSx(make_path)(self, path_in);
int ret = LFSx_API(file_opencfg)(&self->lfs, &o->file, path, flags, &o->cfg);
if (ret < 0) {
Expand Down Expand Up @@ -131,6 +145,11 @@ STATIC mp_uint_t MP_VFS_LFSx(file_read)(mp_obj_t self_in, void *buf, mp_uint_t s
STATIC mp_uint_t MP_VFS_LFSx(file_write)(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
MP_OBJ_VFS_LFSx_FILE *self = MP_OBJ_TO_PTR(self_in);
MP_VFS_LFSx(check_open)(self);
#if LFS_BUILD_VERSION == 2
if (self->vfs->enable_mtime) {
self->mtime = get_fattime();
}
#endif
LFSx_API(ssize_t) sz = LFSx_API(file_write)(&self->vfs->lfs, &self->file, buf, size);
if (sz < 0) {
*errcode = -sz;
Expand Down
10 changes: 9 additions & 1 deletion ports/unix/fatfs_port.c
@@ -1,5 +1,13 @@
#include <time.h>
#include "lib/oofatfs/ff.h"

DWORD get_fattime(void) {
return 0;
time_t now = time(NULL);
struct tm *tm = localtime(&now);
return ((1900 + tm->tm_year - 1980) << 25)
| (tm->tm_mon << 21)
| (tm->tm_mday << 16)
| (tm->tm_hour << 11)
| (tm->tm_min << 5)
| (tm->tm_sec / 2);
}

0 comments on commit b192532

Please sign in to comment.