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) 2017 ARM Limited
borlanic 0:fbdae7e6d805 3 *
borlanic 0:fbdae7e6d805 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:fbdae7e6d805 5 * you may not use this file except in compliance with the License.
borlanic 0:fbdae7e6d805 6 * You may obtain a copy of the License at
borlanic 0:fbdae7e6d805 7 *
borlanic 0:fbdae7e6d805 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:fbdae7e6d805 9 *
borlanic 0:fbdae7e6d805 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:fbdae7e6d805 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:fbdae7e6d805 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:fbdae7e6d805 13 * See the License for the specific language governing permissions and
borlanic 0:fbdae7e6d805 14 * limitations under the License.
borlanic 0:fbdae7e6d805 15 */
borlanic 0:fbdae7e6d805 16 #ifndef MBED_LFSFILESYSTEM_H
borlanic 0:fbdae7e6d805 17 #define MBED_LFSFILESYSTEM_H
borlanic 0:fbdae7e6d805 18
borlanic 0:fbdae7e6d805 19 #include "FileSystem.h"
borlanic 0:fbdae7e6d805 20 #include "BlockDevice.h"
borlanic 0:fbdae7e6d805 21 #include "PlatformMutex.h"
borlanic 0:fbdae7e6d805 22 extern "C" {
borlanic 0:fbdae7e6d805 23 #include "lfs.h"
borlanic 0:fbdae7e6d805 24 }
borlanic 0:fbdae7e6d805 25
borlanic 0:fbdae7e6d805 26
borlanic 0:fbdae7e6d805 27 /**
borlanic 0:fbdae7e6d805 28 * LittleFileSystem, a little filesystem
borlanic 0:fbdae7e6d805 29 */
borlanic 0:fbdae7e6d805 30 class LittleFileSystem : public mbed::FileSystem {
borlanic 0:fbdae7e6d805 31 public:
borlanic 0:fbdae7e6d805 32 /** Lifetime of the LittleFileSystem
borlanic 0:fbdae7e6d805 33 *
borlanic 0:fbdae7e6d805 34 * @param name Name to add filesystem to tree as
borlanic 0:fbdae7e6d805 35 * @param bd BlockDevice to mount, may be passed instead to mount call
borlanic 0:fbdae7e6d805 36 * @param read_size
borlanic 0:fbdae7e6d805 37 * Minimum size of a block read. This determines the size of read buffers.
borlanic 0:fbdae7e6d805 38 * This may be larger than the physical read size to improve performance
borlanic 0:fbdae7e6d805 39 * by caching more of the block device.
borlanic 0:fbdae7e6d805 40 * @param prog_size
borlanic 0:fbdae7e6d805 41 * Minimum size of a block program. This determines the size of program
borlanic 0:fbdae7e6d805 42 * buffers. This may be larger than the physical program size to improve
borlanic 0:fbdae7e6d805 43 * performance by caching more of the block device.
borlanic 0:fbdae7e6d805 44 * @param block_size
borlanic 0:fbdae7e6d805 45 * Size of an erasable block. This does not impact ram consumption and
borlanic 0:fbdae7e6d805 46 * may be larger than the physical erase size. However, this should be
borlanic 0:fbdae7e6d805 47 * kept small as each file currently takes up an entire block.
borlanic 0:fbdae7e6d805 48 * @param lookahead
borlanic 0:fbdae7e6d805 49 * Number of blocks to lookahead during block allocation. A larger
borlanic 0:fbdae7e6d805 50 * lookahead reduces the number of passes required to allocate a block.
borlanic 0:fbdae7e6d805 51 * The lookahead buffer requires only 1 bit per block so it can be quite
borlanic 0:fbdae7e6d805 52 * large with little ram impact. Should be a multiple of 32.
borlanic 0:fbdae7e6d805 53 */
borlanic 0:fbdae7e6d805 54 LittleFileSystem(const char *name=NULL, BlockDevice *bd=NULL,
borlanic 0:fbdae7e6d805 55 lfs_size_t read_size=MBED_LFS_READ_SIZE,
borlanic 0:fbdae7e6d805 56 lfs_size_t prog_size=MBED_LFS_PROG_SIZE,
borlanic 0:fbdae7e6d805 57 lfs_size_t block_size=MBED_LFS_BLOCK_SIZE,
borlanic 0:fbdae7e6d805 58 lfs_size_t lookahead=MBED_LFS_LOOKAHEAD);
borlanic 0:fbdae7e6d805 59 virtual ~LittleFileSystem();
borlanic 0:fbdae7e6d805 60
borlanic 0:fbdae7e6d805 61 /** Formats a block device with the LittleFileSystem
borlanic 0:fbdae7e6d805 62 *
borlanic 0:fbdae7e6d805 63 * The block device to format should be mounted when this function is called.
borlanic 0:fbdae7e6d805 64 *
borlanic 0:fbdae7e6d805 65 * @param bd This is the block device that will be formated.
borlanic 0:fbdae7e6d805 66 * @param read_size
borlanic 0:fbdae7e6d805 67 * Minimum size of a block read. This determines the size of read buffers.
borlanic 0:fbdae7e6d805 68 * This may be larger than the physical read size to improve performance
borlanic 0:fbdae7e6d805 69 * by caching more of the block device.
borlanic 0:fbdae7e6d805 70 * @param prog_size
borlanic 0:fbdae7e6d805 71 * Minimum size of a block program. This determines the size of program
borlanic 0:fbdae7e6d805 72 * buffers. This may be larger than the physical program size to improve
borlanic 0:fbdae7e6d805 73 * performance by caching more of the block device.
borlanic 0:fbdae7e6d805 74 * @param block_size
borlanic 0:fbdae7e6d805 75 * Size of an erasable block. This does not impact ram consumption and
borlanic 0:fbdae7e6d805 76 * may be larger than the physical erase size. However, this should be
borlanic 0:fbdae7e6d805 77 * kept small as each file currently takes up an entire block.
borlanic 0:fbdae7e6d805 78 * @param lookahead
borlanic 0:fbdae7e6d805 79 * Number of blocks to lookahead during block allocation. A larger
borlanic 0:fbdae7e6d805 80 * lookahead reduces the number of passes required to allocate a block.
borlanic 0:fbdae7e6d805 81 * The lookahead buffer requires only 1 bit per block so it can be quite
borlanic 0:fbdae7e6d805 82 * large with little ram impact. Should be a multiple of 32.
borlanic 0:fbdae7e6d805 83 */
borlanic 0:fbdae7e6d805 84 static int format(BlockDevice *bd,
borlanic 0:fbdae7e6d805 85 lfs_size_t read_size=MBED_LFS_READ_SIZE,
borlanic 0:fbdae7e6d805 86 lfs_size_t prog_size=MBED_LFS_PROG_SIZE,
borlanic 0:fbdae7e6d805 87 lfs_size_t block_size=MBED_LFS_BLOCK_SIZE,
borlanic 0:fbdae7e6d805 88 lfs_size_t lookahead=MBED_LFS_LOOKAHEAD);
borlanic 0:fbdae7e6d805 89
borlanic 0:fbdae7e6d805 90 /** Mounts a filesystem to a block device
borlanic 0:fbdae7e6d805 91 *
borlanic 0:fbdae7e6d805 92 * @param bd BlockDevice to mount to
borlanic 0:fbdae7e6d805 93 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 94 */
borlanic 0:fbdae7e6d805 95 virtual int mount(BlockDevice *bd);
borlanic 0:fbdae7e6d805 96
borlanic 0:fbdae7e6d805 97 /** Unmounts a filesystem from the underlying block device
borlanic 0:fbdae7e6d805 98 *
borlanic 0:fbdae7e6d805 99 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 100 */
borlanic 0:fbdae7e6d805 101 virtual int unmount();
borlanic 0:fbdae7e6d805 102
borlanic 0:fbdae7e6d805 103 /** Reformats a filesystem, results in an empty and mounted filesystem
borlanic 0:fbdae7e6d805 104 *
borlanic 0:fbdae7e6d805 105 * @param bd
borlanic 0:fbdae7e6d805 106 * BlockDevice to reformat and mount. If NULL, the mounted
borlanic 0:fbdae7e6d805 107 * block device will be used.
borlanic 0:fbdae7e6d805 108 * Note: if mount fails, bd must be provided.
borlanic 0:fbdae7e6d805 109 * Default: NULL
borlanic 0:fbdae7e6d805 110 *
borlanic 0:fbdae7e6d805 111 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 112 */
borlanic 0:fbdae7e6d805 113 virtual int reformat(BlockDevice *bd);
borlanic 0:fbdae7e6d805 114
borlanic 0:fbdae7e6d805 115 /** Remove a file from the filesystem.
borlanic 0:fbdae7e6d805 116 *
borlanic 0:fbdae7e6d805 117 * @param path The name of the file to remove.
borlanic 0:fbdae7e6d805 118 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 119 */
borlanic 0:fbdae7e6d805 120 virtual int remove(const char *path);
borlanic 0:fbdae7e6d805 121
borlanic 0:fbdae7e6d805 122 /** Rename a file in the filesystem.
borlanic 0:fbdae7e6d805 123 *
borlanic 0:fbdae7e6d805 124 * @param path The name of the file to rename.
borlanic 0:fbdae7e6d805 125 * @param newpath The name to rename it to
borlanic 0:fbdae7e6d805 126 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 127 */
borlanic 0:fbdae7e6d805 128 virtual int rename(const char *path, const char *newpath);
borlanic 0:fbdae7e6d805 129
borlanic 0:fbdae7e6d805 130 /** Store information about the file in a stat structure
borlanic 0:fbdae7e6d805 131 *
borlanic 0:fbdae7e6d805 132 * @param path The name of the file to find information about
borlanic 0:fbdae7e6d805 133 * @param st The stat buffer to write to
borlanic 0:fbdae7e6d805 134 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 135 */
borlanic 0:fbdae7e6d805 136 virtual int stat(const char *path, struct stat *st);
borlanic 0:fbdae7e6d805 137
borlanic 0:fbdae7e6d805 138 /** Create a directory in the filesystem.
borlanic 0:fbdae7e6d805 139 *
borlanic 0:fbdae7e6d805 140 * @param path The name of the directory to create.
borlanic 0:fbdae7e6d805 141 * @param mode The permissions with which to create the directory
borlanic 0:fbdae7e6d805 142 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 143 */
borlanic 0:fbdae7e6d805 144 virtual int mkdir(const char *path, mode_t mode);
borlanic 0:fbdae7e6d805 145
borlanic 0:fbdae7e6d805 146 /** Store information about the mounted filesystem in a statvfs structure
borlanic 0:fbdae7e6d805 147 *
borlanic 0:fbdae7e6d805 148 * @param path The name of the file to find information about
borlanic 0:fbdae7e6d805 149 * @param buf The stat buffer to write to
borlanic 0:fbdae7e6d805 150 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 151 */
borlanic 0:fbdae7e6d805 152 virtual int statvfs(const char *path, struct statvfs *buf);
borlanic 0:fbdae7e6d805 153
borlanic 0:fbdae7e6d805 154 protected:
borlanic 0:fbdae7e6d805 155 /** Open a file on the filesystem
borlanic 0:fbdae7e6d805 156 *
borlanic 0:fbdae7e6d805 157 * @param file Destination for the handle to a newly created file
borlanic 0:fbdae7e6d805 158 * @param path The name of the file to open
borlanic 0:fbdae7e6d805 159 * @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
borlanic 0:fbdae7e6d805 160 * bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
borlanic 0:fbdae7e6d805 161 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 162 */
borlanic 0:fbdae7e6d805 163 virtual int file_open(mbed::fs_file_t *file, const char *path, int flags);
borlanic 0:fbdae7e6d805 164
borlanic 0:fbdae7e6d805 165 /** Close a file
borlanic 0:fbdae7e6d805 166 *
borlanic 0:fbdae7e6d805 167 * @param file File handle
borlanic 0:fbdae7e6d805 168 * return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 169 */
borlanic 0:fbdae7e6d805 170 virtual int file_close(mbed::fs_file_t file);
borlanic 0:fbdae7e6d805 171
borlanic 0:fbdae7e6d805 172 /** Read the contents of a file into a buffer
borlanic 0:fbdae7e6d805 173 *
borlanic 0:fbdae7e6d805 174 * @param file File handle
borlanic 0:fbdae7e6d805 175 * @param buffer The buffer to read in to
borlanic 0:fbdae7e6d805 176 * @param size The number of bytes to read
borlanic 0:fbdae7e6d805 177 * @return The number of bytes read, 0 at end of file, negative error on failure
borlanic 0:fbdae7e6d805 178 */
borlanic 0:fbdae7e6d805 179 virtual ssize_t file_read(mbed::fs_file_t file, void *buffer, size_t size);
borlanic 0:fbdae7e6d805 180
borlanic 0:fbdae7e6d805 181 /** Write the contents of a buffer to a file
borlanic 0:fbdae7e6d805 182 *
borlanic 0:fbdae7e6d805 183 * @param file File handle
borlanic 0:fbdae7e6d805 184 * @param buffer The buffer to write from
borlanic 0:fbdae7e6d805 185 * @param size The number of bytes to write
borlanic 0:fbdae7e6d805 186 * @return The number of bytes written, negative error on failure
borlanic 0:fbdae7e6d805 187 */
borlanic 0:fbdae7e6d805 188 virtual ssize_t file_write(mbed::fs_file_t file, const void *buffer, size_t size);
borlanic 0:fbdae7e6d805 189
borlanic 0:fbdae7e6d805 190 /** Flush any buffers associated with the file
borlanic 0:fbdae7e6d805 191 *
borlanic 0:fbdae7e6d805 192 * @param file File handle
borlanic 0:fbdae7e6d805 193 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 194 */
borlanic 0:fbdae7e6d805 195 virtual int file_sync(mbed::fs_file_t file);
borlanic 0:fbdae7e6d805 196
borlanic 0:fbdae7e6d805 197 /** Move the file position to a given offset from from a given location
borlanic 0:fbdae7e6d805 198 *
borlanic 0:fbdae7e6d805 199 * @param file File handle
borlanic 0:fbdae7e6d805 200 * @param offset The offset from whence to move to
borlanic 0:fbdae7e6d805 201 * @param whence The start of where to seek
borlanic 0:fbdae7e6d805 202 * SEEK_SET to start from beginning of file,
borlanic 0:fbdae7e6d805 203 * SEEK_CUR to start from current position in file,
borlanic 0:fbdae7e6d805 204 * SEEK_END to start from end of file
borlanic 0:fbdae7e6d805 205 * @return The new offset of the file
borlanic 0:fbdae7e6d805 206 */
borlanic 0:fbdae7e6d805 207 virtual off_t file_seek(mbed::fs_file_t file, off_t offset, int whence);
borlanic 0:fbdae7e6d805 208
borlanic 0:fbdae7e6d805 209 /** Get the file position of the file
borlanic 0:fbdae7e6d805 210 *
borlanic 0:fbdae7e6d805 211 * @param file File handle
borlanic 0:fbdae7e6d805 212 * @return The current offset in the file
borlanic 0:fbdae7e6d805 213 */
borlanic 0:fbdae7e6d805 214 virtual off_t file_tell(mbed::fs_file_t file);
borlanic 0:fbdae7e6d805 215
borlanic 0:fbdae7e6d805 216 /** Get the size of the file
borlanic 0:fbdae7e6d805 217 *
borlanic 0:fbdae7e6d805 218 * @param file File handle
borlanic 0:fbdae7e6d805 219 * @return Size of the file in bytes
borlanic 0:fbdae7e6d805 220 */
borlanic 0:fbdae7e6d805 221 virtual off_t file_size(mbed::fs_file_t file);
borlanic 0:fbdae7e6d805 222
borlanic 0:fbdae7e6d805 223 /** Open a directory on the filesystem
borlanic 0:fbdae7e6d805 224 *
borlanic 0:fbdae7e6d805 225 * @param dir Destination for the handle to the directory
borlanic 0:fbdae7e6d805 226 * @param path Name of the directory to open
borlanic 0:fbdae7e6d805 227 * @return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 228 */
borlanic 0:fbdae7e6d805 229 virtual int dir_open(mbed::fs_dir_t *dir, const char *path);
borlanic 0:fbdae7e6d805 230
borlanic 0:fbdae7e6d805 231 /** Close a directory
borlanic 0:fbdae7e6d805 232 *
borlanic 0:fbdae7e6d805 233 * @param dir Dir handle
borlanic 0:fbdae7e6d805 234 * return 0 on success, negative error code on failure
borlanic 0:fbdae7e6d805 235 */
borlanic 0:fbdae7e6d805 236 virtual int dir_close(mbed::fs_dir_t dir);
borlanic 0:fbdae7e6d805 237
borlanic 0:fbdae7e6d805 238 /** Read the next directory entry
borlanic 0:fbdae7e6d805 239 *
borlanic 0:fbdae7e6d805 240 * @param dir Dir handle
borlanic 0:fbdae7e6d805 241 * @param ent The directory entry to fill out
borlanic 0:fbdae7e6d805 242 * @return 1 on reading a filename, 0 at end of directory, negative error on failure
borlanic 0:fbdae7e6d805 243 */
borlanic 0:fbdae7e6d805 244 virtual ssize_t dir_read(mbed::fs_dir_t dir, struct dirent *ent);
borlanic 0:fbdae7e6d805 245
borlanic 0:fbdae7e6d805 246 /** Set the current position of the directory
borlanic 0:fbdae7e6d805 247 *
borlanic 0:fbdae7e6d805 248 * @param dir Dir handle
borlanic 0:fbdae7e6d805 249 * @param offset Offset of the location to seek to,
borlanic 0:fbdae7e6d805 250 * must be a value returned from dir_tell
borlanic 0:fbdae7e6d805 251 */
borlanic 0:fbdae7e6d805 252 virtual void dir_seek(mbed::fs_dir_t dir, off_t offset);
borlanic 0:fbdae7e6d805 253
borlanic 0:fbdae7e6d805 254 /** Get the current position of the directory
borlanic 0:fbdae7e6d805 255 *
borlanic 0:fbdae7e6d805 256 * @param dir Dir handle
borlanic 0:fbdae7e6d805 257 * @return Position of the directory that can be passed to dir_rewind
borlanic 0:fbdae7e6d805 258 */
borlanic 0:fbdae7e6d805 259 virtual off_t dir_tell(mbed::fs_dir_t dir);
borlanic 0:fbdae7e6d805 260
borlanic 0:fbdae7e6d805 261 /** Rewind the current position to the beginning of the directory
borlanic 0:fbdae7e6d805 262 *
borlanic 0:fbdae7e6d805 263 * @param dir Dir handle
borlanic 0:fbdae7e6d805 264 */
borlanic 0:fbdae7e6d805 265 virtual void dir_rewind(mbed::fs_dir_t dir);
borlanic 0:fbdae7e6d805 266
borlanic 0:fbdae7e6d805 267 private:
borlanic 0:fbdae7e6d805 268 lfs_t _lfs; // _the actual filesystem
borlanic 0:fbdae7e6d805 269 struct lfs_config _config;
borlanic 0:fbdae7e6d805 270 BlockDevice *_bd; // the block device
borlanic 0:fbdae7e6d805 271
borlanic 0:fbdae7e6d805 272 // default parameters
borlanic 0:fbdae7e6d805 273 const lfs_size_t _read_size;
borlanic 0:fbdae7e6d805 274 const lfs_size_t _prog_size;
borlanic 0:fbdae7e6d805 275 const lfs_size_t _block_size;
borlanic 0:fbdae7e6d805 276 const lfs_size_t _lookahead;
borlanic 0:fbdae7e6d805 277
borlanic 0:fbdae7e6d805 278 // thread-safe locking
borlanic 0:fbdae7e6d805 279 PlatformMutex _mutex;
borlanic 0:fbdae7e6d805 280 };
borlanic 0:fbdae7e6d805 281
borlanic 0:fbdae7e6d805 282
borlanic 0:fbdae7e6d805 283 #endif