Skip to content

LocalFileSystem: Repair the FileSystemLike hooks #4087

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 3 commits into from
Jun 4, 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
63 changes: 56 additions & 7 deletions features/filesystem/FileSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
Expand All @@ -21,8 +20,28 @@


FileSystem::FileSystem(const char *name)
: FileBase(name, FileSystemPathType)
: FileSystemLike(name)
{
}

int FileSystem::remove(const char *path)
{
return -ENOSYS;
}

int FileSystem::rename(const char *path, const char *newpath)
{
return -ENOSYS;
}

int FileSystem::stat(const char *path, struct stat *st)
{
return -ENOSYS;
}

int FileSystem::mkdir(const char *path, mode_t mode)
{
return -ENOSYS;
}

int FileSystem::file_sync(fs_file_t file)
Expand Down Expand Up @@ -53,11 +72,6 @@ off_t FileSystem::file_size(fs_file_t file)
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;
Expand Down Expand Up @@ -109,3 +123,38 @@ size_t FileSystem::dir_size(fs_dir_t dir)
return size;
}

// Internally used file wrapper that manages memory on close
template <typename F>
class Managed : public F {
public:
virtual int close() {
int err = F::close();
delete this;
return err;
}
};

int FileSystem::open(FileHandle **file, const char *path, int flags)
{
File *f = new Managed<File>;
int err = f->open(this, path, flags);
if (err) {
delete f;
return err;
}

*file = f;
return 0;
}

int FileSystem::open(DirHandle **dir, const char *path) {
Dir *d = new Managed<Dir>;
int err = d->open(this, path);
if (err) {
delete d;
return err;
}

*dir = d;
return 0;
}
30 changes: 21 additions & 9 deletions features/filesystem/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "platform/platform.h"

#include "platform/FileBase.h"
#include "platform/FileHandle.h"
#include "platform/DirHandle.h"
#include "platform/FileSystemLike.h"
#include "BlockDevice.h"

namespace mbed {
Expand All @@ -31,15 +34,19 @@ namespace mbed {
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)
// Predeclared classes
class Dir;
class File;

/** A filesystem object provides filesystem operations and file operations
* for the File and Dir classes on a block device.
*
* Implementations must define at least open (the default definitions
* of the rest of the functions just return error values).
* Implementations must provide at minimum file operations and mount
* operations for block devices.
*
* @Note Synchronization level: Set by subclass
* @Note Synchronization level: Set by subclass
*/
class FileSystem : public FileBase {
class FileSystem : public FileSystemLike {
public:
/** FileSystem lifetime
*/
Expand All @@ -64,23 +71,23 @@ class FileSystem : public FileBase {
* @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;
virtual int remove(const char *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 rename(const char *path, const char *newpath) = 0;
virtual int rename(const char *path, const char *newpath);

/** 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;
virtual int stat(const char *path, struct stat *st);

/** Create a directory in the filesystem.
*
Expand Down Expand Up @@ -227,6 +234,11 @@ class FileSystem : public FileBase {
* @return Number of files in the directory
*/
virtual size_t dir_size(fs_dir_t dir);

protected:
// Hooks for FileSystemHandle
virtual int open(FileHandle **file, const char *path, int flags);
virtual int open(DirHandle **dir, const char *path);
};


Expand Down
3 changes: 3 additions & 0 deletions mbed.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
#include "platform/mbed_rtc_time.h"
#include "platform/mbed_poll.h"
#include "platform/ATCmdParser.h"
#include "platform/FileSystemHandle.h"
#include "platform/FileHandle.h"
#include "platform/DirHandle.h"

// mbed Non-hardware components
#include "platform/Callback.h"
Expand Down
4 changes: 2 additions & 2 deletions platform/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);
}

FileSystem* FilePath::fileSystem(void) {
FileSystemLike* FilePath::fileSystem(void) {
if (isFileSystem()) {
return (FileSystem*)fb;
return static_cast<FileSystemLike*>(fb);
}
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion platform/FilePath.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FilePath {
const char* fileName(void);

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

bool isFile(void);
FileLike* file(void);
Expand Down
44 changes: 44 additions & 0 deletions platform/FileSystemHandle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* 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 "FileSystemHandle.h"
#include <errno.h>

int FileSystemHandle::open(DirHandle **dir, const char *path)
{
return -ENOSYS;
}

int FileSystemHandle::remove(const char *path)
{
return -ENOSYS;
}

int FileSystemHandle::rename(const char *path, const char *newpath)
{
return -ENOSYS;
}

int FileSystemHandle::stat(const char *path, struct stat *st)
{
return -ENOSYS;
}

int FileSystemHandle::mkdir(const char *path, mode_t mode)
{
return -ENOSYS;
}
99 changes: 99 additions & 0 deletions platform/FileSystemHandle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* 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_FILESYSTEMHANDLE_H
#define MBED_FILESYSTEMHANDLE_H

#include "platform/platform.h"

#include "platform/FileBase.h"
#include "platform/FileHandle.h"
#include "platform/DirHandle.h"

namespace mbed {
/** \addtogroup drivers */
/** @{*/


/** A filesystem-like object is one that can be used to open file-like
* objects though it by fopen("/name/filename", mode)
*
* 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 FileSystemHandle {
public:
/** FileSystemHandle lifetime
*/
virtual ~FileSystemHandle() {}

/** 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 open(FileHandle **file, const char *filename, int flags) = 0;

/** 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 open(DirHandle **dir, const char *path);

/** 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);

/** 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);

/** 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);

/** 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);
};


} // namespace mbed

#endif

/** @}*/
Loading