From 96a42581be99dc2b76a42eec274ad5cf769c6af4 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sat, 1 Apr 2017 12:52:41 -0500 Subject: [PATCH] Added the lfs_stat function --- lfs.c | 28 ++++++++++++++++++++++++++++ lfs.h | 1 + 2 files changed, 29 insertions(+) diff --git a/lfs.c b/lfs.c index de12223..ac14730 100644 --- a/lfs.c +++ b/lfs.c @@ -1376,3 +1376,31 @@ int lfs_remove(lfs_t *lfs, const char *path) { return 0; } +int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { + lfs_dir_t cwd; + int err = lfs_dir_fetch(lfs, &cwd, lfs->cwd); + if (err) { + return err; + } + + lfs_entry_t entry; + err = lfs_dir_find(lfs, &cwd, &path, &entry); + if (err) { + return err; + } + + // TODO abstract out info assignment + memset(info, 0, sizeof(*info)); + info->type = entry.d.type & 0xff; + if (info->type == LFS_TYPE_REG) { + info->size = entry.d.u.file.size; + } + + err = lfs_bd_read(lfs, entry.dir[0], entry.off + sizeof(entry.d), + entry.d.len - sizeof(entry.d), info->name); + if (err) { + return err; + } + + return 0; +} diff --git a/lfs.h b/lfs.h index 4ac48ff..eac822f 100644 --- a/lfs.h +++ b/lfs.h @@ -138,6 +138,7 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *config); int lfs_unmount(lfs_t *lfs); int lfs_remove(lfs_t *lfs, const char *path); +int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info); int lfs_mkdir(lfs_t *lfs, const char *path); int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path);