Filesystem: Add support for stat

Provided through FileSystemLike::stat
pull/3762/head
Christopher Haster 2017-01-19 09:30:06 -06:00 committed by Simon Hughes
parent 7514476e2f
commit 9299a88e8f
4 changed files with 44 additions and 1 deletions

View File

@ -100,7 +100,15 @@ public:
*/ */
virtual int mkdir(const char *name, mode_t mode) { (void) name, (void) mode; return -1; } virtual int mkdir(const char *name, mode_t mode) { (void) name, (void) mode; return -1; }
// TODO other filesystem functions (mkdir, rm, rn, ls etc) /** Store information about file in stat structure
*
* @param name The name of the file to find information about
* @param st The stat buffer to write to
* @returns
* 0 on success or un-needed,
* -1 on error
*/
virtual int stat(const char *name, struct stat *st) = 0;
}; };
} // namespace mbed } // namespace mbed

View File

@ -182,6 +182,28 @@ int FATFileSystem::mkdir(const char *name, mode_t mode) {
return res == 0 ? 0 : -1; return res == 0 ? 0 : -1;
} }
int FATFileSystem::stat(const char *name, struct stat *st) {
lock();
FILINFO f;
memset(&f, 0, sizeof(f));
FRESULT res = f_stat(name, &f);
if (res != 0) {
unlock();
return -1;
}
st->st_size = f.fsize;
st->st_mode = 0;
st->st_mode |= (f.fattrib & AM_DIR) ? S_IFDIR : S_IFREG;
st->st_mode |= (f.fattrib & AM_RDO) ?
(S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) :
(S_IRWXU | S_IRWXG | S_IRWXO);
unlock();
return 0;
}
int FATFileSystem::mount() { int FATFileSystem::mount() {
lock(); lock();
FRESULT res = f_mount(&_fs, _fsid, 1); FRESULT res = f_mount(&_fs, _fsid, 1);

View File

@ -73,6 +73,11 @@ public:
*/ */
virtual int mkdir(const char *name, mode_t mode); virtual int mkdir(const char *name, mode_t mode);
/**
* Store information about file in stat structure
*/
virtual int stat(const char *name, struct stat *st);
/** /**
* Mounts the filesystem * Mounts the filesystem
*/ */

View File

@ -472,6 +472,14 @@ extern "C" int mkdir(const char *path, mode_t mode) {
return fs->mkdir(fp.fileName(), mode); return fs->mkdir(fp.fileName(), mode);
} }
extern "C" int stat(const char *path, struct stat *st) {
FilePath fp(path);
FileSystemLike *fs = fp.fileSystem();
if (fs == NULL) return -1;
return fs->stat(fp.fileName(), st);
}
#if defined(TOOLCHAIN_GCC) #if defined(TOOLCHAIN_GCC)
/* prevents the exception handling name demangling code getting pulled in */ /* prevents the exception handling name demangling code getting pulled in */
#include "mbed_error.h" #include "mbed_error.h"