Filesystem: Initial integration with the FATFileSystem

pull/3773/head
Christopher Haster 2017-02-13 11:13:23 -06:00
parent ee3e920ed1
commit e5197ceaca
11 changed files with 524 additions and 718 deletions

View File

@ -69,7 +69,8 @@ ssize_t FileSystem::dir_read(fs_dir_t dir, char *path, size_t len)
ssize_t FileSystem::dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type)
{
return FS_ERROR_UNSUPPORTED;
*type = DT_UNKNOWN;
return dir_read(dir, path, len);
}
void FileSystem::dir_seek(fs_dir_t dir, off_t offset)

View File

@ -22,6 +22,7 @@
#include "drivers/FileBase.h"
#include "drivers/FileHandle.h"
#include "drivers/DirHandle.h"
#include "BlockDevice.h"
namespace mbed {
/** \addtogroup filesystem */
@ -56,7 +57,20 @@ class FileSystem {
public:
/** FileSystem lifetime
*/
~FileSystem() {}
virtual ~FileSystem() {}
/** Mounts a filesystem to a block device
*
* @param bd BlockDevice to mount to
* @return 0 on success, negative error code on failure
*/
virtual int mount(BlockDevice *bd) = 0;
/** Unmounts a filesystem from the underlying block device
*
* @return 0 on success, negative error code on failure
*/
virtual int unmount() = 0;
/** Remove a file from the filesystem.
*
@ -174,7 +188,7 @@ protected:
* @param file File handle
* @return Size of the file in bytes
*/
virtual size_t file_size(fs_file_t);
virtual size_t file_size(fs_file_t file);
/** Open a directory on the filesystem
*

View File

@ -1,116 +0,0 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2012 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include "ff.h"
#include "FATDirHandle.h"
#include "FATMisc.h"
using namespace mbed;
FATDirHandle::FATDirHandle(const FATFS_DIR &the_dir, PlatformMutex * mutex): _mutex(mutex)
{
dir = the_dir;
}
int FATDirHandle::closedir()
{
lock();
FRESULT retval = f_closedir(&dir);
fat_filesystem_set_errno(retval);
unlock();
delete this;
return retval;
}
struct dirent *FATDirHandle::readdir()
{
FILINFO finfo;
lock();
#if _USE_LFN
finfo.lfname = cur_entry.d_name;
finfo.lfsize = sizeof(cur_entry.d_name);
#endif // _USE_LFN
FRESULT res = f_readdir(&dir, &finfo);
fat_filesystem_set_errno(res);
cur_entry.d_type = (finfo.fattrib & AM_DIR) ? DT_DIR : DT_REG;
#if _USE_LFN
if(res != 0 || finfo.fname[0]==0) {
unlock();
return NULL;
} else {
if(cur_entry.d_name[0]==0) {
// No long filename so use short filename.
memcpy(cur_entry.d_name, finfo.fname, sizeof(finfo.fname));
}
unlock();
return &cur_entry;
}
#else
if(res != 0 || finfo.fname[0]==0) {
unlock();
return NULL;
} else {
memcpy(cur_entry.d_name, finfo.fname, sizeof(finfo.fname));
unlock();
return &cur_entry;
}
#endif /* _USE_LFN */
}
void FATDirHandle::rewinddir()
{
lock();
dir.index = 0;
fat_filesystem_set_errno(FR_OK);
unlock();
}
off_t FATDirHandle::telldir()
{
lock();
off_t offset = dir.index;
fat_filesystem_set_errno(FR_OK);
unlock();
return offset;
}
void FATDirHandle::seekdir(off_t location)
{
lock();
dir.index = location;
fat_filesystem_set_errno(FR_OK);
unlock();
}
void FATDirHandle::lock()
{
_mutex->lock();
}
void FATDirHandle::unlock()
{
_mutex->unlock();
}

View File

@ -1,54 +0,0 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2012 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef MBED_FATDIRHANDLE_H
#define MBED_FATDIRHANDLE_H
#include "DirHandle.h"
#include "PlatformMutex.h"
using namespace mbed;
class FATDirHandle : public DirHandle
{
public:
FATDirHandle(const FATFS_DIR &the_dir, PlatformMutex * mutex);
virtual int closedir();
virtual struct dirent *readdir();
virtual void rewinddir();
virtual off_t telldir();
virtual void seekdir(off_t location);
protected:
virtual void lock();
virtual void unlock();
PlatformMutex * _mutex;
private:
FATFS_DIR dir;
struct dirent cur_entry;
};
#endif

View File

@ -1,133 +0,0 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2012 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "ff.h"
#include "ffconf.h"
#include "mbed_debug.h"
#include "FATFileHandle.h"
#include "FATMisc.h"
FATFileHandle::FATFileHandle(FIL fh, PlatformMutex * mutex): _mutex(mutex)
{
_fh = fh;
}
int FATFileHandle::close()
{
lock();
FRESULT retval = f_close(&_fh);
fat_filesystem_set_errno(retval);
unlock();
delete this;
return retval;
}
ssize_t FATFileHandle::write(const void* buffer, size_t length)
{
lock();
UINT n;
FRESULT res = f_write(&_fh, buffer, length, &n);
fat_filesystem_set_errno(res);
if (res) {
debug_if(FFS_DBG, "f_write() failed: %d", res);
unlock();
return -1;
}
unlock();
return n;
}
ssize_t FATFileHandle::read(void* buffer, size_t length)
{
lock();
debug_if(FFS_DBG, "read(%d)\n", length);
UINT n;
FRESULT res = f_read(&_fh, buffer, length, &n);
fat_filesystem_set_errno(res);
if (res) {
debug_if(FFS_DBG, "f_read() failed: %d\n", res);
unlock();
return -1;
}
unlock();
return n;
}
int FATFileHandle::isatty()
{
fat_filesystem_set_errno(FR_OK);
return 0;
}
off_t FATFileHandle::lseek(off_t position, int whence)
{
lock();
if (whence == SEEK_END) {
position += _fh.fsize;
} else if(whence==SEEK_CUR) {
position += _fh.fptr;
}
FRESULT res = f_lseek(&_fh, position);
fat_filesystem_set_errno(res);
if (res) {
debug_if(FFS_DBG, "lseek failed: %d\n", res);
unlock();
return -1;
} else {
debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr);
unlock();
return _fh.fptr;
}
}
int FATFileHandle::fsync()
{
lock();
FRESULT res = f_sync(&_fh);
fat_filesystem_set_errno(res);
if (res) {
debug_if(FFS_DBG, "f_sync() failed: %d\n", res);
unlock();
return -1;
}
unlock();
return 0;
}
off_t FATFileHandle::flen()
{
lock();
off_t size = _fh.fsize;
fat_filesystem_set_errno(FR_OK);
unlock();
return size;
}
void FATFileHandle::lock()
{
_mutex->lock();
}
void FATFileHandle::unlock()
{
_mutex->unlock();
}

View File

@ -1,53 +0,0 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2012 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef MBED_FATFILEHANDLE_H
#define MBED_FATFILEHANDLE_H
#include "FileHandle.h"
#include "PlatformMutex.h"
using namespace mbed;
class FATFileHandle : public FileHandle
{
public:
FATFileHandle(FIL fh, PlatformMutex * mutex);
virtual int close();
virtual ssize_t write(const void* buffer, size_t length);
virtual ssize_t read(void* buffer, size_t length);
virtual int isatty();
virtual off_t lseek(off_t position, int whence);
virtual int fsync();
virtual off_t flen();
protected:
virtual void lock();
virtual void unlock();
FIL _fh;
PlatformMutex * _mutex;
};
#endif

View File

@ -24,14 +24,70 @@
#include "diskio.h"
#include "ffconf.h"
#include "mbed_debug.h"
#include "mbed_critical.h"
#include <errno.h>
#include "FATFileSystem.h"
#include "FATFileHandle.h"
#include "FATDirHandle.h"
#include "mbed_critical.h"
#include "FATMisc.h"
////// Error handling /////
void fat_filesystem_set_errno(FRESULT res)
{
switch(res) {
case FR_OK: /* (0) Succeeded */
errno = 0; /* no error */
break;
case FR_DISK_ERR: /* (1) A hard error occurred in the low level disk I/O layer */
case FR_NOT_READY: /* (3) The physical drive cannot work */
errno = EIO; /* I/O error */
break;
case FR_NO_FILE: /* (4) Could not find the file */
case FR_NO_PATH: /* (5) Could not find the path */
case FR_INVALID_NAME: /* (6) The path name format is invalid */
case FR_INVALID_DRIVE: /* (11) The logical drive number is invalid */
case FR_NO_FILESYSTEM: /* (13) There is no valid FAT volume */
errno = ENOENT; /* No such file or directory */
break;
case FR_DENIED: /* (7) Access denied due to prohibited access or directory full */
errno = EACCES; /* Permission denied */
break;
case FR_EXIST: /* (8) Access denied due to prohibited access */
errno = EEXIST; /* File exists */
break;
case FR_WRITE_PROTECTED: /* (10) The physical drive is write protected */
case FR_LOCKED: /* (16) The operation is rejected according to the file sharing policy */
errno = EACCES; /* Permission denied */
break;
case FR_INVALID_OBJECT: /* (9) The file/directory object is invalid */
errno = EFAULT; /* Bad address */
break;
case FR_NOT_ENABLED: /* (12) The volume has no work area */
errno = ENXIO; /* No such device or address */
break;
case FR_NOT_ENOUGH_CORE: /* (17) LFN working buffer could not be allocated */
errno = ENOMEM; /* Not enough space */
break;
case FR_TOO_MANY_OPEN_FILES: /* (18) Number of open files > _FS_LOCK */
errno = ENFILE; /* Too many open files in system */
break;
case FR_INVALID_PARAMETER: /* (19) Given parameter is invalid */
errno = ENOEXEC; /* Exec format error */
break;
case FR_INT_ERR: /* (2) Assertion failed */
case FR_MKFS_ABORTED: /* (14) The f_mkfs() aborted due to any parameter error */
case FR_TIMEOUT: /* (15) Could not get a grant to access the volume within defined period */
default:
errno = EBADF; /* Bad file number */
break;
}
return;
}
////// Disk operations //////
// Global access to block device from FAT driver
static BlockDevice *_ffs[_VOLUMES] = {0};
static SingletonPtr<PlatformMutex> _ffs_mutex;
@ -123,10 +179,13 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
return RES_PARERR;
}
////// Generic filesystem operations //////
// Filesystem implementation (See FATFilySystem.h)
FATFileSystem::FATFileSystem(const char *n, BlockDevice *bd)
: FileSystemLike(n), _id(-1)
{
FATFileSystem::FATFileSystem(const char *name, BlockDevice *bd)
: /*FileSystem(n),*/ _id(-1) {
// TODO handle mount name
if (bd) {
mount(bd);
}
@ -138,8 +197,12 @@ FATFileSystem::~FATFileSystem()
unmount();
}
int FATFileSystem::mount(BlockDevice *bd, bool force)
{
int FATFileSystem::mount(BlockDevice *bd) {
// requires duplicate definition to allow virtual overload to work
return mount(bd, false);
}
int FATFileSystem::mount(BlockDevice *bd, bool force) {
lock();
if (_id != -1) {
unlock();
@ -152,7 +215,7 @@ int FATFileSystem::mount(BlockDevice *bd, bool force)
_ffs[_id] = bd;
_fsid[0] = '0' + _id;
_fsid[1] = '\0';
debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid);
//debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid); TODO FIXME
FRESULT res = f_mount(&_fs, _fsid, force);
fat_filesystem_set_errno(res);
unlock();
@ -180,20 +243,6 @@ int FATFileSystem::unmount()
return res == 0 ? 0 : -1;
}
int FATFileSystem::sync()
{
lock();
if (_id == -1) {
unlock();
return -1;
}
// Always synchronized
fat_filesystem_set_errno(FR_OK);
unlock();
return 0;
}
/* See http://elm-chan.org/fsw/ff/en/mkfs.html for details of f_mkfs() and
* associated arguments. */
int FATFileSystem::format(BlockDevice *bd, int allocation_unit)
@ -217,12 +266,80 @@ int FATFileSystem::format(BlockDevice *bd, int allocation_unit)
return res == 0 ? 0 : -1;
}
FileHandle *FATFileSystem::open(const char* name, int flags)
{
int FATFileSystem::remove(const char *filename) {
lock();
debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, getName(), _fsid);
FRESULT res = f_unlink(filename);
fat_filesystem_set_errno(res);
if (res) {
debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
unlock();
return -1;
}
unlock();
return 0;
}
int FATFileSystem::rename(const char *oldname, const char *newname) {
lock();
FRESULT res = f_rename(oldname, newname);
fat_filesystem_set_errno(res);
if (res) {
debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
unlock();
return -1;
}
unlock();
return 0;
}
int FATFileSystem::mkdir(const char *name, mode_t mode) {
lock();
FRESULT res = f_mkdir(name);
fat_filesystem_set_errno(res);
unlock();
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);
fat_filesystem_set_errno(res);
if (res != 0) {
unlock();
return -1;
}
/* ARMCC doesnt support stat(), and these symbols are not defined by the toolchain. */
#ifdef TOOLCHAIN_GCC
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);
#endif /* TOOLCHAIN_GCC */
unlock();
return res == 0 ? 0 : -1;
}
void FATFileSystem::lock() {
_ffs_mutex->lock();
}
void FATFileSystem::unlock() {
_ffs_mutex->unlock();
}
////// File operations //////
int FATFileSystem::file_open(fs_file_t *file, const char *path, int flags) {
lock();
//debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, getName(), _fsid); TODO FIXME
char n[64];
sprintf(n, "%s:/%s", _fsid, name);
sprintf(n, "%s:/%s", _fsid, path);
/* POSIX flags -> FatFS open mode */
BYTE openmode;
@ -241,29 +358,75 @@ FileHandle *FATFileSystem::open(const char* name, int flags)
}
}
FIL fh;
FRESULT res = f_open(&fh, n, openmode);
FIL *fh = new FIL;
FRESULT res = f_open(fh, n, openmode);
fat_filesystem_set_errno(res);
if (res) {
debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
unlock();
return NULL;
delete fh;
return -1;
}
if (flags & O_APPEND) {
f_lseek(&fh, fh.fsize);
f_lseek(fh, fh->fsize);
}
FATFileHandle *handle = new FATFileHandle(fh, _ffs_mutex.get());
*file = fh;
unlock();
return handle;
return 0;
}
int FATFileSystem::remove(const char *filename)
{
int FATFileSystem::file_close(fs_file_t file) {
FIL *fh = static_cast<FIL*>(file);
lock();
FRESULT res = f_unlink(filename);
FRESULT retval = f_close(fh);
fat_filesystem_set_errno(retval);
unlock();
delete fh;
return retval;
}
ssize_t FATFileSystem::file_read(fs_file_t file, void *buffer, size_t len) {
FIL *fh = static_cast<FIL*>(file);
lock();
UINT n;
FRESULT res = f_read(fh, buffer, len, &n);
fat_filesystem_set_errno(res);
if (res) {
debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
debug_if(FFS_DBG, "f_read() failed: %d\n", res);
unlock();
return -1;
}
unlock();
return n;
}
ssize_t FATFileSystem::file_write(fs_file_t file, const void *buffer, size_t len) {
FIL *fh = static_cast<FIL*>(file);
lock();
UINT n;
FRESULT res = f_write(fh, buffer, len, &n);
fat_filesystem_set_errno(res);
if (res) {
debug_if(FFS_DBG, "f_write() failed: %d", res);
unlock();
return -1;
}
unlock();
return n;
}
int FATFileSystem::file_sync(fs_file_t file) {
FIL *fh = static_cast<FIL*>(file);
lock();
FRESULT res = f_sync(fh);
fat_filesystem_set_errno(res);
if (res) {
debug_if(FFS_DBG, "f_sync() failed: %d\n", res);
unlock();
return -1;
}
@ -271,78 +434,142 @@ int FATFileSystem::remove(const char *filename)
return 0;
}
int FATFileSystem::rename(const char *oldname, const char *newname)
{
off_t FATFileSystem::file_seek(fs_file_t file, off_t offset, int whence) {
FIL *fh = static_cast<FIL*>(file);
lock();
FRESULT res = f_rename(oldname, newname);
if (whence == SEEK_END) {
offset += fh->fsize;
} else if(whence==SEEK_CUR) {
offset += fh->fptr;
}
FRESULT res = f_lseek(fh, offset);
fat_filesystem_set_errno(res);
if (res) {
debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
debug_if(FFS_DBG, "lseek failed: %d\n", res);
unlock();
return -1;
} else {
debug_if(FFS_DBG, "lseek OK, returning %i\n", fh->fptr);
unlock();
return fh->fptr;
}
}
off_t FATFileSystem::file_tell(fs_file_t file) {
FIL *fh = static_cast<FIL*>(file);
lock();
off_t res = fh->fptr;
fat_filesystem_set_errno(FR_OK);
unlock();
return res;
}
size_t FATFileSystem::file_size(fs_file_t file) {
FIL *fh = static_cast<FIL*>(file);
lock();
size_t res = fh->fsize;
fat_filesystem_set_errno(FR_OK);
unlock();
return res;
}
////// Dir operations //////
int FATFileSystem::dir_open(fs_dir_t *dir, const char *path) {
lock();
FATFS_DIR *dh = new FATFS_DIR;
FRESULT res = f_opendir(dh, path);
fat_filesystem_set_errno(res);
if (res != 0) {
unlock();
delete dh;
return -1;
}
*dir = dh;
unlock();
return 0;
}
DirHandle *FATFileSystem::opendir(const char *name)
{
int FATFileSystem::dir_close(fs_dir_t dir) {
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
lock();
FATFS_DIR dir;
FRESULT res = f_opendir(&dir, name);
FRESULT retval = f_closedir(dh);
fat_filesystem_set_errno(retval);
unlock();
delete dh;
return retval;
}
ssize_t FATFileSystem::dir_read(fs_dir_t dir, char *path, size_t len) {
return dir_read(dir, path, len, NULL);
}
ssize_t FATFileSystem::dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type) {
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
FILINFO finfo;
lock();
#if _USE_LFN
finfo.lfname = path;
finfo.lfsize = len;
#endif // _USE_LFN
FRESULT res = f_readdir(dh, &finfo);
fat_filesystem_set_errno(res);
if (res != 0) {
unlock();
return NULL;
if (type) {
*type = (finfo.fattrib & AM_DIR) ? DT_DIR : DT_REG;
}
FATDirHandle *handle = new FATDirHandle(dir, _ffs_mutex.get());
unlock();
return handle;
}
int FATFileSystem::mkdir(const char *name, mode_t mode)
{
lock();
FRESULT res = f_mkdir(name);
fat_filesystem_set_errno(res);
unlock();
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);
fat_filesystem_set_errno(res);
if (res != 0) {
if(res != 0 || finfo.fname[0] == 0) {
unlock();
return -1;
} else {
#if _USE_LFN
if (path[0] == 0) {
// No long filename so use short filename.
strncpy(path, finfo.fname, len);
}
#else
strncpy(path, finfo.fname, len);
#endif
unlock();
return 0;
}
}
/* ARMCC doesnt support stat(), and these symbols are not defined by the toolchain.
* Build only for GCC_ARM compiler. */
#if defined(__GNU__)
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);
#endif /* __GNU__ */
void FATFileSystem::dir_seek(fs_dir_t dir, off_t offset) {
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
lock();
dh->index = offset;
fat_filesystem_set_errno(FR_OK);
unlock();
return res == 0 ? 0 : -1;
}
void FATFileSystem::lock()
{
_ffs_mutex->lock();
off_t FATFileSystem::dir_tell(fs_dir_t dir) {
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
lock();
off_t offset = dh->index;
fat_filesystem_set_errno(FR_OK);
unlock();
return offset;
}
void FATFileSystem::unlock()
{
_ffs_mutex->unlock();
void FATFileSystem::dir_rewind(fs_dir_t dir) {
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
lock();
dh->index = 0;
fat_filesystem_set_errno(FR_OK);
unlock();
}

View File

@ -22,7 +22,7 @@
#ifndef MBED_FATFILESYSTEM_H
#define MBED_FATFILESYSTEM_H
#include "FileSystemLike.h"
#include "FileSystem.h"
#include "BlockDevice.h"
#include "FileHandle.h"
#include "ff.h"
@ -34,107 +34,221 @@ using namespace mbed;
/**
* FATFileSystem based on ChaN's Fat Filesystem library v0.8
*/
class FATFileSystem : public FileSystemLike
{
class FATFileSystem : public FileSystem {
public:
FATFileSystem(const char* n, BlockDevice *bd = NULL);
/** Lifetime of the FATFileSystem
*
* @param name Name to add filesystem to tree as
* @param bd BlockDevice to mount, may be passed instead to mount call
*/
FATFileSystem(const char *name, BlockDevice *bd = NULL);
virtual ~FATFileSystem();
/**
* @brief Mounts the filesystem
/** Formats a logical drive, FDISK partitioning rule.
*
* @param bd
* This is the block device that will be formated.
* The block device to format should be mounted when this function is called.
*
* @param force
* Flag to underlying filesystem to force the mounting of the filesystem.
*/
virtual int mount(BlockDevice *bd, bool force = true);
/**
* Unmounts the filesystem
*/
virtual int unmount();
/**
* Flush any underlying transactions
*/
virtual int sync();
/**
* @brief Formats a logical drive, FDISK partitioning rule.
* @param bd
* This is the block device that will be formated.
*
* The block device to format should be mounted when this function is called.
*
* @param bd
* This is the block device that will be formated.
*
* @param allocation_unit
* This is the number of bytes per cluster size. The valid value is N
* times the sector size. N is a power of 2 from 1 to 128 for FAT
* volume and upto 16MiB for exFAT volume. If zero is given,
* the default allocation unit size is selected by the underlying
* filesystem, which depends on the volume size.
* @param allocation_unit
* This is the number of bytes per cluster size. The valid value is N
* times the sector size. N is a power of 2 from 1 to 128 for FAT
* volume and upto 16MiB for exFAT volume. If zero is given,
* the default allocation unit size is selected by the underlying
* filesystem, which depends on the volume size.
*/
static int format(BlockDevice *bd, int allocation_unit = 0);
/**
* Opens a file on the filesystem
/** Mounts a filesystem to a block device
*
* @param bd BlockDevice to mount to
* @return 0 on success, negative error code on failure
*/
virtual FileHandle *open(const char* name, int flags);
virtual int mount(BlockDevice *bd);
/**
* Removes a file path
/** Mounts a filesystem to a block device
*
* @param bd BlockDevice to mount to
* @param force Flag to force the underlying filesystem to force mounting the filesystem
* @return 0 on success, negative error code on failure
*/
virtual int remove(const char *filename);
virtual int mount(BlockDevice *bd, bool force);
/**
* Renames a file
/** Unmounts a filesystem from the underlying block device
*
* @return 0 on success, negative error code on failure
*/
virtual int rename(const char *oldname, const char *newname);
virtual int unmount();
/**
* Opens a directory on the filesystem
/** Remove a file from the filesystem.
*
* @param path The name of the file to remove.
* @return 0 on success, negative error code on failure
*/
virtual DirHandle *opendir(const char *name);
virtual int remove(const char *path);
/**
* Creates a directory path
/** Rename a file in the filesystem.
*
* @param path The name of the file to rename.
* @param newpath The name to rename it to
* @return 0 on success, negative error code on failure
*/
virtual int mkdir(const char *name, mode_t mode);
virtual int rename(const char *path, const char *newpath);
/**
* Store information about file in stat structure
/** Store information about the file in a stat structure
*
* @param path The name of the file to find information about
* @param st The stat buffer to write to
* @return 0 on success, negative error code on failure
*/
virtual int stat(const char *name, struct stat *st);
virtual int stat(const char *path, struct stat *st);
/** Create a directory in the filesystem.
*
* @param path The name of the directory to create.
* @param mode The permissions with which to create the directory
* @return 0 on success, negative error code on failure
*/
virtual int mkdir(const char *path, mode_t mode);
protected:
/** Open a file on the filesystem
*
* @param file Destination for the handle to a newly created file
* @param path The name of the file to open
* @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
* bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
* @return 0 on success, negative error code on failure
*/
virtual int file_open(fs_file_t *file, const char *path, int flags);
/** Close a file
*
* @param file File handle
* return 0 on success, negative error code on failure
*/
virtual int file_close(fs_file_t file);
/** Read the contents of a file into a buffer
*
* @param file File handle
* @param buffer The buffer to read in to
* @param size The number of bytes to read
* @return The number of bytes read, 0 at end of file, negative error on failure
*/
virtual ssize_t file_read(fs_file_t file, void *buffer, size_t len);
/** Write the contents of a buffer to a file
*
* @param file File handle
* @param buffer The buffer to write from
* @param size The number of bytes to write
* @return The number of bytes written, negative error on failure
*/
virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t len);
/** Flush any buffers associated with the file
*
* @param file File handle
* @return 0 on success, negative error code on failure
*/
virtual int file_sync(fs_file_t file);
/** Move the file position to a given offset from from a given location
*
* @param file File handle
* @param offset The offset from whence to move to
* @param whence The start of where to seek
* SEEK_SET to start from beginning of file,
* SEEK_CUR to start from current position in file,
* SEEK_END to start from end of file
* @return The new offset of the file
*/
virtual off_t file_seek(fs_file_t file, off_t offset, int whence);
/** Get the file position of the file
*
* @param file File handle
* @return The current offset in the file
*/
virtual off_t file_tell(fs_file_t file);
/** Get the size of the file
*
* @param file File handle
* @return Size of the file in bytes
*/
virtual size_t file_size(fs_file_t file);
/** Open a directory on the filesystem
*
* @param dir Destination for the handle to the directory
* @param path Name of the directory to open
* @return 0 on success, negative error code on failure
*/
virtual int dir_open(fs_dir_t *dir, const char *path);
/** Close a directory
*
* @param dir Dir handle
* return 0 on success, negative error code on failure
*/
virtual int dir_close(fs_dir_t dir);
/** Read the next directory entry
*
* @param dir Dir handle
* @param path The buffer to read the null terminated path name in to
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
*/
virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len);
/** Read the next directory entry
*
* @param dir Dir handle
* @param path The buffer to read the null terminated path name in to
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
* @param type The type of the file, one of DT_DIR, DT_REG, etc...
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
*/
virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type);
/** Set the current position of the directory
*
* @param dir Dir handle
* @param offset Offset of the location to seek to,
* must be a value returned from dir_tell
*/
virtual void dir_seek(fs_dir_t dir, off_t offset);
/** Get the current position of the directory
*
* @param dir Dir handle
* @return Position of the directory that can be passed to dir_rewind
*/
virtual off_t dir_tell(fs_dir_t dir);
/** Rewind the current position to the beginning of the directory
*
* @param dir Dir handle
*/
virtual void dir_rewind(fs_dir_t dir);
/** Get the sizeof the directory
*
* @param dir Dir handle
* @return Number of files in the directory
*/
virtual size_t dir_size(fs_dir_t dir);
private:
FATFS _fs; // Work area (file system object) for logical drive
char _fsid[2];
int _id;
/* Access to the underlying FAT filesystem implementation from multiple
* concurrent clients (i.e. threads) is serialised using a single
* platform mutex (_ffs_mutex) The implementation is therefore thread
* safe (but this requires further validation and testing).
* - The platform mutex _ffs_mutex is defined in the FileSystemLike
* implementation.
* - FileSystemLike uses the mutex to gain exclusive access to the
* underlying block device for mount()/unmount()/format()/sync()
* operations.
* - FileSystemLike::open() uses the mutex to serialise FATFileHandle
* object creation. The FATFileHandle constructor is passed a pointer
* to the platform mutex so file operations (e.g. read()/write()/flen()
* /fsync()) are serialised with the rest of the implementation.
* - FileSystemLike::opendir() uses the mutex to serialise FATDirHandle
* object creation. The FATDirHandle constructor is passed a pointer
* to the platform mutex so directory operations (e.g. readdir()/
* telldir()/rewinddir()/closedir()) are serialised with the rest
* of the implementation.
* - The platform mutex _ffs_mutex is taken/given using the lock()/unlock()
* methods defined below, and similar helper methods exist in the
* FATFileHandle and FATDirHandle classes.
*/
protected:
virtual void lock();
virtual void unlock();
};

View File

@ -1,76 +0,0 @@
/*
* mbed Microcontroller Library
* Copyright (c) 2006-2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file contains miscellaneous functionality used by the FAT filesystem interface code.
*/
#include "FATMisc.h"
#include <errno.h>
#include "platform/retarget.h"
void fat_filesystem_set_errno(FRESULT res)
{
switch(res) {
case FR_OK: /* (0) Succeeded */
errno = 0; /* no error */
break;
case FR_DISK_ERR: /* (1) A hard error occurred in the low level disk I/O layer */
case FR_NOT_READY: /* (3) The physical drive cannot work */
errno = EIO; /* I/O error */
break;
case FR_NO_FILE: /* (4) Could not find the file */
case FR_NO_PATH: /* (5) Could not find the path */
case FR_INVALID_NAME: /* (6) The path name format is invalid */
case FR_INVALID_DRIVE: /* (11) The logical drive number is invalid */
case FR_NO_FILESYSTEM: /* (13) There is no valid FAT volume */
errno = ENOENT; /* No such file or directory */
break;
case FR_DENIED: /* (7) Access denied due to prohibited access or directory full */
errno = EACCES; /* Permission denied */
break;
case FR_EXIST: /* (8) Access denied due to prohibited access */
errno = EEXIST; /* File exists */
break;
case FR_WRITE_PROTECTED: /* (10) The physical drive is write protected */
case FR_LOCKED: /* (16) The operation is rejected according to the file sharing policy */
errno = EACCES; /* Permission denied */
break;
case FR_INVALID_OBJECT: /* (9) The file/directory object is invalid */
errno = EFAULT; /* Bad address */
break;
case FR_NOT_ENABLED: /* (12) The volume has no work area */
errno = ENXIO; /* No such device or address */
break;
case FR_NOT_ENOUGH_CORE: /* (17) LFN working buffer could not be allocated */
errno = ENOMEM; /* Not enough space */
break;
case FR_TOO_MANY_OPEN_FILES: /* (18) Number of open files > _FS_LOCK */
errno = ENFILE; /* Too many open files in system */
break;
case FR_INVALID_PARAMETER: /* (19) Given parameter is invalid */
errno = ENOEXEC; /* Exec format error */
break;
case FR_INT_ERR: /* (2) Assertion failed */
case FR_MKFS_ABORTED: /* (14) The f_mkfs() aborted due to any parameter error */
case FR_TIMEOUT: /* (15) Could not get a grant to access the volume within defined period */
default:
errno = EBADF; /* Bad file number */
break;
}
return;
}

View File

@ -1,36 +0,0 @@
/*
* mbed Microcontroller Library
* Copyright (c) 2006-2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file contains miscellaneous functionality used by the FAT filesystem interface code.
*/
#ifndef FILESYSTEM_FAT_MISC_H
#define FILESYSTEM_FAT_MISC_H
#include "ff.h"
/* @brief Set errno based on the error code returned from underlying FAT32 filesystem.
*
* @note Note that errno reporting is not thread safe as this is not supported
* by current versions of standard libraries.
*
* @param res result returned from underlying filesystem which is mapped to a
* suitable errno value.
*
* @return No return value
*/void fat_filesystem_set_errno(FRESULT res);
#endif /* FILESYSTEM_FAT_MISC_H */

View File

@ -1,82 +0,0 @@
/* mbed Microcontroller Library - MemFileSystem
* Copyright (c) 2008, sford
*/
#ifndef MBED_MEMFILESYSTEM_H
#define MBED_MEMFILESYSTEM_H
#include "FATFileSystem.h"
namespace mbed
{
class MemFileSystem : public FATFileSystem
{
public:
// 2000 sectors, each 512 bytes (malloced as required)
char *sectors[2000];
MemFileSystem(const char* name) : FATFileSystem(name)
{
memset(sectors, 0, sizeof(sectors));
}
virtual ~MemFileSystem()
{
for(int i = 0; i < 2000; i++) {
if(sectors[i]) {
free(sectors[i]);
}
}
}
// read a sector in to the buffer, return 0 if ok
virtual int disk_read(char *buffer, int sector)
{
if(sectors[sector] == 0) {
// nothing allocated means sector is empty
memset(buffer, 0, 512);
} else {
memcpy(buffer, sectors[sector], 512);
}
return 0;
}
// write a sector from the buffer, return 0 if ok
virtual int disk_write(const char *buffer, int sector)
{
// if buffer is zero deallocate sector
char zero[512];
memset(zero, 0, 512);
if(memcmp(zero, buffer, 512)==0) {
if(sectors[sector] != 0) {
free(sectors[sector]);
sectors[sector] = 0;
}
return 0;
}
// else allocate a sector if needed, and write
if(sectors[sector] == 0) {
char *sec = (char*)malloc(512);
if(sec==0) {
return 1; // out of memory
}
sectors[sector] = sec;
}
memcpy(sectors[sector], buffer, 512);
return 0;
}
// return the number of sectors
virtual int disk_sectors()
{
return sizeof(sectors)/sizeof(sectors[0]);
}
};
}
#endif