diff --git a/src/filestore.hpp b/src/filestore.hpp index 196050820..6086671ca 100644 --- a/src/filestore.hpp +++ b/src/filestore.hpp @@ -27,6 +27,11 @@ class FileStore { typedef uint16_t InodeId_t; + struct StatInfo { + InodeId_t inodeId; + FsSize_t size; + }; + private: struct Inode { // the next Inode in memory @@ -92,6 +97,14 @@ class FileStore { */ int read(InodeId_t id, void *data, FsSize_t *size); + /** + * Reads the stat information of the inode of the given inode id. + * If the returned inode id is 0, then the requested inode was not found. + * @param id id of the inode to stat + * @return the stat information of the inode of the given inode id + */ + StatInfo stat(InodeId_t id); + static uint8_t version(); static uint8_t *format(uint8_t *buffer, FsSize_t size); @@ -232,6 +245,19 @@ int FileStore::read(InodeId_t id, void *data, FsSize_t *size) { return retval; } +template +typename FileStore::StatInfo FileStore::stat(InodeId_t id) { + auto inode = getRecord(m_root, id); + StatInfo stat; + if (inode) { + stat.size = inode->dataLen; + stat.inodeId = id; + } else { + stat.inodeId = 0; + } + return stat; +} + template typename FileStore::FsHeader *FileStore::getHeader() { return (FsHeader*) m_begin; diff --git a/src/filesystem.hpp b/src/filesystem.hpp index 947ede089..b936d5766 100644 --- a/src/filesystem.hpp +++ b/src/filesystem.hpp @@ -16,13 +16,32 @@ namespace wombat { namespace fs { +struct FileStat { + uint64_t inode; + uint64_t size; +}; + template class FileSystem { private: FileStore *store = nullptr; + + public: + int read(const char *path, void *buffer); + + FileStat stat(const char *path); }; +template +FileStat FileSystem::stat(const char *path) { + FileStat stat; + auto s = store->stat(path); + stat.size = s.size; + stat.inode = s.inodeId; + return stat; +} + typedef FileSystem FileSystem16; typedef FileSystem FileSystem32; typedef FileSystem FileSystem64; diff --git a/test/filestoreio.cpp b/test/filestoreio.cpp index 1c85d71c6..88c78028f 100644 --- a/test/filestoreio.cpp +++ b/test/filestoreio.cpp @@ -31,6 +31,7 @@ int main() { return 1; } + // make sure first value was not overwritten if (fs.read(1, (char*) out, &outSize) || strcmp("Hello", out)) { return 1;