Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between fallocate and ftruncate

They can all change file size according to my test. why can they all change file to larger and to shorter? what's the difference between fallocate and ftruncate?

like image 855
Lenge Avatar asked Mar 07 '23 15:03

Lenge


1 Answers

ftruncate is a simple, single-purpose function. Per the POSIX documentation, it simply sets the file to the requested length:

If fildes refers to a regular file, the ftruncate() function shall cause the size of the file to be truncated to length. ...

ftruncate() is also a standard POSIX function and is portable. Note that POSIX does not specify how an OS sets the file length, such as whether or not a file set to any length is a sparse file.

fallocate() is a Linux-specific function that does a lot more, and in very specific ways:

Allocating disk space

The default operation (i.e., mode is zero) of fallocate() allocates the disk space within the range specified by offset and len. The file size (as reported by stat(2)) will be changed if offset+len is greater than the file size. Any subregion within the range specified by offset and len that did not contain data before the call will be initialized to zero. This default behavior closely resembles the behavior of the posix_fallocate(3) library function, and is intended as a method of optimally implementing that function.

...

Deallocating file space

Specifying the FALLOC_FL_PUNCH_HOLE flag (available since Linux 2.6.38) in mode deallocates space (i.e., creates a hole) in the byte range starting at offset and continuing for len bytes. Within the specified range, partial filesystem blocks are zeroed, and whole filesystem blocks are removed from the file. After a successful call, subsequent reads from this range will return zeroes.

...

Collapsing file space

Specifying the FALLOC_FL_COLLAPSE_RANGE flag (available since Linux 3.15) in mode removes a byte range from a file, without leaving a hole. The byte range to be collapsed starts at offset and continues for len bytes. At the completion of the operation, the contents of the file starting at the location offset+len will be appended at the location offset, and the file will be len bytes smaller.

...

Zeroing file space

Specifying the FALLOC_FL_ZERO_RANGE flag (available since Linux 3.15) in mode zeroes space in the byte range starting at offset and continuing for len bytes. Within the specified range, blocks are preallocated for the regions that span the holes in the file. After a successful call, subsequent reads from this range will return zeroes.

...

Increasing file space

Specifying the FALLOC_FL_INSERT_RANGE flag (available since Linux 4.1) in mode increases the file space by inserting a hole within the file size without overwriting any existing data. The hole will start at offset and continue for len bytes. When inserting the hole inside file, the contents of the file starting at offset will be shifted upward (i.e., to a higher file offset) by len bytes. Inserting a hole inside a file increases the file size by len bytes.

...

like image 190
Andrew Henle Avatar answered Mar 10 '23 11:03

Andrew Henle