fallocate()有什么作用? [英] What fallocate() does?

查看:127
本文介绍了fallocate()有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我有两个问题.

Actually, I have two questions.

我阅读了手册页,并具有以下理解.对于支持文件中漏洞的文件系统,fallocate()用于打孔并在文件中分配新空间.对于没有孔的文件系统,fallocate()仅可用于在文件末尾之后分配新空间,即,仅当len + offset > file_size时才有效.

I read the man page and have the following understanding. For filesystems that support hole in file, fallocate() is used to punch holes and allocate new spaces in the file. For filesystems without holes, fallocate() can only be used to allocate new space after the end of file, i.e., it only has effect when len + offset > file_size.

我的理解正确吗?如果是这样,我还有以下问题.

Is my understanding correct? If so, I also have the following question.

现在,我想创建一个新文件,并在文件中分配零位填充字节的特定大小.我意识到fallocate()ftruncate()都能胜任.他们有什么区别?

Now I want to create a new file and allocate a specific size of zero-filled bytes in the file. I realized that both fallocate() and ftruncate() can do the job. What's their difference?

顺便说一句,我知道fallocate()不是可移植的,但是由于我的程序仅适用于Linux,因此未考虑移植到其他类似Unix的系统上.

BTW, I know that fallocate() is not portable, but since my program is only for Linux, portability to other Unix-like systems is not taken into consideration.

谢谢!

推荐答案

使用 ftruncate(2)的区别在于(在文件上支持它的系统(例如Ext4)确实由posix_fallocate保留了磁盘空间,但是ftruncate通过添加孔(而不保留磁盘空间)来扩展文件.

Use posix_fallocate(3) in your code. The difference with ftruncate(2) is that (on file systems supporting it, e.g. Ext4) disk space is indeed reserved by posix_fallocate but ftruncate extends the file by adding holes (and without reserving disk space).

例如如果您的磁盘是100GB,并且posix_fallocate一个5Gb文件,您将看到(使用df)可用空间减少了(因为在调用之前);如果您只执行ftruncate,则不会看到可用空间减少.

E.g. if you disk is 100Gbytes, and you posix_fallocate a 5Gb file, you'll see (with df) that available space has reduced (since before the call); if you did only an ftruncate you won't see available space reducing.

另请参见有关相关问题的答案.

这篇关于fallocate()有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆