您好,登錄后才能下訂單哦!
這篇文章主要講解了“fallocate的基本使用方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“fallocate的基本使用方法”吧!
什么是空洞文件?
“在UNIX文件操作中,文件位移量可以大于文件的當(dāng)前長(zhǎng)度,在這種情況下,對(duì)該文件的下一次寫將延長(zhǎng)該文件,并在文件中構(gòu)成一個(gè)空洞,這一點(diǎn)是允許的。位于文件中但沒有寫過的字節(jié)都被設(shè)為 0?!?/p>
如果 offset 比文件的當(dāng)前長(zhǎng)度更大,下一個(gè)寫操作就會(huì)把文件“撐大(extend)”。這就是所謂的在文件里創(chuàng)造“空洞(hole)”。沒有被實(shí)際寫入文件的所有字節(jié)由重復(fù)的 0 表示??斩词欠裾加糜脖P空間是由文件系統(tǒng)(file system)決定的。大部分文件系統(tǒng)是不占用的。
怎么獲得一個(gè)空洞文件?
以Linux來說,使用lseek或truncate到一個(gè)固定位置生成的“空洞文件”是不會(huì)占據(jù)真正的磁盤空間的。
空洞文件特點(diǎn)就是offset大于實(shí)際大小,也就是說一個(gè)文件的兩頭有數(shù)據(jù)而中間為空,以‘\0‘填充。那文件系統(tǒng)會(huì)不會(huì)不做任何處理的將其存放在硬盤上呢?大部分文件系統(tǒng)是不會(huì)將其存放在硬盤上。
文件預(yù)留
快速的為某個(gè)文件分配實(shí)際的磁盤空間在Linux下可通過fallocate(對(duì)應(yīng)的posix接口為posix_fallocate)系統(tǒng)調(diào)用來實(shí)現(xiàn),大部分主流文件系統(tǒng)如ext4,xfs還是支持fallocate
fallocate
#include <fcntl.h>
int fallocate(int fd, int mode, off_t offset, off_t len);
fd就是open產(chǎn)生的文件描述符,offset就是進(jìn)行fallocate的文件偏移位置,len為fallocate的的長(zhǎng)度。offset和len一起構(gòu)成了要釋放的文件范圍。
[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的基本使用方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)fallocate的基本使用方法這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。