Hyp*_*oyz 3 c linux file cp lseek
我正在处理APUE的问题,以某种方式编写一个程序,如cp来复制文件(第4章问题4.6).如果文件中包含孔(或稀疏文件),则间隙中的\ 0将永远不会被处理.理想的方法是逐块读写,其大小由lseek(fd,current_off,SEEK_HOLE)决定.我以/ bin/ls为例.但是evertime我lseek这个文件(或其他文件)文件的偏移总是设置为文件的末尾.我查过这篇文章,但似乎没有令人满意的答案.这是我的代码:
#include <stdio.h>
/* and other headers */
int main(void) {
int fd;
off_t off;
fd = open("/bin/ls", O_RDONLY);
if ((off = lseek(fd, 0, SEEK_HOLE) == -1)
exit(-1);
printf("%d\n", off);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的内核是linux 3.13.0-rc3从最新的稳定树中拉出来的,我的fs是ext4.如果lseek不可用,将任何'\ 0'视为洞的开头是否合适?谢谢你的回答.
来自'man lseek'(手册页是你的朋友.第一个寻找信息的地方.)
SEEK_HOLE
Adjust the file offset to the next hole in the file greater than
or equal to offset. If offset points into the middle of a hole,
then the file offset is set to offset. If there is no hole past
offset, then the file offset is adjusted to the end of the file
(i.e., there is an implicit hole at the end of any file).
Run Code Online (Sandbox Code Playgroud)
换句话说,你看到了完全预期的行为.没有洞ls,所以你在文件的末尾有一个洞.
您可以使用以下命令创建用于测试的稀疏文件dd:
dd if=/dev/zero of=sparsefile bs=1 count=1 seek=40G
Run Code Online (Sandbox Code Playgroud)
至于你的最后一个问题:不,那是不合理的.文件中的文件很可能只有0个字节.这并不表示它们是稀疏文件.
| 归档时间: |
|
| 查看次数: |
645 次 |
| 最近记录: |