Filesystem: Created prototypical filesystem class

Intention is to make filesystem api and network stack api consistent
as current designs diverge greatly. Attempted to change as little as
possible outside of api structure.
pull/3773/head
Christopher Haster 2017-01-19 19:54:49 -06:00
parent 9844a390d9
commit a5245e32fe
6 changed files with 818 additions and 0 deletions

View File

@ -0,0 +1,94 @@
/* 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"
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 FS_ERROR_PARAMETER;
}
_fs = fs;
return _fs->dir_open(&_dir, path);
}
int Dir::close()
{
if (!_fs) {
return FS_ERROR_PARAMETER;
}
int err = _fs->dir_close(_dir);
_fs = 0;
return err;
}
ssize_t Dir::read(char *path, size_t len)
{
MBED_ASSERT(_fs);
return _fs->dir_read(_dir, path, len);
}
ssize_t Dir::read(char *path, size_t len, uint8_t *type)
{
MBED_ASSERT(_fs);
return _fs->dir_read(_dir, path, len, type);
}
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);
}

114
features/filesystem/Dir.h Normal file
View File

@ -0,0 +1,114 @@
/* 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 size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
*/
virtual ssize_t read(char *path, size_t len);
/** Read the next directory entry
*
* @param dir Dir handle
* @param path The buffer to read the null terminated path name in to
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
* @param type The type of the file, one of DT_DIR, DT_REG, etc...
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
*/
virtual ssize_t read(char *path, size_t len, uint8_t *type);
/** 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

View File

@ -0,0 +1,107 @@
/* 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"
File::File()
: _fs(0), _file(0)
{
}
File::File(FileSystem *fs, const char *path, int flags)
: _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 FS_ERROR_PARAMETER;
}
_fs = fs;
return _fs->file_open(&_file, path, flags);
}
int File::close()
{
if (!_fs) {
return FS_ERROR_PARAMETER;
}
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);
}
bool 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);
}

137
features/filesystem/File.h Normal file
View File

@ -0,0 +1,137 @@
/* 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"
namespace mbed {
/** \addtogroup filesystem */
/** @{*/
/** File class
*/
class File {
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 bool 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

View File

@ -0,0 +1,108 @@
/* 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"
int FileSystem::file_sync(fs_file_t file)
{
return 0;
}
bool 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 FS_ERROR_UNSUPPORTED;
}
int FileSystem::dir_open(fs_dir_t *dir, const char *path)
{
return FS_ERROR_UNSUPPORTED;
}
int FileSystem::dir_close(fs_dir_t dir)
{
return FS_ERROR_UNSUPPORTED;
}
ssize_t FileSystem::dir_read(fs_dir_t dir, char *path, size_t len)
{
return FS_ERROR_UNSUPPORTED;
}
ssize_t FileSystem::dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type)
{
return FS_ERROR_UNSUPPORTED;
}
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;
dir_rewind(dir);
while (true) {
int res = dir_read(dir, NULL, 0);
if (res <= 0) {
break;
}
size += 1;
}
dir_seek(dir, off);
return size;
}

View File

@ -0,0 +1,258 @@
/* 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 "drivers/FileHandle.h"
#include "drivers/DirHandle.h"
namespace mbed {
/** \addtogroup filesystem */
/** @{*/
/** Enum of standardized error codes
*
* Valid error codes have negative values and may
* be returned by any network operation
*/
enum fs_error {
FS_ERROR_OK = 0, ///< no error
FS_ERROR_UNSUPPORTED = -5001, ///< unsupported functionality
FS_ERROR_PARAMETER = -5002, ///< invalid parameter
};
// TODO move to retarget
enum fs_type {
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.
};
// 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:
/** FileSystem lifetime
*/
~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 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 bool 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);
/** Open a directory on the filesystem
*
* @param dir Destination for the handle to the directory
* @param path Name of the directory to open
* @return 0 on success, negative error code on failure
*/
virtual int dir_open(fs_dir_t *dir, const char *path);
/** Close a directory
*
* @param dir Dir handle
* return 0 on success, negative error code on failure
*/
virtual int dir_close(fs_dir_t dir);
/** Read the next directory entry
*
* @param dir Dir handle
* @param path The buffer to read the null terminated path name in to
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
*/
virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len);
/** Read the next directory entry
*
* @param dir Dir handle
* @param path The buffer to read the null terminated path name in to
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
* @param type The type of the file, one of DT_DIR, DT_REG, etc...
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
*/
virtual ssize_t dir_read(fs_dir_t dir, char *path, size_t len, uint8_t *type);
/** Set the current position of the directory
*
* @param dir Dir handle
* @param offset Offset of the location to seek to,
* must be a value returned from dir_tell
*/
virtual void dir_seek(fs_dir_t dir, off_t offset);
/** Get the current position of the directory
*
* @param dir Dir handle
* @return Position of the directory that can be passed to dir_rewind
*/
virtual off_t dir_tell(fs_dir_t dir);
/** Rewind the current position to the beginning of the directory
*
* @param dir Dir handle
*/
virtual void dir_rewind(fs_dir_t dir);
/** Get the sizeof the directory
*
* @param dir Dir handle
* @return Number of files in the directory
*/
virtual size_t dir_size(fs_dir_t dir);
};
/** @}*/
} // namespace mbed
#endif