BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Committer:
gustavatmel
Date:
Tue May 01 15:55:34 2018 +0000
Revision:
2:798925c9e4a8
Parent:
1:9c5af431a1f1
bluetooth

Who changed what in which revision?

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