]> granicus.if.org Git - esp-idf/commitdiff
vfs,fat: add fsync to VFS interface, implement it for fatfs
authorIvan Grokhotkov <ivan@espressif.com>
Tue, 17 Oct 2017 06:00:01 +0000 (14:00 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Tue, 17 Oct 2017 07:14:53 +0000 (15:14 +0800)
components/fatfs/src/vfs_fat.c
components/vfs/include/esp_vfs.h
components/vfs/vfs.c

index 9b33bd20e6cf53403a5efeb3d0b2de16a5d5a95b..a0eaaa3b4072f9d5f6278a980fc4ced732cd6db3 100644 (file)
@@ -52,6 +52,7 @@ static int vfs_fat_open(void* ctx, const char * path, int flags, int mode);
 static int vfs_fat_close(void* ctx, int fd);
 static int vfs_fat_fstat(void* ctx, int fd, struct stat * st);
 static int vfs_fat_stat(void* ctx, const char * path, struct stat * st);
+static int vfs_fat_fsync(void* ctx, int fd);
 static int vfs_fat_link(void* ctx, const char* n1, const char* n2);
 static int vfs_fat_unlink(void* ctx, const char *path);
 static int vfs_fat_rename(void* ctx, const char *src, const char *dst);
@@ -109,6 +110,7 @@ esp_err_t esp_vfs_fat_register(const char* base_path, const char* fat_drive, siz
         .close_p = &vfs_fat_close,
         .fstat_p = &vfs_fat_fstat,
         .stat_p = &vfs_fat_stat,
+        .fsync_p = &vfs_fat_fsync,
         .link_p = &vfs_fat_link,
         .unlink_p = &vfs_fat_unlink,
         .rename_p = &vfs_fat_rename,
@@ -322,6 +324,22 @@ static ssize_t vfs_fat_read(void* ctx, int fd, void * dst, size_t size)
     return read;
 }
 
+static int vfs_fat_fsync(void* ctx, int fd)
+{
+    vfs_fat_ctx_t* fat_ctx = (vfs_fat_ctx_t*) ctx;
+    _lock_acquire(&fat_ctx->lock);
+    FIL* file = &fat_ctx->files[fd];
+    FRESULT res = f_sync(file);
+    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;
+}
+
 static int vfs_fat_close(void* ctx, int fd)
 {
     vfs_fat_ctx_t* fat_ctx = (vfs_fat_ctx_t*) ctx;
index bd1cf322c2da60ff3d140e90f06a953198b5d5fb..9cd8e5e5691aa1db3fecc9291fd4cf415a3a8ec3 100644 (file)
@@ -162,6 +162,10 @@ typedef struct
         int (*ioctl_p)(void* ctx, int fd, int cmd, va_list args);
         int (*ioctl)(int fd, int cmd, va_list args);
     };
+    union {
+        int (*fsync_p)(void* ctx, int fd);
+        int (*fsync)(int fd);
+    };
 } esp_vfs_t;
 
 
index 24fde2e3064da21cca99611593f0e55c698c3937..92f60b6029369b271ca5f9220d7779a07aef70f1 100644 (file)
@@ -534,3 +534,17 @@ int ioctl(int fd, int cmd, ...)
     va_end(args);
     return ret;
 }
+
+int fsync(int fd)
+{
+    const vfs_entry_t* vfs = get_vfs_for_fd(fd);
+    struct _reent* r = __getreent();
+    if (vfs == NULL) {
+        __errno_r(r) = EBADF;
+        return -1;
+    }
+    int local_fd = translate_fd(vfs, fd);
+    int ret;
+    CHECK_AND_CALL(ret, r, vfs, fsync, local_fd);
+    return ret;
+}