mirror of https://github.com/ARMmbed/mbed-os.git
Merge pull request #3773 from geky/fs-filesystem-simple-3
Filesystem: Restructure the filesystem api to be consistent with mbed OSpull/3840/head mbed-os-5.4.0-rc1
commit
3a27568a50
|
@ -17,33 +17,10 @@
|
|||
#define MBED_DIRHANDLE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
|
||||
# define NAME_MAX 255
|
||||
typedef int mode_t;
|
||||
|
||||
#else
|
||||
# include <sys/syslimits.h>
|
||||
#endif
|
||||
#include "platform/platform.h"
|
||||
|
||||
#include "FileHandle.h"
|
||||
|
||||
struct dirent {
|
||||
char d_name[NAME_MAX+1];
|
||||
uint8_t d_type;
|
||||
};
|
||||
|
||||
enum {
|
||||
DT_UNKNOWN, // The file type could not be determined.
|
||||
DT_FIFO, // This is a named pipe (FIFO).
|
||||
DT_CHR, // This is a character device.
|
||||
DT_DIR, // This is a directory.
|
||||
DT_BLK, // This is a block device.
|
||||
DT_REG, // This is a regular file.
|
||||
DT_LNK, // This is a symbolic link.
|
||||
DT_SOCK, // This is a UNIX domain socket.
|
||||
};
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
@ -64,8 +41,12 @@ namespace mbed {
|
|||
* @Note Synchronization level: Set by subclass
|
||||
*/
|
||||
class DirHandle {
|
||||
|
||||
public:
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4",
|
||||
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
|
||||
"Replaced by File")
|
||||
DirHandle() {}
|
||||
|
||||
/** Closes the directory.
|
||||
*
|
||||
* @returns
|
||||
|
@ -117,22 +98,18 @@ protected:
|
|||
virtual void unlock() {
|
||||
// Stub
|
||||
}
|
||||
|
||||
protected:
|
||||
/** Internal-only constructor to work around deprecated notices when not used
|
||||
*. due to nested deprecations and difficulty of compilers finding their way around
|
||||
* the class hierarchy
|
||||
*/
|
||||
friend class FileSystemLike;
|
||||
DirHandle(int) {}
|
||||
};
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
typedef mbed::DirHandle DIR;
|
||||
|
||||
extern "C" {
|
||||
DIR *opendir(const char*);
|
||||
struct dirent *readdir(DIR *);
|
||||
int closedir(DIR*);
|
||||
void rewinddir(DIR*);
|
||||
long telldir(DIR*);
|
||||
void seekdir(DIR*, long);
|
||||
int mkdir(const char *name, mode_t n);
|
||||
};
|
||||
|
||||
#endif /* MBED_DIRHANDLE_H */
|
||||
|
||||
/** @}*/
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
#include "drivers/FileBase.h"
|
||||
#include "drivers/FileLike.h"
|
||||
|
||||
namespace mbed {
|
||||
|
||||
|
@ -49,6 +50,11 @@ FileBase::~FileBase() {
|
|||
}
|
||||
}
|
||||
_mutex->unlock();
|
||||
|
||||
if (getPathType() == FilePathType) {
|
||||
extern void remove_filehandle(FileLike *file);
|
||||
remove_filehandle(static_cast<FileLike*>(this));
|
||||
}
|
||||
}
|
||||
|
||||
FileBase *FileBase::lookup(const char *name, unsigned int len) {
|
||||
|
|
|
@ -21,26 +21,6 @@ typedef int FILEHANDLE;
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
|
||||
# define O_RDONLY 0
|
||||
# define O_WRONLY 1
|
||||
# define O_RDWR 2
|
||||
# define O_CREAT 0x0200
|
||||
# define O_TRUNC 0x0400
|
||||
# define O_APPEND 0x0008
|
||||
|
||||
# define NAME_MAX 255
|
||||
|
||||
typedef int mode_t;
|
||||
typedef int ssize_t;
|
||||
typedef long off_t;
|
||||
|
||||
#else
|
||||
# include <sys/fcntl.h>
|
||||
# include <sys/types.h>
|
||||
# include <sys/syslimits.h>
|
||||
#endif
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "platform/SingletonPtr.h"
|
||||
#include "platform/PlatformMutex.h"
|
||||
|
@ -57,7 +37,6 @@ typedef enum {
|
|||
class FileBase {
|
||||
public:
|
||||
FileBase(const char *name, PathType t);
|
||||
|
||||
virtual ~FileBase();
|
||||
|
||||
const char* getName(void);
|
||||
|
|
|
@ -19,14 +19,7 @@
|
|||
typedef int FILEHANDLE;
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
|
||||
typedef int ssize_t;
|
||||
typedef long off_t;
|
||||
|
||||
#else
|
||||
# include <sys/types.h>
|
||||
#endif
|
||||
#include "platform/platform.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
|
@ -46,6 +39,11 @@ namespace mbed {
|
|||
class FileHandle {
|
||||
|
||||
public:
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4",
|
||||
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
|
||||
"Replaced by File")
|
||||
FileHandle() {}
|
||||
|
||||
/** Write the contents of a buffer to the file
|
||||
*
|
||||
* @param buffer the buffer to write from
|
||||
|
@ -120,7 +118,7 @@ public:
|
|||
return res;
|
||||
}
|
||||
|
||||
virtual ~FileHandle();
|
||||
virtual ~FileHandle() {};
|
||||
|
||||
protected:
|
||||
|
||||
|
|
|
@ -16,36 +16,139 @@
|
|||
#ifndef MBED_FILELIKE_H
|
||||
#define MBED_FILELIKE_H
|
||||
|
||||
#include "platform/toolchain.h"
|
||||
#include "drivers/FileBase.h"
|
||||
#include "drivers/FileHandle.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
|
||||
/* Class FileLike
|
||||
* A file-like object is one that can be opened with fopen by
|
||||
* fopen("/name", mode). It is intersection of the classes Base and
|
||||
* FileHandle.
|
||||
* fopen("/name", mode).
|
||||
*
|
||||
* @Note Synchronization level: Set by subclass
|
||||
*/
|
||||
class FileLike : public FileHandle, public FileBase {
|
||||
|
||||
class FileLike : public FileBase {
|
||||
public:
|
||||
/* Constructor FileLike
|
||||
/** Constructor FileLike
|
||||
*
|
||||
* Variables
|
||||
* name - The name to use to open the file.
|
||||
* @param name The name to use to open the file.
|
||||
*/
|
||||
FileLike(const char *name);
|
||||
FileLike(const char *name = NULL) : FileBase(name, FilePathType) {}
|
||||
virtual ~FileLike() {}
|
||||
|
||||
virtual ~FileLike();
|
||||
/** Read the contents of a file into a buffer
|
||||
*
|
||||
* @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 read(void *buffer, size_t len) = 0;
|
||||
|
||||
/** Write the contents of a buffer to a file
|
||||
*
|
||||
* @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 write(const void *buffer, size_t len) = 0;
|
||||
|
||||
/** Close a file
|
||||
*
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
virtual int close() = 0;
|
||||
|
||||
/** Flush any buffers associated with the file
|
||||
*
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
virtual int sync() = 0;
|
||||
|
||||
/** Check if the file in an interactive terminal device
|
||||
*
|
||||
* @return True if the file is a terminal
|
||||
*/
|
||||
virtual int isatty() = 0;
|
||||
|
||||
/** Move the file position to a given offset from from a given location
|
||||
*
|
||||
* @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 seek(off_t offset, int whence = SEEK_SET) = 0;
|
||||
|
||||
/** Get the file position of the file
|
||||
*
|
||||
* @return The current offset in the file
|
||||
*/
|
||||
virtual off_t tell() = 0;
|
||||
|
||||
/** Rewind the file position to the beginning of the file
|
||||
*
|
||||
* @note This is equivalent to file_seek(file, 0, FS_SEEK_SET)
|
||||
*/
|
||||
virtual void rewind() = 0;
|
||||
|
||||
/** Get the size of the file
|
||||
*
|
||||
* @return Size of the file in bytes
|
||||
*/
|
||||
virtual size_t size() = 0;
|
||||
|
||||
/** Move the file position to a given offset from a given location.
|
||||
*
|
||||
* @param offset The offset from whence to move to
|
||||
* @param whence SEEK_SET for the start of the file, SEEK_CUR for the
|
||||
* current file position, or SEEK_END for the end of the file.
|
||||
*
|
||||
* @returns
|
||||
* new file position on success,
|
||||
* -1 on failure or unsupported
|
||||
*/
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::seek")
|
||||
virtual off_t lseek(off_t offset, int whence) { return seek(offset, whence); }
|
||||
|
||||
/** Flush any buffers associated with the FileHandle, ensuring it
|
||||
* is up to date on disk
|
||||
*
|
||||
* @returns
|
||||
* 0 on success or un-needed,
|
||||
* -1 on error
|
||||
*/
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::sync")
|
||||
virtual int fsync() { return sync(); }
|
||||
|
||||
/** Find the length of the file
|
||||
*
|
||||
* @returns
|
||||
* Length of the file
|
||||
*/
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::size")
|
||||
virtual off_t flen() { return size(); }
|
||||
|
||||
protected:
|
||||
/** Acquire exclusive access to this object.
|
||||
*/
|
||||
virtual void lock() {
|
||||
// Stub
|
||||
}
|
||||
|
||||
/** Release exclusive access to this object.
|
||||
*/
|
||||
virtual void unlock() {
|
||||
// Stub
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** @}*/
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
|
|
|
@ -49,9 +49,9 @@ bool FilePath::isFileSystem(void) {
|
|||
return (fb->getPathType() == FileSystemPathType);
|
||||
}
|
||||
|
||||
FileSystemLike* FilePath::fileSystem(void) {
|
||||
FileSystem* FilePath::fileSystem(void) {
|
||||
if (isFileSystem()) {
|
||||
return (FileSystemLike*)fb;
|
||||
return (FileSystem*)fb;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -25,6 +25,8 @@ namespace mbed {
|
|||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
class FileSystem;
|
||||
|
||||
class FilePath {
|
||||
public:
|
||||
FilePath(const char* file_path);
|
||||
|
@ -32,7 +34,7 @@ public:
|
|||
const char* fileName(void);
|
||||
|
||||
bool isFileSystem(void);
|
||||
FileSystemLike* fileSystem(void);
|
||||
FileSystem* fileSystem(void);
|
||||
|
||||
bool isFile(void);
|
||||
FileLike* file(void);
|
||||
|
|
|
@ -29,7 +29,7 @@ public:
|
|||
off_t n;
|
||||
struct dirent cur_entry;
|
||||
|
||||
BaseDirHandle() : n(0), cur_entry() {
|
||||
BaseDirHandle() : DirHandle(0), n(0), cur_entry() {
|
||||
}
|
||||
|
||||
virtual int closedir() {
|
||||
|
|
|
@ -41,10 +41,16 @@ public:
|
|||
*
|
||||
* @param name The name to use for the filesystem.
|
||||
*/
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4",
|
||||
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
|
||||
"Replaced by FileSystem")
|
||||
FileSystemLike(const char *name);
|
||||
|
||||
virtual ~FileSystemLike();
|
||||
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4",
|
||||
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
|
||||
"Replaced by FileSystem")
|
||||
static DirHandle *opendir();
|
||||
friend class BaseDirHandle;
|
||||
|
||||
|
|
|
@ -94,19 +94,26 @@ ssize_t Stream::read(void* buffer, size_t length) {
|
|||
return ptr - (const char*)buffer;
|
||||
}
|
||||
|
||||
off_t Stream::lseek(off_t offset, int whence) {
|
||||
off_t Stream::seek(off_t offset, int whence) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
off_t Stream::tell() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Stream::rewind() {
|
||||
}
|
||||
|
||||
int Stream::isatty() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Stream::fsync() {
|
||||
int Stream::sync() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
off_t Stream::flen() {
|
||||
size_t Stream::size() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include "platform/platform.h"
|
||||
#include "drivers/FileLike.h"
|
||||
#include "drivers/FileHandle.h"
|
||||
#include <cstdarg>
|
||||
|
||||
namespace mbed {
|
||||
|
@ -53,10 +54,12 @@ protected:
|
|||
virtual int close();
|
||||
virtual ssize_t write(const void* buffer, size_t length);
|
||||
virtual ssize_t read(void* buffer, size_t length);
|
||||
virtual off_t lseek(off_t offset, int whence);
|
||||
virtual off_t seek(off_t offset, int whence);
|
||||
virtual off_t tell();
|
||||
virtual void rewind();
|
||||
virtual int isatty();
|
||||
virtual int fsync();
|
||||
virtual off_t flen();
|
||||
virtual int sync();
|
||||
virtual size_t size();
|
||||
|
||||
virtual int _putc(int c) = 0;
|
||||
virtual int _getc() = 0;
|
||||
|
|
|
@ -0,0 +1,160 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 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.
|
||||
*/
|
||||
#include "mbed.h"
|
||||
#include "greentea-client/test_env.h"
|
||||
#include "unity.h"
|
||||
#include "utest.h"
|
||||
|
||||
#include "HeapBlockDevice.h"
|
||||
#include "FATFileSystem.h"
|
||||
#include <stdlib.h>
|
||||
#include "retarget.h"
|
||||
|
||||
using namespace utest::v1;
|
||||
|
||||
#ifndef MBED_EXTENDED_TESTS
|
||||
#error [NOT_SUPPORTED] Filesystem tests not supported by default
|
||||
#endif
|
||||
|
||||
// Test block device
|
||||
#define BLOCK_SIZE 512
|
||||
HeapBlockDevice bd(128*BLOCK_SIZE, BLOCK_SIZE);
|
||||
|
||||
|
||||
// Test formatting
|
||||
void test_format() {
|
||||
int err = FATFileSystem::format(&bd);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
}
|
||||
|
||||
|
||||
// Simple test for reading/writing files
|
||||
template <ssize_t TEST_SIZE>
|
||||
void test_read_write() {
|
||||
FATFileSystem fs("fat");
|
||||
|
||||
int err = fs.mount(&bd);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
uint8_t *buffer = (uint8_t *)malloc(TEST_SIZE);
|
||||
TEST_ASSERT(buffer);
|
||||
|
||||
// Fill with random sequence
|
||||
srand(1);
|
||||
for (int i = 0; i < TEST_SIZE; i++) {
|
||||
buffer[i] = 0xff & rand();
|
||||
}
|
||||
|
||||
// write and read file
|
||||
File file;
|
||||
err = file.open(&fs, "test_read_write.dat", O_WRONLY | O_CREAT);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
ssize_t size = file.write(buffer, TEST_SIZE);
|
||||
TEST_ASSERT_EQUAL(TEST_SIZE, size);
|
||||
err = file.close();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
err = file.open(&fs, "test_read_write.dat", O_RDONLY);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
size = file.read(buffer, TEST_SIZE);
|
||||
TEST_ASSERT_EQUAL(TEST_SIZE, size);
|
||||
err = file.close();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Check that the data was unmodified
|
||||
srand(1);
|
||||
for (int i = 0; i < TEST_SIZE; i++) {
|
||||
TEST_ASSERT_EQUAL(0xff & rand(), buffer[i]);
|
||||
}
|
||||
|
||||
err = fs.unmount();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
}
|
||||
|
||||
|
||||
// Simple test for iterating dir entries
|
||||
void test_read_dir() {
|
||||
FATFileSystem fs("fat");
|
||||
|
||||
int err = fs.mount(&bd);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
err = fs.mkdir("test_read_dir", S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
err = fs.mkdir("test_read_dir/test_dir", S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
File file;
|
||||
err = file.open(&fs, "test_read_dir/test_file", O_WRONLY | O_CREAT);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
err = file.close();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Iterate over dir checking for known files
|
||||
Dir dir;
|
||||
err = dir.open(&fs, "test_read_dir");
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
struct dirent *de;
|
||||
bool test_dir_found = false;
|
||||
bool test_file_found = true;
|
||||
|
||||
while ((de = readdir(&dir))) {
|
||||
printf("d_name: %.32s, d_type: %x\n", de->d_name, de->d_type);
|
||||
|
||||
if (strcmp(de->d_name, "test_dir") == 0) {
|
||||
test_dir_found = true;
|
||||
TEST_ASSERT_EQUAL(DT_DIR, de->d_type);
|
||||
} else if (strcmp(de->d_name, "test_file") == 0) {
|
||||
test_file_found = true;
|
||||
TEST_ASSERT_EQUAL(DT_REG, de->d_type);
|
||||
} else {
|
||||
char *buf = new char[NAME_MAX];
|
||||
snprintf(buf, NAME_MAX, "Unexpected file \"%s\"", de->d_name);
|
||||
TEST_ASSERT_MESSAGE(false, buf);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_ASSERT_MESSAGE(test_dir_found, "Could not find \"test_dir\"");
|
||||
TEST_ASSERT_MESSAGE(test_file_found, "Could not find \"test_file\"");
|
||||
|
||||
err = dir.close();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
err = fs.unmount();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
}
|
||||
|
||||
|
||||
// Test setup
|
||||
utest::v1::status_t test_setup(const size_t number_of_cases) {
|
||||
GREENTEA_SETUP(10, "default_auto");
|
||||
return verbose_test_setup_handler(number_of_cases);
|
||||
}
|
||||
|
||||
Case cases[] = {
|
||||
Case("Testing formating", test_format),
|
||||
Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>),
|
||||
Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>),
|
||||
Case("Testing dir iteration", test_read_dir),
|
||||
};
|
||||
|
||||
Specification specification(test_setup, cases);
|
||||
|
||||
int main() {
|
||||
return !Harness::run(specification);
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2015 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.
|
||||
*/
|
||||
|
||||
#include "Dir.h"
|
||||
#include "mbed.h"
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
Dir::Dir()
|
||||
: _fs(0), _dir(0)
|
||||
{
|
||||
}
|
||||
|
||||
Dir::Dir(FileSystem *fs, const char *path)
|
||||
: _fs(0), _dir(0)
|
||||
{
|
||||
open(fs, path);
|
||||
}
|
||||
|
||||
Dir::~Dir()
|
||||
{
|
||||
if (_fs) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
int Dir::open(FileSystem *fs, const char *path)
|
||||
{
|
||||
if (_fs) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
_fs = fs;
|
||||
return _fs->dir_open(&_dir, path);
|
||||
}
|
||||
|
||||
int Dir::close()
|
||||
{
|
||||
if (!_fs) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
int err = _fs->dir_close(_dir);
|
||||
_fs = 0;
|
||||
return err;
|
||||
}
|
||||
|
||||
ssize_t Dir::read(struct dirent *ent)
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
memset(ent, 0, sizeof(struct dirent));
|
||||
return _fs->dir_read(_dir, ent);
|
||||
}
|
||||
|
||||
void Dir::seek(off_t offset)
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->dir_seek(_dir, offset);
|
||||
}
|
||||
|
||||
off_t Dir::tell()
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->dir_tell(_dir);
|
||||
}
|
||||
|
||||
void Dir::rewind()
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->dir_rewind(_dir);
|
||||
}
|
||||
|
||||
size_t Dir::size()
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->dir_size(_dir);
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2015 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.
|
||||
*/
|
||||
|
||||
#ifndef DIR_H
|
||||
#define DIR_H
|
||||
|
||||
#include "filesystem/FileSystem.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup filesystem */
|
||||
/** @{*/
|
||||
|
||||
|
||||
/** Dir class
|
||||
*/
|
||||
class Dir {
|
||||
public:
|
||||
/** Create an uninitialized directory
|
||||
*
|
||||
* Must call open to initialize the directory on a file system
|
||||
*/
|
||||
Dir();
|
||||
|
||||
/** Open a directory on a filesystem
|
||||
*
|
||||
* @param fs Filesystem as target for a directory
|
||||
* @param path Name of the directory to open
|
||||
*/
|
||||
Dir(FileSystem *fs, const char *path);
|
||||
|
||||
/** Destroy a file
|
||||
*
|
||||
* Closes file if the file is still open
|
||||
*/
|
||||
virtual ~Dir();
|
||||
|
||||
/** Open a directory on the filesystem
|
||||
*
|
||||
* @param fs Filesystem as target for a directory
|
||||
* @param path Name of the directory to open
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
virtual int open(FileSystem *fs, const char *path);
|
||||
|
||||
/** Close a directory
|
||||
*
|
||||
* return 0 on success, negative error code on failure
|
||||
*/
|
||||
virtual int close();
|
||||
|
||||
/** Read the next directory entry
|
||||
*
|
||||
* @param path The buffer to read the null terminated path name in to
|
||||
* @param ent The directory entry to fill out
|
||||
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
|
||||
*/
|
||||
virtual ssize_t read(struct dirent *ent);
|
||||
|
||||
/** Set the current position of the directory
|
||||
*
|
||||
* @param offset Offset of the location to seek to,
|
||||
* must be a value returned from tell
|
||||
*/
|
||||
virtual void seek(off_t offset);
|
||||
|
||||
/** Get the current position of the directory
|
||||
*
|
||||
* @return Position of the directory that can be passed to rewind
|
||||
*/
|
||||
virtual off_t tell();
|
||||
|
||||
/** Rewind the current position to the beginning of the directory
|
||||
*/
|
||||
virtual void rewind();
|
||||
|
||||
/** Get the sizeof the directory
|
||||
*
|
||||
* @return Number of files in the directory
|
||||
*/
|
||||
virtual size_t size();
|
||||
|
||||
private:
|
||||
FileSystem *_fs;
|
||||
fs_dir_t _dir;
|
||||
};
|
||||
|
||||
|
||||
/** @}*/
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
|
@ -0,0 +1,112 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2015 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.
|
||||
*/
|
||||
|
||||
#include "File.h"
|
||||
#include "mbed.h"
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
File::File()
|
||||
: _fs(0), _file(0)
|
||||
{
|
||||
}
|
||||
|
||||
File::File(FileSystem *fs, const char *path, int flags)
|
||||
: FileLike(path), _fs(0), _file(0)
|
||||
{
|
||||
open(fs, path, flags);
|
||||
}
|
||||
|
||||
File::~File()
|
||||
{
|
||||
if (_fs) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
int File::open(FileSystem *fs, const char *path, int flags)
|
||||
{
|
||||
if (_fs) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
int err = fs->file_open(&_file, path, flags);
|
||||
if (!err) {
|
||||
_fs = fs;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int File::close()
|
||||
{
|
||||
if (!_fs) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
int err = _fs->file_close(_file);
|
||||
_fs = 0;
|
||||
return err;
|
||||
}
|
||||
|
||||
ssize_t File::read(void *buffer, size_t len)
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->file_read(_file, buffer, len);
|
||||
}
|
||||
|
||||
ssize_t File::write(const void *buffer, size_t len)
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->file_write(_file, buffer, len);
|
||||
}
|
||||
|
||||
int File::sync()
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->file_sync(_file);
|
||||
}
|
||||
|
||||
int File::isatty()
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->file_isatty(_file);
|
||||
}
|
||||
|
||||
off_t File::seek(off_t offset, int whence)
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->file_seek(_file, offset, whence);
|
||||
}
|
||||
|
||||
off_t File::tell()
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->file_tell(_file);
|
||||
}
|
||||
|
||||
void File::rewind()
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->file_rewind(_file);
|
||||
}
|
||||
|
||||
size_t File::size()
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->file_size(_file);
|
||||
}
|
||||
|
|
@ -0,0 +1,138 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2015 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.
|
||||
*/
|
||||
|
||||
#ifndef FILE_H
|
||||
#define FILE_H
|
||||
|
||||
#include "filesystem/FileSystem.h"
|
||||
#include "drivers/FileLike.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup filesystem */
|
||||
/** @{*/
|
||||
|
||||
|
||||
/** File class
|
||||
*/
|
||||
class File : public FileLike {
|
||||
public:
|
||||
/** Create an uninitialized file
|
||||
*
|
||||
* Must call open to initialize the file on a file system
|
||||
*/
|
||||
File();
|
||||
|
||||
/** Create a file on a filesystem
|
||||
*
|
||||
* Creates and opens a file on a filesystem
|
||||
*
|
||||
* @param fs Filesystem as target for the 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
|
||||
*/
|
||||
File(FileSystem *fs, const char *path, int flags = O_RDONLY);
|
||||
|
||||
/** Destroy a file
|
||||
*
|
||||
* Closes file if the file is still open
|
||||
*/
|
||||
virtual ~File();
|
||||
|
||||
/** Open a file on the filesystem
|
||||
*
|
||||
* @param fs Filesystem as target for the 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 open(FileSystem *fs, const char *path, int flags=O_RDONLY);
|
||||
|
||||
/** Close a file
|
||||
*
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
virtual int close();
|
||||
|
||||
/** Read the contents of a file into a buffer
|
||||
*
|
||||
* @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 read(void *buffer, size_t len);
|
||||
|
||||
/** Write the contents of a buffer to a file
|
||||
*
|
||||
* @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 write(const void *buffer, size_t len);
|
||||
|
||||
/** Flush any buffers associated with the file
|
||||
*
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
virtual int sync();
|
||||
|
||||
/** Check if the file in an interactive terminal device
|
||||
*
|
||||
* @return True if the file is a terminal
|
||||
*/
|
||||
virtual int isatty();
|
||||
|
||||
/** Move the file position to a given offset from from a given location
|
||||
*
|
||||
* @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 seek(off_t offset, int whence = SEEK_SET);
|
||||
|
||||
/** Get the file position of the file
|
||||
*
|
||||
* @return The current offset in the file
|
||||
*/
|
||||
virtual off_t tell();
|
||||
|
||||
/** Rewind the file position to the beginning of the file
|
||||
*
|
||||
* @note This is equivalent to file_seek(file, 0, FS_SEEK_SET)
|
||||
*/
|
||||
virtual void rewind();
|
||||
|
||||
/** Get the size of the file
|
||||
*
|
||||
* @return Size of the file in bytes
|
||||
*/
|
||||
virtual size_t size();
|
||||
|
||||
private:
|
||||
FileSystem *_fs;
|
||||
fs_file_t _file;
|
||||
};
|
||||
|
||||
|
||||
/** @}*/
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
|
@ -0,0 +1,111 @@
|
|||
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 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.
|
||||
*/
|
||||
|
||||
#include "mbed.h"
|
||||
#include "filesystem/FileSystem.h"
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
FileSystem::FileSystem(const char *name)
|
||||
: FileBase(name, FileSystemPathType)
|
||||
{
|
||||
}
|
||||
|
||||
int FileSystem::file_sync(fs_file_t file)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FileSystem::file_isatty(fs_file_t file)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
off_t FileSystem::file_tell(fs_file_t file)
|
||||
{
|
||||
return file_seek(file, 0, SEEK_CUR);
|
||||
}
|
||||
|
||||
void FileSystem::file_rewind(fs_file_t file)
|
||||
{
|
||||
file_seek(file, 0, SEEK_SET);
|
||||
}
|
||||
|
||||
size_t FileSystem::file_size(fs_file_t file)
|
||||
{
|
||||
off_t off = file_tell(file);
|
||||
size_t size = file_seek(file, 0, SEEK_END);
|
||||
file_seek(file, off, SEEK_SET);
|
||||
return size;
|
||||
}
|
||||
|
||||
int FileSystem::mkdir(const char *path, mode_t mode)
|
||||
{
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
int FileSystem::dir_open(fs_dir_t *dir, const char *path)
|
||||
{
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
int FileSystem::dir_close(fs_dir_t dir)
|
||||
{
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
ssize_t FileSystem::dir_read(fs_dir_t dir, struct dirent *ent)
|
||||
{
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
void FileSystem::dir_seek(fs_dir_t dir, off_t offset)
|
||||
{
|
||||
}
|
||||
|
||||
off_t FileSystem::dir_tell(fs_dir_t dir)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void FileSystem::dir_rewind(fs_dir_t dir)
|
||||
{
|
||||
// Note, the may not satisfy rewind on all filesystems
|
||||
dir_seek(dir, 0);
|
||||
}
|
||||
|
||||
size_t FileSystem::dir_size(fs_dir_t dir)
|
||||
{
|
||||
off_t off = dir_tell(dir);
|
||||
size_t size = 0;
|
||||
struct dirent *ent = new struct dirent;
|
||||
|
||||
dir_rewind(dir);
|
||||
while (true) {
|
||||
int res = dir_read(dir, ent);
|
||||
if (res <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
size += 1;
|
||||
}
|
||||
dir_seek(dir, off);
|
||||
|
||||
delete ent;
|
||||
return size;
|
||||
}
|
||||
|
|
@ -0,0 +1,236 @@
|
|||
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 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.
|
||||
*/
|
||||
#ifndef MBED_FILESYSTEM_H
|
||||
#define MBED_FILESYSTEM_H
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#include "drivers/FileBase.h"
|
||||
#include "BlockDevice.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup filesystem */
|
||||
/** @{*/
|
||||
|
||||
|
||||
// Opaque pointer representing files and directories
|
||||
typedef void *fs_file_t;
|
||||
typedef void *fs_dir_t;
|
||||
|
||||
/** A filesystem-like object is one that can be used to open files
|
||||
* though it by fopen("/name/filename", flags)
|
||||
*
|
||||
* Implementations must define at least open (the default definitions
|
||||
* of the rest of the functions just return error values).
|
||||
*
|
||||
* @Note Synchronization level: Set by subclass
|
||||
*/
|
||||
class FileSystem : public FileBase {
|
||||
public:
|
||||
/** FileSystem lifetime
|
||||
*/
|
||||
FileSystem(const char *name = NULL);
|
||||
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.
|
||||
*
|
||||
* @param path The name of the file to remove.
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
virtual int remove(const char *path) = 0;
|
||||
|
||||
/** 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 rename(const char *path, const char *newpath) = 0;
|
||||
|
||||
/** 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 *path, struct stat *st) = 0;
|
||||
|
||||
/** 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:
|
||||
friend class File;
|
||||
friend class Dir;
|
||||
|
||||
/** 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) = 0;
|
||||
|
||||
/** Close a file
|
||||
*
|
||||
* @param file File handle
|
||||
* return 0 on success, negative error code on failure
|
||||
*/
|
||||
virtual int file_close(fs_file_t file) = 0;
|
||||
|
||||
/** 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) = 0;
|
||||
|
||||
/** 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) = 0;
|
||||
|
||||
/** 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);
|
||||
|
||||
/** Check if the file in an interactive terminal device
|
||||
* If so, line buffered behaviour is used by default
|
||||
*
|
||||
* @param file File handle
|
||||
* @return True if the file is a terminal
|
||||
*/
|
||||
virtual int file_isatty(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) = 0;
|
||||
|
||||
/** 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);
|
||||
|
||||
/** Rewind the file position to the beginning of the file
|
||||
*
|
||||
* @param file File handle
|
||||
* @note This is equivalent to file_seek(file, 0, FS_SEEK_SET)
|
||||
*/
|
||||
virtual void file_rewind(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 ent The directory entry to fill out
|
||||
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
|
||||
*/
|
||||
virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent);
|
||||
|
||||
/** 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);
|
||||
};
|
||||
|
||||
|
||||
/** @}*/
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
|
@ -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();
|
||||
}
|
||||
|
|
@ -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
|
|
@ -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();
|
||||
}
|
|
@ -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
|
|
@ -24,14 +24,109 @@
|
|||
#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 /////
|
||||
|
||||
static int fat_error_remap(FRESULT res)
|
||||
{
|
||||
switch(res) {
|
||||
case FR_OK: /* (0) Succeeded */
|
||||
return 0; /* no error */
|
||||
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 */
|
||||
return -EIO; /* I/O error */
|
||||
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 */
|
||||
return -ENOENT; /* No such file or directory */
|
||||
case FR_DENIED: /* (7) Access denied due to prohibited access or directory full */
|
||||
return -EACCES; /* Permission denied */
|
||||
case FR_EXIST: /* (8) Access denied due to prohibited access */
|
||||
return -EEXIST; /* File exists */
|
||||
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 */
|
||||
return -EACCES; /* Permission denied */
|
||||
case FR_INVALID_OBJECT: /* (9) The file/directory object is invalid */
|
||||
return -EFAULT; /* Bad address */
|
||||
case FR_NOT_ENABLED: /* (12) The volume has no work area */
|
||||
return -ENXIO; /* No such device or address */
|
||||
case FR_NOT_ENOUGH_CORE: /* (17) LFN working buffer could not be allocated */
|
||||
return -ENOMEM; /* Not enough space */
|
||||
case FR_TOO_MANY_OPEN_FILES: /* (18) Number of open files > _FS_LOCK */
|
||||
return -ENFILE; /* Too many open files in system */
|
||||
case FR_INVALID_PARAMETER: /* (19) Given parameter is invalid */
|
||||
return -ENOEXEC; /* Exec format error */
|
||||
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: /* Bad file number */
|
||||
return -EBADF;
|
||||
}
|
||||
}
|
||||
|
||||
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 +218,12 @@ 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(name), _id(-1) {
|
||||
if (bd) {
|
||||
mount(bd);
|
||||
}
|
||||
|
@ -138,12 +235,16 @@ 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();
|
||||
return -1;
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _VOLUMES; i++) {
|
||||
|
@ -154,14 +255,13 @@ 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);
|
||||
fat_filesystem_set_errno(res);
|
||||
unlock();
|
||||
return res == 0 ? 0 : -1;
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
}
|
||||
|
||||
unlock();
|
||||
return -1;
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
int FATFileSystem::unmount()
|
||||
|
@ -169,180 +269,338 @@ int FATFileSystem::unmount()
|
|||
lock();
|
||||
if (_id == -1) {
|
||||
unlock();
|
||||
return -1;
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
FRESULT res = f_mount(NULL, _fsid, 0);
|
||||
fat_filesystem_set_errno(res);
|
||||
_ffs[_id] = NULL;
|
||||
_id = -1;
|
||||
unlock();
|
||||
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;
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
/* 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 FATFileSystem::format(BlockDevice *bd, int allocation_unit) {
|
||||
FATFileSystem fs;
|
||||
int err = fs.mount(bd, false);
|
||||
if (err) {
|
||||
return -1;
|
||||
return err;
|
||||
}
|
||||
|
||||
// Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
|
||||
fs.lock();
|
||||
FRESULT res = f_mkfs(fs._fsid, 0, allocation_unit);
|
||||
fat_filesystem_set_errno(res);
|
||||
fs.unlock();
|
||||
if (res != FR_OK) {
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
err = fs.unmount();
|
||||
if (err) {
|
||||
return -1;
|
||||
return err;
|
||||
}
|
||||
return res == 0 ? 0 : -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
char n[64];
|
||||
sprintf(n, "%s:/%s", _fsid, name);
|
||||
FRESULT res = f_unlink(filename);
|
||||
unlock();
|
||||
|
||||
if (res != FR_OK) {
|
||||
debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
|
||||
}
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
int FATFileSystem::rename(const char *oldname, const char *newname) {
|
||||
lock();
|
||||
FRESULT res = f_rename(oldname, newname);
|
||||
unlock();
|
||||
|
||||
if (res != FR_OK) {
|
||||
debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
|
||||
}
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
int FATFileSystem::mkdir(const char *name, mode_t mode) {
|
||||
lock();
|
||||
FRESULT res = f_mkdir(name);
|
||||
unlock();
|
||||
|
||||
if (res != FR_OK) {
|
||||
debug_if(FFS_DBG, "f_mkdir() failed: %d\n", res);
|
||||
}
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
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 != FR_OK) {
|
||||
unlock();
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
/* 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 0;
|
||||
}
|
||||
|
||||
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) {
|
||||
debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", path, getName(), _fsid);
|
||||
|
||||
FIL *fh = new FIL;
|
||||
char *buffer = new char[strlen(_fsid) + strlen(path) + 3];
|
||||
sprintf(buffer, "%s:/%s", _fsid, path);
|
||||
|
||||
/* POSIX flags -> FatFS open mode */
|
||||
BYTE openmode;
|
||||
if (flags & O_RDWR) {
|
||||
openmode = FA_READ|FA_WRITE;
|
||||
} else if(flags & O_WRONLY) {
|
||||
openmode = FA_READ | FA_WRITE;
|
||||
} else if (flags & O_WRONLY) {
|
||||
openmode = FA_WRITE;
|
||||
} else {
|
||||
openmode = FA_READ;
|
||||
}
|
||||
if(flags & O_CREAT) {
|
||||
if(flags & O_TRUNC) {
|
||||
if (flags & O_CREAT) {
|
||||
if (flags & O_TRUNC) {
|
||||
openmode |= FA_CREATE_ALWAYS;
|
||||
} else {
|
||||
openmode |= FA_OPEN_ALWAYS;
|
||||
}
|
||||
}
|
||||
|
||||
FIL fh;
|
||||
FRESULT res = f_open(&fh, n, openmode);
|
||||
fat_filesystem_set_errno(res);
|
||||
if (res) {
|
||||
lock();
|
||||
FRESULT res = f_open(fh, buffer, openmode);
|
||||
|
||||
if (res != FR_OK) {
|
||||
unlock();
|
||||
debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
|
||||
unlock();
|
||||
return NULL;
|
||||
delete[] buffer;
|
||||
delete fh;
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
if (flags & O_APPEND) {
|
||||
f_lseek(&fh, fh.fsize);
|
||||
f_lseek(fh, fh->fsize);
|
||||
}
|
||||
FATFileHandle *handle = new FATFileHandle(fh, _ffs_mutex.get());
|
||||
unlock();
|
||||
return handle;
|
||||
}
|
||||
|
||||
int FATFileSystem::remove(const char *filename)
|
||||
{
|
||||
lock();
|
||||
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();
|
||||
delete[] buffer;
|
||||
*file = fh;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FATFileSystem::rename(const char *oldname, const char *newname)
|
||||
{
|
||||
int FATFileSystem::file_close(fs_file_t file) {
|
||||
FIL *fh = static_cast<FIL*>(file);
|
||||
|
||||
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;
|
||||
}
|
||||
FRESULT res = f_close(fh);
|
||||
unlock();
|
||||
|
||||
delete fh;
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
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);
|
||||
unlock();
|
||||
|
||||
if (res != FR_OK) {
|
||||
debug_if(FFS_DBG, "f_read() failed: %d\n", res);
|
||||
return fat_error_remap(res);
|
||||
} else {
|
||||
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);
|
||||
unlock();
|
||||
|
||||
if (res != FR_OK) {
|
||||
debug_if(FFS_DBG, "f_write() failed: %d", res);
|
||||
return fat_error_remap(res);
|
||||
} else {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
int FATFileSystem::file_sync(fs_file_t file) {
|
||||
FIL *fh = static_cast<FIL*>(file);
|
||||
|
||||
lock();
|
||||
FRESULT res = f_sync(fh);
|
||||
unlock();
|
||||
|
||||
if (res != FR_OK) {
|
||||
debug_if(FFS_DBG, "f_sync() failed: %d\n", res);
|
||||
}
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
off_t FATFileSystem::file_seek(fs_file_t file, off_t offset, int whence) {
|
||||
FIL *fh = static_cast<FIL*>(file);
|
||||
|
||||
lock();
|
||||
if (whence == SEEK_END) {
|
||||
offset += fh->fsize;
|
||||
} else if(whence==SEEK_CUR) {
|
||||
offset += fh->fptr;
|
||||
}
|
||||
|
||||
FRESULT res = f_lseek(fh, offset);
|
||||
off_t noffset = fh->fptr;
|
||||
unlock();
|
||||
|
||||
if (res != FR_OK) {
|
||||
debug_if(FFS_DBG, "lseek failed: %d\n", res);
|
||||
return fat_error_remap(res);
|
||||
} else {
|
||||
return noffset;
|
||||
}
|
||||
}
|
||||
|
||||
off_t FATFileSystem::file_tell(fs_file_t file) {
|
||||
FIL *fh = static_cast<FIL*>(file);
|
||||
|
||||
lock();
|
||||
off_t res = fh->fptr;
|
||||
unlock();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
size_t FATFileSystem::file_size(fs_file_t file) {
|
||||
FIL *fh = static_cast<FIL*>(file);
|
||||
|
||||
lock();
|
||||
size_t res = fh->fsize;
|
||||
unlock();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
////// Dir operations //////
|
||||
int FATFileSystem::dir_open(fs_dir_t *dir, const char *path) {
|
||||
FATFS_DIR *dh = new FATFS_DIR;
|
||||
|
||||
lock();
|
||||
FRESULT res = f_opendir(dh, path);
|
||||
unlock();
|
||||
|
||||
if (res != FR_OK) {
|
||||
debug_if(FFS_DBG, "f_opendir() failed: %d\n", res);
|
||||
delete dh;
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
*dir = dh;
|
||||
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);
|
||||
fat_filesystem_set_errno(res);
|
||||
if (res != 0) {
|
||||
unlock();
|
||||
return NULL;
|
||||
}
|
||||
FATDirHandle *handle = new FATDirHandle(dir, _ffs_mutex.get());
|
||||
FRESULT res = f_closedir(dh);
|
||||
unlock();
|
||||
return handle;
|
||||
|
||||
delete dh;
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
int FATFileSystem::mkdir(const char *name, mode_t mode)
|
||||
{
|
||||
ssize_t FATFileSystem::dir_read(fs_dir_t dir, struct dirent *ent) {
|
||||
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
|
||||
FILINFO finfo;
|
||||
|
||||
#if _USE_LFN
|
||||
finfo.lfname = ent->d_name;
|
||||
finfo.lfsize = NAME_MAX;
|
||||
#endif // _USE_LFN
|
||||
|
||||
lock();
|
||||
FRESULT res = f_mkdir(name);
|
||||
fat_filesystem_set_errno(res);
|
||||
FRESULT res = f_readdir(dh, &finfo);
|
||||
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;
|
||||
if (res != FR_OK) {
|
||||
return fat_error_remap(res);
|
||||
} else if (finfo.fname[0] == 0) {
|
||||
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__ */
|
||||
ent->d_type = (finfo.fattrib & AM_DIR) ? DT_DIR : DT_REG;
|
||||
|
||||
#if _USE_LFN
|
||||
if (ent->d_name[0] == 0) {
|
||||
// No long filename so use short filename.
|
||||
strncpy(ent->d_name, finfo.fname, NAME_MAX);
|
||||
}
|
||||
#else
|
||||
strncpy(end->d_name, finfo.fname, len);
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void FATFileSystem::dir_seek(fs_dir_t dir, off_t offset) {
|
||||
FATFS_DIR *dh = static_cast<FATFS_DIR*>(dir);
|
||||
|
||||
lock();
|
||||
dh->index = offset;
|
||||
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;
|
||||
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;
|
||||
unlock();
|
||||
}
|
||||
|
||||
|
|
|
@ -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,203 @@ 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 = NULL, 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 ent The directory entry to fill out
|
||||
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
|
||||
*/
|
||||
virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent);
|
||||
|
||||
/** 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);
|
||||
|
||||
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();
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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 */
|
|
@ -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
|
|
@ -1,5 +1,5 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
/* filesystem
|
||||
* Copyright (c) 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.
|
||||
|
@ -13,16 +13,27 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "drivers/FileLike.h"
|
||||
#ifndef MBED_FILESYSTEM_API_H
|
||||
#define MBED_FILESYSTEM_API_H
|
||||
/** \addtogroup filesystem */
|
||||
/** @{*/
|
||||
|
||||
namespace mbed {
|
||||
|
||||
FileLike::FileLike(const char *name) : FileHandle(), FileBase(name, FilePathType) {
|
||||
// Standard types
|
||||
#include "platform/platform.h"
|
||||
|
||||
}
|
||||
// FileSystem classes
|
||||
#include "filesystem/FileSystem.h"
|
||||
#include "filesystem/File.h"
|
||||
#include "filesystem/Dir.h"
|
||||
|
||||
FileLike::~FileLike() {
|
||||
// BlockDevice classes
|
||||
#include "bd/BlockDevice.h"
|
||||
#include "bd/BlockDevice.h"
|
||||
#include "bd/ChainingBlockDevice.h"
|
||||
#include "bd/SlicingBlockDevice.h"
|
||||
#include "bd/HeapBlockDevice.h"
|
||||
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
/** @}*/
|
||||
#endif
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"name": "filesystem",
|
||||
"config": {
|
||||
"present": 1
|
||||
}
|
||||
}
|
4
mbed.h
4
mbed.h
|
@ -47,6 +47,10 @@
|
|||
#include "events/mbed_events.h"
|
||||
#endif
|
||||
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
#include "filesystem/mbed_filesystem.h"
|
||||
#endif
|
||||
|
||||
#include "platform/mbed_toolchain.h"
|
||||
#include "platform/platform.h"
|
||||
#include "platform/mbed_application.h"
|
||||
|
|
|
@ -14,8 +14,6 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
#include "platform/platform.h"
|
||||
#include "drivers/FileHandle.h"
|
||||
#include "drivers/FileSystemLike.h"
|
||||
#include "drivers/FilePath.h"
|
||||
#include "hal/serial_api.h"
|
||||
#include "platform/mbed_toolchain.h"
|
||||
|
@ -25,6 +23,11 @@
|
|||
#include "platform/PlatformMutex.h"
|
||||
#include "platform/mbed_error.h"
|
||||
#include "platform/mbed_stats.h"
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
#include "filesystem/FileSystem.h"
|
||||
#include "filesystem/File.h"
|
||||
#include "filesystem/Dir.h"
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#if DEVICE_STDIO_MESSAGES
|
||||
|
@ -82,19 +85,21 @@ uint32_t mbed_heap_size = 0;
|
|||
* put it in a filehandles array and return the index into that array
|
||||
* (or rather index+3, as filehandles 0-2 are stdin/out/err).
|
||||
*/
|
||||
static FileHandle *filehandles[OPEN_MAX];
|
||||
static FileLike *filehandles[OPEN_MAX];
|
||||
static SingletonPtr<PlatformMutex> filehandle_mutex;
|
||||
|
||||
FileHandle::~FileHandle() {
|
||||
namespace mbed {
|
||||
void remove_filehandle(FileLike *file) {
|
||||
filehandle_mutex->lock();
|
||||
/* Remove all open filehandles for this */
|
||||
for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
|
||||
if (filehandles[fh_i] == this) {
|
||||
if (filehandles[fh_i] == file) {
|
||||
filehandles[fh_i] = NULL;
|
||||
}
|
||||
}
|
||||
filehandle_mutex->unlock();
|
||||
}
|
||||
}
|
||||
|
||||
#if DEVICE_SERIAL
|
||||
extern int stdio_uart_inited;
|
||||
|
@ -154,6 +159,27 @@ extern "C" WEAK void mbed_sdk_init(void);
|
|||
extern "C" WEAK void mbed_sdk_init(void) {
|
||||
}
|
||||
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
// Internally used file objects with managed memory on close
|
||||
class ManagedFile : public File {
|
||||
public:
|
||||
virtual int close() {
|
||||
int err = File::close();
|
||||
delete this;
|
||||
return err;
|
||||
}
|
||||
};
|
||||
|
||||
class ManagedDir : public Dir {
|
||||
public:
|
||||
virtual int close() {
|
||||
int err = Dir::close();
|
||||
delete this;
|
||||
return err;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
/* @brief standard c library fopen() retargeting function.
|
||||
*
|
||||
* This function is invoked by the standard c library retargeting to handle fopen()
|
||||
|
@ -207,16 +233,16 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
|
|||
filehandle_mutex->unlock();
|
||||
return -1;
|
||||
}
|
||||
filehandles[fh_i] = (FileHandle*)FILE_HANDLE_RESERVED;
|
||||
filehandles[fh_i] = (FileLike*)FILE_HANDLE_RESERVED;
|
||||
filehandle_mutex->unlock();
|
||||
|
||||
FileHandle *res;
|
||||
FileLike *res = NULL;
|
||||
|
||||
/* FILENAME: ":0x12345678" describes a FileLike* */
|
||||
if (name[0] == ':') {
|
||||
void *p;
|
||||
sscanf(name, ":%p", &p);
|
||||
res = (FileHandle*)p;
|
||||
res = (FileLike*)p;
|
||||
|
||||
/* FILENAME: "/file_system/file_name" */
|
||||
} else {
|
||||
|
@ -232,8 +258,9 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
|
|||
return -1;
|
||||
} else if (path.isFile()) {
|
||||
res = path.file();
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
} else {
|
||||
FileSystemLike *fs = path.fileSystem();
|
||||
FileSystem *fs = path.fileSystem();
|
||||
if (fs == NULL) {
|
||||
/* The filesystem instance managing the namespace under the mount point
|
||||
* has not been found. Free file handle */
|
||||
|
@ -242,7 +269,15 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
|
|||
return -1;
|
||||
}
|
||||
int posix_mode = openmode_to_posix(openmode);
|
||||
res = fs->open(path.fileName(), posix_mode); /* NULL if fails */
|
||||
File *file = new ManagedFile;
|
||||
int err = file->open(fs, path.fileName(), posix_mode);
|
||||
if (err < 0) {
|
||||
errno = -err;
|
||||
delete file;
|
||||
} else {
|
||||
res = file;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -260,11 +295,17 @@ extern "C" int PREFIX(_close)(FILEHANDLE fh) {
|
|||
if (fh < 3) return 0;
|
||||
|
||||
errno = EBADF;
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
filehandles[fh-3] = NULL;
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
return fhc->close();
|
||||
int err = fhc->close();
|
||||
if (err < 0) {
|
||||
errno = -err;
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(__ICCARM__)
|
||||
|
@ -294,10 +335,13 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
|
|||
#endif
|
||||
n = length;
|
||||
} else {
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
n = fhc->write(buffer, length);
|
||||
if (n < 0) {
|
||||
errno = -n;
|
||||
}
|
||||
}
|
||||
#ifdef __ARMCC_VERSION
|
||||
return length-n;
|
||||
|
@ -343,10 +387,13 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
|
|||
#endif
|
||||
n = 1;
|
||||
} else {
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
n = fhc->read(buffer, length);
|
||||
if (n < 0) {
|
||||
errno = -n;
|
||||
}
|
||||
}
|
||||
#ifdef __ARMCC_VERSION
|
||||
return length-n;
|
||||
|
@ -365,10 +412,16 @@ extern "C" int _isatty(FILEHANDLE fh)
|
|||
/* stdin, stdout and stderr should be tty */
|
||||
if (fh < 3) return 1;
|
||||
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
return fhc->isatty();
|
||||
int err = fhc->isatty();
|
||||
if (err < 0) {
|
||||
errno = -err;
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C"
|
||||
|
@ -383,13 +436,13 @@ int _lseek(FILEHANDLE fh, int offset, int whence)
|
|||
errno = EBADF;
|
||||
if (fh < 3) return 0;
|
||||
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
#if defined(__ARMCC_VERSION)
|
||||
return fhc->lseek(position, SEEK_SET);
|
||||
return fhc->seek(position, SEEK_SET);
|
||||
#else
|
||||
return fhc->lseek(offset, whence);
|
||||
return fhc->seek(offset, whence);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -398,20 +451,26 @@ extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
|
|||
errno = EBADF;
|
||||
if (fh < 3) return 0;
|
||||
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
return fhc->fsync();
|
||||
int err = fhc->sync();
|
||||
if (err < 0) {
|
||||
errno = -err;
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" long PREFIX(_flen)(FILEHANDLE fh) {
|
||||
errno = EBADF;
|
||||
if (fh < 3) return 0;
|
||||
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
return fhc->flen();
|
||||
return fhc->size();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -429,25 +488,47 @@ extern "C" int _fstat(int fd, struct stat *st) {
|
|||
|
||||
namespace std {
|
||||
extern "C" int remove(const char *path) {
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
errno = EBADF;
|
||||
FilePath fp(path);
|
||||
FileSystemLike *fs = fp.fileSystem();
|
||||
FileSystem *fs = fp.fileSystem();
|
||||
if (fs == NULL) return -1;
|
||||
|
||||
return fs->remove(fp.fileName());
|
||||
int err = fs->remove(fp.fileName());
|
||||
if (err < 0) {
|
||||
errno = -err;
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" int rename(const char *oldname, const char *newname) {
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
errno = EBADF;
|
||||
FilePath fpOld(oldname);
|
||||
FilePath fpNew(newname);
|
||||
FileSystemLike *fsOld = fpOld.fileSystem();
|
||||
FileSystemLike *fsNew = fpNew.fileSystem();
|
||||
FileSystem *fsOld = fpOld.fileSystem();
|
||||
FileSystem *fsNew = fpNew.fileSystem();
|
||||
|
||||
/* rename only if both files are on the same FS */
|
||||
if (fsOld != fsNew || fsOld == NULL) return -1;
|
||||
|
||||
return fsOld->rename(fpOld.fileName(), fpNew.fileName());
|
||||
int err = fsOld->rename(fpOld.fileName(), fpNew.fileName());
|
||||
if (err < 0) {
|
||||
errno = -err;
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" char *tmpnam(char *s) {
|
||||
|
@ -468,53 +549,122 @@ extern "C" char *_sys_command_string(char *cmd, int len) {
|
|||
#endif
|
||||
|
||||
extern "C" DIR *opendir(const char *path) {
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
errno = EBADF;
|
||||
/* root dir is FileSystemLike */
|
||||
if (path[0] == '/' && path[1] == 0) {
|
||||
return FileSystemLike::opendir();
|
||||
}
|
||||
|
||||
FilePath fp(path);
|
||||
FileSystemLike* fs = fp.fileSystem();
|
||||
FileSystem* fs = fp.fileSystem();
|
||||
if (fs == NULL) return NULL;
|
||||
|
||||
return fs->opendir(fp.fileName());
|
||||
Dir *dir = new ManagedDir;
|
||||
int err = dir->open(fs, fp.fileName());
|
||||
if (err < 0) {
|
||||
errno = -err;
|
||||
delete dir;
|
||||
dir = NULL;
|
||||
}
|
||||
|
||||
return dir;
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" struct dirent *readdir(DIR *dir) {
|
||||
return dir->readdir();
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
static struct dirent ent;
|
||||
int err = dir->read(&ent);
|
||||
if (err < 1) {
|
||||
if (err < 0) {
|
||||
errno = -err;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &ent;
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" int closedir(DIR *dir) {
|
||||
return dir->closedir();
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
int err = dir->close();
|
||||
if (err < 0) {
|
||||
errno = -err;
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" void rewinddir(DIR *dir) {
|
||||
dir->rewinddir();
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
dir->rewind();
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" off_t telldir(DIR *dir) {
|
||||
return dir->telldir();
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
return dir->tell();
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" void seekdir(DIR *dir, off_t off) {
|
||||
dir->seekdir(off);
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
dir->seek(off);
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" int mkdir(const char *path, mode_t mode) {
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
FilePath fp(path);
|
||||
FileSystemLike *fs = fp.fileSystem();
|
||||
FileSystem *fs = fp.fileSystem();
|
||||
if (fs == NULL) return -1;
|
||||
|
||||
return fs->mkdir(fp.fileName(), mode);
|
||||
int err = fs->mkdir(fp.fileName(), mode);
|
||||
if (err < 0) {
|
||||
errno = -err;
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" int stat(const char *path, struct stat *st) {
|
||||
#if MBED_CONF_FILESYSTEM_PRESENT
|
||||
FilePath fp(path);
|
||||
FileSystemLike *fs = fp.fileSystem();
|
||||
FileSystem *fs = fp.fileSystem();
|
||||
if (fs == NULL) return -1;
|
||||
|
||||
return fs->stat(fp.fileName(), st);
|
||||
int err = fs->stat(fp.fileName(), st);
|
||||
if (err < 0) {
|
||||
errno = -err;
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(TOOLCHAIN_GCC)
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "platform/retarget.h"
|
||||
#include "platform/toolchain.h"
|
||||
#include "device.h"
|
||||
#include "PinNames.h"
|
||||
#include "PeripheralNames.h"
|
||||
|
|
|
@ -19,6 +19,49 @@
|
|||
#ifndef RETARGET_H
|
||||
#define RETARGET_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* We can get the following standard types from sys/types for gcc, but we
|
||||
* need to define the types ourselves for the other compilers that normally
|
||||
* target embedded systems */
|
||||
#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
|
||||
typedef int ssize_t; ///< Signed size type, usually encodes negative errors
|
||||
typedef long off_t; ///< Offset in a data stream
|
||||
typedef int mode_t; ///< Mode for opening files
|
||||
|
||||
#define O_RDONLY 0
|
||||
#define O_WRONLY 1
|
||||
#define O_RDWR 2
|
||||
#define O_CREAT 0x0200
|
||||
#define O_TRUNC 0x0400
|
||||
#define O_APPEND 0x0008
|
||||
|
||||
#define NAME_MAX 255 ///< Maximum size of a name in a file path
|
||||
|
||||
#else
|
||||
#include <sys/fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/syslimits.h>
|
||||
#endif
|
||||
|
||||
|
||||
/* DIR declarations must also be here */
|
||||
#if __cplusplus
|
||||
namespace mbed { class Dir; }
|
||||
typedef mbed::Dir DIR;
|
||||
|
||||
extern "C" {
|
||||
DIR *opendir(const char*);
|
||||
struct dirent *readdir(DIR *);
|
||||
int closedir(DIR*);
|
||||
void rewinddir(DIR*);
|
||||
long telldir(DIR*);
|
||||
void seekdir(DIR*, long);
|
||||
int mkdir(const char *name, mode_t n);
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
|
||||
/* The intent of this section is to unify the errno error values to match
|
||||
* the POSIX definitions for the GCC_ARM, ARMCC and IAR compilers. This is
|
||||
|
@ -72,6 +115,11 @@
|
|||
#endif
|
||||
#define EEXIST 17 /* File exists */
|
||||
|
||||
#ifdef EINVAL
|
||||
#undef EINVAL
|
||||
#endif
|
||||
#define EINVAL 22 /* Invalid argument */
|
||||
|
||||
#ifdef ENFILE
|
||||
#undef ENFILE
|
||||
#endif
|
||||
|
@ -82,6 +130,11 @@
|
|||
#endif
|
||||
#define EMFILE 24 /* File descriptor value too large */
|
||||
|
||||
#ifdef ENOSYS
|
||||
#undef ENOSYS
|
||||
#endif
|
||||
#define ENOSYS 38 /* Function not implemented */
|
||||
|
||||
/* Missing stat.h defines.
|
||||
* The following are sys/stat.h definitions not currently present in the ARMCC
|
||||
* errno.h. Note, ARMCC errno.h defines some symbol values differing from
|
||||
|
@ -108,4 +161,24 @@
|
|||
|
||||
#endif /* defined(__ARMCC_VERSION) || defined(__ICCARM__) */
|
||||
|
||||
|
||||
/* The following are dirent.h definitions are declared here to garuntee
|
||||
* consistency where structure may be different with different toolchains */
|
||||
struct dirent {
|
||||
char d_name[NAME_MAX+1];
|
||||
uint8_t d_type;
|
||||
};
|
||||
|
||||
enum {
|
||||
DT_UNKNOWN, // The file type could not be determined.
|
||||
DT_FIFO, // This is a named pipe (FIFO).
|
||||
DT_CHR, // This is a character device.
|
||||
DT_DIR, // This is a directory.
|
||||
DT_BLK, // This is a block device.
|
||||
DT_REG, // This is a regular file.
|
||||
DT_LNK, // This is a symbolic link.
|
||||
DT_SOCK, // This is a UNIX domain socket.
|
||||
};
|
||||
|
||||
|
||||
#endif /* RETARGET_H */
|
||||
|
|
Loading…
Reference in New Issue