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