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.
mbed-os/features/filesystem/FileSystem.h@0:fb8047b156bb, 2020-01-15 (annotated)
- Committer:
- rahul_dahiya
- Date:
- Wed Jan 15 15:57:15 2020 +0530
- Revision:
- 0:fb8047b156bb
STM32F7 LWIP
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rahul_dahiya |
0:fb8047b156bb | 1 | |
rahul_dahiya |
0:fb8047b156bb | 2 | /* mbed Microcontroller Library |
rahul_dahiya |
0:fb8047b156bb | 3 | * Copyright (c) 2006-2013 ARM Limited |
rahul_dahiya |
0:fb8047b156bb | 4 | * |
rahul_dahiya |
0:fb8047b156bb | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rahul_dahiya |
0:fb8047b156bb | 6 | * you may not use this file except in compliance with the License. |
rahul_dahiya |
0:fb8047b156bb | 7 | * You may obtain a copy of the License at |
rahul_dahiya |
0:fb8047b156bb | 8 | * |
rahul_dahiya |
0:fb8047b156bb | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
rahul_dahiya |
0:fb8047b156bb | 10 | * |
rahul_dahiya |
0:fb8047b156bb | 11 | * Unless required by applicable law or agreed to in writing, software |
rahul_dahiya |
0:fb8047b156bb | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
rahul_dahiya |
0:fb8047b156bb | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rahul_dahiya |
0:fb8047b156bb | 14 | * See the License for the specific language governing permissions and |
rahul_dahiya |
0:fb8047b156bb | 15 | * limitations under the License. |
rahul_dahiya |
0:fb8047b156bb | 16 | */ |
rahul_dahiya |
0:fb8047b156bb | 17 | #ifndef MBED_FILESYSTEM_H |
rahul_dahiya |
0:fb8047b156bb | 18 | #define MBED_FILESYSTEM_H |
rahul_dahiya |
0:fb8047b156bb | 19 | |
rahul_dahiya |
0:fb8047b156bb | 20 | #include "platform/platform.h" |
rahul_dahiya |
0:fb8047b156bb | 21 | |
rahul_dahiya |
0:fb8047b156bb | 22 | #include "platform/FileBase.h" |
rahul_dahiya |
0:fb8047b156bb | 23 | #include "platform/FileHandle.h" |
rahul_dahiya |
0:fb8047b156bb | 24 | #include "platform/DirHandle.h" |
rahul_dahiya |
0:fb8047b156bb | 25 | #include "platform/FileSystemLike.h" |
rahul_dahiya |
0:fb8047b156bb | 26 | #include "BlockDevice.h" |
rahul_dahiya |
0:fb8047b156bb | 27 | |
rahul_dahiya |
0:fb8047b156bb | 28 | namespace mbed { |
rahul_dahiya |
0:fb8047b156bb | 29 | /** \addtogroup filesystem */ |
rahul_dahiya |
0:fb8047b156bb | 30 | /** @{*/ |
rahul_dahiya |
0:fb8047b156bb | 31 | |
rahul_dahiya |
0:fb8047b156bb | 32 | |
rahul_dahiya |
0:fb8047b156bb | 33 | // Opaque pointer representing files and directories |
rahul_dahiya |
0:fb8047b156bb | 34 | typedef void *fs_file_t; |
rahul_dahiya |
0:fb8047b156bb | 35 | typedef void *fs_dir_t; |
rahul_dahiya |
0:fb8047b156bb | 36 | |
rahul_dahiya |
0:fb8047b156bb | 37 | // Predeclared classes |
rahul_dahiya |
0:fb8047b156bb | 38 | class Dir; |
rahul_dahiya |
0:fb8047b156bb | 39 | class File; |
rahul_dahiya |
0:fb8047b156bb | 40 | |
rahul_dahiya |
0:fb8047b156bb | 41 | /** A filesystem object provides filesystem operations and file operations |
rahul_dahiya |
0:fb8047b156bb | 42 | * for the File and Dir classes on a block device. |
rahul_dahiya |
0:fb8047b156bb | 43 | * |
rahul_dahiya |
0:fb8047b156bb | 44 | * Implementations must provide at minimum file operations and mount |
rahul_dahiya |
0:fb8047b156bb | 45 | * operations for block devices. |
rahul_dahiya |
0:fb8047b156bb | 46 | * |
rahul_dahiya |
0:fb8047b156bb | 47 | * @note Synchronization level: Set by subclass |
rahul_dahiya |
0:fb8047b156bb | 48 | */ |
rahul_dahiya |
0:fb8047b156bb | 49 | class FileSystem : public FileSystemLike { |
rahul_dahiya |
0:fb8047b156bb | 50 | public: |
rahul_dahiya |
0:fb8047b156bb | 51 | /** FileSystem lifetime |
rahul_dahiya |
0:fb8047b156bb | 52 | * |
rahul_dahiya |
0:fb8047b156bb | 53 | * @param name Name to add filesystem to tree as |
rahul_dahiya |
0:fb8047b156bb | 54 | */ |
rahul_dahiya |
0:fb8047b156bb | 55 | FileSystem(const char *name = NULL); |
rahul_dahiya |
0:fb8047b156bb | 56 | virtual ~FileSystem() {} |
rahul_dahiya |
0:fb8047b156bb | 57 | |
rahul_dahiya |
0:fb8047b156bb | 58 | /** Mounts a filesystem to a block device |
rahul_dahiya |
0:fb8047b156bb | 59 | * |
rahul_dahiya |
0:fb8047b156bb | 60 | * @param bd BlockDevice to mount to |
rahul_dahiya |
0:fb8047b156bb | 61 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 62 | */ |
rahul_dahiya |
0:fb8047b156bb | 63 | virtual int mount(BlockDevice *bd) = 0; |
rahul_dahiya |
0:fb8047b156bb | 64 | |
rahul_dahiya |
0:fb8047b156bb | 65 | /** Unmounts a filesystem from the underlying block device |
rahul_dahiya |
0:fb8047b156bb | 66 | * |
rahul_dahiya |
0:fb8047b156bb | 67 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 68 | */ |
rahul_dahiya |
0:fb8047b156bb | 69 | virtual int unmount() = 0; |
rahul_dahiya |
0:fb8047b156bb | 70 | |
rahul_dahiya |
0:fb8047b156bb | 71 | /** Reformats a filesystem, results in an empty and mounted filesystem |
rahul_dahiya |
0:fb8047b156bb | 72 | * |
rahul_dahiya |
0:fb8047b156bb | 73 | * @param bd BlockDevice to reformat and mount. If NULL, the mounted |
rahul_dahiya |
0:fb8047b156bb | 74 | * block device will be used. |
rahul_dahiya |
0:fb8047b156bb | 75 | * Note: if mount fails, bd must be provided. |
rahul_dahiya |
0:fb8047b156bb | 76 | * Default: NULL |
rahul_dahiya |
0:fb8047b156bb | 77 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 78 | */ |
rahul_dahiya |
0:fb8047b156bb | 79 | virtual int reformat(BlockDevice *bd = NULL); |
rahul_dahiya |
0:fb8047b156bb | 80 | |
rahul_dahiya |
0:fb8047b156bb | 81 | /** Remove a file from the filesystem. |
rahul_dahiya |
0:fb8047b156bb | 82 | * |
rahul_dahiya |
0:fb8047b156bb | 83 | * @param path The name of the file to remove. |
rahul_dahiya |
0:fb8047b156bb | 84 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 85 | */ |
rahul_dahiya |
0:fb8047b156bb | 86 | virtual int remove(const char *path); |
rahul_dahiya |
0:fb8047b156bb | 87 | |
rahul_dahiya |
0:fb8047b156bb | 88 | /** Rename a file in the filesystem. |
rahul_dahiya |
0:fb8047b156bb | 89 | * |
rahul_dahiya |
0:fb8047b156bb | 90 | * @param path The name of the file to rename. |
rahul_dahiya |
0:fb8047b156bb | 91 | * @param newpath The name to rename it to |
rahul_dahiya |
0:fb8047b156bb | 92 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 93 | */ |
rahul_dahiya |
0:fb8047b156bb | 94 | virtual int rename(const char *path, const char *newpath); |
rahul_dahiya |
0:fb8047b156bb | 95 | |
rahul_dahiya |
0:fb8047b156bb | 96 | /** Store information about the file in a stat structure |
rahul_dahiya |
0:fb8047b156bb | 97 | * |
rahul_dahiya |
0:fb8047b156bb | 98 | * @param path The name of the file to find information about |
rahul_dahiya |
0:fb8047b156bb | 99 | * @param st The stat buffer to write to |
rahul_dahiya |
0:fb8047b156bb | 100 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 101 | */ |
rahul_dahiya |
0:fb8047b156bb | 102 | virtual int stat(const char *path, struct stat *st); |
rahul_dahiya |
0:fb8047b156bb | 103 | |
rahul_dahiya |
0:fb8047b156bb | 104 | /** Create a directory in the filesystem. |
rahul_dahiya |
0:fb8047b156bb | 105 | * |
rahul_dahiya |
0:fb8047b156bb | 106 | * @param path The name of the directory to create. |
rahul_dahiya |
0:fb8047b156bb | 107 | * @param mode The permissions with which to create the directory |
rahul_dahiya |
0:fb8047b156bb | 108 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 109 | */ |
rahul_dahiya |
0:fb8047b156bb | 110 | virtual int mkdir(const char *path, mode_t mode); |
rahul_dahiya |
0:fb8047b156bb | 111 | |
rahul_dahiya |
0:fb8047b156bb | 112 | protected: |
rahul_dahiya |
0:fb8047b156bb | 113 | friend class File; |
rahul_dahiya |
0:fb8047b156bb | 114 | friend class Dir; |
rahul_dahiya |
0:fb8047b156bb | 115 | |
rahul_dahiya |
0:fb8047b156bb | 116 | /** Open a file on the filesystem |
rahul_dahiya |
0:fb8047b156bb | 117 | * |
rahul_dahiya |
0:fb8047b156bb | 118 | * @param file Destination for the handle to a newly created file |
rahul_dahiya |
0:fb8047b156bb | 119 | * @param path The name of the file to open |
rahul_dahiya |
0:fb8047b156bb | 120 | * @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR, |
rahul_dahiya |
0:fb8047b156bb | 121 | * bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND |
rahul_dahiya |
0:fb8047b156bb | 122 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 123 | */ |
rahul_dahiya |
0:fb8047b156bb | 124 | virtual int file_open(fs_file_t *file, const char *path, int flags) = 0; |
rahul_dahiya |
0:fb8047b156bb | 125 | |
rahul_dahiya |
0:fb8047b156bb | 126 | /** Close a file |
rahul_dahiya |
0:fb8047b156bb | 127 | * |
rahul_dahiya |
0:fb8047b156bb | 128 | * @param file File handle |
rahul_dahiya |
0:fb8047b156bb | 129 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 130 | */ |
rahul_dahiya |
0:fb8047b156bb | 131 | virtual int file_close(fs_file_t file) = 0; |
rahul_dahiya |
0:fb8047b156bb | 132 | |
rahul_dahiya |
0:fb8047b156bb | 133 | /** Read the contents of a file into a buffer |
rahul_dahiya |
0:fb8047b156bb | 134 | * |
rahul_dahiya |
0:fb8047b156bb | 135 | * @param file File handle |
rahul_dahiya |
0:fb8047b156bb | 136 | * @param buffer The buffer to read in to |
rahul_dahiya |
0:fb8047b156bb | 137 | * @param size The number of bytes to read |
rahul_dahiya |
0:fb8047b156bb | 138 | * @return The number of bytes read, 0 at end of file, negative error on failure |
rahul_dahiya |
0:fb8047b156bb | 139 | */ |
rahul_dahiya |
0:fb8047b156bb | 140 | virtual ssize_t file_read(fs_file_t file, void *buffer, size_t size) = 0; |
rahul_dahiya |
0:fb8047b156bb | 141 | |
rahul_dahiya |
0:fb8047b156bb | 142 | /** Write the contents of a buffer to a file |
rahul_dahiya |
0:fb8047b156bb | 143 | * |
rahul_dahiya |
0:fb8047b156bb | 144 | * @param file File handle |
rahul_dahiya |
0:fb8047b156bb | 145 | * @param buffer The buffer to write from |
rahul_dahiya |
0:fb8047b156bb | 146 | * @param size The number of bytes to write |
rahul_dahiya |
0:fb8047b156bb | 147 | * @return The number of bytes written, negative error on failure |
rahul_dahiya |
0:fb8047b156bb | 148 | */ |
rahul_dahiya |
0:fb8047b156bb | 149 | virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t size) = 0; |
rahul_dahiya |
0:fb8047b156bb | 150 | |
rahul_dahiya |
0:fb8047b156bb | 151 | /** Flush any buffers associated with the file |
rahul_dahiya |
0:fb8047b156bb | 152 | * |
rahul_dahiya |
0:fb8047b156bb | 153 | * @param file File handle |
rahul_dahiya |
0:fb8047b156bb | 154 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 155 | */ |
rahul_dahiya |
0:fb8047b156bb | 156 | virtual int file_sync(fs_file_t file); |
rahul_dahiya |
0:fb8047b156bb | 157 | |
rahul_dahiya |
0:fb8047b156bb | 158 | /** Check if the file in an interactive terminal device |
rahul_dahiya |
0:fb8047b156bb | 159 | * If so, line buffered behaviour is used by default |
rahul_dahiya |
0:fb8047b156bb | 160 | * |
rahul_dahiya |
0:fb8047b156bb | 161 | * @param file File handle |
rahul_dahiya |
0:fb8047b156bb | 162 | * @return True if the file is a terminal |
rahul_dahiya |
0:fb8047b156bb | 163 | */ |
rahul_dahiya |
0:fb8047b156bb | 164 | virtual int file_isatty(fs_file_t file); |
rahul_dahiya |
0:fb8047b156bb | 165 | |
rahul_dahiya |
0:fb8047b156bb | 166 | /** Move the file position to a given offset from from a given location |
rahul_dahiya |
0:fb8047b156bb | 167 | * |
rahul_dahiya |
0:fb8047b156bb | 168 | * @param file File handle |
rahul_dahiya |
0:fb8047b156bb | 169 | * @param offset The offset from whence to move to |
rahul_dahiya |
0:fb8047b156bb | 170 | * @param whence The start of where to seek |
rahul_dahiya |
0:fb8047b156bb | 171 | * SEEK_SET to start from beginning of file, |
rahul_dahiya |
0:fb8047b156bb | 172 | * SEEK_CUR to start from current position in file, |
rahul_dahiya |
0:fb8047b156bb | 173 | * SEEK_END to start from end of file |
rahul_dahiya |
0:fb8047b156bb | 174 | * @return The new offset of the file |
rahul_dahiya |
0:fb8047b156bb | 175 | */ |
rahul_dahiya |
0:fb8047b156bb | 176 | virtual off_t file_seek(fs_file_t file, off_t offset, int whence) = 0; |
rahul_dahiya |
0:fb8047b156bb | 177 | |
rahul_dahiya |
0:fb8047b156bb | 178 | /** Get the file position of the file |
rahul_dahiya |
0:fb8047b156bb | 179 | * |
rahul_dahiya |
0:fb8047b156bb | 180 | * @param file File handle |
rahul_dahiya |
0:fb8047b156bb | 181 | * @return The current offset in the file |
rahul_dahiya |
0:fb8047b156bb | 182 | */ |
rahul_dahiya |
0:fb8047b156bb | 183 | virtual off_t file_tell(fs_file_t file); |
rahul_dahiya |
0:fb8047b156bb | 184 | |
rahul_dahiya |
0:fb8047b156bb | 185 | /** Rewind the file position to the beginning of the file |
rahul_dahiya |
0:fb8047b156bb | 186 | * |
rahul_dahiya |
0:fb8047b156bb | 187 | * @param file File handle |
rahul_dahiya |
0:fb8047b156bb | 188 | * @note This is equivalent to file_seek(file, 0, FS_SEEK_SET) |
rahul_dahiya |
0:fb8047b156bb | 189 | */ |
rahul_dahiya |
0:fb8047b156bb | 190 | virtual void file_rewind(fs_file_t file); |
rahul_dahiya |
0:fb8047b156bb | 191 | |
rahul_dahiya |
0:fb8047b156bb | 192 | /** Get the size of the file |
rahul_dahiya |
0:fb8047b156bb | 193 | * |
rahul_dahiya |
0:fb8047b156bb | 194 | * @param file File handle |
rahul_dahiya |
0:fb8047b156bb | 195 | * @return Size of the file in bytes |
rahul_dahiya |
0:fb8047b156bb | 196 | */ |
rahul_dahiya |
0:fb8047b156bb | 197 | virtual off_t file_size(fs_file_t file); |
rahul_dahiya |
0:fb8047b156bb | 198 | |
rahul_dahiya |
0:fb8047b156bb | 199 | /** Open a directory on the filesystem |
rahul_dahiya |
0:fb8047b156bb | 200 | * |
rahul_dahiya |
0:fb8047b156bb | 201 | * @param dir Destination for the handle to the directory |
rahul_dahiya |
0:fb8047b156bb | 202 | * @param path Name of the directory to open |
rahul_dahiya |
0:fb8047b156bb | 203 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 204 | */ |
rahul_dahiya |
0:fb8047b156bb | 205 | virtual int dir_open(fs_dir_t *dir, const char *path); |
rahul_dahiya |
0:fb8047b156bb | 206 | |
rahul_dahiya |
0:fb8047b156bb | 207 | /** Close a directory |
rahul_dahiya |
0:fb8047b156bb | 208 | * |
rahul_dahiya |
0:fb8047b156bb | 209 | * @param dir Dir handle |
rahul_dahiya |
0:fb8047b156bb | 210 | * @return 0 on success, negative error code on failure |
rahul_dahiya |
0:fb8047b156bb | 211 | */ |
rahul_dahiya |
0:fb8047b156bb | 212 | virtual int dir_close(fs_dir_t dir); |
rahul_dahiya |
0:fb8047b156bb | 213 | |
rahul_dahiya |
0:fb8047b156bb | 214 | /** Read the next directory entry |
rahul_dahiya |
0:fb8047b156bb | 215 | * |
rahul_dahiya |
0:fb8047b156bb | 216 | * @param dir Dir handle |
rahul_dahiya |
0:fb8047b156bb | 217 | * @param ent The directory entry to fill out |
rahul_dahiya |
0:fb8047b156bb | 218 | * @return 1 on reading a filename, 0 at end of directory, negative error on failure |
rahul_dahiya |
0:fb8047b156bb | 219 | */ |
rahul_dahiya |
0:fb8047b156bb | 220 | virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent); |
rahul_dahiya |
0:fb8047b156bb | 221 | |
rahul_dahiya |
0:fb8047b156bb | 222 | /** Set the current position of the directory |
rahul_dahiya |
0:fb8047b156bb | 223 | * |
rahul_dahiya |
0:fb8047b156bb | 224 | * @param dir Dir handle |
rahul_dahiya |
0:fb8047b156bb | 225 | * @param offset Offset of the location to seek to, |
rahul_dahiya |
0:fb8047b156bb | 226 | * must be a value returned from dir_tell |
rahul_dahiya |
0:fb8047b156bb | 227 | */ |
rahul_dahiya |
0:fb8047b156bb | 228 | virtual void dir_seek(fs_dir_t dir, off_t offset); |
rahul_dahiya |
0:fb8047b156bb | 229 | |
rahul_dahiya |
0:fb8047b156bb | 230 | /** Get the current position of the directory |
rahul_dahiya |
0:fb8047b156bb | 231 | * |
rahul_dahiya |
0:fb8047b156bb | 232 | * @param dir Dir handle |
rahul_dahiya |
0:fb8047b156bb | 233 | * @return Position of the directory that can be passed to dir_rewind |
rahul_dahiya |
0:fb8047b156bb | 234 | */ |
rahul_dahiya |
0:fb8047b156bb | 235 | virtual off_t dir_tell(fs_dir_t dir); |
rahul_dahiya |
0:fb8047b156bb | 236 | |
rahul_dahiya |
0:fb8047b156bb | 237 | /** Rewind the current position to the beginning of the directory |
rahul_dahiya |
0:fb8047b156bb | 238 | * |
rahul_dahiya |
0:fb8047b156bb | 239 | * @param dir Dir handle |
rahul_dahiya |
0:fb8047b156bb | 240 | */ |
rahul_dahiya |
0:fb8047b156bb | 241 | virtual void dir_rewind(fs_dir_t dir); |
rahul_dahiya |
0:fb8047b156bb | 242 | |
rahul_dahiya |
0:fb8047b156bb | 243 | /** Get the sizeof the directory |
rahul_dahiya |
0:fb8047b156bb | 244 | * |
rahul_dahiya |
0:fb8047b156bb | 245 | * @param dir Dir handle |
rahul_dahiya |
0:fb8047b156bb | 246 | * @return Number of files in the directory |
rahul_dahiya |
0:fb8047b156bb | 247 | */ |
rahul_dahiya |
0:fb8047b156bb | 248 | virtual size_t dir_size(fs_dir_t dir); |
rahul_dahiya |
0:fb8047b156bb | 249 | |
rahul_dahiya |
0:fb8047b156bb | 250 | protected: |
rahul_dahiya |
0:fb8047b156bb | 251 | // Hooks for FileSystemHandle |
rahul_dahiya |
0:fb8047b156bb | 252 | virtual int open(FileHandle **file, const char *path, int flags); |
rahul_dahiya |
0:fb8047b156bb | 253 | virtual int open(DirHandle **dir, const char *path); |
rahul_dahiya |
0:fb8047b156bb | 254 | }; |
rahul_dahiya |
0:fb8047b156bb | 255 | |
rahul_dahiya |
0:fb8047b156bb | 256 | |
rahul_dahiya |
0:fb8047b156bb | 257 | /** @}*/ |
rahul_dahiya |
0:fb8047b156bb | 258 | } // namespace mbed |
rahul_dahiya |
0:fb8047b156bb | 259 | |
rahul_dahiya |
0:fb8047b156bb | 260 | #endif |