mirror of https://github.com/ARMmbed/mbed-os.git
Filesystem: Revert deprecation of FileHandle
As identified by @hasnainvirk, @kjbracey-arm, the FileHandle and FileBase serve two separate functions and their integration is limiting for certain use cases. FileLike is actually the redundant class here, but the multiple inheritance it provides is used as a hack by the retargeting code to get at the FileHandle implementation bound to the FileBase name. It may make more sense for the FileBase to inherit from FileHandle, (with perhaps a different name), but rather than explore the possibility, this will just restore the previous hierarchy.pull/3867/head
parent
cc58a7fb0c
commit
77243ef46b
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
#include "drivers/FileBase.h"
|
||||
#include "drivers/FileLike.h"
|
||||
#include "drivers/FileHandle.h"
|
||||
|
||||
namespace mbed {
|
||||
|
||||
|
@ -52,8 +53,8 @@ FileBase::~FileBase() {
|
|||
_mutex->unlock();
|
||||
|
||||
if (getPathType() == FilePathType) {
|
||||
extern void remove_filehandle(FileLike *file);
|
||||
remove_filehandle(static_cast<FileLike*>(this));
|
||||
extern void remove_filehandle(FileHandle *file);
|
||||
remove_filehandle(static_cast<FileHandle*>(static_cast<FileLike*>(this)));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,62 +25,97 @@ namespace mbed {
|
|||
/** \addtogroup drivers */
|
||||
/** @{*/
|
||||
|
||||
/** An OO equivalent of the internal FILEHANDLE variable
|
||||
* and associated _sys_* functions.
|
||||
|
||||
/** Class FileHandle
|
||||
*
|
||||
* FileHandle is an abstract class, needing at least sys_write and
|
||||
* sys_read to be implmented for a simple interactive device.
|
||||
* An abstract interface that represents operations on a file-like
|
||||
* object. The core functions are read, write, and seek, but only
|
||||
* a subset of these operations can be provided.
|
||||
*
|
||||
* No one ever directly tals to/instanciates a FileHandle - it gets
|
||||
* created by FileSystem, and wrapped up by stdio.
|
||||
*
|
||||
* @Note Synchronization level: Set by subclass
|
||||
* @note to create a file, @see File
|
||||
* @note Synchronization level: Set by subclass
|
||||
*/
|
||||
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() {}
|
||||
virtual ~FileHandle() {}
|
||||
|
||||
/** Write the contents of a buffer to the file
|
||||
/** Read the contents of a file into a buffer
|
||||
*
|
||||
* @param buffer the buffer to write from
|
||||
* @param length the number of characters to write
|
||||
*
|
||||
* @returns
|
||||
* The number of characters written (possibly 0) on success, -1 on error.
|
||||
* @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 write(const void* buffer, size_t length) = 0;
|
||||
virtual ssize_t read(void *buffer, size_t len) = 0;
|
||||
|
||||
/** Close the file
|
||||
/** Write the contents of a buffer to a file
|
||||
*
|
||||
* @returns
|
||||
* Zero on success, -1 on error.
|
||||
* @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;
|
||||
|
||||
/** Function read
|
||||
* Reads the contents of the file into a buffer
|
||||
/** Flush any buffers associated with the file
|
||||
*
|
||||
* @param buffer the buffer to read in to
|
||||
* @param length the number of characters to read
|
||||
*
|
||||
* @returns
|
||||
* The number of characters read (zero at end of file) on success, -1 on error.
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
virtual ssize_t read(void* buffer, size_t length) = 0;
|
||||
virtual int sync() = 0;
|
||||
|
||||
/** Check if the handle is for a interactive terminal device.
|
||||
* If so, line buffered behaviour is used by default
|
||||
/** Check if the file in an interactive terminal device
|
||||
*
|
||||
* @returns
|
||||
* 1 if it is a terminal,
|
||||
* 0 otherwise
|
||||
* @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
|
||||
*
|
||||
* @note This is equivalent to seek(0, SEEK_CUR)
|
||||
*
|
||||
* @return The current offset in the file
|
||||
*/
|
||||
virtual off_t tell()
|
||||
{
|
||||
return seek(0, SEEK_CUR);
|
||||
}
|
||||
|
||||
/** Rewind the file position to the beginning of the file
|
||||
*
|
||||
* @note This is equivalent to seek(0, SEEK_SET)
|
||||
*/
|
||||
virtual void rewind()
|
||||
{
|
||||
seek(0, SEEK_SET);
|
||||
}
|
||||
|
||||
/** Get the size of the file
|
||||
*
|
||||
* @return Size of the file in bytes
|
||||
*/
|
||||
virtual size_t size()
|
||||
{
|
||||
off_t off = tell();
|
||||
size_t size = seek(0, SEEK_END);
|
||||
seek(off, SEEK_SET);
|
||||
return size;
|
||||
}
|
||||
|
||||
/** Move the file position to a given offset from a given location.
|
||||
*
|
||||
* @param offset The offset from whence to move to
|
||||
|
@ -91,7 +126,8 @@ public:
|
|||
* new file position on success,
|
||||
* -1 on failure or unsupported
|
||||
*/
|
||||
virtual off_t lseek(off_t offset, int whence) = 0;
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::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
|
||||
|
@ -100,43 +136,20 @@ public:
|
|||
* 0 on success or un-needed,
|
||||
* -1 on error
|
||||
*/
|
||||
virtual int fsync() = 0;
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::sync")
|
||||
virtual int fsync() { return sync(); }
|
||||
|
||||
virtual off_t flen() {
|
||||
lock();
|
||||
/* remember our current position */
|
||||
off_t pos = lseek(0, SEEK_CUR);
|
||||
if(pos == -1) {
|
||||
unlock();
|
||||
return -1;
|
||||
}
|
||||
/* seek to the end to get the file length */
|
||||
off_t res = lseek(0, SEEK_END);
|
||||
/* return to our old position */
|
||||
lseek(pos, SEEK_SET);
|
||||
unlock();
|
||||
return res;
|
||||
}
|
||||
|
||||
virtual ~FileHandle() {};
|
||||
|
||||
protected:
|
||||
|
||||
/** Acquire exclusive access to this object.
|
||||
/** Find the length of the file
|
||||
*
|
||||
* @returns
|
||||
* Length of the file
|
||||
*/
|
||||
virtual void lock() {
|
||||
// Stub
|
||||
}
|
||||
|
||||
/** Release exclusive access to this object.
|
||||
*/
|
||||
virtual void unlock() {
|
||||
// Stub
|
||||
}
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::size")
|
||||
virtual off_t flen() { return size(); }
|
||||
};
|
||||
|
||||
|
||||
/** @}*/
|
||||
} // namespace mbed
|
||||
|
||||
#endif
|
||||
|
||||
/** @}*/
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include "platform/mbed_toolchain.h"
|
||||
#include "drivers/FileBase.h"
|
||||
#include "drivers/FileHandle.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup drivers */
|
||||
|
@ -30,7 +31,7 @@ namespace mbed {
|
|||
*
|
||||
* @Note Synchronization level: Set by subclass
|
||||
*/
|
||||
class FileLike : public FileBase {
|
||||
class FileLike : public FileHandle, public FileBase {
|
||||
public:
|
||||
/** Constructor FileLike
|
||||
*
|
||||
|
@ -38,113 +39,6 @@ public:
|
|||
*/
|
||||
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
|
||||
*
|
||||
* @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
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -142,7 +142,7 @@ int LocalFileHandle::isatty() {
|
|||
return ret;
|
||||
}
|
||||
|
||||
off_t LocalFileHandle::lseek(off_t position, int whence) {
|
||||
off_t LocalFileHandle::seek(off_t position, int whence) {
|
||||
lock();
|
||||
if (whence == SEEK_CUR) {
|
||||
position += pos;
|
||||
|
@ -157,14 +157,14 @@ off_t LocalFileHandle::lseek(off_t position, int whence) {
|
|||
return position;
|
||||
}
|
||||
|
||||
int LocalFileHandle::fsync() {
|
||||
int LocalFileHandle::sync() {
|
||||
lock();
|
||||
int ret = semihost_ensure(_fh);
|
||||
unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
off_t LocalFileHandle::flen() {
|
||||
size_t LocalFileHandle::size() {
|
||||
lock();
|
||||
off_t off = semihost_flen(_fh);
|
||||
unlock();
|
||||
|
|
|
@ -42,11 +42,11 @@ public:
|
|||
|
||||
virtual int isatty();
|
||||
|
||||
virtual off_t lseek(off_t position, int whence);
|
||||
virtual off_t seek(off_t position, int whence);
|
||||
|
||||
virtual int fsync();
|
||||
virtual int sync();
|
||||
|
||||
virtual off_t flen();
|
||||
virtual size_t size();
|
||||
|
||||
protected:
|
||||
virtual void lock();
|
||||
|
|
|
@ -66,6 +66,18 @@ protected:
|
|||
|
||||
std::FILE *_file;
|
||||
|
||||
/** Acquire exclusive access to this object.
|
||||
*/
|
||||
virtual void lock() {
|
||||
// Stub
|
||||
}
|
||||
|
||||
/** Release exclusive access to this object.
|
||||
*/
|
||||
virtual void unlock() {
|
||||
// Stub
|
||||
}
|
||||
|
||||
/* disallow copy constructor and assignment operators */
|
||||
private:
|
||||
Stream(const Stream&);
|
||||
|
|
|
@ -25,7 +25,7 @@ File::File()
|
|||
}
|
||||
|
||||
File::File(FileSystem *fs, const char *path, int flags)
|
||||
: FileLike(path), _fs(0), _file(0)
|
||||
: _fs(0), _file(0)
|
||||
{
|
||||
open(fs, path, flags);
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
#define FILE_H
|
||||
|
||||
#include "filesystem/FileSystem.h"
|
||||
#include "drivers/FileLike.h"
|
||||
#include "drivers/FileHandle.h"
|
||||
|
||||
namespace mbed {
|
||||
/** \addtogroup filesystem */
|
||||
|
@ -27,7 +27,7 @@ namespace mbed {
|
|||
|
||||
/** File class
|
||||
*/
|
||||
class File : public FileLike {
|
||||
class File : public FileHandle {
|
||||
public:
|
||||
/** Create an uninitialized file
|
||||
*
|
||||
|
|
|
@ -85,11 +85,11 @@ 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 FileLike *filehandles[OPEN_MAX];
|
||||
static FileHandle *filehandles[OPEN_MAX];
|
||||
static SingletonPtr<PlatformMutex> filehandle_mutex;
|
||||
|
||||
namespace mbed {
|
||||
void remove_filehandle(FileLike *file) {
|
||||
void remove_filehandle(FileHandle *file) {
|
||||
filehandle_mutex->lock();
|
||||
/* Remove all open filehandles for this */
|
||||
for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
|
||||
|
@ -233,16 +233,16 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
|
|||
filehandle_mutex->unlock();
|
||||
return -1;
|
||||
}
|
||||
filehandles[fh_i] = (FileLike*)FILE_HANDLE_RESERVED;
|
||||
filehandles[fh_i] = (FileHandle*)FILE_HANDLE_RESERVED;
|
||||
filehandle_mutex->unlock();
|
||||
|
||||
FileLike *res = NULL;
|
||||
FileHandle *res = NULL;
|
||||
|
||||
/* FILENAME: ":0x12345678" describes a FileLike* */
|
||||
/* FILENAME: ":0x12345678" describes a FileHandle* */
|
||||
if (name[0] == ':') {
|
||||
void *p;
|
||||
sscanf(name, ":%p", &p);
|
||||
res = (FileLike*)p;
|
||||
res = (FileHandle*)p;
|
||||
|
||||
/* FILENAME: "/file_system/file_name" */
|
||||
} else {
|
||||
|
@ -295,7 +295,7 @@ extern "C" int PREFIX(_close)(FILEHANDLE fh) {
|
|||
if (fh < 3) return 0;
|
||||
|
||||
errno = EBADF;
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
filehandles[fh-3] = NULL;
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
|
@ -335,7 +335,7 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
|
|||
#endif
|
||||
n = length;
|
||||
} else {
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
n = fhc->write(buffer, length);
|
||||
|
@ -387,7 +387,7 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
|
|||
#endif
|
||||
n = 1;
|
||||
} else {
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
n = fhc->read(buffer, length);
|
||||
|
@ -412,7 +412,7 @@ extern "C" int _isatty(FILEHANDLE fh)
|
|||
/* stdin, stdout and stderr should be tty */
|
||||
if (fh < 3) return 1;
|
||||
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
int err = fhc->isatty();
|
||||
|
@ -436,7 +436,7 @@ int _lseek(FILEHANDLE fh, int offset, int whence)
|
|||
errno = EBADF;
|
||||
if (fh < 3) return 0;
|
||||
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
#if defined(__ARMCC_VERSION)
|
||||
|
@ -451,7 +451,7 @@ extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
|
|||
errno = EBADF;
|
||||
if (fh < 3) return 0;
|
||||
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
int err = fhc->sync();
|
||||
|
@ -467,7 +467,7 @@ extern "C" long PREFIX(_flen)(FILEHANDLE fh) {
|
|||
errno = EBADF;
|
||||
if (fh < 3) return 0;
|
||||
|
||||
FileLike* fhc = filehandles[fh-3];
|
||||
FileHandle* fhc = filehandles[fh-3];
|
||||
if (fhc == NULL) return -1;
|
||||
|
||||
return fhc->size();
|
||||
|
|
Loading…
Reference in New Issue