Custom commands in vfs_fcntl

I am working with vfs over littlefs2. In my algorithm i need to get the current position in some file I need. The problem with “seek(fd, SEEK_CUR, 0)” is that the littlefs “seek” operation lead to erase block rewriting. That is why i want to add “F_GETPOS” command to fcntl, so I can get the current position without “seek”. Can you add some implementation of custom commands for fcntl?

The problem with seek(fd, SEEK_CUR, 0) is that the littlefs “seek” operation lead to erase block rewriting.

Is that caused by the lfs_file_flush() in lfs_file_seek_()?

And you’d rather use lfs_file_tell()?

I think we could as well fix then in upstream littlefs then.

diff --git i/lfs.c w/lfs.c
index da4bfca..011cd74 100644
--- i/lfs.c
+++ w/lfs.c
@@ -6253,7 +6253,12 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
             (void*)lfs, (void*)file, off, whence);
     LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
 
-    lfs_soff_t res = lfs_file_seek_(lfs, file, off, whence);
+    lfs_soff_t res;
+    if (whence == LFS_SEEK_CUR && off == 0) {
+        res = lfs_file_tell_(lfs, file);
+    } else {
+        res = lfs_file_seek_(lfs, file, off, whence);
+    }
 
     LFS_TRACE("lfs_file_seek -> %"PRId32, res);
     LFS_UNLOCK(lfs->cfg);

But wait, lfs_file_seek_() already does the right thing:

    if (whence == LFS_SEEK_CUR) {
        npos = file->pos + (lfs_off_t)off;
    }

    if (file->pos == npos) {
        // noop - position has not changed
        return npos;
    }

So for the seek(fd, SEEK_CUR, 0) case, this will just return the current file position and not flush anything,

Where did you see the erase block rewriting?