【发布时间】:2011-08-04 11:23:06
【问题描述】:
我创建了 1 个程序来创建包含备用空块和数据块的稀疏文件。 比如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;
}
假设,我使用上面的代码创建了一个 new_sparse_file。
当我运行这个程序时,在块大小为 4KB 的 ext3 FS 上,ls -lh 显示 new_sparse_file 的大小为 16KB,而du -h 显示为 8 kb,我认为这是正确的。
在 xfs 上,块大小为 4kb,ls -lh 显示为 16KB,但 `du -h* 显示为 12kb。
为什么会有不同种类的行为?
【问题讨论】:
标签: linux filesystems block