Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
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 "drivers/FileBase.h" 00023 #include "BlockDevice.h" 00024 00025 namespace mbed { 00026 /** \addtogroup filesystem */ 00027 /** @{*/ 00028 00029 00030 // Opaque pointer representing files and directories 00031 typedef void *fs_file_t; 00032 typedef void *fs_dir_t; 00033 00034 /** A filesystem-like object is one that can be used to open files 00035 * though it by fopen("/name/filename", flags) 00036 * 00037 * Implementations must define at least open (the default definitions 00038 * of the rest of the functions just return error values). 00039 * 00040 * @Note Synchronization level: Set by subclass 00041 */ 00042 class FileSystem : public FileBase { 00043 public: 00044 /** FileSystem lifetime 00045 */ 00046 FileSystem(const char *name = NULL); 00047 virtual ~FileSystem() {} 00048 00049 /** Mounts a filesystem to a block device 00050 * 00051 * @param bd BlockDevice to mount to 00052 * @return 0 on success, negative error code on failure 00053 */ 00054 virtual int mount(BlockDevice *bd) = 0; 00055 00056 /** Unmounts a filesystem from the underlying block device 00057 * 00058 * @return 0 on success, negative error code on failure 00059 */ 00060 virtual int unmount() = 0; 00061 00062 /** Remove a file from the filesystem. 00063 * 00064 * @param path The name of the file to remove. 00065 * @return 0 on success, negative error code on failure 00066 */ 00067 virtual int remove(const char *path) = 0; 00068 00069 /** Rename a file in the filesystem. 00070 * 00071 * @param path The name of the file to rename. 00072 * @param newpath The name to rename it to 00073 * @return 0 on success, negative error code on failure 00074 */ 00075 virtual int rename(const char *path, const char *newpath) = 0; 00076 00077 /** Store information about the file in a stat structure 00078 * 00079 * @param path The name of the file to find information about 00080 * @param st The stat buffer to write to 00081 * @return 0 on success, negative error code on failure 00082 */ 00083 virtual int stat(const char *path, struct stat *st) = 0; 00084 00085 /** Create a directory in the filesystem. 00086 * 00087 * @param path The name of the directory to create. 00088 * @param mode The permissions with which to create the directory 00089 * @return 0 on success, negative error code on failure 00090 */ 00091 virtual int mkdir(const char *path, mode_t mode); 00092 00093 protected: 00094 friend class File; 00095 friend class Dir; 00096 00097 /** Open a file on the filesystem 00098 * 00099 * @param file Destination for the handle to a newly created file 00100 * @param path The name of the file to open 00101 * @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR, 00102 * bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND 00103 * @return 0 on success, negative error code on failure 00104 */ 00105 virtual int file_open(fs_file_t *file, const char *path, int flags) = 0; 00106 00107 /** Close a file 00108 * 00109 * @param file File handle 00110 * return 0 on success, negative error code on failure 00111 */ 00112 virtual int file_close(fs_file_t file) = 0; 00113 00114 /** Read the contents of a file into a buffer 00115 * 00116 * @param file File handle 00117 * @param buffer The buffer to read in to 00118 * @param size The number of bytes to read 00119 * @return The number of bytes read, 0 at end of file, negative error on failure 00120 */ 00121 virtual ssize_t file_read(fs_file_t file, void *buffer, size_t len) = 0; 00122 00123 /** Write the contents of a buffer to a file 00124 * 00125 * @param file File handle 00126 * @param buffer The buffer to write from 00127 * @param size The number of bytes to write 00128 * @return The number of bytes written, negative error on failure 00129 */ 00130 virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t len) = 0; 00131 00132 /** Flush any buffers associated with the file 00133 * 00134 * @param file File handle 00135 * @return 0 on success, negative error code on failure 00136 */ 00137 virtual int file_sync(fs_file_t file); 00138 00139 /** Check if the file in an interactive terminal device 00140 * If so, line buffered behaviour is used by default 00141 * 00142 * @param file File handle 00143 * @return True if the file is a terminal 00144 */ 00145 virtual int file_isatty(fs_file_t file); 00146 00147 /** Move the file position to a given offset from from a given location 00148 * 00149 * @param file File handle 00150 * @param offset The offset from whence to move to 00151 * @param whence The start of where to seek 00152 * SEEK_SET to start from beginning of file, 00153 * SEEK_CUR to start from current position in file, 00154 * SEEK_END to start from end of file 00155 * @return The new offset of the file 00156 */ 00157 virtual off_t file_seek(fs_file_t file, off_t offset, int whence) = 0; 00158 00159 /** Get the file position of the file 00160 * 00161 * @param file File handle 00162 * @return The current offset in the file 00163 */ 00164 virtual off_t file_tell(fs_file_t file); 00165 00166 /** Rewind the file position to the beginning of the file 00167 * 00168 * @param file File handle 00169 * @note This is equivalent to file_seek(file, 0, FS_SEEK_SET) 00170 */ 00171 virtual void file_rewind(fs_file_t file); 00172 00173 /** Get the size of the file 00174 * 00175 * @param file File handle 00176 * @return Size of the file in bytes 00177 */ 00178 virtual size_t file_size(fs_file_t file); 00179 00180 /** Open a directory on the filesystem 00181 * 00182 * @param dir Destination for the handle to the directory 00183 * @param path Name of the directory to open 00184 * @return 0 on success, negative error code on failure 00185 */ 00186 virtual int dir_open(fs_dir_t *dir, const char *path); 00187 00188 /** Close a directory 00189 * 00190 * @param dir Dir handle 00191 * return 0 on success, negative error code on failure 00192 */ 00193 virtual int dir_close(fs_dir_t dir); 00194 00195 /** Read the next directory entry 00196 * 00197 * @param dir Dir handle 00198 * @param ent The directory entry to fill out 00199 * @return 1 on reading a filename, 0 at end of directory, negative error on failure 00200 */ 00201 virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent); 00202 00203 /** Set the current position of the directory 00204 * 00205 * @param dir Dir handle 00206 * @param offset Offset of the location to seek to, 00207 * must be a value returned from dir_tell 00208 */ 00209 virtual void dir_seek(fs_dir_t dir, off_t offset); 00210 00211 /** Get the current position of the directory 00212 * 00213 * @param dir Dir handle 00214 * @return Position of the directory that can be passed to dir_rewind 00215 */ 00216 virtual off_t dir_tell(fs_dir_t dir); 00217 00218 /** Rewind the current position to the beginning of the directory 00219 * 00220 * @param dir Dir handle 00221 */ 00222 virtual void dir_rewind(fs_dir_t dir); 00223 00224 /** Get the sizeof the directory 00225 * 00226 * @param dir Dir handle 00227 * @return Number of files in the directory 00228 */ 00229 virtual size_t dir_size(fs_dir_t dir); 00230 }; 00231 00232 00233 /** @}*/ 00234 } // namespace mbed 00235 00236 #endif
Generated on Tue Jul 12 2022 11:02:24 by
1.7.2