Merge pull request #3867 from geky/fs-revert-filehandle

Filesystem: Revert deprecation of FileHandle
pull/4020/head
Anna Bridge 2017-03-23 15:45:01 +00:00 committed by GitHub
commit 039ef42822
21 changed files with 338 additions and 378 deletions

View File

@ -1,115 +0,0 @@
/* 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_DIRHANDLE_H
#define MBED_DIRHANDLE_H
#include <stdint.h>
#include "platform/platform.h"
#include "FileHandle.h"
namespace mbed {
/** \addtogroup drivers */
/** @{*/
/** Represents a directory stream. Objects of this type are returned
* by a FileSystemLike's opendir method. Implementations must define
* at least closedir, readdir and rewinddir.
*
* If a FileSystemLike class defines the opendir method, then the
* directories of an object of that type can be accessed by
* DIR *d = opendir("/example/directory") (or opendir("/example")
* to open the root of the filesystem), and then using readdir(d) etc.
*
* The root directory is considered to contain all FileLike and
* FileSystemLike objects, so the DIR* returned by opendir("/") will
* reflect this.
*
* @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
* 0 on success,
* -1 on error.
*/
virtual int closedir()=0;
/** Return the directory entry at the current position, and
* advances the position to the next entry.
*
* @returns
* A pointer to a dirent structure representing the
* directory entry at the current position, or NULL on reaching
* end of directory or error.
*/
virtual struct dirent *readdir()=0;
/** Resets the position to the beginning of the directory.
*/
virtual void rewinddir()=0;
/** Returns the current position of the DirHandle.
*
* @returns
* the current position,
* -1 on error.
*/
virtual off_t telldir() { return -1; }
/** Sets the position of the DirHandle.
*
* @param location The location to seek to. Must be a value returned by telldir.
*/
virtual void seekdir(off_t location) { (void)location;}
virtual ~DirHandle() {}
protected:
/** Acquire exclusive access to this object.
*/
virtual void lock() {
// Stub
}
/** Release exclusive access to this object.
*/
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
#endif /* MBED_DIRHANDLE_H */
/** @}*/

View File

@ -1,142 +0,0 @@
/* 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_FILEHANDLE_H
#define MBED_FILEHANDLE_H
typedef int FILEHANDLE;
#include <stdio.h>
#include "platform/platform.h"
namespace mbed {
/** \addtogroup drivers */
/** @{*/
/** An OO equivalent of the internal FILEHANDLE variable
* and associated _sys_* functions.
*
* FileHandle is an abstract class, needing at least sys_write and
* sys_read to be implmented for a simple interactive device.
*
* 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
*/
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
* @param length the number of characters to write
*
* @returns
* The number of characters written (possibly 0) on success, -1 on error.
*/
virtual ssize_t write(const void* buffer, size_t length) = 0;
/** Close the file
*
* @returns
* Zero on success, -1 on error.
*/
virtual int close() = 0;
/** Function read
* Reads the contents of the file into a buffer
*
* @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.
*/
virtual ssize_t read(void* buffer, size_t length) = 0;
/** Check if the handle is for a interactive terminal device.
* If so, line buffered behaviour is used by default
*
* @returns
* 1 if it is a terminal,
* 0 otherwise
*/
virtual int isatty() = 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
*/
virtual off_t lseek(off_t offset, int whence) = 0;
/** 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
*/
virtual int fsync() = 0;
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.
*/
virtual void lock() {
// Stub
}
/** Release exclusive access to this object.
*/
virtual void unlock() {
// Stub
}
};
} // namespace mbed
#endif
/** @}*/

View File

@ -18,6 +18,7 @@
#define DIR_H
#include "filesystem/FileSystem.h"
#include "platform/DirHandle.h"
namespace mbed {
/** \addtogroup filesystem */
@ -26,7 +27,7 @@ namespace mbed {
/** Dir class
*/
class Dir {
class Dir : public DirHandle {
public:
/** Create an uninitialized directory
*

View File

@ -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);
}

View File

@ -18,7 +18,7 @@
#define FILE_H
#include "filesystem/FileSystem.h"
#include "drivers/FileLike.h"
#include "platform/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
*
@ -75,7 +75,7 @@ public:
* @return The number of bytes read, 0 at end of file, negative error on failure
*/
virtual ssize_t read(void *buffer, size_t len);
virtual ssize_t read(void *buffer, size_t size);
/** Write the contents of a buffer to a file
*
@ -83,7 +83,7 @@ public:
* @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);
virtual ssize_t write(const void *buffer, size_t size);
/** Flush any buffers associated with the file
*

View File

@ -19,7 +19,7 @@
#include "platform/platform.h"
#include "drivers/FileBase.h"
#include "platform/FileBase.h"
#include "BlockDevice.h"
namespace mbed {
@ -118,7 +118,7 @@ protected:
* @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;
virtual ssize_t file_read(fs_file_t file, void *buffer, size_t size) = 0;
/** Write the contents of a buffer to a file
*
@ -127,7 +127,7 @@ protected:
* @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;
virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t size) = 0;
/** Flush any buffers associated with the file
*

2
mbed.h
View File

@ -94,7 +94,7 @@
#include "drivers/LowPowerTimeout.h"
#include "drivers/LowPowerTicker.h"
#include "drivers/LowPowerTimer.h"
#include "drivers/LocalFileSystem.h"
#include "platform/LocalFileSystem.h"
#include "drivers/InterruptIn.h"
#include "platform/mbed_wait_api.h"
#include "hal/sleep_api.h"

150
platform/DirHandle.h Normal file
View File

@ -0,0 +1,150 @@
/* 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_DIRHANDLE_H
#define MBED_DIRHANDLE_H
#include <stdint.h>
#include "platform/platform.h"
#include "platform/FileHandle.h"
namespace mbed {
/** \addtogroup drivers */
/** @{*/
/** Represents a directory stream. Objects of this type are returned
* by an opendir function. The core functions are read and seek,
* but only a subset needs to be provided.
*
* If a FileSystemLike class defines the opendir method, then the
* directories of an object of that type can be accessed by
* DIR *d = opendir("/example/directory") (or opendir("/example")
* to open the root of the filesystem), and then using readdir(d) etc.
*
* The root directory is considered to contain all FileHandle and
* FileSystem objects, so the DIR* returned by opendir("/") will
* reflect this.
*
* @note to create a directory, @see Dir
* @Note Synchronization level: Set by subclass
*/
class DirHandle {
public:
virtual ~DirHandle() {}
/** 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) = 0;
/** Close a directory
*
* return 0 on success, negative error code on failure
*/
virtual int close() = 0;
/** 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) = 0;
/** Get the current position of the directory
*
* @return Position of the directory that can be passed to rewind
*/
virtual off_t tell() = 0;
/** Rewind the current position to the beginning of the directory
*/
virtual void rewind() = 0;
/** Get the sizeof the directory
*
* @return Number of files in the directory
*/
virtual size_t size()
{
off_t off = tell();
size_t size = 0;
struct dirent *ent = new struct dirent;
rewind();
while (read(ent) > 0) {
size += 1;
}
seek(off);
delete ent;
return size;
}
/** Closes the directory.
*
* @returns
* 0 on success,
* -1 on error.
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::close")
virtual int closedir() { return close(); };
/** Return the directory entry at the current position, and
* advances the position to the next entry.
*
* @returns
* A pointer to a dirent structure representing the
* directory entry at the current position, or NULL on reaching
* end of directory or error.
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::read")
virtual struct dirent *readdir()
{
static struct dirent ent;
return (read(&ent) > 0) ? &ent : NULL;
}
/** Resets the position to the beginning of the directory.
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::rewind")
virtual void rewinddir() { rewind(); }
/** Returns the current position of the DirHandle.
*
* @returns
* the current position,
* -1 on error.
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::tell")
virtual off_t telldir() { return tell(); }
/** Sets the position of the DirHandle.
*
* @param location The location to seek to. Must be a value returned by telldir.
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::seek")
virtual void seekdir(off_t location) { seek(location); }
};
} // namespace mbed
#endif /* MBED_DIRHANDLE_H */
/** @}*/

View File

@ -13,8 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "drivers/FileBase.h"
#include "drivers/FileLike.h"
#include "platform/FileBase.h"
#include "platform/FileLike.h"
#include "platform/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)));
}
}

View File

@ -13,31 +13,31 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_FILELIKE_H
#define MBED_FILELIKE_H
#ifndef MBED_FILEHANDLE_H
#define MBED_FILEHANDLE_H
#include "platform/mbed_toolchain.h"
#include "drivers/FileBase.h"
typedef int FILEHANDLE;
#include <stdio.h>
#include "platform/platform.h"
namespace mbed {
/** \addtogroup drivers */
/** @{*/
/* Class FileLike
* A file-like object is one that can be opened with fopen by
* fopen("/name", mode).
/** Class FileHandle
*
* @Note Synchronization level: Set by subclass
* 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.
*
* @note to create a file, @see File
* @note Synchronization level: Set by subclass
*/
class FileLike : public FileBase {
class FileHandle {
public:
/** Constructor FileLike
*
* @param name The name to use to open the file.
*/
FileLike(const char *name = NULL) : FileBase(name, FilePathType) {}
virtual ~FileLike() {}
virtual ~FileHandle() {}
/** Read the contents of a file into a buffer
*
@ -45,7 +45,7 @@ public:
* @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;
virtual ssize_t read(void *buffer, size_t size) = 0;
/** Write the contents of a buffer to a file
*
@ -53,25 +53,7 @@ public:
* @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;
virtual ssize_t write(const void *buffer, size_t size) = 0;
/** Move the file position to a given offset from from a given location
*
@ -84,23 +66,61 @@ public:
*/
virtual off_t seek(off_t offset, int whence = SEEK_SET) = 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()
{
return 0;
}
/** Check if the file in an interactive terminal device
*
* @return True if the file is a terminal
*/
virtual int isatty()
{
return false;
}
/** 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() = 0;
virtual off_t tell()
{
return seek(0, SEEK_CUR);
}
/** Rewind the file position to the beginning of the file
*
* @note This is equivalent to file_seek(file, 0, FS_SEEK_SET)
* @note This is equivalent to seek(0, SEEK_SET)
*/
virtual void rewind() = 0;
virtual void rewind()
{
seek(0, SEEK_SET);
}
/** Get the size of the file
*
* @return Size of the file in bytes
*/
virtual size_t size() = 0;
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.
*
@ -112,7 +132,7 @@ public:
* new file position on success,
* -1 on failure or unsupported
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::seek")
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
@ -122,7 +142,7 @@ public:
* 0 on success or un-needed,
* -1 on error
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::sync")
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::sync")
virtual int fsync() { return sync(); }
/** Find the length of the file
@ -130,21 +150,8 @@ public:
* @returns
* Length of the file
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::size")
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::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
}
};

48
platform/FileLike.h Normal file
View File

@ -0,0 +1,48 @@
/* 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_FILELIKE_H
#define MBED_FILELIKE_H
#include "platform/mbed_toolchain.h"
#include "platform/FileBase.h"
#include "platform/FileHandle.h"
namespace mbed {
/** \addtogroup drivers */
/** @{*/
/* Class FileLike
* A file-like object is one that can be opened with fopen by
* fopen("/name", mode).
*
* @Note Synchronization level: Set by subclass
*/
class FileLike : public FileHandle, public FileBase {
public:
/** Constructor FileLike
*
* @param name The name to use to open the file.
*/
FileLike(const char *name = NULL) : FileBase(name, FilePathType) {}
virtual ~FileLike() {}
};
/** @}*/
} // namespace mbed
#endif

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "drivers/FilePath.h"
#include "platform/FilePath.h"
namespace mbed {

View File

@ -18,8 +18,8 @@
#include "platform/platform.h"
#include "drivers/FileSystemLike.h"
#include "drivers/FileLike.h"
#include "platform/FileSystemLike.h"
#include "platform/FileLike.h"
namespace mbed {
/** \addtogroup drivers */

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "drivers/FileSystemLike.h"
#include "platform/FileSystemLike.h"
namespace mbed {
@ -27,48 +27,47 @@ public:
give unusual results from readdir.
*/
off_t n;
struct dirent cur_entry;
BaseDirHandle() : DirHandle(0), n(0), cur_entry() {
BaseDirHandle() : DirHandle(), n(0) {
}
virtual int closedir() {
virtual int close() {
// No lock can be used in destructor
delete this;
return 0;
}
virtual struct dirent *readdir() {
virtual int read(struct dirent *ent) {
lock();
FileBase *ptr = FileBase::get(n);
if (ptr == NULL) {
unlock();
return NULL;
return -1;
}
/* Increment n, so next readdir gets the next item */
n++;
/* Setup cur entry and return a pointer to it */
std::strncpy(cur_entry.d_name, ptr->getName(), NAME_MAX);
std::strncpy(ent->d_name, ptr->getName(), NAME_MAX);
unlock();
return &cur_entry;
return 0;
}
virtual off_t telldir() {
virtual off_t tell() {
lock();
off_t offset = n;
unlock();
return offset;
}
virtual void seekdir(off_t offset) {
virtual void seek(off_t offset) {
lock();
n = offset;
unlock();
}
virtual void rewinddir() {
virtual void rewind() {
lock();
n = 0;
unlock();

View File

@ -18,9 +18,9 @@
#include "platform/platform.h"
#include "drivers/FileBase.h"
#include "drivers/FileHandle.h"
#include "drivers/DirHandle.h"
#include "platform/FileBase.h"
#include "platform/FileHandle.h"
#include "platform/DirHandle.h"
namespace mbed {
/** \addtogroup drivers */

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "drivers/LocalFileSystem.h"
#include "platform/LocalFileSystem.h"
#if DEVICE_LOCALFILESYSTEM
@ -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();
@ -182,43 +182,42 @@ void LocalFileHandle::unlock() {
class LocalDirHandle : public DirHandle {
public:
struct dirent cur_entry;
XFINFO info;
LocalDirHandle() : cur_entry(), info() {
LocalDirHandle() : info() {
}
virtual int closedir() {
virtual int close() {
// No lock can be used in destructor
delete this;
return 0;
}
virtual struct dirent *readdir() {
virtual int read(struct dirent *ent) {
lock();
if (xffind("*", &info)!=0) {
unlock();
return NULL;
return 0;
}
memcpy(cur_entry.d_name, info.name, sizeof(info.name));
memcpy(ent->d_name, info.name, sizeof(info.name));
unlock();
return &cur_entry;
return 1;
}
virtual void rewinddir() {
virtual void rewind() {
lock();
info.fileID = 0;
unlock();
}
virtual off_t telldir() {
virtual off_t tell() {
lock();
int fileId = info.fileID;
unlock();
return fileId;
}
virtual void seekdir(off_t offset) {
virtual void seek(off_t offset) {
lock();
info.fileID = offset;
unlock();

View File

@ -20,7 +20,7 @@
#if DEVICE_LOCALFILESYSTEM
#include "drivers/FileSystemLike.h"
#include "platform/FileSystemLike.h"
#include "platform/PlatformMutex.h"
namespace mbed {
@ -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();

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "drivers/Stream.h"
#include "platform/Stream.h"
namespace mbed {

View File

@ -17,8 +17,8 @@
#define MBED_STREAM_H
#include "platform/platform.h"
#include "drivers/FileLike.h"
#include "drivers/FileHandle.h"
#include "platform/FileLike.h"
#include "platform/FileHandle.h"
#include <cstdarg>
namespace mbed {
@ -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&);

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "platform/platform.h"
#include "drivers/FilePath.h"
#include "platform/FilePath.h"
#include "hal/serial_api.h"
#include "platform/mbed_toolchain.h"
#include "platform/mbed_semihost_api.h"
@ -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();