79767

what's the difference between fallocate and ftruncate

<h3>Question</h3>

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?


<h3>Answer1:</h3>

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

<blockquote>

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

</blockquote>

ftruncate() is also a standard POSIX function and is portable. Note that POSIX does not specify <em>how</em> 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:

<blockquote>

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.

...

</blockquote>
<h3>Answer2:</h3>

fallocate is used to preallocate blocks to a file

The following command will allocate a file with a size of 1GB.

fallocate -l 1G test_file1.img

ftruncate - set a file to a specified length

ftruncate(fileno(fout),size);
<h3>Answer3:</h3>

As I now know:
1.fallocate can't change file to shorter. it add actual space to file.
2.ftruncate add len to "describe", just like declaration.


<h3>Answer4:</h3>

With ftruncate you tell linux the size you want this file to be. It will truncate extra space if the file is getting shorter (including freeing up disk space) or add zeros, allocating disk space if you make the file longer.

fallocate is a general purpose function to effect change to a range to bytes belonging to a file.

Depending on how you use fallocate, you can accomplish everything you could with ftruncate with fallocate. Its just a little more complicated as you will have to know which range you need to allocate/deallocate (initial offset+length).

With fallocate you can pre allocate disk space without logically growing a file (example a zero byte file at the ls level that uses 1GB).

I just wrote a C program to perform high performance gzipping of a file with direct I/O using fallocate and ftruncate. I use fallocate to pre-allocate 64MB at a time to the file. In the end I use ftruncate to trim the excess space allocated.

Works perfectly with XFS, I confirmed ftruncate actually frees disk space with xfs_bmap -vp on a few files.

来源:https://stackoverflow.com/questions/49360932/whats-the-difference-between-fallocate-and-ftruncate

Recommend

  • How to disable autosave in Google Colab?
  • C/C++ : Deallocating or deleting a block of dynamically created memory [duplicate]
  • std::vector and std::string reallocation strategy
  • Add onload function to an opening window
  • Jquery's Ajax Property For Asp.Net 2.0
  • visual studio (SSDT) 2017 unable to start debugging
  • Inject Javascript code into a web page
  • Conditional Redirect on Login
  • Question mark placeholder
  • Muxing a H.264 Annex B & AAC stream using libavformat with vcopy/acopy
  • PDF Add Text and Flatten
  • How to place UI widgets on top of multiple Z ordered Surface Views in Android
  • How to write multiple objects to file?
  • How to Enable Systemd service in openshift/jenkins-1-centos7 docker container?
  • Decompress string in java from compressed string in C#
  • Getting nil from standardUserDefaults,
  • I am trying to create an app in android to insert data into sql server through a web service.
  • Django IN query as a string result - invalid literal for int() with base 10
  • JSF validateLength question
  • How to create wsdl from xsd
  • Python Equivalent of Java's 'Keystore'?
  • javax.net.ssl.SSLException: SSL handshake aborted Connection reset by peer while calling webservice
  • Signed Java web start application with Glassfish 4.1 and Java7
  • playing mp3 from nsbundle
  • Autocomplete source from project settings
  • How to create subsets of a single set of elements with XSLT?
  • How to specify generic type when the type is only known at runtime?
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • Android Library Projects on Windows and Mac
  • Angular FormGroup won't update it's value immediately after patchValue or setValue