From 9f36b7886d4b7745260ce8a8b3425757c12f8d84 Mon Sep 17 00:00:00 2001
From: Reinhold Gschweicher <pyro4hell@gmail.com>
Date: Mon, 4 Apr 2022 21:22:57 +0200
Subject: [PATCH] FS: copy lfs_info and implement FS::Stat()

Implement the subset of the functionality to be usable to check if the
file exists or not and check the size of the file.
---
 sim/components/fs/FS.cpp | 10 ++++++++++
 sim/components/fs/FS.h   | 25 ++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/sim/components/fs/FS.cpp b/sim/components/fs/FS.cpp
index 173995c..4d44b55 100644
--- a/sim/components/fs/FS.cpp
+++ b/sim/components/fs/FS.cpp
@@ -163,6 +163,16 @@ int FS::DirDelete(const char* path) {
   //return LFS_ERR_OK;
 }
 
+// check if file exists, if so write file-size into info object
+int FS::Stat(const char* path, lfs_info* info) {
+  if (!std::filesystem::exists(path))
+  {
+    return LFS_ERR_NOENT; // No directory entry
+  }
+  info->size = std::filesystem::file_size(path);
+  return LFS_ERR_OK;
+}
+
 /*
 
     ----------- Interface between littlefs and SpiNorFlash -----------
diff --git a/sim/components/fs/FS.h b/sim/components/fs/FS.h
index 708ba30..ab151e2 100644
--- a/sim/components/fs/FS.h
+++ b/sim/components/fs/FS.h
@@ -49,8 +49,31 @@ enum lfs_whence_flags {
     LFS_SEEK_END = 2,   // Seek relative to the end of the file
 };
 
+typedef uint32_t lfs_size_t;
 typedef int32_t  lfs_ssize_t;
 
+// Maximum name size in bytes, may be redefined to reduce the size of the
+// info struct. Limited to <= 1022. Stored in superblock and must be
+// respected by other littlefs drivers.
+//#ifndef LFS_NAME_MAX
+//#define LFS_NAME_MAX 255
+//#endif
+
+// File info structure
+struct lfs_info {
+//    // Type of the file, either LFS_TYPE_REG or LFS_TYPE_DIR
+//    uint8_t type;
+
+    // Size of the file, only valid for REG files. Limited to 32-bits.
+    lfs_size_t size;
+
+//    // Name of the file stored as a null-terminated string. Limited to
+//    // LFS_NAME_MAX+1, which can be changed by redefining LFS_NAME_MAX to
+//    // reduce RAM. LFS_NAME_MAX is stored in superblock and must be
+//    // respected by other littlefs drivers.
+//    char name[LFS_NAME_MAX+1];
+};
+
 namespace Pinetime {
   namespace Controllers {
     class FS {
@@ -77,7 +100,7 @@ namespace Pinetime {
 
       lfs_ssize_t GetFSSize();
       int Rename(const char* oldPath, const char* newPath);
-      //int Stat(const char* path, lfs_info* info);
+      int Stat(const char* path, lfs_info* info);
       void VerifyResource();
 
       static size_t getSize() {