简体   繁体   English

C - Linux稀疏文件:如何检查文件是否稀疏并打印0填充的磁盘块

[英]C - Linux Sparse File: How to check if file is sparse and print 0-filled disk blocks

What I am trying to do is to write a C program on linux which should be checking in the current directory if there are sparse files, and also I would like to print the number of disk blocks that already represent gaps in the file and the number of disk blocks that are 0-filled but take up disk space. 我想要做的是在linux上编写一个C程序,如果有稀疏文件,应该在当前目录中检查,并且我还要打印已经代表文件中的间隙和数量的磁盘块数量磁盘块是0填充但占用磁盘空间。

So far I can access the current directory and print just the files with 到目前为止,我可以访问当前目录并打印文件

DIR *dirp;
struct dirent *dp;

To get done the second part with sparse file I tried to use stat() but it seems not to be working because I don't get the required results as I wished. 为了完成第二部分与稀疏文件我尝试使用stat()但它似乎没有工作,因为我没有得到所需的结果,因为我希望。

So, could anyone show me how to do the part with the sparse file? 那么,有人能告诉我如何使用稀疏文件做这个部分吗?

If you want to look for holes in sparse files, see the manpage for lseek , specifically the bit concerning SEEK_HOLE and SEEK_DATA . 如果要在稀疏文件中查找漏洞,请参阅lseek的联机帮助页,特别是有关SEEK_HOLESEEK_DATA

If you want to just know the allocated size on disk, then look at then manpage for stat (2) : 如果你只想知道磁盘上分配的大小,那么请查看stat (2) manpage:

       struct stat {
           ...
           off_t     st_size;    /* total size, in bytes */
           ...
           blksize_t st_blksize; /* blocksize for file system I/O */
           blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
       };

st_size tells you the total size in bytes, st_blksize * st_blocks gives you the allocated size. st_size告诉你总大小(以字节为单位), st_blksize * st_blocks为你提供分配的大小。 If you round st_size up to the next multiple of st_blksize and subtract the file size, that's the size of the holes. 如果将st_size向上舍st_sizest_blksize的下一个倍数并减去文件大小,那就是孔的大小。

Check size, returned by du utility, and compare with the "apparent size". 检查du实用程序返回的大小,并与“表观大小”进行比较。 If you wish you may take a look on the block counting algorithm from du 如果您愿意,可以查看du的块计数算法

You can try the following trick with stat result: 您可以使用stat结果尝试以下技巧:

if (st.st_blocks * st.st_blksize < st.st_size) { 
  SPARSE-FILE 
} else { 
  PROBABLY NOT SPARSE
}

Not sure if it identifies all sparse files however. 但是不确定它是否识别所有稀疏文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM