fallocate的基本使用方法

发布时间:2021-06-28 17:08:53 作者:chen
来源:亿速云 阅读:1410

这篇文章主要讲解了“fallocate的基本使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“fallocate的基本使用方法”吧!

什么是空洞文件?
“在UNIX文件操作中,文件位移量可以大于文件的当前长度,在这种情况下,对该文件的下一次写将延长该文件,并在文件中构成一个空洞,这一点是允许的。位于文件中但没有写过的字节都被设为 0。”

如果 offset 比文件的当前长度更大,下一个写操作就会把文件“撑大(extend)”。这就是所谓的在文件里创造“空洞(hole)”。没有被实际写入文件的所有字节由重复的 0 表示。空洞是否占用硬盘空间是由文件系统(file system)决定的。大部分文件系统是不占用的。

怎么获得一个空洞文件?
以Linux来说,使用lseek或truncate到一个固定位置生成的“空洞文件”是不会占据真正的磁盘空间的。
空洞文件特点就是offset大于实际大小,也就是说一个文件的两头有数据而中间为空,以‘\0‘填充。那文件系统会不会不做任何处理的将其存放在硬盘上呢?大部分文件系统是不会将其存放在硬盘上。

文件预留
快速的为某个文件分配实际的磁盘空间在Linux下可通过fallocate(对应的posix接口为posix_fallocate)系统调用来实现,大部分主流文件系统如ext4,xfs还是支持fallocate

fallocate
   #include <fcntl.h>

   int fallocate(int fd, int mode, off_t offset, off_t len);
fd就是open产生的文件描述符,offset就是进行fallocate的文件偏移位置,len为fallocate的的长度。offset和len一起构成了要释放的文件范围。

[root@centos ~]# fallocate --help

Usage:
 fallocate [options] <filename>

Preallocate space to, or deallocate space from a file.

Options:
 -c, --collapse-range remove a range from the file
 -d, --dig-holes      detect zeroes and replace with holes
 -i, --insert-range   insert a hole at range, shifting existing data
 -l, --length <num>   length for range operations, in bytes
 -n, --keep-size      maintain the apparent size of the file
 -o, --offset <num>   offset for range operations, in bytes
 -p, --punch-hole     replace a range with a hole (implies -n)
 -z, --zero-range     zero and ensure allocation of a range
 -x, --posix          use posix_fallocate(3) instead of fallocate(2)
 -v, --verbose        verbose mode

 -h, --help           display this help
 -V, --version        display version

For more details see fallocate(1).
[root@centos ~]# fallocate -l 10M dctestfile
[root@centos ~]# ls
dctestfile 
[root@centos ~]# cat dctestfile 
^C
[root@centos ~]# ll -alhtr dctestfile 
-rw-r--r-- 1 root root 10M May  7 18:10 dctestfile

参考:

https://blog.csdn.net/weixin_36145588/article/details/78822837

感谢各位的阅读,以上就是“fallocate的基本使用方法”的内容了,经过本文的学习后,相信大家对fallocate的基本使用方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. Sentinel的使用方法
  2. SpringBatch的使用方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

fallocate

上一篇:Android中如何播放音乐

下一篇:Android 中View.onDraw(Canvas canvas)方法如何使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》