Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of gr-peach-opencv-project-sd-card by
FATFileSystem.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2012 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 #ifndef MBED_FATFILESYSTEM_H 00023 #define MBED_FATFILESYSTEM_H 00024 00025 #include "FileSystem.h" 00026 #include "BlockDevice.h" 00027 #include "FileHandle.h" 00028 #include "ff.h" 00029 #include <stdint.h> 00030 #include "PlatformMutex.h" 00031 00032 using namespace mbed; 00033 00034 /** 00035 * FATFileSystem based on ChaN's Fat Filesystem library v0.8 00036 */ 00037 class FATFileSystem : public FileSystem { 00038 public: 00039 /** Lifetime of the FATFileSystem 00040 * 00041 * @param name Name to add filesystem to tree as 00042 * @param bd BlockDevice to mount, may be passed instead to mount call 00043 */ 00044 FATFileSystem(const char *name = NULL, BlockDevice *bd = NULL); 00045 virtual ~FATFileSystem(); 00046 00047 /** Formats a logical drive, FDISK partitioning rule. 00048 * 00049 * The block device to format should be mounted when this function is called. 00050 * 00051 * @param bd 00052 * This is the block device that will be formated. 00053 * 00054 * @param allocation_unit 00055 * This is the number of bytes per cluster size. The valid value is N 00056 * times the sector size. N is a power of 2 from 1 to 128 for FAT 00057 * volume and upto 16MiB for exFAT volume. If zero is given, 00058 * the default allocation unit size is selected by the underlying 00059 * filesystem, which depends on the volume size. 00060 */ 00061 static int format(BlockDevice *bd, int allocation_unit = 0); 00062 00063 /** Mounts a filesystem to a block device 00064 * 00065 * @param bd BlockDevice to mount to 00066 * @return 0 on success, negative error code on failure 00067 */ 00068 virtual int mount(BlockDevice *bd); 00069 00070 /** Mounts a filesystem to a block device 00071 * 00072 * @param bd BlockDevice to mount to 00073 * @param force Flag to force the underlying filesystem to force mounting the filesystem 00074 * @return 0 on success, negative error code on failure 00075 */ 00076 virtual int mount(BlockDevice *bd, bool force); 00077 00078 /** Unmounts a filesystem from the underlying block device 00079 * 00080 * @return 0 on success, negative error code on failure 00081 */ 00082 virtual int unmount(); 00083 00084 /** Remove a file from the filesystem. 00085 * 00086 * @param path The name of the file to remove. 00087 * @return 0 on success, negative error code on failure 00088 */ 00089 virtual int remove(const char *path); 00090 00091 /** Rename a file in the filesystem. 00092 * 00093 * @param path The name of the file to rename. 00094 * @param newpath The name to rename it to 00095 * @return 0 on success, negative error code on failure 00096 */ 00097 virtual int rename(const char *path, const char *newpath); 00098 00099 /** Store information about the file in a stat structure 00100 * 00101 * @param path The name of the file to find information about 00102 * @param st The stat buffer to write to 00103 * @return 0 on success, negative error code on failure 00104 */ 00105 virtual int stat(const char *path, struct stat *st); 00106 00107 /** Create a directory in the filesystem. 00108 * 00109 * @param path The name of the directory to create. 00110 * @param mode The permissions with which to create the directory 00111 * @return 0 on success, negative error code on failure 00112 */ 00113 virtual int mkdir(const char *path, mode_t mode); 00114 00115 protected: 00116 /** Open a file on the filesystem 00117 * 00118 * @param file Destination for the handle to a newly created file 00119 * @param path The name of the file to open 00120 * @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR, 00121 * bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND 00122 * @return 0 on success, negative error code on failure 00123 */ 00124 virtual int file_open(fs_file_t *file, const char *path, int flags); 00125 00126 /** Close a file 00127 * 00128 * @param file File handle 00129 * return 0 on success, negative error code on failure 00130 */ 00131 virtual int file_close(fs_file_t file); 00132 00133 /** Read the contents of a file into a buffer 00134 * 00135 * @param file File handle 00136 * @param buffer The buffer to read in to 00137 * @param size The number of bytes to read 00138 * @return The number of bytes read, 0 at end of file, negative error on failure 00139 */ 00140 virtual ssize_t file_read(fs_file_t file, void *buffer, size_t len); 00141 00142 /** Write the contents of a buffer to a file 00143 * 00144 * @param file File handle 00145 * @param buffer The buffer to write from 00146 * @param size The number of bytes to write 00147 * @return The number of bytes written, negative error on failure 00148 */ 00149 virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t len); 00150 00151 /** Flush any buffers associated with the file 00152 * 00153 * @param file File handle 00154 * @return 0 on success, negative error code on failure 00155 */ 00156 virtual int file_sync(fs_file_t file); 00157 00158 /** Move the file position to a given offset from from a given location 00159 * 00160 * @param file File handle 00161 * @param offset The offset from whence to move to 00162 * @param whence The start of where to seek 00163 * SEEK_SET to start from beginning of file, 00164 * SEEK_CUR to start from current position in file, 00165 * SEEK_END to start from end of file 00166 * @return The new offset of the file 00167 */ 00168 virtual off_t file_seek(fs_file_t file, off_t offset, int whence); 00169 00170 /** Get the file position of the file 00171 * 00172 * @param file File handle 00173 * @return The current offset in the file 00174 */ 00175 virtual off_t file_tell(fs_file_t file); 00176 00177 /** Get the size of the file 00178 * 00179 * @param file File handle 00180 * @return Size of the file in bytes 00181 */ 00182 virtual off_t file_size(fs_file_t file); 00183 00184 /** Open a directory on the filesystem 00185 * 00186 * @param dir Destination for the handle to the directory 00187 * @param path Name of the directory to open 00188 * @return 0 on success, negative error code on failure 00189 */ 00190 virtual int dir_open(fs_dir_t *dir, const char *path); 00191 00192 /** Close a directory 00193 * 00194 * @param dir Dir handle 00195 * return 0 on success, negative error code on failure 00196 */ 00197 virtual int dir_close(fs_dir_t dir); 00198 00199 /** Read the next directory entry 00200 * 00201 * @param dir Dir handle 00202 * @param ent The directory entry to fill out 00203 * @return 1 on reading a filename, 0 at end of directory, negative error on failure 00204 */ 00205 virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent); 00206 00207 /** Set the current position of the directory 00208 * 00209 * @param dir Dir handle 00210 * @param offset Offset of the location to seek to, 00211 * must be a value returned from dir_tell 00212 */ 00213 virtual void dir_seek(fs_dir_t dir, off_t offset); 00214 00215 /** Get the current position of the directory 00216 * 00217 * @param dir Dir handle 00218 * @return Position of the directory that can be passed to dir_rewind 00219 */ 00220 virtual off_t dir_tell(fs_dir_t dir); 00221 00222 /** Rewind the current position to the beginning of the directory 00223 * 00224 * @param dir Dir handle 00225 */ 00226 virtual void dir_rewind(fs_dir_t dir); 00227 00228 private: 00229 FATFS _fs; // Work area (file system object) for logical drive 00230 char _fsid[sizeof("0:")]; 00231 int _id; 00232 00233 protected: 00234 virtual void lock(); 00235 virtual void unlock(); 00236 }; 00237 00238 #endif 00239
Generated on Tue Jul 12 2022 14:46:36 by
