开发者

Create sparse file with alternate data and hole on ext3 and XFS

开发者 https://www.devze.com 2023-03-26 00:37 出处:网络
I created 1 program to create sparse file which contains alternate empty blocks and data blocks. For example block1=empty, block2=data, block3=empty .....

I created 1 program to create sparse file which contains alternate empty blocks and data blocks. For example block1=empty, block2=data, block3=empty .....

#define BLOCK_SIZE 4096
void *buf;
int main(int argc, char **argv)
{
buf=malloc(512);
memset(buf,"a",512);
int fd=0;
int i;
int sector_per_block=BLOCK_SIZE/512;
int block_count=4;
if(argc !=2 ){
        printf("Wrong usage\n USAGE: program absolute_path_to_write\n");
        _exit(-1);
}
fd=open(argv[1],O_RDWR | O_CREAT,0666);
if(fd <= 0){
        printf("file open failed\n");
        _exit(0);
}
while(block_count > 0){
        lseek(fd,BLOCK_SIZE,SEEK_CUR);
        block_count--;
        for(i=0;i<sector_per_block;i++)
        write(fd,buf,512);
        block_count--;
}
close(fd);
return 0;
}

Suppose, I create a new_sparse_file using this above code.

When I run this program, on ext3 FS with block size 4KB, ls -lh shows size of new_sparse_file as 16KB, while du -h shows 8 kb, which, I think is correct.

On xfs, block size of 4kb, ls -lh shows 16KB but `du -h* shows 12kb.

Why are there different kinds of behavior?开发者_如何学JAVA


This is a bug in XFS :

http://oss.sgi.com/archives/xfs/2011-06/msg00225.html


Sparse files is just an optimization of space, and any FS may not to create a sparse file to optimize file fragmentation and file access speed. So, you can't depend on how spare the file will be on some FS.

So, 12kb on XFS is correct too.

0

精彩评论

暂无评论...
验证码 换一张
取 消