Added file stat.
This commit is contained in:
@@ -27,6 +27,11 @@ class FileStore {
|
|||||||
|
|
||||||
typedef uint16_t InodeId_t;
|
typedef uint16_t InodeId_t;
|
||||||
|
|
||||||
|
struct StatInfo {
|
||||||
|
InodeId_t inodeId;
|
||||||
|
FsSize_t size;
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Inode {
|
struct Inode {
|
||||||
// the next Inode in memory
|
// the next Inode in memory
|
||||||
@@ -92,6 +97,14 @@ class FileStore {
|
|||||||
*/
|
*/
|
||||||
int read(InodeId_t id, void *data, FsSize_t *size);
|
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 version();
|
||||||
|
|
||||||
static uint8_t *format(uint8_t *buffer, FsSize_t size);
|
static uint8_t *format(uint8_t *buffer, FsSize_t size);
|
||||||
@@ -232,6 +245,19 @@ int FileStore<FsSize_t>::read(InodeId_t id, void *data, FsSize_t *size) {
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename FsSize_t>
|
||||||
|
typename FileStore<FsSize_t>::StatInfo FileStore<FsSize_t>::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 FsSize_t>
|
template<typename FsSize_t>
|
||||||
typename FileStore<FsSize_t>::FsHeader *FileStore<FsSize_t>::getHeader() {
|
typename FileStore<FsSize_t>::FsHeader *FileStore<FsSize_t>::getHeader() {
|
||||||
return (FsHeader*) m_begin;
|
return (FsHeader*) m_begin;
|
||||||
|
|||||||
@@ -16,13 +16,32 @@
|
|||||||
namespace wombat {
|
namespace wombat {
|
||||||
namespace fs {
|
namespace fs {
|
||||||
|
|
||||||
|
struct FileStat {
|
||||||
|
uint64_t inode;
|
||||||
|
uint64_t size;
|
||||||
|
};
|
||||||
|
|
||||||
template<typename FileStore>
|
template<typename FileStore>
|
||||||
class FileSystem {
|
class FileSystem {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FileStore *store = nullptr;
|
FileStore *store = nullptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
int read(const char *path, void *buffer);
|
||||||
|
|
||||||
|
FileStat stat(const char *path);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename FileStore>
|
||||||
|
FileStat FileSystem<FileStore>::stat(const char *path) {
|
||||||
|
FileStat stat;
|
||||||
|
auto s = store->stat(path);
|
||||||
|
stat.size = s.size;
|
||||||
|
stat.inode = s.inodeId;
|
||||||
|
return stat;
|
||||||
|
}
|
||||||
|
|
||||||
typedef FileSystem<FileStore16> FileSystem16;
|
typedef FileSystem<FileStore16> FileSystem16;
|
||||||
typedef FileSystem<FileStore32> FileSystem32;
|
typedef FileSystem<FileStore32> FileSystem32;
|
||||||
typedef FileSystem<FileStore64> FileSystem64;
|
typedef FileSystem<FileStore64> FileSystem64;
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ int main() {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure first value was not overwritten
|
||||||
if (fs.read(1, (char*) out, &outSize) ||
|
if (fs.read(1, (char*) out, &outSize) ||
|
||||||
strcmp("Hello", out)) {
|
strcmp("Hello", out)) {
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user