mirror of https://github.com/ARMmbed/mbed-os.git
STORAGE: fixing error handling for fopen() and other upper edge filesystem API methods.
- integrated work with new block api. - Conflicts: features/filesystem/fat/FATFileSystem.cpppull/3762/head
parent
0b7a2ca030
commit
860d7f03f2
|
@ -84,29 +84,43 @@ using namespace utest::v1;
|
|||
#define DEVICE_SPI
|
||||
#if defined(DEVICE_SPI) && defined(FSFAT_SDCARD_INSTALLED)
|
||||
|
||||
#define FSFAT_BASIC_TEST_00 fsfat_basic_test_00
|
||||
#define FSFAT_BASIC_TEST_01 fsfat_basic_test_01
|
||||
#define FSFAT_BASIC_TEST_02 fsfat_basic_test_02
|
||||
#define FSFAT_BASIC_TEST_03 fsfat_basic_test_03
|
||||
#define FSFAT_BASIC_TEST_04 fsfat_basic_test_04
|
||||
|
||||
#define FSFAT_BASIC_MSG_BUF_SIZE 256
|
||||
|
||||
static const char *sd_file_path = "/sd/out.txt";
|
||||
static const char *sd_mount_pt = "sd";
|
||||
const int FSFAT_BASIC_DATA_SIZE = 256;
|
||||
static char fsfat_basic_msg_g[FSFAT_BASIC_MSG_BUF_SIZE];
|
||||
|
||||
|
||||
#if defined(TARGET_KL25Z)
|
||||
SDBlockDevice sd(PTD2, PTD3, PTD1, PTD0);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_KL46Z) || defined(TARGET_KL43Z)
|
||||
SDBlockDevice sd(PTD6, PTD7, PTD5, PTD4);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_K64F) || defined(TARGET_K66F)
|
||||
SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_K22F)
|
||||
SDBlockDevice sd(PTD6, PTD7, PTD5, PTD4);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_K20D50M)
|
||||
SDBlockDevice sd(PTD2, PTD3, PTD1, PTC2);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_nRF51822)
|
||||
SDBlockDevice sd(p12, p13, p15, p14);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_NUCLEO_F030R8) || \
|
||||
defined(TARGET_NUCLEO_F070RB) || \
|
||||
|
@ -123,52 +137,38 @@ FATFileSystem fs("sd", &sd);
|
|||
defined(TARGET_NUCLEO_L073RZ) || \
|
||||
defined(TARGET_NUCLEO_L152RE)
|
||||
SDBlockDevice sd(D11, D12, D13, D10);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_DISCO_F051R8) || \
|
||||
defined(TARGET_NUCLEO_L031K6)
|
||||
SDBlockDevice sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_LPC2368)
|
||||
SDBlockDevice sd(p11, p12, p13, p14);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_LPC11U68)
|
||||
SDBlockDevice sd(D11, D12, D13, D10);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_LPC1549)
|
||||
SDBlockDevice sd(D11, D12, D13, D10);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_RZ_A1H)
|
||||
SDBlockDevice sd(P8_5, P8_6, P8_3, P8_4);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#elif defined(TARGET_LPC11U37H_401)
|
||||
SDBlockDevice sd(SDMOSI, SDMISO, SDSCLK, SDSSEL);
|
||||
FATFileSystem fs("sd", &sd);
|
||||
FATFileSystem fs(sd_mount_pt, &sd);
|
||||
|
||||
#else
|
||||
#error "[NOT SUPPORTED] Instantiate SDBlockDevice sd(p11, p12, p13, p14) with the correct pin specification for target"
|
||||
#endif
|
||||
|
||||
|
||||
#define FSFAT_BASIC_TEST_00 fsfat_basic_test_00
|
||||
#define FSFAT_BASIC_TEST_01 fsfat_basic_test_01
|
||||
#define FSFAT_BASIC_TEST_02 fsfat_basic_test_02
|
||||
#define FSFAT_BASIC_TEST_03 fsfat_basic_test_03
|
||||
#define FSFAT_BASIC_TEST_04 fsfat_basic_test_04
|
||||
|
||||
|
||||
#define FSFAT_BASIC_MSG_BUF_SIZE 256
|
||||
|
||||
static const char *sd_file_path = "/sd/out.txt";
|
||||
static const char *sd_mount_pt = "sd";
|
||||
const int FSFAT_BASIC_DATA_SIZE = 256;
|
||||
static char fsfat_basic_msg_g[FSFAT_BASIC_MSG_BUF_SIZE];
|
||||
|
||||
#define FSFAT_BASIC_MSG(_buf, _max_len, _fmt, ...) \
|
||||
do \
|
||||
{ \
|
||||
|
@ -190,13 +190,12 @@ static control_t fsfat_basic_test_00()
|
|||
{
|
||||
|
||||
uint8_t data_written[FSFAT_BASIC_DATA_SIZE] = { 0 };
|
||||
bool result = false;
|
||||
bool read_result = false;
|
||||
bool write_result = false;
|
||||
|
||||
// Fill data_written buffer with random data
|
||||
// Write these data into the file
|
||||
FSFAT_FENTRYLOG("%s:entered\n", __func__);
|
||||
bool write_result = false;
|
||||
{
|
||||
printf("SD: Writing ... ");
|
||||
FILE *f = fopen(sd_file_path, "w");
|
||||
|
@ -210,13 +209,14 @@ static control_t fsfat_basic_test_00()
|
|||
}
|
||||
printf("[%s]\r\n", write_result ? "OK" : "FAIL");
|
||||
}
|
||||
TEST_ASSERT_MESSAGE(write_result == true, "Error: write_result is set to false.");
|
||||
|
||||
// Read back the data from the file and store them in data_read
|
||||
{
|
||||
printf("SD: Reading data ... ");
|
||||
FILE *f = fopen(sd_file_path, "r");
|
||||
if (f) {
|
||||
read_result = true;
|
||||
read_result = true;
|
||||
for (int i = 0; i < FSFAT_BASIC_DATA_SIZE; i++) {
|
||||
uint8_t data = fgetc(f);
|
||||
if (data != data_written[i]) {
|
||||
|
@ -228,14 +228,11 @@ static control_t fsfat_basic_test_00()
|
|||
}
|
||||
printf("[%s]\r\n", read_result ? "OK" : "FAIL");
|
||||
}
|
||||
result = write_result && read_result;
|
||||
TEST_ASSERT_MESSAGE(read_result == true, "Error: write_result is set to false.");
|
||||
return CaseNext;
|
||||
}
|
||||
|
||||
|
||||
// todo: commented out for ARMCC support. It this still needed for gcc?
|
||||
//extern int errno;
|
||||
|
||||
/** @brief test-fseek.c test ported from glibc project. See the licence at REF_LICENCE_GLIBC.
|
||||
*
|
||||
* @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
|
||||
|
@ -310,7 +307,7 @@ static control_t fsfat_basic_test_02()
|
|||
static const char hello[] = "Hello, world.\n";
|
||||
static const char replace[] = "Hewwo, world.\n";
|
||||
static const size_t replace_from = 2, replace_to = 4;
|
||||
char* filename = (char*) sd_file_path; //todo: remove
|
||||
char* filename = (char*) sd_file_path;
|
||||
char buf[BUFSIZ];
|
||||
FILE *f;
|
||||
int lose = 0;
|
||||
|
@ -324,7 +321,6 @@ static control_t fsfat_basic_test_02()
|
|||
TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g);
|
||||
}
|
||||
|
||||
|
||||
ret = fputs(hello, f);
|
||||
if (ret == EOF) {
|
||||
FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fputs() failed to write string to file (filename=%s, string=%s).\n", __func__, filename, hello);
|
||||
|
@ -393,7 +389,7 @@ static control_t fsfat_basic_test_02()
|
|||
}
|
||||
else
|
||||
{
|
||||
printf("ftell returns %u; should be %u.\n", where, replace_from);
|
||||
printf("ftell returns %ld; should be %u.\n", where, replace_from);
|
||||
lose = 1;
|
||||
}
|
||||
}
|
||||
|
@ -430,8 +426,8 @@ static control_t fsfat_basic_test_02()
|
|||
*/
|
||||
static control_t fsfat_basic_test_03()
|
||||
{
|
||||
char *fn;
|
||||
FILE *fp;
|
||||
char *fn = NULL;
|
||||
FILE *fp = NULL;
|
||||
char *files[500];
|
||||
int i;
|
||||
|
||||
|
@ -538,7 +534,7 @@ Case cases[] = {
|
|||
/* 1 2 3 4 5 6 7 */
|
||||
/* 1234567890123456789012345678901234567890123456789012345678901234567890 */
|
||||
Case("FSFAT_BASIC_TEST_00: fopen()/fgetc()/fprintf()/fclose() test.", FSFAT_BASIC_TEST_00),
|
||||
Case("FSFAT_BASIC_TEST_01: fopen()/fseek()/fclose() test.", FSFAT_BASIC_TEST_01)
|
||||
Case("FSFAT_BASIC_TEST_01: fopen()/fseek()/fclose() test.", FSFAT_BASIC_TEST_01),
|
||||
/* WARNING: Test case not working but currently not required for PAL support
|
||||
* Case("FSFAT_BASIC_TEST_02: fopen()/fgets()/fputs()/ftell()/rewind()/remove() test.", FSFAT_BASIC_TEST_02)
|
||||
* Case("FSFAT_BASIC_TEST_03: tmpnam() test.", FSFAT_BASIC_TEST_03)
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <string.h>
|
||||
#include "ff.h"
|
||||
#include "FATDirHandle.h"
|
||||
#include "FATMisc.h"
|
||||
|
||||
using namespace mbed;
|
||||
|
||||
|
@ -31,7 +32,8 @@ FATDirHandle::FATDirHandle(const FATFS_DIR &the_dir, PlatformMutex * mutex): _mu
|
|||
|
||||
int FATDirHandle::closedir() {
|
||||
lock();
|
||||
int retval = f_closedir(&dir);
|
||||
FRESULT retval = f_closedir(&dir);
|
||||
FATFileSystemSetErrno(retval);
|
||||
unlock();
|
||||
delete this;
|
||||
return retval;
|
||||
|
@ -47,6 +49,7 @@ struct dirent *FATDirHandle::readdir() {
|
|||
#endif // _USE_LFN
|
||||
|
||||
FRESULT res = f_readdir(&dir, &finfo);
|
||||
FATFileSystemSetErrno(res);
|
||||
|
||||
#if _USE_LFN
|
||||
if(res != 0 || finfo.fname[0]==0) {
|
||||
|
@ -75,12 +78,14 @@ struct dirent *FATDirHandle::readdir() {
|
|||
void FATDirHandle::rewinddir() {
|
||||
lock();
|
||||
dir.index = 0;
|
||||
FATFileSystemSetErrno(FR_OK);
|
||||
unlock();
|
||||
}
|
||||
|
||||
off_t FATDirHandle::telldir() {
|
||||
lock();
|
||||
off_t offset = dir.index;
|
||||
FATFileSystemSetErrno(FR_OK);
|
||||
unlock();
|
||||
return offset;
|
||||
}
|
||||
|
@ -88,6 +93,7 @@ off_t FATDirHandle::telldir() {
|
|||
void FATDirHandle::seekdir(off_t location) {
|
||||
lock();
|
||||
dir.index = location;
|
||||
FATFileSystemSetErrno(FR_OK);
|
||||
unlock();
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "mbed_debug.h"
|
||||
|
||||
#include "FATFileHandle.h"
|
||||
#include "FATMisc.h"
|
||||
|
||||
FATFileHandle::FATFileHandle(FIL fh, PlatformMutex * mutex): _mutex(mutex) {
|
||||
_fh = fh;
|
||||
|
@ -31,7 +32,8 @@ FATFileHandle::FATFileHandle(FIL fh, PlatformMutex * mutex): _mutex(mutex) {
|
|||
|
||||
int FATFileHandle::close() {
|
||||
lock();
|
||||
int retval = f_close(&_fh);
|
||||
FRESULT retval = f_close(&_fh);
|
||||
FATFileSystemSetErrno(retval);
|
||||
unlock();
|
||||
delete this;
|
||||
return retval;
|
||||
|
@ -41,6 +43,7 @@ ssize_t FATFileHandle::write(const void* buffer, size_t length) {
|
|||
lock();
|
||||
UINT n;
|
||||
FRESULT res = f_write(&_fh, buffer, length, &n);
|
||||
FATFileSystemSetErrno(res);
|
||||
if (res) {
|
||||
debug_if(FFS_DBG, "f_write() failed: %d", res);
|
||||
unlock();
|
||||
|
@ -55,6 +58,7 @@ ssize_t FATFileHandle::read(void* buffer, size_t length) {
|
|||
debug_if(FFS_DBG, "read(%d)\n", length);
|
||||
UINT n;
|
||||
FRESULT res = f_read(&_fh, buffer, length, &n);
|
||||
FATFileSystemSetErrno(res);
|
||||
if (res) {
|
||||
debug_if(FFS_DBG, "f_read() failed: %d\n", res);
|
||||
unlock();
|
||||
|
@ -65,6 +69,7 @@ ssize_t FATFileHandle::read(void* buffer, size_t length) {
|
|||
}
|
||||
|
||||
int FATFileHandle::isatty() {
|
||||
FATFileSystemSetErrno(FR_OK);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -76,6 +81,7 @@ off_t FATFileHandle::lseek(off_t position, int whence) {
|
|||
position += _fh.fptr;
|
||||
}
|
||||
FRESULT res = f_lseek(&_fh, position);
|
||||
FATFileSystemSetErrno(res);
|
||||
if (res) {
|
||||
debug_if(FFS_DBG, "lseek failed: %d\n", res);
|
||||
unlock();
|
||||
|
@ -90,6 +96,7 @@ off_t FATFileHandle::lseek(off_t position, int whence) {
|
|||
int FATFileHandle::fsync() {
|
||||
lock();
|
||||
FRESULT res = f_sync(&_fh);
|
||||
FATFileSystemSetErrno(res);
|
||||
if (res) {
|
||||
debug_if(FFS_DBG, "f_sync() failed: %d\n", res);
|
||||
unlock();
|
||||
|
@ -102,6 +109,7 @@ int FATFileHandle::fsync() {
|
|||
off_t FATFileHandle::flen() {
|
||||
lock();
|
||||
off_t size = _fh.fsize;
|
||||
FATFileSystemSetErrno(FR_OK);
|
||||
unlock();
|
||||
return size;
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
//<<<<<<< HEAD
|
||||
//<<<<<<< HEAD
|
||||
//<<<<<<< HEAD
|
||||
//<<<<<<< HEAD
|
||||
#include "mbed_critical.h"
|
||||
//=======
|
||||
//#include "critical.h"
|
||||
|
@ -49,6 +50,10 @@
|
|||
* consistent values for all toolchains */
|
||||
#include "toolchain_support.h"
|
||||
//>>>>>>> STORAGE: test case fixes to support ARMCC and IAR toolchains.
|
||||
=======
|
||||
//#include "critical.h"
|
||||
#include "FATMisc.h"
|
||||
//>>>>>>> STORAGE: fixing error handling for fopen() and other upper edge filesystem API methods.
|
||||
|
||||
|
||||
// Global access to block device from FAT driver
|
||||
|
@ -127,53 +132,6 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) {
|
|||
return RES_PARERR;
|
||||
}
|
||||
|
||||
/* @brief Set errno based on the error code returned from underlying filesystem
|
||||
*
|
||||
* @param res result returned from underlying filesystem
|
||||
*
|
||||
* @return No return value
|
||||
*/
|
||||
static void FATFileSystemSetErrno(FRESULT res)
|
||||
{
|
||||
switch(res) {
|
||||
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 */
|
||||
case FR_EXIST: /* (8) Access denied due to prohibited access */
|
||||
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_INT_ERR: /* (2) Assertion failed */
|
||||
case FR_TIMEOUT: /* (15) Could not get a grant to access the volume within defined period */
|
||||
default:
|
||||
errno = EBADF; /* Bad file number */
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Filesystem implementation (See FATFilySystem.h)
|
||||
FATFileSystem::FATFileSystem(const char *n, BlockDevice *bd)
|
||||
: FileSystemLike(n), _id(-1) {
|
||||
|
@ -202,6 +160,7 @@ int FATFileSystem::mount(BlockDevice *bd, bool force) {
|
|||
_fsid[1] = '\0';
|
||||
debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid);
|
||||
FRESULT res = f_mount(&_fs, _fsid, force);
|
||||
FATFileSystemSetErrno(res);
|
||||
unlock();
|
||||
return res == 0 ? 0 : -1;
|
||||
}
|
||||
|
@ -219,6 +178,7 @@ int FATFileSystem::unmount() {
|
|||
}
|
||||
|
||||
FRESULT res = f_mount(NULL, _fsid, 0);
|
||||
FATFileSystemSetErrno(res);
|
||||
_ffs[_id] = NULL;
|
||||
_id = -1;
|
||||
unlock();
|
||||
|
@ -233,71 +193,22 @@ int FATFileSystem::sync() {
|
|||
}
|
||||
|
||||
// Always synchronized
|
||||
FATFileSystemSetErrno(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(int allocation_unit) {
|
||||
lock();
|
||||
FRESULT res = f_mkfs(_fsid, 0, allocation_unit); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
|
||||
if (res) {
|
||||
debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
|
||||
unlock();
|
||||
return -1;
|
||||
}
|
||||
unlock();
|
||||
return 0;
|
||||
}
|
||||
int FATFileSystem::format(BlockDevice *bd, int allocation_unit) {
|
||||
FATFileSystem fs("");
|
||||
int err = fs.mount(bd, false);
|
||||
if (err) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
|
||||
fs.lock();
|
||||
FRESULT res = f_mkfs(fs._fsid, 0, int allocation_unit);
|
||||
fs.unlock();
|
||||
|
||||
err = fs.unmount();
|
||||
if (err) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return res == 0 ? 0 : -1;
|
||||
}
|
||||
*/
|
||||
|
||||
/* 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) {
|
||||
//FATFileSystem fs("");
|
||||
//int err = fs.mount(bd, false);
|
||||
//if (err) {
|
||||
// return -1;
|
||||
//}
|
||||
|
||||
// Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
|
||||
//fs.lock();
|
||||
lock();
|
||||
//FRESULT res = f_mkfs(fs._fsid, 0, allocation_unit);
|
||||
FRESULT res = f_mkfs(_fsid, 0, allocation_unit);
|
||||
//fs.unlock();
|
||||
FATFileSystemSetErrno(res);
|
||||
unlock();
|
||||
|
||||
//err = fs.unmount();
|
||||
//if (err) {
|
||||
// return -1;
|
||||
//}
|
||||
|
||||
return res == 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
|
||||
FileHandle *FATFileSystem::open(const char* name, int flags) {
|
||||
lock();
|
||||
debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, getName(), _fsid);
|
||||
|
@ -323,6 +234,7 @@ FileHandle *FATFileSystem::open(const char* name, int flags) {
|
|||
|
||||
FIL fh;
|
||||
FRESULT res = f_open(&fh, n, openmode);
|
||||
FATFileSystemSetErrno(res);
|
||||
if (res) {
|
||||
debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
|
||||
unlock();
|
||||
|
@ -339,6 +251,7 @@ FileHandle *FATFileSystem::open(const char* name, int flags) {
|
|||
int FATFileSystem::remove(const char *filename) {
|
||||
lock();
|
||||
FRESULT res = f_unlink(filename);
|
||||
FATFileSystemSetErrno(res);
|
||||
if (res) {
|
||||
debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
|
||||
unlock();
|
||||
|
@ -351,6 +264,7 @@ int FATFileSystem::remove(const char *filename) {
|
|||
int FATFileSystem::rename(const char *oldname, const char *newname) {
|
||||
lock();
|
||||
FRESULT res = f_rename(oldname, newname);
|
||||
FATFileSystemSetErrno(res);
|
||||
if (res) {
|
||||
debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
|
||||
unlock();
|
||||
|
@ -360,11 +274,11 @@ int FATFileSystem::rename(const char *oldname, const char *newname) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
DirHandle *FATFileSystem::opendir(const char *name) {
|
||||
lock();
|
||||
FATFS_DIR dir;
|
||||
FRESULT res = f_opendir(&dir, name);
|
||||
FATFileSystemSetErrno(res);
|
||||
if (res != 0) {
|
||||
unlock();
|
||||
return NULL;
|
||||
|
@ -377,9 +291,7 @@ DirHandle *FATFileSystem::opendir(const char *name) {
|
|||
int FATFileSystem::mkdir(const char *name, mode_t mode) {
|
||||
lock();
|
||||
FRESULT res = f_mkdir(name);
|
||||
if (res != 0) {
|
||||
errno = (res == FR_EXIST) ? EEXIST : 0;
|
||||
}
|
||||
FATFileSystemSetErrno(res);
|
||||
unlock();
|
||||
return res == 0 ? 0 : -1;
|
||||
}
|
||||
|
@ -390,6 +302,7 @@ int FATFileSystem::stat(const char *name, struct stat *st) {
|
|||
memset(&f, 0, sizeof(f));
|
||||
|
||||
FRESULT res = f_stat(name, &f);
|
||||
FATFileSystemSetErrno(res);
|
||||
if (res != 0) {
|
||||
unlock();
|
||||
return -1;
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* 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 "toolchain_support.h"
|
||||
|
||||
|
||||
/* @brief Set errno based on the error code returned from underlying filesystem
|
||||
*
|
||||
* @param res result returned from underlying filesystem
|
||||
*
|
||||
* @return No return value
|
||||
*/
|
||||
void FATFileSystemSetErrno(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;
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef FILESYSTEM_FAT_MISC_H
|
||||
#define FILESYSTEM_FAT_MISC_H
|
||||
|
||||
#include "ff.h"
|
||||
|
||||
void FATFileSystemSetErrno(FRESULT res);
|
||||
|
||||
#endif /* FILESYSTEM_FAT_MISC_H */
|
|
@ -31,9 +31,7 @@
|
|||
|
||||
#ifdef FSFAT_DEBUG
|
||||
uint32_t fsfat_optDebug_g = 1;
|
||||
// todo: revert change
|
||||
//uint32_t fsfat_optLogLevel_g = FSFAT_LOG_NONE; /*FSFAT_LOG_NONE|FSFAT_LOG_ERR|FSFAT_LOG_DEBUG|FSFAT_LOG_FENTRY */
|
||||
uint32_t fsfat_optLogLevel_g = FSFAT_LOG_FENTRY; /*FSFAT_LOG_NONE|FSFAT_LOG_ERR|FSFAT_LOG_DEBUG|FSFAT_LOG_FENTRY */
|
||||
uint32_t fsfat_optLogLevel_g = FSFAT_LOG_NONE; /*FSFAT_LOG_NONE|FSFAT_LOG_ERR|FSFAT_LOG_DEBUG|FSFAT_LOG_FENTRY */
|
||||
#endif
|
||||
|
||||
/* ruler for measuring text strings */
|
||||
|
|
Loading…
Reference in New Issue