| 283 | } |
| 284 | |
| 285 | int AP_Filesystem_FATFS::open(const char *pathname, int flags, bool allow_absolute_path) |
| 286 | { |
| 287 | int fileno; |
| 288 | int fatfs_modes; |
| 289 | FAT_FILE *stream; |
| 290 | FIL *fh; |
| 291 | int res; |
| 292 | |
| 293 | FS_CHECK_ALLOWED(-1); |
| 294 | WITH_SEMAPHORE(sem); |
| 295 | |
| 296 | CHECK_REMOUNT(); |
| 297 | |
| 298 | errno = 0; |
| 299 | debug("Open %s 0x%x", pathname, flags); |
| 300 | |
| 301 | if ((flags & O_ACCMODE) == O_RDWR) { |
| 302 | fatfs_modes = FA_READ | FA_WRITE; |
| 303 | } else if ((flags & O_ACCMODE) == O_RDONLY) { |
| 304 | fatfs_modes = FA_READ; |
| 305 | } else { |
| 306 | fatfs_modes = FA_WRITE; |
| 307 | } |
| 308 | |
| 309 | if (flags & O_CREAT) { |
| 310 | if (flags & O_TRUNC) { |
| 311 | fatfs_modes |= FA_CREATE_ALWAYS; |
| 312 | } else { |
| 313 | fatfs_modes |= FA_OPEN_ALWAYS; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | fileno = new_file_descriptor(pathname); |
| 318 | |
| 319 | // checks if fileno out of bounds |
| 320 | stream = fileno_to_stream(fileno); |
| 321 | if (stream == nullptr) { |
| 322 | free_file_descriptor(fileno); |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | // fileno_to_fatfs checks for fileno out of bounds |
| 327 | fh = fileno_to_fatfs(fileno); |
| 328 | if (fh == nullptr) { |
| 329 | free_file_descriptor(fileno); |
| 330 | errno = EBADF; |
| 331 | return -1; |
| 332 | } |
| 333 | res = f_open(fh, pathname, (BYTE) (fatfs_modes & 0xff)); |
| 334 | if (res == FR_DISK_ERR && RETRY_ALLOWED()) { |
| 335 | // one retry on disk error |
| 336 | hal.scheduler->delay(100); |
| 337 | if (remount_file_system()) { |
| 338 | res = f_open(fh, pathname, (BYTE) (fatfs_modes & 0xff)); |
| 339 | } |
| 340 | } |
| 341 | if (res != FR_OK) { |
| 342 | errno = fatfs_to_errno((FRESULT)res); |
nothing calls this directly
no test coverage detected