gpt4 book ai didi

c - fallocate 和 ftruncate 有什么区别

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:26:11 29 4
gpt4 key购买 nike

他们都可以根据我的测试改变文件大小。为什么他们都可以将文件更改为更大或更短?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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com