3.1.3 空洞文件

/********************
 * 功能:产生5G空洞文件
 *
 * *****************/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>


int main(int argc ,char **argv)
{
    int fd ;
    //1.判断输入是否正确
    if(argc < 2 )
    {
        fprintf(stderr,"Usage....
");
        exit(1);
    }
    //2.打开文件
    fd = open(argv[1],O_WRONLY | O_CREAT | O_TRUNC,0600);
    if(fd < 0)
    {
        perror("open()");
        exit(1);
    }
    //3.设置偏移
    lseek(fd ,5LL*1024LL*1024LL*1024LL-1,SEEK_SET);
    //4.写尾0
    write(fd ,"",1);
    exit(0);
}

Makefile:

CFLAGS+=-D_FILE_OFFSET_BIT=64

[root] : make big

[root] : ./big /tmp/out ------> 产生空洞文件

[root] : ls -l /tmp/out  ------>查看空洞文件大小

cp命令是会检查文件是否是空洞文件,所以

[root] : cp /tmp/out /tmp/out1--->会发现out1文件是不占用磁盘空间的。block为0

原文地址:https://www.cnblogs.com/muzihuan/p/5275560.html