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