takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileSystem.h Source File

FileSystem.h

00001 
00002 /* mbed Microcontroller Library
00003  * Copyright (c) 2006-2013 ARM Limited
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef MBED_FILESYSTEM_H
00018 #define MBED_FILESYSTEM_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #include "platform/FileBase.h"
00023 #include "platform/FileHandle.h"
00024 #include "platform/DirHandle.h"
00025 #include "platform/FileSystemLike.h"
00026 #include "BlockDevice.h"
00027 
00028 namespace mbed {
00029 /** \addtogroup filesystem */
00030 /** @{*/
00031 
00032 
00033 // Opaque pointer representing files and directories
00034 typedef void *fs_file_t;
00035 typedef void *fs_dir_t;
00036 
00037 // Predeclared classes
00038 class Dir;
00039 class File;
00040 
00041 /** A filesystem object provides filesystem operations and file operations
00042  *  for the File and Dir classes on a block device.
00043  *
00044  *  Implementations must provide at minimum file operations and mount
00045  *  operations for block devices.
00046  *
00047  * @note Synchronization level: Set by subclass
00048  */
00049 class FileSystem : public FileSystemLike {
00050 public:
00051 
00052     /** FileSystem lifetime
00053      *
00054      *  @param name       Name to add filesystem to tree as
00055      */
00056     FileSystem(const char *name = NULL);
00057     virtual ~FileSystem() {}
00058 
00059     /** Return the default filesystem
00060      *
00061      * Returns the default FileSystem base on the default BlockDevice configuration.
00062      * Use the components in target.json or application config to change
00063      * the default block device and affect the default filesystem.
00064      * SD block device => FAT filesystem
00065      * SPIF block device => LITTLE filesystem
00066      * DATAFLASH block device => LITTLE filesystem
00067      *
00068      * An application can override all target settings by implementing
00069      * FileSystem::get_default_instance() themselves - the default
00070      * definition is weak, and calls get_target_default_instance().
00071      */
00072     static FileSystem *get_default_instance();
00073 
00074     /** Mounts a filesystem to a block device
00075      *
00076      *  @param bd       BlockDevice to mount to
00077      *  @return         0 on success, negative error code on failure
00078      */
00079     virtual int mount(BlockDevice *bd) = 0;
00080 
00081     /** Unmounts a filesystem from the underlying block device
00082      *
00083      *  @return         0 on success, negative error code on failure
00084      */
00085     virtual int unmount() = 0;
00086 
00087     /** Reformats a filesystem, results in an empty and mounted filesystem
00088      *
00089      *  @param bd       BlockDevice to reformat and mount. If NULL, the mounted
00090      *                  block device will be used.
00091      *                  Note: if mount fails, bd must be provided.
00092      *                  Default: NULL
00093      *  @return         0 on success, negative error code on failure
00094      */
00095     virtual int reformat(BlockDevice *bd = NULL);
00096 
00097     /** Remove a file from the filesystem.
00098      *
00099      *  @param path     The name of the file to remove.
00100      *  @return         0 on success, negative error code on failure
00101      */
00102     virtual int remove(const char *path);
00103 
00104     /** Rename a file in the filesystem.
00105      *
00106      *  @param path     The name of the file to rename.
00107      *  @param newpath  The name to rename it to
00108      *  @return         0 on success, negative error code on failure
00109      */
00110     virtual int rename(const char *path, const char *newpath);
00111 
00112     /** Store information about the file in a stat structure
00113      *
00114      *  @param path     The name of the file to find information about
00115      *  @param st       The stat buffer to write to
00116      *  @return         0 on success, negative error code on failure
00117      */
00118     virtual int stat(const char *path, struct stat *st);
00119 
00120     /** Create a directory in the filesystem.
00121      *
00122      *  @param path     The name of the directory to create.
00123      *  @param mode     The permissions with which to create the directory
00124      *  @return         0 on success, negative error code on failure
00125      */
00126     virtual int mkdir(const char *path, mode_t mode);
00127 
00128     /** Store information about the mounted filesystem in a statvfs structure
00129      *
00130      *  @param path     The name of the file to find information about
00131      *  @param buf      The stat buffer to write to
00132      *  @return         0 on success, negative error code on failure
00133      */
00134      virtual int statvfs(const char *path, struct statvfs *buf);
00135 
00136 protected:
00137     friend class File;
00138     friend class Dir;
00139 
00140     /** Open a file on the filesystem
00141      *
00142      *  @param file     Destination for the handle to a newly created file
00143      *  @param path     The name of the file to open
00144      *  @param flags    The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
00145      *                  bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
00146      *  @return         0 on success, negative error code on failure
00147      */
00148     virtual int file_open(fs_file_t *file, const char *path, int flags) = 0;
00149 
00150     /** Close a file
00151      *
00152      *  @param file     File handle
00153      *  @return         0 on success, negative error code on failure
00154      */
00155     virtual int file_close(fs_file_t file) = 0;
00156 
00157     /** Read the contents of a file into a buffer
00158      *
00159      *  @param file     File handle
00160      *  @param buffer   The buffer to read in to
00161      *  @param size     The number of bytes to read
00162      *  @return         The number of bytes read, 0 at end of file, negative error on failure
00163      */
00164     virtual ssize_t file_read(fs_file_t file, void *buffer, size_t size) = 0;
00165 
00166     /** Write the contents of a buffer to a file
00167      *
00168      *  @param file     File handle
00169      *  @param buffer   The buffer to write from
00170      *  @param size     The number of bytes to write
00171      *  @return         The number of bytes written, negative error on failure
00172      */
00173     virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t size) = 0;
00174 
00175     /** Flush any buffers associated with the file
00176      *
00177      *  @param file     File handle
00178      *  @return         0 on success, negative error code on failure
00179      */
00180     virtual int file_sync(fs_file_t file);
00181 
00182     /** Check if the file in an interactive terminal device
00183      *  If so, line buffered behaviour is used by default
00184      *
00185      *  @param file     File handle
00186      *  @return         True if the file is a terminal
00187      */
00188     virtual int file_isatty(fs_file_t file);
00189 
00190     /** Move the file position to a given offset from from a given location
00191      *
00192      *  @param file     File handle
00193      *  @param offset   The offset from whence to move to
00194      *  @param whence   The start of where to seek
00195      *      SEEK_SET to start from beginning of file,
00196      *      SEEK_CUR to start from current position in file,
00197      *      SEEK_END to start from end of file
00198      *  @return         The new offset of the file
00199      */
00200     virtual off_t file_seek(fs_file_t file, off_t offset, int whence) = 0;
00201 
00202     /** Get the file position of the file
00203      *
00204      *  @param file     File handle
00205      *  @return         The current offset in the file
00206      */
00207     virtual off_t file_tell(fs_file_t file);
00208 
00209     /** Rewind the file position to the beginning of the file
00210      *
00211      *  @param file     File handle
00212      *  @note This is equivalent to file_seek(file, 0, FS_SEEK_SET)
00213      */
00214     virtual void file_rewind(fs_file_t file);
00215 
00216     /** Get the size of the file
00217      *
00218      *  @param file     File handle
00219      *  @return         Size of the file in bytes
00220      */
00221     virtual off_t file_size(fs_file_t file);
00222 
00223     /** Open a directory on the filesystem
00224      *
00225      *  @param dir      Destination for the handle to the directory
00226      *  @param path     Name of the directory to open
00227      *  @return         0 on success, negative error code on failure
00228      */
00229     virtual int dir_open(fs_dir_t *dir, const char *path);
00230 
00231     /** Close a directory
00232      *
00233      *  @param dir      Dir handle
00234      *  @return         0 on success, negative error code on failure
00235      */
00236     virtual int dir_close(fs_dir_t dir);
00237 
00238     /** Read the next directory entry
00239      *
00240      *  @param dir      Dir handle
00241      *  @param ent      The directory entry to fill out
00242      *  @return         1 on reading a filename, 0 at end of directory, negative error on failure
00243      */
00244     virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent);
00245 
00246     /** Set the current position of the directory
00247      *
00248      *  @param dir      Dir handle
00249      *  @param offset   Offset of the location to seek to,
00250      *                  must be a value returned from dir_tell
00251      */
00252     virtual void dir_seek(fs_dir_t dir, off_t offset);
00253 
00254     /** Get the current position of the directory
00255      *
00256      *  @param dir      Dir handle
00257      *  @return         Position of the directory that can be passed to dir_rewind
00258      */
00259     virtual off_t dir_tell(fs_dir_t dir);
00260 
00261     /** Rewind the current position to the beginning of the directory
00262      *
00263      *  @param dir      Dir handle
00264      */
00265     virtual void dir_rewind(fs_dir_t dir);
00266 
00267     /** Get the sizeof the directory
00268      *
00269      *  @param dir      Dir handle
00270      *  @return         Number of files in the directory
00271      */
00272     virtual size_t dir_size(fs_dir_t dir);
00273 
00274 protected:
00275     // Hooks for FileSystemHandle
00276     virtual int open(FileHandle **file, const char *path, int flags);
00277     virtual int open(DirHandle **dir, const char *path);
00278 };
00279 
00280 
00281 /** @}*/
00282 } // namespace mbed
00283 
00284 #endif