fallocate和ftruncate有什么区别 [英] what's the difference between fallocate and ftruncate
问题描述
他们都可以根据我的测试更改文件大小. 他们为什么都可以将文件更改为更大和更短? fallocate和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?
推荐答案
ftruncate
是一个简单的单用途函数. 根据POSIX文档,它只是将文件设置为所需的长度:
ftruncate
is a simple, single-purpose function. Per the POSIX documentation, it simply sets the file to the requested length:
如果
fildes
引用常规文件,则ftruncate()
函数将导致文件大小被截断为length
. ...
If
fildes
refers to a regular file, theftruncate()
function shall cause the size of the file to be truncated tolength
. ...
ftruncate()
也是标准的POSIX功能,并且是可移植的.请注意,POSIX未指定操作系统设置文件长度的方式,例如,设置为任何长度的文件是否为
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()
是特定于Linux的功能并以非常特定的方式做更多的事情:
fallocate()
is a Linux-specific function that does a lot more, and in very specific ways:
分配磁盘空间
fallocate()的默认操作(即,模式为零)分配
在offset
和len
指定的范围内的磁盘空间.这
如果offset+len
为
大于文件大小.指定范围内的任何子区域
通过offset和len在呼叫之前不包含数据的方式将是
初始化为零.此默认行为与
posix_fallocate(3)
库函数的行为,并且旨在
作为最佳实现该功能的一种方法.
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.
...
取消分配文件空间
指定FALLOC_FL_PUNCH_HOLE
标志(从Linux开始可用
2.6.38)在模式下释放字节空间(即创建一个空位)
范围从offset
开始并持续到len
个字节.内
指定范围,部分文件系统块被清零,整个
文件系统块将从文件中删除.成功之后
调用,此范围内的后续读取将返回零.
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.
...
折叠文件空间
指定FALLOC_FL_COLLAPSE_RANGE
标志(从Linux开始可用
3.15)在模式下从文件中删除字节范围,而不会留下
孔.要折叠的字节范围从offset
开始并继续
len
个字节.操作完成后,
从位置offset+len
开始的文件将被附加到
位置偏移,文件将缩小len
个字节.
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.
...
归零文件空间
指定FALLOC_FL_ZERO_RANGE
标志(从Linux 3.15开始可用)
在模式下,从offset
开始的字节范围中的空间为零,并且
连续len
个字节.在指定范围内,块为
为文件中跨越孔的区域预先分配.后
成功调用后,此范围内的后续读取将返回
零.
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.
...
增加文件空间
指定FALLOC_FL_INSERT_RANGE
标志(从Linux开始可用
4.1)在模式下,通过在
文件大小而不会覆盖任何现有数据.孔会开始
在offset
处并继续len
个字节.将孔插入内部时
文件,从offset
开始的文件内容将被移动
len
个字节向上(即,到更高的文件偏移量).插入一个
文件内的孔会使文件大小增加len
个字节.
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.
...
这篇关于fallocate和ftruncate有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!