Skip to content

Commit 46e9a8d

Browse files
authored
Fix GetLength reseting file position (#3057)
1 parent 2cbd142 commit 46e9a8d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

targets/ESP32/_littlefs/littlefs_FS_Driver.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ HRESULT LITTLEFS_FS_Driver::Seek(void *handle, int64_t offset, uint32_t origin,
412412
HRESULT LITTLEFS_FS_Driver::GetLength(void *handle, int64_t *length)
413413
{
414414
LITTLEFS_FileHandle *fileHandle;
415+
long currentPosition;
415416

416417
if (handle == 0)
417418
{
@@ -420,6 +421,13 @@ HRESULT LITTLEFS_FS_Driver::GetLength(void *handle, int64_t *length)
420421

421422
fileHandle = (LITTLEFS_FileHandle *)handle;
422423

424+
// Store current position
425+
currentPosition = ftell(fileHandle->file);
426+
if (currentPosition == -1L)
427+
{
428+
return CLR_E_FILE_IO;
429+
}
430+
423431
// Move to the end of the file to determine its size
424432
if (fseek(fileHandle->file, 0, SEEK_END) != 0)
425433
{
@@ -430,8 +438,12 @@ HRESULT LITTLEFS_FS_Driver::GetLength(void *handle, int64_t *length)
430438
// Get the current position, which is the size of the file
431439
*length = ftell(fileHandle->file);
432440

433-
// rewind to the start of the file if you need to read from it next
434-
rewind(fileHandle->file);
441+
// Restore file position
442+
if (fseek(fileHandle->file, currentPosition, SEEK_SET) != 0)
443+
{
444+
// Handle error
445+
return CLR_E_FILE_IO;
446+
}
435447

436448
return S_OK;
437449
}

0 commit comments

Comments
 (0)