Webserver+3d print
common/fs_port_fatfs.c@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file fs_port_fatfs.c |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief File system abstraction layer (FatFs) |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 10 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 11 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 12 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 13 | * |
Sergunb | 0:8918a71cdbe9 | 14 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 17 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 18 | * |
Sergunb | 0:8918a71cdbe9 | 19 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 20 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 21 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 22 | * |
Sergunb | 0:8918a71cdbe9 | 23 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 24 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 25 | **/ |
Sergunb | 0:8918a71cdbe9 | 26 | |
Sergunb | 0:8918a71cdbe9 | 27 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 28 | #include <string.h> |
Sergunb | 0:8918a71cdbe9 | 29 | #include "fs_port.h" |
Sergunb | 0:8918a71cdbe9 | 30 | #include "error.h" |
Sergunb | 0:8918a71cdbe9 | 31 | #include "debug.h" |
Sergunb | 0:8918a71cdbe9 | 32 | |
Sergunb | 0:8918a71cdbe9 | 33 | //FatFs specific headers |
Sergunb | 0:8918a71cdbe9 | 34 | #include "ff.h" |
Sergunb | 0:8918a71cdbe9 | 35 | |
Sergunb | 0:8918a71cdbe9 | 36 | //File system objects |
Sergunb | 0:8918a71cdbe9 | 37 | static FATFS fs; |
Sergunb | 0:8918a71cdbe9 | 38 | static FIL fileTable[FS_MAX_FILES]; |
Sergunb | 0:8918a71cdbe9 | 39 | static DIR dirTable[FS_MAX_DIRS]; |
Sergunb | 0:8918a71cdbe9 | 40 | |
Sergunb | 0:8918a71cdbe9 | 41 | //Mutex that protects critical sections |
Sergunb | 0:8918a71cdbe9 | 42 | static OsMutex fsMutex; |
Sergunb | 0:8918a71cdbe9 | 43 | |
Sergunb | 0:8918a71cdbe9 | 44 | |
Sergunb | 0:8918a71cdbe9 | 45 | /** |
Sergunb | 0:8918a71cdbe9 | 46 | * @brief File system initialization |
Sergunb | 0:8918a71cdbe9 | 47 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 48 | **/ |
Sergunb | 0:8918a71cdbe9 | 49 | |
Sergunb | 0:8918a71cdbe9 | 50 | error_t fsInit(void) |
Sergunb | 0:8918a71cdbe9 | 51 | { |
Sergunb | 0:8918a71cdbe9 | 52 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 53 | |
Sergunb | 0:8918a71cdbe9 | 54 | //Clear file system objects |
Sergunb | 0:8918a71cdbe9 | 55 | memset(fileTable, 0, sizeof(fileTable)); |
Sergunb | 0:8918a71cdbe9 | 56 | memset(dirTable, 0, sizeof(dirTable)); |
Sergunb | 0:8918a71cdbe9 | 57 | |
Sergunb | 0:8918a71cdbe9 | 58 | //Create a mutex to protect critical sections |
Sergunb | 0:8918a71cdbe9 | 59 | if(!osCreateMutex(&fsMutex)) |
Sergunb | 0:8918a71cdbe9 | 60 | { |
Sergunb | 0:8918a71cdbe9 | 61 | //Failed to create mutex |
Sergunb | 0:8918a71cdbe9 | 62 | return ERROR_OUT_OF_RESOURCES; |
Sergunb | 0:8918a71cdbe9 | 63 | } |
Sergunb | 0:8918a71cdbe9 | 64 | |
Sergunb | 0:8918a71cdbe9 | 65 | //Revision 0.09b or lower? |
Sergunb | 0:8918a71cdbe9 | 66 | #if (_FATFS == 124 || _FATFS == 126 || _FATFS == 8085 || _FATFS == 8255 || \ |
Sergunb | 0:8918a71cdbe9 | 67 | _FATFS == 8237 || _FATFS == 6502 || _FATFS == 4004 || _FATFS == 82786) |
Sergunb | 0:8918a71cdbe9 | 68 | //Mount file system |
Sergunb | 0:8918a71cdbe9 | 69 | res = f_mount(0, &fs); |
Sergunb | 0:8918a71cdbe9 | 70 | //Revision 0.10 or higher? |
Sergunb | 0:8918a71cdbe9 | 71 | #else |
Sergunb | 0:8918a71cdbe9 | 72 | //Mount file system |
Sergunb | 0:8918a71cdbe9 | 73 | res = f_mount(&fs, "", 1); |
Sergunb | 0:8918a71cdbe9 | 74 | #endif |
Sergunb | 0:8918a71cdbe9 | 75 | |
Sergunb | 0:8918a71cdbe9 | 76 | //Failed to mount file system? |
Sergunb | 0:8918a71cdbe9 | 77 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 78 | { |
Sergunb | 0:8918a71cdbe9 | 79 | //Clean up side effects |
Sergunb | 0:8918a71cdbe9 | 80 | osDeleteMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 81 | //Report an error |
Sergunb | 0:8918a71cdbe9 | 82 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 83 | } |
Sergunb | 0:8918a71cdbe9 | 84 | |
Sergunb | 0:8918a71cdbe9 | 85 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 86 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 87 | } |
Sergunb | 0:8918a71cdbe9 | 88 | |
Sergunb | 0:8918a71cdbe9 | 89 | |
Sergunb | 0:8918a71cdbe9 | 90 | /** |
Sergunb | 0:8918a71cdbe9 | 91 | * @brief Check whether a file exists |
Sergunb | 0:8918a71cdbe9 | 92 | * @param[in] path NULL-terminated string specifying the filename |
Sergunb | 0:8918a71cdbe9 | 93 | * @return The function returns TRUE if the file exists. Otherwise FALSE is returned |
Sergunb | 0:8918a71cdbe9 | 94 | **/ |
Sergunb | 0:8918a71cdbe9 | 95 | |
Sergunb | 0:8918a71cdbe9 | 96 | bool_t fsFileExists(const char_t *path) |
Sergunb | 0:8918a71cdbe9 | 97 | { |
Sergunb | 0:8918a71cdbe9 | 98 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 99 | FILINFO fno; |
Sergunb | 0:8918a71cdbe9 | 100 | |
Sergunb | 0:8918a71cdbe9 | 101 | //Long filename support? |
Sergunb | 0:8918a71cdbe9 | 102 | #if (_USE_LFN != 0) |
Sergunb | 0:8918a71cdbe9 | 103 | fno.lfname = NULL; |
Sergunb | 0:8918a71cdbe9 | 104 | fno.lfsize = 0; |
Sergunb | 0:8918a71cdbe9 | 105 | #endif |
Sergunb | 0:8918a71cdbe9 | 106 | |
Sergunb | 0:8918a71cdbe9 | 107 | //Make sure the pathname is valid |
Sergunb | 0:8918a71cdbe9 | 108 | if(path == NULL) |
Sergunb | 0:8918a71cdbe9 | 109 | return FALSE; |
Sergunb | 0:8918a71cdbe9 | 110 | |
Sergunb | 0:8918a71cdbe9 | 111 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 112 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 113 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 114 | #endif |
Sergunb | 0:8918a71cdbe9 | 115 | |
Sergunb | 0:8918a71cdbe9 | 116 | //Check whether the file exists |
Sergunb | 0:8918a71cdbe9 | 117 | res = f_stat(path, &fno); |
Sergunb | 0:8918a71cdbe9 | 118 | |
Sergunb | 0:8918a71cdbe9 | 119 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 120 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 121 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 122 | #endif |
Sergunb | 0:8918a71cdbe9 | 123 | |
Sergunb | 0:8918a71cdbe9 | 124 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 125 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 126 | return FALSE; |
Sergunb | 0:8918a71cdbe9 | 127 | |
Sergunb | 0:8918a71cdbe9 | 128 | //Valid file? |
Sergunb | 0:8918a71cdbe9 | 129 | if(fno.fattrib & AM_DIR) |
Sergunb | 0:8918a71cdbe9 | 130 | return FALSE; |
Sergunb | 0:8918a71cdbe9 | 131 | else |
Sergunb | 0:8918a71cdbe9 | 132 | return TRUE; |
Sergunb | 0:8918a71cdbe9 | 133 | } |
Sergunb | 0:8918a71cdbe9 | 134 | |
Sergunb | 0:8918a71cdbe9 | 135 | |
Sergunb | 0:8918a71cdbe9 | 136 | /** |
Sergunb | 0:8918a71cdbe9 | 137 | * @brief Retrieve the size of the specified file |
Sergunb | 0:8918a71cdbe9 | 138 | * @param[in] path NULL-terminated string specifying the filename |
Sergunb | 0:8918a71cdbe9 | 139 | * @param[out] size Size of the file in bytes |
Sergunb | 0:8918a71cdbe9 | 140 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 141 | **/ |
Sergunb | 0:8918a71cdbe9 | 142 | |
Sergunb | 0:8918a71cdbe9 | 143 | error_t fsGetFileSize(const char_t *path, uint32_t *size) |
Sergunb | 0:8918a71cdbe9 | 144 | { |
Sergunb | 0:8918a71cdbe9 | 145 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 146 | FILINFO fno; |
Sergunb | 0:8918a71cdbe9 | 147 | |
Sergunb | 0:8918a71cdbe9 | 148 | //Long filename support? |
Sergunb | 0:8918a71cdbe9 | 149 | #if (_USE_LFN != 0) |
Sergunb | 0:8918a71cdbe9 | 150 | fno.lfname = NULL; |
Sergunb | 0:8918a71cdbe9 | 151 | fno.lfsize = 0; |
Sergunb | 0:8918a71cdbe9 | 152 | #endif |
Sergunb | 0:8918a71cdbe9 | 153 | |
Sergunb | 0:8918a71cdbe9 | 154 | //Check parameters |
Sergunb | 0:8918a71cdbe9 | 155 | if(path == NULL || size == NULL) |
Sergunb | 0:8918a71cdbe9 | 156 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 157 | |
Sergunb | 0:8918a71cdbe9 | 158 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 159 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 160 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 161 | #endif |
Sergunb | 0:8918a71cdbe9 | 162 | |
Sergunb | 0:8918a71cdbe9 | 163 | //Retrieve information about the specified file |
Sergunb | 0:8918a71cdbe9 | 164 | res = f_stat(path, &fno); |
Sergunb | 0:8918a71cdbe9 | 165 | |
Sergunb | 0:8918a71cdbe9 | 166 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 167 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 168 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 169 | #endif |
Sergunb | 0:8918a71cdbe9 | 170 | |
Sergunb | 0:8918a71cdbe9 | 171 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 172 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 173 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 174 | //Valid file? |
Sergunb | 0:8918a71cdbe9 | 175 | if(fno.fattrib & AM_DIR) |
Sergunb | 0:8918a71cdbe9 | 176 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 177 | |
Sergunb | 0:8918a71cdbe9 | 178 | //Return the size of the file |
Sergunb | 0:8918a71cdbe9 | 179 | *size = fno.fsize; |
Sergunb | 0:8918a71cdbe9 | 180 | |
Sergunb | 0:8918a71cdbe9 | 181 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 182 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 183 | } |
Sergunb | 0:8918a71cdbe9 | 184 | |
Sergunb | 0:8918a71cdbe9 | 185 | |
Sergunb | 0:8918a71cdbe9 | 186 | /** |
Sergunb | 0:8918a71cdbe9 | 187 | * @brief Rename the specified file |
Sergunb | 0:8918a71cdbe9 | 188 | * @param[in] oldPath NULL-terminated string specifying the pathname of the file to be renamed |
Sergunb | 0:8918a71cdbe9 | 189 | * @param[in] newPath NULL-terminated string specifying the new filename |
Sergunb | 0:8918a71cdbe9 | 190 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 191 | **/ |
Sergunb | 0:8918a71cdbe9 | 192 | |
Sergunb | 0:8918a71cdbe9 | 193 | error_t fsRenameFile(const char_t *oldPath, const char_t *newPath) |
Sergunb | 0:8918a71cdbe9 | 194 | { |
Sergunb | 0:8918a71cdbe9 | 195 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 196 | |
Sergunb | 0:8918a71cdbe9 | 197 | //Check parameters |
Sergunb | 0:8918a71cdbe9 | 198 | if(oldPath == NULL || newPath == NULL) |
Sergunb | 0:8918a71cdbe9 | 199 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 200 | |
Sergunb | 0:8918a71cdbe9 | 201 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 202 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 203 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 204 | #endif |
Sergunb | 0:8918a71cdbe9 | 205 | |
Sergunb | 0:8918a71cdbe9 | 206 | //Rename the specified file |
Sergunb | 0:8918a71cdbe9 | 207 | res = f_rename(oldPath, newPath); |
Sergunb | 0:8918a71cdbe9 | 208 | |
Sergunb | 0:8918a71cdbe9 | 209 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 210 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 211 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 212 | #endif |
Sergunb | 0:8918a71cdbe9 | 213 | |
Sergunb | 0:8918a71cdbe9 | 214 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 215 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 216 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 217 | |
Sergunb | 0:8918a71cdbe9 | 218 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 219 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 220 | } |
Sergunb | 0:8918a71cdbe9 | 221 | |
Sergunb | 0:8918a71cdbe9 | 222 | |
Sergunb | 0:8918a71cdbe9 | 223 | /** |
Sergunb | 0:8918a71cdbe9 | 224 | * @brief Delete a file |
Sergunb | 0:8918a71cdbe9 | 225 | * @param[in] path NULL-terminated string specifying the filename |
Sergunb | 0:8918a71cdbe9 | 226 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 227 | **/ |
Sergunb | 0:8918a71cdbe9 | 228 | |
Sergunb | 0:8918a71cdbe9 | 229 | error_t fsDeleteFile(const char_t *path) |
Sergunb | 0:8918a71cdbe9 | 230 | { |
Sergunb | 0:8918a71cdbe9 | 231 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 232 | |
Sergunb | 0:8918a71cdbe9 | 233 | //Make sure the pathname is valid |
Sergunb | 0:8918a71cdbe9 | 234 | if(path == NULL) |
Sergunb | 0:8918a71cdbe9 | 235 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 236 | |
Sergunb | 0:8918a71cdbe9 | 237 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 238 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 239 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 240 | #endif |
Sergunb | 0:8918a71cdbe9 | 241 | |
Sergunb | 0:8918a71cdbe9 | 242 | //Delete the specified file |
Sergunb | 0:8918a71cdbe9 | 243 | res = f_unlink(path); |
Sergunb | 0:8918a71cdbe9 | 244 | |
Sergunb | 0:8918a71cdbe9 | 245 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 246 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 247 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 248 | #endif |
Sergunb | 0:8918a71cdbe9 | 249 | |
Sergunb | 0:8918a71cdbe9 | 250 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 251 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 252 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 253 | |
Sergunb | 0:8918a71cdbe9 | 254 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 255 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 256 | } |
Sergunb | 0:8918a71cdbe9 | 257 | |
Sergunb | 0:8918a71cdbe9 | 258 | |
Sergunb | 0:8918a71cdbe9 | 259 | /** |
Sergunb | 0:8918a71cdbe9 | 260 | * @brief Open the specified file for reading or writing |
Sergunb | 0:8918a71cdbe9 | 261 | * @param[in] path NULL-terminated string specifying the filename |
Sergunb | 0:8918a71cdbe9 | 262 | * @param[in] mode Type of access permitted (FS_FILE_MODE_READ, |
Sergunb | 0:8918a71cdbe9 | 263 | * FS_FILE_MODE_WRITE or FS_FILE_MODE_CREATE) |
Sergunb | 0:8918a71cdbe9 | 264 | * @return File handle |
Sergunb | 0:8918a71cdbe9 | 265 | **/ |
Sergunb | 0:8918a71cdbe9 | 266 | |
Sergunb | 0:8918a71cdbe9 | 267 | FsFile *fsOpenFile(const char_t *path, uint_t mode) |
Sergunb | 0:8918a71cdbe9 | 268 | { |
Sergunb | 0:8918a71cdbe9 | 269 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 270 | uint_t flags; |
Sergunb | 0:8918a71cdbe9 | 271 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 272 | |
Sergunb | 0:8918a71cdbe9 | 273 | //File pointer |
Sergunb | 0:8918a71cdbe9 | 274 | FsFile *file = NULL; |
Sergunb | 0:8918a71cdbe9 | 275 | |
Sergunb | 0:8918a71cdbe9 | 276 | //Make sure the pathname is valid |
Sergunb | 0:8918a71cdbe9 | 277 | if(path == NULL) |
Sergunb | 0:8918a71cdbe9 | 278 | return NULL; |
Sergunb | 0:8918a71cdbe9 | 279 | |
Sergunb | 0:8918a71cdbe9 | 280 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 281 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 282 | |
Sergunb | 0:8918a71cdbe9 | 283 | //Loop through the file objects |
Sergunb | 0:8918a71cdbe9 | 284 | for(i = 0; i < FS_MAX_FILES; i++) |
Sergunb | 0:8918a71cdbe9 | 285 | { |
Sergunb | 0:8918a71cdbe9 | 286 | //Unused file object found? |
Sergunb | 0:8918a71cdbe9 | 287 | if(fileTable[i].fs == NULL) |
Sergunb | 0:8918a71cdbe9 | 288 | { |
Sergunb | 0:8918a71cdbe9 | 289 | //Default access mode |
Sergunb | 0:8918a71cdbe9 | 290 | flags = 0; |
Sergunb | 0:8918a71cdbe9 | 291 | |
Sergunb | 0:8918a71cdbe9 | 292 | //Check access mode |
Sergunb | 0:8918a71cdbe9 | 293 | if(mode & FS_FILE_MODE_READ) |
Sergunb | 0:8918a71cdbe9 | 294 | flags |= FA_READ; |
Sergunb | 0:8918a71cdbe9 | 295 | if(mode & FS_FILE_MODE_WRITE) |
Sergunb | 0:8918a71cdbe9 | 296 | flags |= FA_WRITE; |
Sergunb | 0:8918a71cdbe9 | 297 | if(mode & FS_FILE_MODE_CREATE) |
Sergunb | 0:8918a71cdbe9 | 298 | flags |= FA_OPEN_ALWAYS; |
Sergunb | 0:8918a71cdbe9 | 299 | if(mode & FS_FILE_MODE_TRUNC) |
Sergunb | 0:8918a71cdbe9 | 300 | flags |= FA_CREATE_ALWAYS; |
Sergunb | 0:8918a71cdbe9 | 301 | |
Sergunb | 0:8918a71cdbe9 | 302 | //Open the specified file |
Sergunb | 0:8918a71cdbe9 | 303 | res = f_open(&fileTable[i], path, flags); |
Sergunb | 0:8918a71cdbe9 | 304 | |
Sergunb | 0:8918a71cdbe9 | 305 | //Check status code |
Sergunb | 0:8918a71cdbe9 | 306 | if(res == FR_OK) |
Sergunb | 0:8918a71cdbe9 | 307 | file = &fileTable[i]; |
Sergunb | 0:8918a71cdbe9 | 308 | |
Sergunb | 0:8918a71cdbe9 | 309 | //Stop immediately |
Sergunb | 0:8918a71cdbe9 | 310 | break; |
Sergunb | 0:8918a71cdbe9 | 311 | } |
Sergunb | 0:8918a71cdbe9 | 312 | } |
Sergunb | 0:8918a71cdbe9 | 313 | |
Sergunb | 0:8918a71cdbe9 | 314 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 315 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 316 | //Return a handle to the file |
Sergunb | 0:8918a71cdbe9 | 317 | return file; |
Sergunb | 0:8918a71cdbe9 | 318 | } |
Sergunb | 0:8918a71cdbe9 | 319 | |
Sergunb | 0:8918a71cdbe9 | 320 | |
Sergunb | 0:8918a71cdbe9 | 321 | /** |
Sergunb | 0:8918a71cdbe9 | 322 | * @brief Move to specified position in file |
Sergunb | 0:8918a71cdbe9 | 323 | * @param[in] file Handle that identifies the file |
Sergunb | 0:8918a71cdbe9 | 324 | * @param[in] offset Number of bytes to move from origin |
Sergunb | 0:8918a71cdbe9 | 325 | * @param[in] origin Position used as reference for the offset (FS_SEEK_SET, |
Sergunb | 0:8918a71cdbe9 | 326 | * FS_SEEK_CUR or FS_SEEK_END) |
Sergunb | 0:8918a71cdbe9 | 327 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 328 | **/ |
Sergunb | 0:8918a71cdbe9 | 329 | |
Sergunb | 0:8918a71cdbe9 | 330 | error_t fsSeekFile(FsFile *file, int_t offset, uint_t origin) |
Sergunb | 0:8918a71cdbe9 | 331 | { |
Sergunb | 0:8918a71cdbe9 | 332 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 333 | |
Sergunb | 0:8918a71cdbe9 | 334 | //Make sure the file pointer is valid |
Sergunb | 0:8918a71cdbe9 | 335 | if(file == NULL) |
Sergunb | 0:8918a71cdbe9 | 336 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 337 | |
Sergunb | 0:8918a71cdbe9 | 338 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 339 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 340 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 341 | #endif |
Sergunb | 0:8918a71cdbe9 | 342 | |
Sergunb | 0:8918a71cdbe9 | 343 | //Offset is relative to the current file pointer position? |
Sergunb | 0:8918a71cdbe9 | 344 | if(origin == FS_SEEK_CUR) |
Sergunb | 0:8918a71cdbe9 | 345 | offset += f_tell((FIL *) file); |
Sergunb | 0:8918a71cdbe9 | 346 | //Offset is relative to the end of the file? |
Sergunb | 0:8918a71cdbe9 | 347 | else if(origin == FS_SEEK_END) |
Sergunb | 0:8918a71cdbe9 | 348 | offset += f_size((FIL *) file); |
Sergunb | 0:8918a71cdbe9 | 349 | |
Sergunb | 0:8918a71cdbe9 | 350 | //Move read/write pointer |
Sergunb | 0:8918a71cdbe9 | 351 | res = f_lseek((FIL *) file, offset); |
Sergunb | 0:8918a71cdbe9 | 352 | |
Sergunb | 0:8918a71cdbe9 | 353 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 354 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 355 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 356 | #endif |
Sergunb | 0:8918a71cdbe9 | 357 | |
Sergunb | 0:8918a71cdbe9 | 358 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 359 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 360 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 361 | |
Sergunb | 0:8918a71cdbe9 | 362 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 363 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 364 | } |
Sergunb | 0:8918a71cdbe9 | 365 | |
Sergunb | 0:8918a71cdbe9 | 366 | |
Sergunb | 0:8918a71cdbe9 | 367 | /** |
Sergunb | 0:8918a71cdbe9 | 368 | * @brief Write data to the specified file |
Sergunb | 0:8918a71cdbe9 | 369 | * @param[in] file Handle that identifies the file to be written |
Sergunb | 0:8918a71cdbe9 | 370 | * @param[in] data Pointer to a buffer containing the data to be written |
Sergunb | 0:8918a71cdbe9 | 371 | * @param[in] length Number of data bytes to write |
Sergunb | 0:8918a71cdbe9 | 372 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 373 | **/ |
Sergunb | 0:8918a71cdbe9 | 374 | |
Sergunb | 0:8918a71cdbe9 | 375 | error_t fsWriteFile(FsFile *file, void *data, size_t length) |
Sergunb | 0:8918a71cdbe9 | 376 | { |
Sergunb | 0:8918a71cdbe9 | 377 | UINT n; |
Sergunb | 0:8918a71cdbe9 | 378 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 379 | |
Sergunb | 0:8918a71cdbe9 | 380 | //Make sure the file pointer is valid |
Sergunb | 0:8918a71cdbe9 | 381 | if(file == NULL) |
Sergunb | 0:8918a71cdbe9 | 382 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 383 | |
Sergunb | 0:8918a71cdbe9 | 384 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 385 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 386 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 387 | #endif |
Sergunb | 0:8918a71cdbe9 | 388 | |
Sergunb | 0:8918a71cdbe9 | 389 | //Write data |
Sergunb | 0:8918a71cdbe9 | 390 | res = f_write((FIL *) file, data, length, &n); |
Sergunb | 0:8918a71cdbe9 | 391 | |
Sergunb | 0:8918a71cdbe9 | 392 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 393 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 394 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 395 | #endif |
Sergunb | 0:8918a71cdbe9 | 396 | |
Sergunb | 0:8918a71cdbe9 | 397 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 398 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 399 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 400 | |
Sergunb | 0:8918a71cdbe9 | 401 | //Sanity check |
Sergunb | 0:8918a71cdbe9 | 402 | if(n != length) |
Sergunb | 0:8918a71cdbe9 | 403 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 404 | |
Sergunb | 0:8918a71cdbe9 | 405 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 406 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 407 | } |
Sergunb | 0:8918a71cdbe9 | 408 | |
Sergunb | 0:8918a71cdbe9 | 409 | |
Sergunb | 0:8918a71cdbe9 | 410 | /** |
Sergunb | 0:8918a71cdbe9 | 411 | * @brief Read data from the specified file |
Sergunb | 0:8918a71cdbe9 | 412 | * @param[in] file Handle that identifies the file to be read |
Sergunb | 0:8918a71cdbe9 | 413 | * @param[in] data Pointer to the buffer where to copy the data |
Sergunb | 0:8918a71cdbe9 | 414 | * @param[in] size Size of the buffer, in bytes |
Sergunb | 0:8918a71cdbe9 | 415 | * @param[out] length Number of data bytes that have been read |
Sergunb | 0:8918a71cdbe9 | 416 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 417 | **/ |
Sergunb | 0:8918a71cdbe9 | 418 | |
Sergunb | 0:8918a71cdbe9 | 419 | error_t fsReadFile(FsFile *file, void *data, size_t size, size_t *length) |
Sergunb | 0:8918a71cdbe9 | 420 | { |
Sergunb | 0:8918a71cdbe9 | 421 | UINT n; |
Sergunb | 0:8918a71cdbe9 | 422 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 423 | |
Sergunb | 0:8918a71cdbe9 | 424 | //No data has been read yet |
Sergunb | 0:8918a71cdbe9 | 425 | *length = 0; |
Sergunb | 0:8918a71cdbe9 | 426 | |
Sergunb | 0:8918a71cdbe9 | 427 | //Make sure the file pointer is valid |
Sergunb | 0:8918a71cdbe9 | 428 | if(file == NULL) |
Sergunb | 0:8918a71cdbe9 | 429 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 430 | |
Sergunb | 0:8918a71cdbe9 | 431 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 432 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 433 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 434 | #endif |
Sergunb | 0:8918a71cdbe9 | 435 | |
Sergunb | 0:8918a71cdbe9 | 436 | //Read data |
Sergunb | 0:8918a71cdbe9 | 437 | res = f_read((FIL *) file, data, size, &n); |
Sergunb | 0:8918a71cdbe9 | 438 | |
Sergunb | 0:8918a71cdbe9 | 439 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 440 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 441 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 442 | #endif |
Sergunb | 0:8918a71cdbe9 | 443 | |
Sergunb | 0:8918a71cdbe9 | 444 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 445 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 446 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 447 | |
Sergunb | 0:8918a71cdbe9 | 448 | //End of file? |
Sergunb | 0:8918a71cdbe9 | 449 | if(!n) |
Sergunb | 0:8918a71cdbe9 | 450 | return ERROR_END_OF_FILE; |
Sergunb | 0:8918a71cdbe9 | 451 | |
Sergunb | 0:8918a71cdbe9 | 452 | //Total number of data that have been read |
Sergunb | 0:8918a71cdbe9 | 453 | *length = n; |
Sergunb | 0:8918a71cdbe9 | 454 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 455 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 456 | } |
Sergunb | 0:8918a71cdbe9 | 457 | |
Sergunb | 0:8918a71cdbe9 | 458 | |
Sergunb | 0:8918a71cdbe9 | 459 | /** |
Sergunb | 0:8918a71cdbe9 | 460 | * @brief Close a file |
Sergunb | 0:8918a71cdbe9 | 461 | * @param[in] file Handle that identifies the file to be closed |
Sergunb | 0:8918a71cdbe9 | 462 | **/ |
Sergunb | 0:8918a71cdbe9 | 463 | |
Sergunb | 0:8918a71cdbe9 | 464 | void fsCloseFile(FsFile *file) |
Sergunb | 0:8918a71cdbe9 | 465 | { |
Sergunb | 0:8918a71cdbe9 | 466 | //Make sure the file pointer is valid |
Sergunb | 0:8918a71cdbe9 | 467 | if(file != NULL) |
Sergunb | 0:8918a71cdbe9 | 468 | { |
Sergunb | 0:8918a71cdbe9 | 469 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 470 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 471 | |
Sergunb | 0:8918a71cdbe9 | 472 | //Close the specified file |
Sergunb | 0:8918a71cdbe9 | 473 | f_close((FIL *) file); |
Sergunb | 0:8918a71cdbe9 | 474 | //Mark the corresponding entry as free |
Sergunb | 0:8918a71cdbe9 | 475 | ((FIL *) file)->fs = NULL; |
Sergunb | 0:8918a71cdbe9 | 476 | |
Sergunb | 0:8918a71cdbe9 | 477 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 478 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 479 | } |
Sergunb | 0:8918a71cdbe9 | 480 | } |
Sergunb | 0:8918a71cdbe9 | 481 | |
Sergunb | 0:8918a71cdbe9 | 482 | |
Sergunb | 0:8918a71cdbe9 | 483 | /** |
Sergunb | 0:8918a71cdbe9 | 484 | * @brief Check whether a directory exists |
Sergunb | 0:8918a71cdbe9 | 485 | * @param[in] path NULL-terminated string specifying the directory path |
Sergunb | 0:8918a71cdbe9 | 486 | * @return The function returns TRUE if the directory exists. Otherwise FALSE is returned |
Sergunb | 0:8918a71cdbe9 | 487 | **/ |
Sergunb | 0:8918a71cdbe9 | 488 | |
Sergunb | 0:8918a71cdbe9 | 489 | bool_t fsDirExists(const char_t *path) |
Sergunb | 0:8918a71cdbe9 | 490 | { |
Sergunb | 0:8918a71cdbe9 | 491 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 492 | FILINFO fno; |
Sergunb | 0:8918a71cdbe9 | 493 | |
Sergunb | 0:8918a71cdbe9 | 494 | //Long filename support? |
Sergunb | 0:8918a71cdbe9 | 495 | #if (_USE_LFN != 0) |
Sergunb | 0:8918a71cdbe9 | 496 | fno.lfname = NULL; |
Sergunb | 0:8918a71cdbe9 | 497 | fno.lfsize = 0; |
Sergunb | 0:8918a71cdbe9 | 498 | #endif |
Sergunb | 0:8918a71cdbe9 | 499 | |
Sergunb | 0:8918a71cdbe9 | 500 | //Make sure the pathname is valid |
Sergunb | 0:8918a71cdbe9 | 501 | if(path == NULL) |
Sergunb | 0:8918a71cdbe9 | 502 | return FALSE; |
Sergunb | 0:8918a71cdbe9 | 503 | |
Sergunb | 0:8918a71cdbe9 | 504 | //Root directory? |
Sergunb | 0:8918a71cdbe9 | 505 | if(!strcmp(path, "/")) |
Sergunb | 0:8918a71cdbe9 | 506 | return TRUE; |
Sergunb | 0:8918a71cdbe9 | 507 | |
Sergunb | 0:8918a71cdbe9 | 508 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 509 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 510 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 511 | #endif |
Sergunb | 0:8918a71cdbe9 | 512 | |
Sergunb | 0:8918a71cdbe9 | 513 | //Check whether the file exists |
Sergunb | 0:8918a71cdbe9 | 514 | res = f_stat(path, &fno); |
Sergunb | 0:8918a71cdbe9 | 515 | |
Sergunb | 0:8918a71cdbe9 | 516 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 517 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 518 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 519 | #endif |
Sergunb | 0:8918a71cdbe9 | 520 | |
Sergunb | 0:8918a71cdbe9 | 521 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 522 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 523 | return FALSE; |
Sergunb | 0:8918a71cdbe9 | 524 | |
Sergunb | 0:8918a71cdbe9 | 525 | //Valid directory? |
Sergunb | 0:8918a71cdbe9 | 526 | if(fno.fattrib & AM_DIR) |
Sergunb | 0:8918a71cdbe9 | 527 | return TRUE; |
Sergunb | 0:8918a71cdbe9 | 528 | else |
Sergunb | 0:8918a71cdbe9 | 529 | return FALSE; |
Sergunb | 0:8918a71cdbe9 | 530 | } |
Sergunb | 0:8918a71cdbe9 | 531 | |
Sergunb | 0:8918a71cdbe9 | 532 | |
Sergunb | 0:8918a71cdbe9 | 533 | /** |
Sergunb | 0:8918a71cdbe9 | 534 | * @brief Create a directory |
Sergunb | 0:8918a71cdbe9 | 535 | * @param[in] path NULL-terminated string specifying the directory path |
Sergunb | 0:8918a71cdbe9 | 536 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 537 | **/ |
Sergunb | 0:8918a71cdbe9 | 538 | |
Sergunb | 0:8918a71cdbe9 | 539 | error_t fsCreateDir(const char_t *path) |
Sergunb | 0:8918a71cdbe9 | 540 | { |
Sergunb | 0:8918a71cdbe9 | 541 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 542 | |
Sergunb | 0:8918a71cdbe9 | 543 | //Make sure the pathname is valid |
Sergunb | 0:8918a71cdbe9 | 544 | if(path == NULL) |
Sergunb | 0:8918a71cdbe9 | 545 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 546 | |
Sergunb | 0:8918a71cdbe9 | 547 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 548 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 549 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 550 | #endif |
Sergunb | 0:8918a71cdbe9 | 551 | |
Sergunb | 0:8918a71cdbe9 | 552 | //Create a new directory |
Sergunb | 0:8918a71cdbe9 | 553 | res = f_mkdir(path); |
Sergunb | 0:8918a71cdbe9 | 554 | |
Sergunb | 0:8918a71cdbe9 | 555 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 556 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 557 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 558 | #endif |
Sergunb | 0:8918a71cdbe9 | 559 | |
Sergunb | 0:8918a71cdbe9 | 560 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 561 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 562 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 563 | |
Sergunb | 0:8918a71cdbe9 | 564 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 565 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 566 | } |
Sergunb | 0:8918a71cdbe9 | 567 | |
Sergunb | 0:8918a71cdbe9 | 568 | |
Sergunb | 0:8918a71cdbe9 | 569 | /** |
Sergunb | 0:8918a71cdbe9 | 570 | * @brief Remove a directory |
Sergunb | 0:8918a71cdbe9 | 571 | * @param[in] path NULL-terminated string specifying the directory path |
Sergunb | 0:8918a71cdbe9 | 572 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 573 | **/ |
Sergunb | 0:8918a71cdbe9 | 574 | |
Sergunb | 0:8918a71cdbe9 | 575 | error_t fsRemoveDir(const char_t *path) |
Sergunb | 0:8918a71cdbe9 | 576 | { |
Sergunb | 0:8918a71cdbe9 | 577 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 578 | |
Sergunb | 0:8918a71cdbe9 | 579 | //Make sure the pathname is valid |
Sergunb | 0:8918a71cdbe9 | 580 | if(path == NULL) |
Sergunb | 0:8918a71cdbe9 | 581 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 582 | |
Sergunb | 0:8918a71cdbe9 | 583 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 584 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 585 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 586 | #endif |
Sergunb | 0:8918a71cdbe9 | 587 | |
Sergunb | 0:8918a71cdbe9 | 588 | //Remove the specified directory |
Sergunb | 0:8918a71cdbe9 | 589 | res = f_unlink(path); |
Sergunb | 0:8918a71cdbe9 | 590 | |
Sergunb | 0:8918a71cdbe9 | 591 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 592 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 593 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 594 | #endif |
Sergunb | 0:8918a71cdbe9 | 595 | |
Sergunb | 0:8918a71cdbe9 | 596 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 597 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 598 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 599 | |
Sergunb | 0:8918a71cdbe9 | 600 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 601 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 602 | } |
Sergunb | 0:8918a71cdbe9 | 603 | |
Sergunb | 0:8918a71cdbe9 | 604 | |
Sergunb | 0:8918a71cdbe9 | 605 | /** |
Sergunb | 0:8918a71cdbe9 | 606 | * @brief Open a directory stream |
Sergunb | 0:8918a71cdbe9 | 607 | * @param[in] path NULL-terminated string specifying the directory path |
Sergunb | 0:8918a71cdbe9 | 608 | * @return Directory handle |
Sergunb | 0:8918a71cdbe9 | 609 | **/ |
Sergunb | 0:8918a71cdbe9 | 610 | |
Sergunb | 0:8918a71cdbe9 | 611 | FsDir *fsOpenDir(const char_t *path) |
Sergunb | 0:8918a71cdbe9 | 612 | { |
Sergunb | 0:8918a71cdbe9 | 613 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 614 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 615 | |
Sergunb | 0:8918a71cdbe9 | 616 | //Directory pointer |
Sergunb | 0:8918a71cdbe9 | 617 | FsDir *dir = NULL; |
Sergunb | 0:8918a71cdbe9 | 618 | |
Sergunb | 0:8918a71cdbe9 | 619 | //Make sure the pathname is valid |
Sergunb | 0:8918a71cdbe9 | 620 | if(path == NULL) |
Sergunb | 0:8918a71cdbe9 | 621 | return NULL; |
Sergunb | 0:8918a71cdbe9 | 622 | |
Sergunb | 0:8918a71cdbe9 | 623 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 624 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 625 | |
Sergunb | 0:8918a71cdbe9 | 626 | //Loop through the directory objects |
Sergunb | 0:8918a71cdbe9 | 627 | for(i = 0; i < FS_MAX_DIRS; i++) |
Sergunb | 0:8918a71cdbe9 | 628 | { |
Sergunb | 0:8918a71cdbe9 | 629 | //Unused directory object found? |
Sergunb | 0:8918a71cdbe9 | 630 | if(dirTable[i].fs == NULL) |
Sergunb | 0:8918a71cdbe9 | 631 | { |
Sergunb | 0:8918a71cdbe9 | 632 | //Open the specified directory |
Sergunb | 0:8918a71cdbe9 | 633 | res = f_opendir(&dirTable[i], path); |
Sergunb | 0:8918a71cdbe9 | 634 | |
Sergunb | 0:8918a71cdbe9 | 635 | //Check status code |
Sergunb | 0:8918a71cdbe9 | 636 | if(res == FR_OK) |
Sergunb | 0:8918a71cdbe9 | 637 | dir = &dirTable[i]; |
Sergunb | 0:8918a71cdbe9 | 638 | |
Sergunb | 0:8918a71cdbe9 | 639 | //Stop immediately |
Sergunb | 0:8918a71cdbe9 | 640 | break; |
Sergunb | 0:8918a71cdbe9 | 641 | } |
Sergunb | 0:8918a71cdbe9 | 642 | } |
Sergunb | 0:8918a71cdbe9 | 643 | |
Sergunb | 0:8918a71cdbe9 | 644 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 645 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 646 | //Return a handle to the directory |
Sergunb | 0:8918a71cdbe9 | 647 | return dir; |
Sergunb | 0:8918a71cdbe9 | 648 | } |
Sergunb | 0:8918a71cdbe9 | 649 | |
Sergunb | 0:8918a71cdbe9 | 650 | |
Sergunb | 0:8918a71cdbe9 | 651 | /** |
Sergunb | 0:8918a71cdbe9 | 652 | * @brief Read an entry from the specified directory stream |
Sergunb | 0:8918a71cdbe9 | 653 | * @param[in] dir Handle that identifies the directory |
Sergunb | 0:8918a71cdbe9 | 654 | * @param[out] dirEntry Pointer to a directory entry |
Sergunb | 0:8918a71cdbe9 | 655 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 656 | **/ |
Sergunb | 0:8918a71cdbe9 | 657 | |
Sergunb | 0:8918a71cdbe9 | 658 | error_t fsReadDir(FsDir *dir, FsDirEntry *dirEntry) |
Sergunb | 0:8918a71cdbe9 | 659 | { |
Sergunb | 0:8918a71cdbe9 | 660 | FRESULT res; |
Sergunb | 0:8918a71cdbe9 | 661 | FILINFO fno; |
Sergunb | 0:8918a71cdbe9 | 662 | char_t *fn; |
Sergunb | 0:8918a71cdbe9 | 663 | size_t n; |
Sergunb | 0:8918a71cdbe9 | 664 | |
Sergunb | 0:8918a71cdbe9 | 665 | //Long filename support? |
Sergunb | 0:8918a71cdbe9 | 666 | #if (_USE_LFN != 0) |
Sergunb | 0:8918a71cdbe9 | 667 | char_t lfn[_MAX_LFN + 1]; |
Sergunb | 0:8918a71cdbe9 | 668 | fno.lfname = lfn; |
Sergunb | 0:8918a71cdbe9 | 669 | fno.lfsize = sizeof(lfn); |
Sergunb | 0:8918a71cdbe9 | 670 | #endif |
Sergunb | 0:8918a71cdbe9 | 671 | |
Sergunb | 0:8918a71cdbe9 | 672 | //Make sure the directory pointer is valid |
Sergunb | 0:8918a71cdbe9 | 673 | if(dir == NULL) |
Sergunb | 0:8918a71cdbe9 | 674 | return ERROR_INVALID_PARAMETER; |
Sergunb | 0:8918a71cdbe9 | 675 | |
Sergunb | 0:8918a71cdbe9 | 676 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 677 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 678 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 679 | #endif |
Sergunb | 0:8918a71cdbe9 | 680 | |
Sergunb | 0:8918a71cdbe9 | 681 | //Read the specified directory |
Sergunb | 0:8918a71cdbe9 | 682 | res = f_readdir((DIR *) dir, &fno); |
Sergunb | 0:8918a71cdbe9 | 683 | |
Sergunb | 0:8918a71cdbe9 | 684 | #if (_FS_REENTRANT == 0) |
Sergunb | 0:8918a71cdbe9 | 685 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 686 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 687 | #endif |
Sergunb | 0:8918a71cdbe9 | 688 | |
Sergunb | 0:8918a71cdbe9 | 689 | //Any error to report? |
Sergunb | 0:8918a71cdbe9 | 690 | if(res != FR_OK) |
Sergunb | 0:8918a71cdbe9 | 691 | return ERROR_FAILURE; |
Sergunb | 0:8918a71cdbe9 | 692 | |
Sergunb | 0:8918a71cdbe9 | 693 | //End of the directory stream? |
Sergunb | 0:8918a71cdbe9 | 694 | if(fno.fname[0] == '\0') |
Sergunb | 0:8918a71cdbe9 | 695 | return ERROR_END_OF_STREAM; |
Sergunb | 0:8918a71cdbe9 | 696 | |
Sergunb | 0:8918a71cdbe9 | 697 | //Long filename support? |
Sergunb | 0:8918a71cdbe9 | 698 | #if (_USE_LFN != 0) |
Sergunb | 0:8918a71cdbe9 | 699 | fn = (*fno.lfname != '\0') ? fno.lfname : fno.fname; |
Sergunb | 0:8918a71cdbe9 | 700 | #else |
Sergunb | 0:8918a71cdbe9 | 701 | fn = fno.fname; |
Sergunb | 0:8918a71cdbe9 | 702 | #endif |
Sergunb | 0:8918a71cdbe9 | 703 | |
Sergunb | 0:8918a71cdbe9 | 704 | //File attributes |
Sergunb | 0:8918a71cdbe9 | 705 | dirEntry->attributes = fno.fattrib; |
Sergunb | 0:8918a71cdbe9 | 706 | //File size |
Sergunb | 0:8918a71cdbe9 | 707 | dirEntry->size = fno.fsize; |
Sergunb | 0:8918a71cdbe9 | 708 | //Last modified date |
Sergunb | 0:8918a71cdbe9 | 709 | dirEntry->modified.year = 1980 + ((fno.fdate >> 9) & 0x7F); |
Sergunb | 0:8918a71cdbe9 | 710 | dirEntry->modified.month = (fno.fdate >> 5) & 0x0F; |
Sergunb | 0:8918a71cdbe9 | 711 | dirEntry->modified.day = fno.fdate & 0x1F; |
Sergunb | 0:8918a71cdbe9 | 712 | dirEntry->modified.dayOfWeek = 0; |
Sergunb | 0:8918a71cdbe9 | 713 | //Last modified time |
Sergunb | 0:8918a71cdbe9 | 714 | dirEntry->modified.hours = (fno.ftime >> 11) & 0x1F; |
Sergunb | 0:8918a71cdbe9 | 715 | dirEntry->modified.minutes = (fno.ftime >> 5) & 0x3F; |
Sergunb | 0:8918a71cdbe9 | 716 | dirEntry->modified.seconds = (fno.ftime & 0x1F) * 2; |
Sergunb | 0:8918a71cdbe9 | 717 | dirEntry->modified.milliseconds = 0; |
Sergunb | 0:8918a71cdbe9 | 718 | |
Sergunb | 0:8918a71cdbe9 | 719 | //Make sure the date is valid |
Sergunb | 0:8918a71cdbe9 | 720 | dirEntry->modified.month = MAX(dirEntry->modified.month, 1); |
Sergunb | 0:8918a71cdbe9 | 721 | dirEntry->modified.month = MIN(dirEntry->modified.month, 12); |
Sergunb | 0:8918a71cdbe9 | 722 | dirEntry->modified.day = MAX(dirEntry->modified.day, 1); |
Sergunb | 0:8918a71cdbe9 | 723 | dirEntry->modified.day = MIN(dirEntry->modified.day, 31); |
Sergunb | 0:8918a71cdbe9 | 724 | |
Sergunb | 0:8918a71cdbe9 | 725 | //Retrieve the length of the file name |
Sergunb | 0:8918a71cdbe9 | 726 | n = strlen(fn); |
Sergunb | 0:8918a71cdbe9 | 727 | //Limit the number of characters to be copied |
Sergunb | 0:8918a71cdbe9 | 728 | n = MIN(n, FS_MAX_NAME_LEN); |
Sergunb | 0:8918a71cdbe9 | 729 | |
Sergunb | 0:8918a71cdbe9 | 730 | //Copy file name |
Sergunb | 0:8918a71cdbe9 | 731 | strncpy(dirEntry->name, fn, n); |
Sergunb | 0:8918a71cdbe9 | 732 | //Properly terminate the string with a NULL character |
Sergunb | 0:8918a71cdbe9 | 733 | dirEntry->name[n] = '\0'; |
Sergunb | 0:8918a71cdbe9 | 734 | |
Sergunb | 0:8918a71cdbe9 | 735 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 736 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 737 | } |
Sergunb | 0:8918a71cdbe9 | 738 | |
Sergunb | 0:8918a71cdbe9 | 739 | |
Sergunb | 0:8918a71cdbe9 | 740 | /** |
Sergunb | 0:8918a71cdbe9 | 741 | * @brief Close a directory stream |
Sergunb | 0:8918a71cdbe9 | 742 | * @param[in] dir Handle that identifies the directory to be closed |
Sergunb | 0:8918a71cdbe9 | 743 | **/ |
Sergunb | 0:8918a71cdbe9 | 744 | |
Sergunb | 0:8918a71cdbe9 | 745 | void fsCloseDir(FsDir *dir) |
Sergunb | 0:8918a71cdbe9 | 746 | { |
Sergunb | 0:8918a71cdbe9 | 747 | //Make sure the directory pointer is valid |
Sergunb | 0:8918a71cdbe9 | 748 | if(dir != NULL) |
Sergunb | 0:8918a71cdbe9 | 749 | { |
Sergunb | 0:8918a71cdbe9 | 750 | //Enter critical section |
Sergunb | 0:8918a71cdbe9 | 751 | osAcquireMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 752 | |
Sergunb | 0:8918a71cdbe9 | 753 | #if (_FATFS == 80960) |
Sergunb | 0:8918a71cdbe9 | 754 | //Close the specified directory |
Sergunb | 0:8918a71cdbe9 | 755 | f_closedir((DIR *) dir); |
Sergunb | 0:8918a71cdbe9 | 756 | #endif |
Sergunb | 0:8918a71cdbe9 | 757 | //Mark the corresponding entry as free |
Sergunb | 0:8918a71cdbe9 | 758 | ((DIR *) dir)->fs = NULL; |
Sergunb | 0:8918a71cdbe9 | 759 | |
Sergunb | 0:8918a71cdbe9 | 760 | //Leave critical section |
Sergunb | 0:8918a71cdbe9 | 761 | osReleaseMutex(&fsMutex); |
Sergunb | 0:8918a71cdbe9 | 762 | } |
Sergunb | 0:8918a71cdbe9 | 763 | } |
Sergunb | 0:8918a71cdbe9 | 764 |