# +FAT ff\_filelength() returns Error when file length is \>2GB

**URL:** <https://forums.freertos.org/t/fat-ff-filelength-returns-error-when-file-length-is-2gb/6598>\
**Category:** Kernel\
**Created:** [June 5, 2017, 4:39pm UTC](https://forums.freertos.org/t/fat-ff-filelength-returns-error-when-file-length-is-2gb/6598 "2017-06-05T16:39:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 5, 2017, 4:39pm UTC](https://forums.freertos.org/t/fat-ff-filelength-returns-error-when-file-length-is-2gb/6598/1 "2017-06-05T16:39:37Z")

</div>

**[sonycman](https://sourceforge.net/u/sonycman/)** wrote on [Monday, June 05, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/248b0724/#8190):

Hello.

The ff\_filelength() function incorrectly interprets big files (\>2GB) length as error and returns zero.  
This is because it uses MSB of the length value returned by FF\_FileSize() as an error flag, which in case of the big files leads to misinterpretation of correct value as an erratic.

Regards  
Vladimir.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [June 6, 2017, 8:38am UTC](https://forums.freertos.org/t/fat-ff-filelength-returns-error-when-file-length-is-2gb/6598/2 "2017-06-06T08:38:04Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Tuesday, June 06, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/248b0724/#311c):

Vladimir, this is indeed something to be finished: files longer than 2 GB.

Throughout the library, signed 32-bit values are used to indicate the length, and negative values are interpreted as an error.

lseek also has an intrinsic problem:

```auto
    long lseek(int fd, long offset, int whence);

```

FreeRTOS+TCP is using the same syntax for `ff_fseek()`.

The Posix varian is ok, if `off_t` is a 64-bit signed integer:

```auto
    off_t lseek(int fd, off_t offset, int whence);

```

The +FAT library will not force people to include 64-bit arithmetic.

Right now I have a 2 GB file ( -2,147,483,648 bytes 🙂 ) on an SD-card and I will test it. To be continued.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2017, 11:28am UTC](https://forums.freertos.org/t/fat-ff-filelength-returns-error-when-file-length-is-2gb/6598/3 "2017-06-06T11:28:08Z")

</div>

**[sonycman](https://sourceforge.net/u/sonycman/)** wrote on [Tuesday, June 06, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/248b0724/#c882):

Hello, Hein  
Thanks for an explanation.

I understand, that big files support is not so important for the embedded systems.  
Its not needed in my case too.

But glad to hear such a support might be available one day in the future 🙂

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [June 6, 2017, 3:43pm UTC](https://forums.freertos.org/t/fat-ff-filelength-returns-error-when-file-length-is-2gb/6598/4 "2017-06-06T15:43:39Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Tuesday, June 06, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/248b0724/#14c4):

All right, I made some minor changes: `ff_filelength()` will return correct values for files between 0 bytes and 4 GB.

A return value of 0 can mean two things: the size is either 0, or the handle is invalid.

```auto
size_t uxLength;

    /* Clear errno. */
    stdioSET_ERRNO( 0 );
    uxLength = ff_filelength( pxFile );

    if( ( uxLength != 0 ) || ( stdioGET_ERRNO() == 0 ) )
    {
        /* Length equals to uxLength. */
    }
    else
    {
        /* Invalid handle. */
    }

```

Also I introduced a new low-level function that splits up the result and the file length:

```auto
    /* Use the following function in case files may get larger than 2 GB. */
    int32_t FF_GetFileSize( FF_FILE *pFile, uint32_t *pulSize );

```

And more importantly: `FF_Seek()` now works correctly with sizes and offsets larger than 2GB.

Here below I attach 3 files, which are a copy of the ‘160919’ release along with mentioned changes.

```auto
    ff_file.c
    ff_stdio.c
    include/ff_file.h
    

```

Hein Tibosch

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 7, 2017, 1:38am UTC](https://forums.freertos.org/t/fat-ff-filelength-returns-error-when-file-length-is-2gb/6598/5 "2017-06-07T01:38:37Z")

</div>

**[sonycman](https://sourceforge.net/u/sonycman/)** wrote on [Wednesday, June 07, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/248b0724/#26a3):

Thanks a lot, Hein!  
This is the best support I have ever seen!  
My respects!
