Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

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