简体   繁体   English

fallocate 和 ftruncate 有什么区别

[英]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? fallocate 和 ftruncate 有什么区别?

ftruncate is a simple, single-purpose function. ftruncate是一个简单的单一用途函数。 Per the POSIX documentation , it simply sets the file to the requested length: 根据 POSIX 文档,它只是将文件设置为请求的长度:

If fildes refers to a regular file, the ftruncate() function shall cause the size of the file to be truncated to length .如果fildes常规文件,则ftruncate()函数应导致文件的大小被截断为length ... ...

ftruncate() is also a standard POSIX function and is portable. ftruncate()也是一个标准的 POSIX 函数并且是可移植的。 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 .请注意,POSIX 并未指定操作系统如何设置文件长度,例如设置为任意长度的文件是否为稀疏文件

fallocate() is a Linux-specific function that does a lot more, and in very specific ways: fallocate()是一个特定于 Linux 的函数,它以非常具体的方式做更多的事情:

Allocating disk space分配磁盘空间

The default operation (ie, mode is zero) of fallocate() allocates the disk space within the range specified by offset and len . fallocate() 的默认操作(即模式为零)在offsetlen指定的范围内分配磁盘空间。 The file size (as reported by stat(2) ) will be changed if offset+len is greater than the file size.如果offset+len大于文件大小,文件大小(由stat(2)报告)将被更改。 Any subregion within the range specified by offset and len that did not contain data before the call will be initialized to zero. offset 和 len 指定范围内的任何在调用前不包含数据的子区域将被初始化为零。 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.此默认行为与posix_fallocate(3)库函数的行为非常相似,旨在作为最佳实现该函数的方法。

... ...

Deallocating file space释放文件空间

Specifying the FALLOC_FL_PUNCH_HOLE flag (available since Linux 2.6.38) in mode deallocates space (ie, creates a hole) in the byte range starting at offset and continuing for len bytes.在 mode 中指定FALLOC_FL_PUNCH_HOLE标志(自 Linux 2.6.38 起可用)会在从offset开始并持续len字节的字节范围内释放空间(即,创建一个洞)。 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.在模式中指定FALLOC_FL_COLLAPSE_RANGE标志(自 Linux 3.15 起可用)会从文件中删除一个字节范围,而不会留下空洞。 The byte range to be collapsed starts at offset and continues for len bytes.要折叠的字节范围从offset开始并持续len个字节。 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.操作完成时,从位置offset+len开始的文件内容将附加到位置 offset,文件将减少len个字节。

... ...

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.在模式中指定FALLOC_FL_ZERO_RANGE标志(自 Linux 3.15 起可用)会将字节范围内的空间归零,从offset开始并持续len个字节。 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.在模式中指定FALLOC_FL_INSERT_RANGE标志(自 Linux 4.1 起可用)通过在文件大小中插入一个空洞而不覆盖任何现有数据来增加文件空间。 The hole will start at offset and continue for len bytes.该洞将从offset开始,并持续len个字节。 When inserting the hole inside file, the contents of the file starting at offset will be shifted upward (ie, to a higher file offset) by len bytes.当在文件中插入空洞时,从offset开始的文件内容将向上移动len个字节(即,到更高的文件偏移量)。 Inserting a hole inside a file increases the file size by len bytes.在文件中插入一个洞会使文件大小增加len字节。

... ...

fallocate is used to preallocate blocks to a file fallocate用于将块预分配给文件

The following command will allocate a file with a size of 1GB.以下命令将分配一个大小为 1GB 的文件。

fallocate -l 1G test_file1.img

ftruncate - set a file to a specified length ftruncate - 将文件设置为指定长度

ftruncate(fileno(fout),size);

As I now know:据我所知:
1.fallocate can't change file to shorter. 1.fallocate 无法将文件更改为更短的文件。 it add actual space to file.它将实际空间添加到文件中。
2.ftruncate add len to "describe", just like declaration. 2.ftruncate 给“describe”加上len,就像声明一样。

With ftruncate you tell linux the size you want this file to be.使用 ftruncate 你告诉 linux 你想要这个文件的大小。 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. fallocate 是一个通用函数,用于影响对属于文件的字节范围的更改。

Depending on how you use fallocate, you can accomplish everything you could with ftruncate with fallocate.根据您使用 fallocate 的方式,您可以完成使用 ftruncate 和 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).使用 fallocate,您可以预先分配磁盘空间而无需逻辑上增加文件(例如 ls 级别的零字节文件使用 1GB)。

I just wrote a C program to perform high performance gzipping of a file with direct I/O using fallocate and ftruncate.我刚刚编写了一个 C 程序,使用 fallocate 和 ftruncate 对具有直接 I/O 的文件执行高性能 gzip 压缩。 I use fallocate to pre-allocate 64MB at a time to the file.我使用 fallocate 一次为文件预分配 64MB。 In the end I use ftruncate to trim the excess space allocated.最后我使用 ftruncate 来修剪分配的多余空间。

Works perfectly with XFS, I confirmed ftruncate actually frees disk space with xfs_bmap -vp on a few files.与 XFS 完美配合,我确认 ftruncate 实际上在一些文件上使用 xfs_bmap -vp 释放了磁盘空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM