Skip to content

Filesystem: Restructure the filesystem api to be consistent with mbed OS #3773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 14 additions & 37 deletions drivers/DirHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/** @{*/
Expand All @@ -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
Expand Down Expand Up @@ -117,22 +98,18 @@ class DirHandle {
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 */

/** @}*/
6 changes: 6 additions & 0 deletions drivers/FileBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
#include "drivers/FileBase.h"
#include "drivers/FileLike.h"

namespace mbed {

Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 0 additions & 21 deletions drivers/FileBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -57,7 +37,6 @@ typedef enum {
class FileBase {
public:
FileBase(const char *name, PathType t);

virtual ~FileBase();

const char* getName(void);
Expand Down
16 changes: 7 additions & 9 deletions drivers/FileHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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
Expand Down Expand Up @@ -120,7 +118,7 @@ class FileHandle {
return res;
}

virtual ~FileHandle();
virtual ~FileHandle() {};

protected:

Expand Down
127 changes: 115 additions & 12 deletions drivers/FileLike.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
* @param name The name to use to open the file.
*/
FileLike(const char *name = NULL) : FileBase(name, FilePathType) {}
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
*
* Variables
* name - The name to use to open the 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
*/
FileLike(const char *name);
virtual ssize_t write(const void *buffer, size_t len) = 0;

virtual ~FileLike();
/** 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

/** @}*/
4 changes: 2 additions & 2 deletions drivers/FilePath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion drivers/FilePath.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ namespace mbed {
/** \addtogroup drivers */
/** @{*/

class FileSystem;

class FilePath {
public:
FilePath(const char* file_path);

const char* fileName(void);

bool isFileSystem(void);
FileSystemLike* fileSystem(void);
FileSystem* fileSystem(void);

bool isFile(void);
FileLike* file(void);
Expand Down
2 changes: 1 addition & 1 deletion drivers/FileSystemLike.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BaseDirHandle : public DirHandle {
off_t n;
struct dirent cur_entry;

BaseDirHandle() : n(0), cur_entry() {
BaseDirHandle() : DirHandle(0), n(0), cur_entry() {
}

virtual int closedir() {
Expand Down
6 changes: 6 additions & 0 deletions drivers/FileSystemLike.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ class FileSystemLike : public FileBase {
*
* @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;

Expand Down
Loading