BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:fbdae7e6d805 1 /* mbed Microcontroller Library
borlanic 0:fbdae7e6d805 2 * Copyright (c) 2006-2012 ARM Limited
borlanic 0:fbdae7e6d805 3 *
borlanic 0:fbdae7e6d805 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
borlanic 0:fbdae7e6d805 5 * of this software and associated documentation files (the "Software"), to deal
borlanic 0:fbdae7e6d805 6 * in the Software without restriction, including without limitation the rights
borlanic 0:fbdae7e6d805 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
borlanic 0:fbdae7e6d805 8 * copies of the Software, and to permit persons to whom the Software is
borlanic 0:fbdae7e6d805 9 * furnished to do so, subject to the following conditions:
borlanic 0:fbdae7e6d805 10 *
borlanic 0:fbdae7e6d805 11 * The above copyright notice and this permission notice shall be included in
borlanic 0:fbdae7e6d805 12 * all copies or substantial portions of the Software.
borlanic 0:fbdae7e6d805 13 *
borlanic 0:fbdae7e6d805 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
borlanic 0:fbdae7e6d805 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
borlanic 0:fbdae7e6d805 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
borlanic 0:fbdae7e6d805 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
borlanic 0:fbdae7e6d805 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
borlanic 0:fbdae7e6d805 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
borlanic 0:fbdae7e6d805 20 * SOFTWARE.
borlanic 0:fbdae7e6d805 21 */
borlanic 0:fbdae7e6d805 22 #ifndef MBED_FATFILESYSTEM_H
borlanic 0:fbdae7e6d805 23 #define MBED_FATFILESYSTEM_H
borlanic 0:fbdae7e6d805 24
borlanic 0:fbdae7e6d805 25 #include "FileSystem.h"
borlanic 0:fbdae7e6d805 26 #include "BlockDevice.h"
borlanic 0:fbdae7e6d805 27 #include "FileHandle.h"
borlanic 0:fbdae7e6d805 28 #include "ff.h"
borlanic 0:fbdae7e6d805 29 #include <stdint.h>
borlanic 0:fbdae7e6d805 30 #include "PlatformMutex.h"
borlanic 0:fbdae7e6d805 31
borlanic 0:fbdae7e6d805 32 using namespace mbed;
borlanic 0:fbdae7e6d805 33
borlanic 0:fbdae7e6d805 34 /**
borlanic 0:fbdae7e6d805 35 * FATFileSystem based on ChaN's Fat Filesystem library v0.8
borlanic 0:fbdae7e6d805 36 */
borlanic 0:fbdae7e6d805 37 class FATFileSystem : public FileSystem {
borlanic 0:fbdae7e6d805 38 public:
borlanic 0:fbdae7e6d805 39 /** Lifetime of the FATFileSystem
borlanic 0:fbdae7e6d805 40 *
borlanic 0:fbdae7e6d805 41 * @param name Name to add filesystem to tree as
borlanic 0:fbdae7e6d805 42 * @param bd BlockDevice to mount, may be passed instead to mount call
borlanic 0:fbdae7e6d805 43 */
borlanic 0:fbdae7e6d805 44 FATFileSystem(const char *name = NULL, BlockDevice *bd = NULL);
borlanic 0:fbdae7e6d805 45 virtual ~FATFileSystem();
borlanic 0:fbdae7e6d805 46
borlanic 0:fbdae7e6d805 47 /** Formats a logical drive, FDISK partitioning rule.
borlanic 0:fbdae7e6d805 48 *
borlanic 0:fbdae7e6d805 49 * The block device to format should be mounted when this function is called.
borlanic 0:fbdae7e6d805 50 *
borlanic 0:fbdae7e6d805 51 * @param bd
borlanic 0:fbdae7e6d805 52 * This is the block device that will be formatted.
borlanic 0:fbdae7e6d805 53 *
borlanic 0:fbdae7e6d805 54 * @param cluster_size
borlanic 0:fbdae7e6d805 55 * This is the number of bytes per cluster. A larger cluster size will decrease
borlanic 0:fbdae7e6d805 56 * the overhead of the FAT table, but also increase the minimum file size. The
borlanic 0:fbdae7e6d805 57 * cluster_size must be a multiple of the underlying device's allocation unit
borlanic 0:fbdae7e6d805 58 * and is currently limited to a max of 32,768 bytes. If zero, a cluster size
borlanic 0:fbdae7e6d805 59 * will be determined from the device's allocation unit. Defaults to zero.
borlanic 0:fbdae7e6d805 60 *
borlanic 0:fbdae7e6d805 61 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 62 */
borlanic 0:fbdae7e6d805 63 static int format(BlockDevice *bd, bd_size_t cluster_size = 0);
borlanic 0:fbdae7e6d805 64
borlanic 0:fbdae7e6d805 65 /** Mounts a filesystem to a block device
borlanic 0:fbdae7e6d805 66 *
borlanic 0:fbdae7e6d805 67 * @param bd BlockDevice to mount to
borlanic 0:fbdae7e6d805 68 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 69 */
borlanic 0:fbdae7e6d805 70 virtual int mount(BlockDevice *bd);
borlanic 0:fbdae7e6d805 71
borlanic 0:fbdae7e6d805 72 /** Unmounts a filesystem from the underlying block device
borlanic 0:fbdae7e6d805 73 *
borlanic 0:fbdae7e6d805 74 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 75 */
borlanic 0:fbdae7e6d805 76 virtual int unmount();
borlanic 0:fbdae7e6d805 77
borlanic 0:fbdae7e6d805 78 /** Reformats a filesystem, results in an empty and mounted filesystem
borlanic 0:fbdae7e6d805 79 *
borlanic 0:fbdae7e6d805 80 * @param bd
borlanic 0:fbdae7e6d805 81 * BlockDevice to reformat and mount. If NULL, the mounted
borlanic 0:fbdae7e6d805 82 * block device will be used.
borlanic 0:fbdae7e6d805 83 * Note: if mount fails, bd must be provided.
borlanic 0:fbdae7e6d805 84 * Default: NULL
borlanic 0:fbdae7e6d805 85 *
borlanic 0:fbdae7e6d805 86 * @param allocation_unit
borlanic 0:fbdae7e6d805 87 * This is the number of bytes per cluster size. The valid value is N
borlanic 0:fbdae7e6d805 88 * times the sector size. N is a power of 2 from 1 to 128 for FAT
borlanic 0:fbdae7e6d805 89 * volume and upto 16MiB for exFAT volume. If zero is given,
borlanic 0:fbdae7e6d805 90 * the default allocation unit size is selected by the underlying
borlanic 0:fbdae7e6d805 91 * filesystem, which depends on the volume size.
borlanic 0:fbdae7e6d805 92 *
borlanic 0:fbdae7e6d805 93 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 94 */
borlanic 0:fbdae7e6d805 95 virtual int reformat(BlockDevice *bd, int allocation_unit);
borlanic 0:fbdae7e6d805 96
borlanic 0:fbdae7e6d805 97 /** Reformats a filesystem, results in an empty and mounted filesystem
borlanic 0:fbdae7e6d805 98 *
borlanic 0:fbdae7e6d805 99 * @param bd BlockDevice to reformat and mount. If NULL, the mounted
borlanic 0:fbdae7e6d805 100 * block device will be used.
borlanic 0:fbdae7e6d805 101 * Note: if mount fails, bd must be provided.
borlanic 0:fbdae7e6d805 102 * Default: NULL
borlanic 0:fbdae7e6d805 103 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 104 */
borlanic 0:fbdae7e6d805 105 virtual int reformat(BlockDevice *bd = NULL)
borlanic 0:fbdae7e6d805 106 {
borlanic 0:fbdae7e6d805 107 // required for virtual inheritance shenanigans
borlanic 0:fbdae7e6d805 108 return reformat(bd, 0);
borlanic 0:fbdae7e6d805 109 }
borlanic 0:fbdae7e6d805 110
borlanic 0:fbdae7e6d805 111 /** Remove a file from the filesystem.
borlanic 0:fbdae7e6d805 112 *
borlanic 0:fbdae7e6d805 113 * @param path The name of the file to remove.
borlanic 0:fbdae7e6d805 114 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 115 */
borlanic 0:fbdae7e6d805 116 virtual int remove(const char *path);
borlanic 0:fbdae7e6d805 117
borlanic 0:fbdae7e6d805 118 /** Rename a file in the filesystem.
borlanic 0:fbdae7e6d805 119 *
borlanic 0:fbdae7e6d805 120 * @param path The name of the file to rename.
borlanic 0:fbdae7e6d805 121 * @param newpath The name to rename it to
borlanic 0:fbdae7e6d805 122 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 123 */
borlanic 0:fbdae7e6d805 124 virtual int rename(const char *path, const char *newpath);
borlanic 0:fbdae7e6d805 125
borlanic 0:fbdae7e6d805 126 /** Store information about the file in a stat structure
borlanic 0:fbdae7e6d805 127 *
borlanic 0:fbdae7e6d805 128 * @param path The name of the file to find information about
borlanic 0:fbdae7e6d805 129 * @param st The stat buffer to write to
borlanic 0:fbdae7e6d805 130 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 131 */
borlanic 0:fbdae7e6d805 132 virtual int stat(const char *path, struct stat *st);
borlanic 0:fbdae7e6d805 133
borlanic 0:fbdae7e6d805 134 /** Create a directory in the filesystem.
borlanic 0:fbdae7e6d805 135 *
borlanic 0:fbdae7e6d805 136 * @param path The name of the directory to create.
borlanic 0:fbdae7e6d805 137 * @param mode The permissions with which to create the directory
borlanic 0:fbdae7e6d805 138 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 139 */
borlanic 0:fbdae7e6d805 140 virtual int mkdir(const char *path, mode_t mode);
borlanic 0:fbdae7e6d805 141
borlanic 0:fbdae7e6d805 142 /** Store information about the mounted filesystem in a statvfs structure
borlanic 0:fbdae7e6d805 143 *
borlanic 0:fbdae7e6d805 144 * @param path The name of the file to find information about
borlanic 0:fbdae7e6d805 145 * @param buf The stat buffer to write to
borlanic 0:fbdae7e6d805 146 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 147 */
borlanic 0:fbdae7e6d805 148 virtual int statvfs(const char *path, struct statvfs *buf);
borlanic 0:fbdae7e6d805 149
borlanic 0:fbdae7e6d805 150 protected:
borlanic 0:fbdae7e6d805 151 /** Open a file on the filesystem
borlanic 0:fbdae7e6d805 152 *
borlanic 0:fbdae7e6d805 153 * @param file Destination for the handle to a newly created file
borlanic 0:fbdae7e6d805 154 * @param path The name of the file to open
borlanic 0:fbdae7e6d805 155 * @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
borlanic 0:fbdae7e6d805 156 * bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
borlanic 0:fbdae7e6d805 157 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 158 */
borlanic 0:fbdae7e6d805 159 virtual int file_open(fs_file_t *file, const char *path, int flags);
borlanic 0:fbdae7e6d805 160
borlanic 0:fbdae7e6d805 161 /** Close a file
borlanic 0:fbdae7e6d805 162 *
borlanic 0:fbdae7e6d805 163 * @param file File handle
borlanic 0:fbdae7e6d805 164 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 165 */
borlanic 0:fbdae7e6d805 166 virtual int file_close(fs_file_t file);
borlanic 0:fbdae7e6d805 167
borlanic 0:fbdae7e6d805 168 /** Read the contents of a file into a buffer
borlanic 0:fbdae7e6d805 169 *
borlanic 0:fbdae7e6d805 170 * @param file File handle
borlanic 0:fbdae7e6d805 171 * @param buffer The buffer to read in to
borlanic 0:fbdae7e6d805 172 * @param len The number of bytes to read
borlanic 0:fbdae7e6d805 173 * @return The number of bytes read, 0 at end of file, negative error on failure
borlanic 0:fbdae7e6d805 174 */
borlanic 0:fbdae7e6d805 175 virtual ssize_t file_read(fs_file_t file, void *buffer, size_t len);
borlanic 0:fbdae7e6d805 176
borlanic 0:fbdae7e6d805 177 /** Write the contents of a buffer to a file
borlanic 0:fbdae7e6d805 178 *
borlanic 0:fbdae7e6d805 179 * @param file File handle
borlanic 0:fbdae7e6d805 180 * @param buffer The buffer to write from
borlanic 0:fbdae7e6d805 181 * @param len The number of bytes to write
borlanic 0:fbdae7e6d805 182 * @return The number of bytes written, negative error on failure
borlanic 0:fbdae7e6d805 183 */
borlanic 0:fbdae7e6d805 184 virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t len);
borlanic 0:fbdae7e6d805 185
borlanic 0:fbdae7e6d805 186 /** Flush any buffers associated with the file
borlanic 0:fbdae7e6d805 187 *
borlanic 0:fbdae7e6d805 188 * @param file File handle
borlanic 0:fbdae7e6d805 189 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 190 */
borlanic 0:fbdae7e6d805 191 virtual int file_sync(fs_file_t file);
borlanic 0:fbdae7e6d805 192
borlanic 0:fbdae7e6d805 193 /** Move the file position to a given offset from from a given location
borlanic 0:fbdae7e6d805 194 *
borlanic 0:fbdae7e6d805 195 * @param file File handle
borlanic 0:fbdae7e6d805 196 * @param offset The offset from whence to move to
borlanic 0:fbdae7e6d805 197 * @param whence The start of where to seek
borlanic 0:fbdae7e6d805 198 * SEEK_SET to start from beginning of file,
borlanic 0:fbdae7e6d805 199 * SEEK_CUR to start from current position in file,
borlanic 0:fbdae7e6d805 200 * SEEK_END to start from end of file
borlanic 0:fbdae7e6d805 201 * @return The new offset of the file
borlanic 0:fbdae7e6d805 202 */
borlanic 0:fbdae7e6d805 203 virtual off_t file_seek(fs_file_t file, off_t offset, int whence);
borlanic 0:fbdae7e6d805 204
borlanic 0:fbdae7e6d805 205 /** Get the file position of the file
borlanic 0:fbdae7e6d805 206 *
borlanic 0:fbdae7e6d805 207 * @param file File handle
borlanic 0:fbdae7e6d805 208 * @return The current offset in the file
borlanic 0:fbdae7e6d805 209 */
borlanic 0:fbdae7e6d805 210 virtual off_t file_tell(fs_file_t file);
borlanic 0:fbdae7e6d805 211
borlanic 0:fbdae7e6d805 212 /** Get the size of the file
borlanic 0:fbdae7e6d805 213 *
borlanic 0:fbdae7e6d805 214 * @param file File handle
borlanic 0:fbdae7e6d805 215 * @return Size of the file in bytes
borlanic 0:fbdae7e6d805 216 */
borlanic 0:fbdae7e6d805 217 virtual off_t file_size(fs_file_t file);
borlanic 0:fbdae7e6d805 218
borlanic 0:fbdae7e6d805 219 /** Open a directory on the filesystem
borlanic 0:fbdae7e6d805 220 *
borlanic 0:fbdae7e6d805 221 * @param dir Destination for the handle to the directory
borlanic 0:fbdae7e6d805 222 * @param path Name of the directory to open
borlanic 0:fbdae7e6d805 223 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 224 */
borlanic 0:fbdae7e6d805 225 virtual int dir_open(fs_dir_t *dir, const char *path);
borlanic 0:fbdae7e6d805 226
borlanic 0:fbdae7e6d805 227 /** Close a directory
borlanic 0:fbdae7e6d805 228 *
borlanic 0:fbdae7e6d805 229 * @param dir Dir handle
borlanic 0:fbdae7e6d805 230 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 231 */
borlanic 0:fbdae7e6d805 232 virtual int dir_close(fs_dir_t dir);
borlanic 0:fbdae7e6d805 233
borlanic 0:fbdae7e6d805 234 /** Read the next directory entry
borlanic 0:fbdae7e6d805 235 *
borlanic 0:fbdae7e6d805 236 * @param dir Dir handle
borlanic 0:fbdae7e6d805 237 * @param ent The directory entry to fill out
borlanic 0:fbdae7e6d805 238 * @return 1 on reading a filename, 0 at end of directory, negative error on failure
borlanic 0:fbdae7e6d805 239 */
borlanic 0:fbdae7e6d805 240 virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent);
borlanic 0:fbdae7e6d805 241
borlanic 0:fbdae7e6d805 242 /** Set the current position of the directory
borlanic 0:fbdae7e6d805 243 *
borlanic 0:fbdae7e6d805 244 * @param dir Dir handle
borlanic 0:fbdae7e6d805 245 * @param offset Offset of the location to seek to,
borlanic 0:fbdae7e6d805 246 * must be a value returned from dir_tell
borlanic 0:fbdae7e6d805 247 */
borlanic 0:fbdae7e6d805 248 virtual void dir_seek(fs_dir_t dir, off_t offset);
borlanic 0:fbdae7e6d805 249
borlanic 0:fbdae7e6d805 250 /** Get the current position of the directory
borlanic 0:fbdae7e6d805 251 *
borlanic 0:fbdae7e6d805 252 * @param dir Dir handle
borlanic 0:fbdae7e6d805 253 * @return Position of the directory that can be passed to dir_rewind
borlanic 0:fbdae7e6d805 254 */
borlanic 0:fbdae7e6d805 255 virtual off_t dir_tell(fs_dir_t dir);
borlanic 0:fbdae7e6d805 256
borlanic 0:fbdae7e6d805 257 /** Rewind the current position to the beginning of the directory
borlanic 0:fbdae7e6d805 258 *
borlanic 0:fbdae7e6d805 259 * @param dir Dir handle
borlanic 0:fbdae7e6d805 260 */
borlanic 0:fbdae7e6d805 261 virtual void dir_rewind(fs_dir_t dir);
borlanic 0:fbdae7e6d805 262
borlanic 0:fbdae7e6d805 263 private:
borlanic 0:fbdae7e6d805 264 FATFS _fs; // Work area (file system object) for logical drive
borlanic 0:fbdae7e6d805 265 char _fsid[sizeof("0:")];
borlanic 0:fbdae7e6d805 266 int _id;
borlanic 0:fbdae7e6d805 267
borlanic 0:fbdae7e6d805 268 protected:
borlanic 0:fbdae7e6d805 269 virtual void lock();
borlanic 0:fbdae7e6d805 270 virtual void unlock();
borlanic 0:fbdae7e6d805 271 virtual int mount(BlockDevice *bd, bool mount);
borlanic 0:fbdae7e6d805 272 };
borlanic 0:fbdae7e6d805 273
borlanic 0:fbdae7e6d805 274 #endif