mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:5b88d5760320 1
kenjiArai 0:5b88d5760320 2 /* mbed Microcontroller Library
kenjiArai 0:5b88d5760320 3 * Copyright (c) 2006-2013 ARM Limited
kenjiArai 0:5b88d5760320 4 *
kenjiArai 0:5b88d5760320 5 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 0:5b88d5760320 6 * you may not use this file except in compliance with the License.
kenjiArai 0:5b88d5760320 7 * You may obtain a copy of the License at
kenjiArai 0:5b88d5760320 8 *
kenjiArai 0:5b88d5760320 9 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 0:5b88d5760320 10 *
kenjiArai 0:5b88d5760320 11 * Unless required by applicable law or agreed to in writing, software
kenjiArai 0:5b88d5760320 12 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 0:5b88d5760320 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 0:5b88d5760320 14 * See the License for the specific language governing permissions and
kenjiArai 0:5b88d5760320 15 * limitations under the License.
kenjiArai 0:5b88d5760320 16 */
kenjiArai 0:5b88d5760320 17 #ifndef MBED_FILESYSTEM_H
kenjiArai 0:5b88d5760320 18 #define MBED_FILESYSTEM_H
kenjiArai 0:5b88d5760320 19
kenjiArai 0:5b88d5760320 20 #include "platform/platform.h"
kenjiArai 0:5b88d5760320 21
kenjiArai 0:5b88d5760320 22 #include "platform/FileBase.h"
kenjiArai 0:5b88d5760320 23 #include "platform/FileHandle.h"
kenjiArai 0:5b88d5760320 24 #include "platform/DirHandle.h"
kenjiArai 0:5b88d5760320 25 #include "platform/FileSystemLike.h"
kenjiArai 1:9db0e321a9f4 26 #include "features/storage/blockdevice/BlockDevice.h"
kenjiArai 0:5b88d5760320 27
kenjiArai 0:5b88d5760320 28 namespace mbed {
kenjiArai 0:5b88d5760320 29 /** \addtogroup file system */
kenjiArai 0:5b88d5760320 30 /** @{*/
kenjiArai 0:5b88d5760320 31
kenjiArai 0:5b88d5760320 32
kenjiArai 0:5b88d5760320 33 // Opaque pointer representing files and directories.
kenjiArai 0:5b88d5760320 34 typedef void *fs_file_t;
kenjiArai 0:5b88d5760320 35 typedef void *fs_dir_t;
kenjiArai 0:5b88d5760320 36
kenjiArai 0:5b88d5760320 37 // Predeclared classes.
kenjiArai 0:5b88d5760320 38 class Dir;
kenjiArai 0:5b88d5760320 39 class File;
kenjiArai 0:5b88d5760320 40
kenjiArai 0:5b88d5760320 41 /** A file system object. Provides file system operations and file operations
kenjiArai 0:5b88d5760320 42 * for the File and Dir classes on a block device.
kenjiArai 0:5b88d5760320 43 *
kenjiArai 0:5b88d5760320 44 * Implementations must provide at minimum file operations and mount
kenjiArai 0:5b88d5760320 45 * operations for block devices.
kenjiArai 0:5b88d5760320 46 *
kenjiArai 0:5b88d5760320 47 * @note Synchronization level: Set by subclass
kenjiArai 0:5b88d5760320 48 */
kenjiArai 0:5b88d5760320 49 class FileSystem : public FileSystemLike {
kenjiArai 0:5b88d5760320 50 public:
kenjiArai 0:5b88d5760320 51
kenjiArai 0:5b88d5760320 52 /** File system lifetime.
kenjiArai 0:5b88d5760320 53 *
kenjiArai 0:5b88d5760320 54 * @param name Name to add file system to tree as.
kenjiArai 0:5b88d5760320 55 */
kenjiArai 0:5b88d5760320 56 FileSystem(const char *name = NULL);
kenjiArai 0:5b88d5760320 57 virtual ~FileSystem() {}
kenjiArai 0:5b88d5760320 58
kenjiArai 0:5b88d5760320 59 /** Return the default file system.
kenjiArai 0:5b88d5760320 60 *
kenjiArai 0:5b88d5760320 61 * Returns the default file system based on the default block device configuration.
kenjiArai 0:5b88d5760320 62 * Use the components in target.json or application config to change
kenjiArai 0:5b88d5760320 63 * the default block device and affect the default filesystem.
kenjiArai 0:5b88d5760320 64 * SD block device => FAT filesystem
kenjiArai 0:5b88d5760320 65 * QSPIF, SPIF, DATAFLASH or FLAHIAP block device => LITTLE filesystem
kenjiArai 0:5b88d5760320 66 *
kenjiArai 0:5b88d5760320 67 * An application can override all target settings by implementing
kenjiArai 0:5b88d5760320 68 * FileSystem::get_default_instance() - the default
kenjiArai 0:5b88d5760320 69 * definition is weak, and calls get_target_default_instance().
kenjiArai 0:5b88d5760320 70 */
kenjiArai 0:5b88d5760320 71 static FileSystem *get_default_instance();
kenjiArai 0:5b88d5760320 72
kenjiArai 0:5b88d5760320 73 /** Mount a file system to a block device.
kenjiArai 0:5b88d5760320 74 *
kenjiArai 0:5b88d5760320 75 * @param bd Block device to mount to.
kenjiArai 0:5b88d5760320 76 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 77 */
kenjiArai 0:5b88d5760320 78 virtual int mount(BlockDevice *bd) = 0;
kenjiArai 0:5b88d5760320 79
kenjiArai 0:5b88d5760320 80 /** Unmount a file system from the underlying block device.
kenjiArai 0:5b88d5760320 81 *
kenjiArai 0:5b88d5760320 82 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 83 */
kenjiArai 0:5b88d5760320 84 virtual int unmount() = 0;
kenjiArai 0:5b88d5760320 85
kenjiArai 0:5b88d5760320 86 /** Reformat a file system. Results in an empty and mounted file system.
kenjiArai 0:5b88d5760320 87 *
kenjiArai 0:5b88d5760320 88 * @param bd Block device to reformat and mount. If NULL, the mounted
kenjiArai 0:5b88d5760320 89 * Block device is used.
kenjiArai 0:5b88d5760320 90 * Note: If mount fails, bd must be provided.
kenjiArai 0:5b88d5760320 91 * Default: NULL
kenjiArai 0:5b88d5760320 92 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 93 */
kenjiArai 0:5b88d5760320 94 virtual int reformat(BlockDevice *bd = NULL);
kenjiArai 0:5b88d5760320 95
kenjiArai 0:5b88d5760320 96 /** Remove a file from the file system.
kenjiArai 0:5b88d5760320 97 *
kenjiArai 0:5b88d5760320 98 * @param path The name of the file to remove.
kenjiArai 0:5b88d5760320 99 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 100 */
kenjiArai 0:5b88d5760320 101 virtual int remove(const char *path);
kenjiArai 0:5b88d5760320 102
kenjiArai 0:5b88d5760320 103 /** Rename a file in the file system.
kenjiArai 0:5b88d5760320 104 *
kenjiArai 0:5b88d5760320 105 * @param path The existing name of the file to rename.
kenjiArai 0:5b88d5760320 106 * @param newpath The new name of the file.
kenjiArai 0:5b88d5760320 107 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 108 */
kenjiArai 0:5b88d5760320 109 virtual int rename(const char *path, const char *newpath);
kenjiArai 0:5b88d5760320 110
kenjiArai 0:5b88d5760320 111 /** Store information about the file in a stat structure.
kenjiArai 0:5b88d5760320 112 *
kenjiArai 0:5b88d5760320 113 * @param path The name of the file to find information about.
kenjiArai 0:5b88d5760320 114 * @param st The stat buffer to write to.
kenjiArai 0:5b88d5760320 115 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 116 */
kenjiArai 0:5b88d5760320 117 virtual int stat(const char *path, struct stat *st);
kenjiArai 0:5b88d5760320 118
kenjiArai 0:5b88d5760320 119 /** Create a directory in the file system.
kenjiArai 0:5b88d5760320 120 *
kenjiArai 0:5b88d5760320 121 * @param path The name of the directory to create.
kenjiArai 0:5b88d5760320 122 * @param mode The permissions with which to create the directory.
kenjiArai 0:5b88d5760320 123 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 124 */
kenjiArai 0:5b88d5760320 125 virtual int mkdir(const char *path, mode_t mode);
kenjiArai 0:5b88d5760320 126
kenjiArai 0:5b88d5760320 127 /** Store information about the mounted file system in a statvfs structure.
kenjiArai 0:5b88d5760320 128 *
kenjiArai 0:5b88d5760320 129 * @param path The name of the file to find information about.
kenjiArai 0:5b88d5760320 130 * @param buf The stat buffer to write to.
kenjiArai 0:5b88d5760320 131 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 132 */
kenjiArai 0:5b88d5760320 133 virtual int statvfs(const char *path, struct statvfs *buf);
kenjiArai 0:5b88d5760320 134
kenjiArai 0:5b88d5760320 135 protected:
kenjiArai 0:5b88d5760320 136 friend class File;
kenjiArai 0:5b88d5760320 137 friend class Dir;
kenjiArai 0:5b88d5760320 138
kenjiArai 0:5b88d5760320 139 #if !(DOXYGEN_ONLY)
kenjiArai 0:5b88d5760320 140 /** Open a file on the file system.
kenjiArai 0:5b88d5760320 141 *
kenjiArai 0:5b88d5760320 142 * @param file Destination of the newly created handle to the referenced file.
kenjiArai 0:5b88d5760320 143 * @param path The name of the file to open.
kenjiArai 0:5b88d5760320 144 * @param flags The flags that trigger opening of the file. These flags are O_RDONLY, O_WRONLY, and O_RDWR,
kenjiArai 0:5b88d5760320 145 * with an O_CREAT, O_TRUNC, or O_APPEND bitwise OR operator.
kenjiArai 0:5b88d5760320 146 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 147 */
kenjiArai 0:5b88d5760320 148 virtual int file_open(fs_file_t *file, const char *path, int flags) = 0;
kenjiArai 0:5b88d5760320 149
kenjiArai 0:5b88d5760320 150 /** Close a file.
kenjiArai 0:5b88d5760320 151 *
kenjiArai 0:5b88d5760320 152 * @param file File handle.
kenjiArai 0:5b88d5760320 153 * return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 154 */
kenjiArai 0:5b88d5760320 155 virtual int file_close(fs_file_t file) = 0;
kenjiArai 0:5b88d5760320 156
kenjiArai 0:5b88d5760320 157 /** Read the contents of a file into a buffer.
kenjiArai 0:5b88d5760320 158 *
kenjiArai 0:5b88d5760320 159 * @param file File handle.
kenjiArai 0:5b88d5760320 160 * @param buffer The buffer to read in to.
kenjiArai 0:5b88d5760320 161 * @param size The number of bytes to read.
kenjiArai 0:5b88d5760320 162 * @return The number of bytes read, 0 at the end of the file, negative error on failure.
kenjiArai 0:5b88d5760320 163 */
kenjiArai 0:5b88d5760320 164 virtual ssize_t file_read(fs_file_t file, void *buffer, size_t size) = 0;
kenjiArai 0:5b88d5760320 165
kenjiArai 0:5b88d5760320 166 /** Write the contents of a buffer to a file.
kenjiArai 0:5b88d5760320 167 *
kenjiArai 0:5b88d5760320 168 * @param file File handle.
kenjiArai 0:5b88d5760320 169 * @param buffer The buffer to write from.
kenjiArai 0:5b88d5760320 170 * @param size The number of bytes to write.
kenjiArai 0:5b88d5760320 171 * @return The number of bytes written, negative error on failure.
kenjiArai 0:5b88d5760320 172 */
kenjiArai 0:5b88d5760320 173 virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t size) = 0;
kenjiArai 0:5b88d5760320 174
kenjiArai 0:5b88d5760320 175 /** Flush any buffers associated with the file.
kenjiArai 0:5b88d5760320 176 *
kenjiArai 0:5b88d5760320 177 * @param file File handle.
kenjiArai 0:5b88d5760320 178 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 179 */
kenjiArai 0:5b88d5760320 180 virtual int file_sync(fs_file_t file);
kenjiArai 0:5b88d5760320 181
kenjiArai 0:5b88d5760320 182 /** Check whether the file is an interactive terminal device.
kenjiArai 0:5b88d5760320 183 * If so, line buffered behavior is used by default.
kenjiArai 0:5b88d5760320 184 *
kenjiArai 0:5b88d5760320 185 * @param file File handle.
kenjiArai 0:5b88d5760320 186 * @return True if the file is a terminal.
kenjiArai 0:5b88d5760320 187 */
kenjiArai 0:5b88d5760320 188 virtual int file_isatty(fs_file_t file);
kenjiArai 0:5b88d5760320 189
kenjiArai 0:5b88d5760320 190 /** Move the file position to a given offset from a given location.
kenjiArai 0:5b88d5760320 191 *
kenjiArai 0:5b88d5760320 192 * @param file File handle.
kenjiArai 0:5b88d5760320 193 * @param offset The offset from whence to move to.
kenjiArai 0:5b88d5760320 194 * @param whence The start of where to seek.
kenjiArai 0:5b88d5760320 195 * SEEK_SET to start from the beginning of the file,
kenjiArai 0:5b88d5760320 196 * SEEK_CUR to start from the current position in the file,
kenjiArai 0:5b88d5760320 197 * SEEK_END to start from the end of the file.
kenjiArai 0:5b88d5760320 198 * @return The new offset of the file
kenjiArai 0:5b88d5760320 199 */
kenjiArai 0:5b88d5760320 200 virtual off_t file_seek(fs_file_t file, off_t offset, int whence) = 0;
kenjiArai 0:5b88d5760320 201
kenjiArai 0:5b88d5760320 202 /** Get the file position of the file.
kenjiArai 0:5b88d5760320 203 *
kenjiArai 0:5b88d5760320 204 * @param file File handle.
kenjiArai 0:5b88d5760320 205 * @return The current offset in the file.
kenjiArai 0:5b88d5760320 206 */
kenjiArai 0:5b88d5760320 207 virtual off_t file_tell(fs_file_t file);
kenjiArai 0:5b88d5760320 208
kenjiArai 0:5b88d5760320 209 /** Rewind the file position to the beginning of the file.
kenjiArai 0:5b88d5760320 210 *
kenjiArai 0:5b88d5760320 211 * @param file File handle.
kenjiArai 0:5b88d5760320 212 * @note This is equivalent to file_seek(file, 0, FS_SEEK_SET)
kenjiArai 0:5b88d5760320 213 */
kenjiArai 0:5b88d5760320 214 virtual void file_rewind(fs_file_t file);
kenjiArai 0:5b88d5760320 215
kenjiArai 0:5b88d5760320 216 /** Get the size of the file.
kenjiArai 0:5b88d5760320 217 *
kenjiArai 0:5b88d5760320 218 * @param file File handle.
kenjiArai 0:5b88d5760320 219 * @return Size of the file in bytes.
kenjiArai 0:5b88d5760320 220 */
kenjiArai 0:5b88d5760320 221 virtual off_t file_size(fs_file_t file);
kenjiArai 0:5b88d5760320 222
kenjiArai 0:5b88d5760320 223 /** Truncate or extend a file.
kenjiArai 0:5b88d5760320 224 *
kenjiArai 0:5b88d5760320 225 * The file's length is set to the specified value. The seek pointer is
kenjiArai 0:5b88d5760320 226 * not changed. If the file is extended, the extended area appears as if
kenjiArai 0:5b88d5760320 227 * it were zero-filled.
kenjiArai 0:5b88d5760320 228 *
kenjiArai 0:5b88d5760320 229 * @param file File handle.
kenjiArai 0:5b88d5760320 230 * @param length The requested new length for the file.
kenjiArai 0:5b88d5760320 231 *
kenjiArai 0:5b88d5760320 232 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 233 */
kenjiArai 0:5b88d5760320 234 virtual int file_truncate(fs_file_t file, off_t length);
kenjiArai 0:5b88d5760320 235
kenjiArai 0:5b88d5760320 236 /** Open a directory on the file system.
kenjiArai 0:5b88d5760320 237 *
kenjiArai 0:5b88d5760320 238 * @param dir Destination for the handle to the directory.
kenjiArai 0:5b88d5760320 239 * @param path Name of the directory to open.
kenjiArai 0:5b88d5760320 240 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 241 */
kenjiArai 0:5b88d5760320 242 virtual int dir_open(fs_dir_t *dir, const char *path);
kenjiArai 0:5b88d5760320 243
kenjiArai 0:5b88d5760320 244 /** Close a directory.
kenjiArai 0:5b88d5760320 245 *
kenjiArai 0:5b88d5760320 246 * @param dir Dir handle.
kenjiArai 0:5b88d5760320 247 * @return 0 on success, negative error code on failure.
kenjiArai 0:5b88d5760320 248 */
kenjiArai 0:5b88d5760320 249 virtual int dir_close(fs_dir_t dir);
kenjiArai 0:5b88d5760320 250
kenjiArai 0:5b88d5760320 251 /** Read the next directory entry.
kenjiArai 0:5b88d5760320 252 *
kenjiArai 0:5b88d5760320 253 * @param dir Dir handle.
kenjiArai 0:5b88d5760320 254 * @param ent The directory entry to fill out.
kenjiArai 0:5b88d5760320 255 * @return 1 on reading a filename, 0 at the end of the directory, negative error on failure.
kenjiArai 0:5b88d5760320 256 */
kenjiArai 0:5b88d5760320 257 virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent);
kenjiArai 0:5b88d5760320 258
kenjiArai 0:5b88d5760320 259 /** Set the current position of the directory.
kenjiArai 0:5b88d5760320 260 *
kenjiArai 0:5b88d5760320 261 * @param dir Dir handle.
kenjiArai 0:5b88d5760320 262 * @param offset Offset of the location to seek to,
kenjiArai 0:5b88d5760320 263 * must be a value returned from dir_tell.
kenjiArai 0:5b88d5760320 264 */
kenjiArai 0:5b88d5760320 265 virtual void dir_seek(fs_dir_t dir, off_t offset);
kenjiArai 0:5b88d5760320 266
kenjiArai 0:5b88d5760320 267 /** Get the current position of the directory.
kenjiArai 0:5b88d5760320 268 *
kenjiArai 0:5b88d5760320 269 * @param dir Dir handle.
kenjiArai 0:5b88d5760320 270 * @return Directory position, which can be passed to dir_rewind.
kenjiArai 0:5b88d5760320 271 */
kenjiArai 0:5b88d5760320 272 virtual off_t dir_tell(fs_dir_t dir);
kenjiArai 0:5b88d5760320 273
kenjiArai 0:5b88d5760320 274 /** Rewind the current position to the beginning of the directory.
kenjiArai 0:5b88d5760320 275 *
kenjiArai 0:5b88d5760320 276 * @param dir Dir handle.
kenjiArai 0:5b88d5760320 277 */
kenjiArai 0:5b88d5760320 278 virtual void dir_rewind(fs_dir_t dir);
kenjiArai 0:5b88d5760320 279
kenjiArai 0:5b88d5760320 280 /** Get the size of the directory.
kenjiArai 0:5b88d5760320 281 *
kenjiArai 0:5b88d5760320 282 * @param dir Dir handle.
kenjiArai 0:5b88d5760320 283 * @return Number of files in the directory.
kenjiArai 0:5b88d5760320 284 */
kenjiArai 0:5b88d5760320 285 virtual size_t dir_size(fs_dir_t dir);
kenjiArai 0:5b88d5760320 286 #endif //!defined(DOXYGEN_ONLY)
kenjiArai 0:5b88d5760320 287
kenjiArai 0:5b88d5760320 288 protected:
kenjiArai 0:5b88d5760320 289 // Hooks for file systemHandle
kenjiArai 0:5b88d5760320 290 virtual int open(FileHandle **file, const char *path, int flags);
kenjiArai 0:5b88d5760320 291 virtual int open(DirHandle **dir, const char *path);
kenjiArai 0:5b88d5760320 292 };
kenjiArai 0:5b88d5760320 293
kenjiArai 0:5b88d5760320 294
kenjiArai 0:5b88d5760320 295 /** @}*/
kenjiArai 0:5b88d5760320 296 } // namespace mbed
kenjiArai 0:5b88d5760320 297
kenjiArai 0:5b88d5760320 298 #endif