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: TYBLE16_simple_data_logger TYBLE16_MP3_Air
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 "features/storage/blockdevice/BlockDevice.h" 00027 00028 namespace mbed { 00029 /** \addtogroup file system */ 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 file system object. Provides file system 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 /** File system lifetime. 00053 * 00054 * @param name Name to add file system to tree as. 00055 */ 00056 FileSystem(const char *name = NULL); 00057 virtual ~FileSystem() {} 00058 00059 /** Return the default file system. 00060 * 00061 * Returns the default file system based on the default block device 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 * QSPIF, SPIF, DATAFLASH or FLAHIAP block device => LITTLE filesystem 00066 * 00067 * An application can override all target settings by implementing 00068 * FileSystem::get_default_instance() - the default 00069 * definition is weak, and calls get_target_default_instance(). 00070 */ 00071 static FileSystem *get_default_instance(); 00072 00073 /** Mount a file system to a block device. 00074 * 00075 * @param bd Block device to mount to. 00076 * @return 0 on success, negative error code on failure. 00077 */ 00078 virtual int mount(BlockDevice *bd) = 0; 00079 00080 /** Unmount a file system from the underlying block device. 00081 * 00082 * @return 0 on success, negative error code on failure. 00083 */ 00084 virtual int unmount() = 0; 00085 00086 /** Reformat a file system. Results in an empty and mounted file system. 00087 * 00088 * @param bd Block device to reformat and mount. If NULL, the mounted 00089 * Block device is used. 00090 * Note: If mount fails, bd must be provided. 00091 * Default: NULL 00092 * @return 0 on success, negative error code on failure. 00093 */ 00094 virtual int reformat(BlockDevice *bd = NULL); 00095 00096 /** Remove a file from the file system. 00097 * 00098 * @param path The name of the file to remove. 00099 * @return 0 on success, negative error code on failure. 00100 */ 00101 virtual int remove(const char *path); 00102 00103 /** Rename a file in the file system. 00104 * 00105 * @param path The existing name of the file to rename. 00106 * @param newpath The new name of the file. 00107 * @return 0 on success, negative error code on failure. 00108 */ 00109 virtual int rename(const char *path, const char *newpath); 00110 00111 /** Store information about the file in a stat structure. 00112 * 00113 * @param path The name of the file to find information about. 00114 * @param st The stat buffer to write to. 00115 * @return 0 on success, negative error code on failure. 00116 */ 00117 virtual int stat(const char *path, struct stat *st); 00118 00119 /** Create a directory in the file system. 00120 * 00121 * @param path The name of the directory to create. 00122 * @param mode The permissions with which to create the directory. 00123 * @return 0 on success, negative error code on failure. 00124 */ 00125 virtual int mkdir(const char *path, mode_t mode); 00126 00127 /** Store information about the mounted file system in a statvfs structure. 00128 * 00129 * @param path The name of the file to find information about. 00130 * @param buf The stat buffer to write to. 00131 * @return 0 on success, negative error code on failure. 00132 */ 00133 virtual int statvfs(const char *path, struct statvfs *buf); 00134 00135 protected: 00136 friend class File; 00137 friend class Dir; 00138 00139 #if !(DOXYGEN_ONLY) 00140 /** Open a file on the file system. 00141 * 00142 * @param file Destination of the newly created handle to the referenced file. 00143 * @param path The name of the file to open. 00144 * @param flags The flags that trigger opening of the file. These flags are O_RDONLY, O_WRONLY, and O_RDWR, 00145 * with an O_CREAT, O_TRUNC, or O_APPEND bitwise OR operator. 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 the end of the 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 whether the file is an interactive terminal device. 00183 * If so, line buffered behavior 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 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 the beginning of the file, 00196 * SEEK_CUR to start from the current position in the file, 00197 * SEEK_END to start from the end of the 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 /** Truncate or extend a file. 00224 * 00225 * The file's length is set to the specified value. The seek pointer is 00226 * not changed. If the file is extended, the extended area appears as if 00227 * it were zero-filled. 00228 * 00229 * @param file File handle. 00230 * @param length The requested new length for the file. 00231 * 00232 * @return 0 on success, negative error code on failure. 00233 */ 00234 virtual int file_truncate(fs_file_t file, off_t length); 00235 00236 /** Open a directory on the file system. 00237 * 00238 * @param dir Destination for the handle to the directory. 00239 * @param path Name of the directory to open. 00240 * @return 0 on success, negative error code on failure. 00241 */ 00242 virtual int dir_open(fs_dir_t *dir, const char *path); 00243 00244 /** Close a directory. 00245 * 00246 * @param dir Dir handle. 00247 * @return 0 on success, negative error code on failure. 00248 */ 00249 virtual int dir_close(fs_dir_t dir); 00250 00251 /** Read the next directory entry. 00252 * 00253 * @param dir Dir handle. 00254 * @param ent The directory entry to fill out. 00255 * @return 1 on reading a filename, 0 at the end of the directory, negative error on failure. 00256 */ 00257 virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent); 00258 00259 /** Set the current position of the directory. 00260 * 00261 * @param dir Dir handle. 00262 * @param offset Offset of the location to seek to, 00263 * must be a value returned from dir_tell. 00264 */ 00265 virtual void dir_seek(fs_dir_t dir, off_t offset); 00266 00267 /** Get the current position of the directory. 00268 * 00269 * @param dir Dir handle. 00270 * @return Directory position, which can be passed to dir_rewind. 00271 */ 00272 virtual off_t dir_tell(fs_dir_t dir); 00273 00274 /** Rewind the current position to the beginning of the directory. 00275 * 00276 * @param dir Dir handle. 00277 */ 00278 virtual void dir_rewind(fs_dir_t dir); 00279 00280 /** Get the size of the directory. 00281 * 00282 * @param dir Dir handle. 00283 * @return Number of files in the directory. 00284 */ 00285 virtual size_t dir_size(fs_dir_t dir); 00286 #endif //!defined(DOXYGEN_ONLY) 00287 00288 protected: 00289 // Hooks for file systemHandle 00290 virtual int open(FileHandle **file, const char *path, int flags); 00291 virtual int open(DirHandle **dir, const char *path); 00292 }; 00293 00294 00295 /** @}*/ 00296 } // namespace mbed 00297 00298 #endif
Generated on Tue Jul 12 2022 13:54:21 by
