1

Using the FatFs and their API, I am trying to pre-allocate file system space for the remainder of the drive to write files of unknown sizes. At the end of the file write process, any unused space is then truncated (f_truncate). However, I'm having a problem allocating enough space when the file system become fragmented after deletion of some files. To overcome this problem, I would like to allocate only enough space as the largest contiguous block of memory on the file system.

In the FatFS API, there are functions to get the amount of free space left on the device, which is f_getfree. There is also the f_expand function, which takes in a size in bytes and returns whether or not a free contiguous block of memory exists for that given size.

Is there an efficient way to calculate the largest free contiguous block of memory available? I'm trying to avoid any sort of brute force "guess and check" method. Thanks

LaneL
  • 737
  • 1
  • 6
  • 25

1 Answers1

1

One way would be to create your own extension to the API to count contiguous FAT entries across all the sectors.

Without modifying the API, you could use f_lseek(). Write open a file, use f_lseek() to expand the size of the file until 'disk full'(end of contiguous space). This would need to be repeated with new files until all of the disk was allocated. Pick from this the maximum allocated file, and delete the others.

B. Wolf
  • 682
  • 6
  • 12
  • Good idea. Thanks! – LaneL Jun 28 '17 at 14:07
  • 1
    It's actually `f_expand` rather than `f_lseek` that returns an area of contiguous, unfragmented space. – tofro Jun 29 '17 at 15:14
  • @tofro Not exactly, f_expand returns a status of whether or not it was able to find a contiguous amount of space of the size that you passed in as a parameter. It also allocates it for you if you pass in that param. You still have to guess and check the size to get the largest contiguous block if you were to only use f_expand. – LaneL Jun 30 '17 at 04:56