prepend_drive_to_path(fat_ctx, &path, NULL);
int fd = get_next_fd(fat_ctx);
if (fd < 0) {
+ _lock_release(&fat_ctx->lock);
ESP_LOGE(TAG, "open: no free file descriptors");
errno = ENFILE;
- fd = -1;
- goto out;
+ return -1;
}
FRESULT res = f_open(&fat_ctx->files[fd], path, fat_mode_conv(flags));
if (res != FR_OK) {
- ESP_LOGD(TAG, "%s: fresult=%d", __func__, res);
file_cleanup(fat_ctx, fd);
+ _lock_release(&fat_ctx->lock);
+ ESP_LOGD(TAG, "%s: fresult=%d", __func__, res);
errno = fresult_to_errno(res);
- fd = -1;
- goto out;
+ return -1;
}
// O_APPEND need to be stored because it is not compatible with FA_OPEN_APPEND:
// - FA_OPEN_APPEND means to jump to the end of file only after open()
// therefore this flag is stored here (at this VFS level) in order to save
// memory.
fat_ctx->o_append[fd] = (flags & O_APPEND) == O_APPEND;
-out:
_lock_release(&fat_ctx->lock);
return fd;
}
_lock_acquire(&fat_ctx->lock);
FIL* file = &fat_ctx->files[fd];
FRESULT res = f_sync(file);
+ _lock_release(&fat_ctx->lock);
int rc = 0;
if (res != FR_OK) {
ESP_LOGD(TAG, "%s: fresult=%d", __func__, res);
errno = fresult_to_errno(res);
rc = -1;
}
- _lock_release(&fat_ctx->lock);
return rc;
}
FIL* file = &fat_ctx->files[fd];
FRESULT res = f_close(file);
file_cleanup(fat_ctx, fd);
+ _lock_release(&fat_ctx->lock);
int rc = 0;
if (res != FR_OK) {
ESP_LOGD(TAG, "%s: fresult=%d", __func__, res);
errno = fresult_to_errno(res);
rc = -1;
}
- _lock_release(&fat_ctx->lock);
return rc;
}
FIL* pf2 = calloc(1, sizeof(FIL));
void* buf = malloc(copy_buf_size);
if (buf == NULL || pf1 == NULL || pf2 == NULL) {
+ _lock_release(&fat_ctx->lock);
ESP_LOGD(TAG, "alloc failed, pf1=%p, pf2=%p, buf=%p", pf1, pf2, buf);
free(pf1);
free(pf2);
free(buf);
errno = ENOMEM;
- _lock_release(&fat_ctx->lock);
return -1;
}
res = f_open(pf1, n1, FA_READ | FA_OPEN_EXISTING);
goto fail1;
}
res = f_open(pf2, n2, FA_WRITE | FA_CREATE_NEW);
+ _lock_release(&fat_ctx->lock);
if (res != FR_OK) {
- _lock_release(&fat_ctx->lock);
goto fail2;
}
- _lock_release(&fat_ctx->lock);
size_t size_left = f_size(pf1);
while (size_left > 0) {
size_t will_copy = (size_left < copy_buf_size) ? size_left : copy_buf_size;
file = (FIL*) calloc(1, sizeof(FIL));
if (file == NULL) {
+ _lock_release(&fat_ctx->lock);
ESP_LOGD(TAG, "truncate alloc failed");
errno = ENOMEM;
ret = -1;
res = f_open(file, path, FA_WRITE);
if (res != FR_OK) {
+ _lock_release(&fat_ctx->lock);
ESP_LOGD(TAG, "%s: fresult=%d", __func__, res);
errno = fresult_to_errno(res);
ret = -1;
res = f_size(file);
if (res < length) {
+ _lock_release(&fat_ctx->lock);
ESP_LOGD(TAG, "truncate does not support extending size");
errno = EPERM;
ret = -1;
res = f_lseek(file, length);
if (res != FR_OK) {
+ _lock_release(&fat_ctx->lock);
ESP_LOGD(TAG, "%s: fresult=%d", __func__, res);
errno = fresult_to_errno(res);
ret = -1;
}
res = f_truncate(file);
+ _lock_release(&fat_ctx->lock);
if (res != FR_OK) {
ESP_LOGD(TAG, "%s: fresult=%d", __func__, res);
out:
free(file);
- _lock_release(&fat_ctx->lock);
return ret;
}