c - fallocate 和 ftruncate 有什么区别

标签 c linux glibc fallocate

他们都可以根据我的测试改变文件大小。 为什么他们都可以将文件更改为更大或更短? fallocate 和 ftruncate 有什么区别?

最佳答案

ftruncate 是一个简单的单一用途函数。 Per the POSIX documentation ,它只是将文件设置为请求的长度:

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

ftruncate() 也是一个标准的 POSIX 函数并且是可移植的。请注意,POSIX 不指定操作系统如何设置文件长度,例如设置为任意长度的文件是否为a sparse file。 .

fallocate() is a Linux-specific function它以非常具体的方式做了更多:

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.

...

关于c - fallocate 和 ftruncate 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49360932/

相关文章:

linux - 将公钥添加到服务器的authorized_keys 文件后是否应该删除它?

mysql - 无法通过套接字 '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器。

linux - 如何让PCI设备发起DMA操作?

c++ - glibc 检测到错误,可能是内存泄漏?

c - 如何使用 -rdynamic 链接器标志?

c - C 程序中的奇怪输出

c - printf 添加额外的 `FFFFFF` 到 char 数组的十六进制打印

linux - 在较新的系统上构建较旧的 GLIBC

c - 使用 fgetc() 逐行读取 c 文件

c - 文件不会在 MS Visual Studio 中编译,但会在 GCC 中编译。为什么?