复制稀疏文件 [英] Copy sparse files

查看:61
本文介绍了复制稀疏文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解Linux(UNIX)低级接口,并且作为练习,我想编写一个代码,该代码将带孔的文件复制到一个新文件中(再次带孔).

I'm trying to understand Linux (UNIX) low-level interfaces and as an exercise want to write a code which will copy a file with holes into a new file (again with holes).

所以我的问题是,如何从第一个文件读取直到第一个孔,而是直到文件的最后?如果我没记错的话,当到达第一个孔(EOF)时, read()返回0.

So my question is, how to read from the first file not till the first hole, but till the very end of the file? If I'm not mistaken, read() returns 0 when reaches the first hole(EOF).

我当时正在考虑逐字节地寻找正确的字节并尝试读取该字节,但是随后我必须提前知道孔的数量.

I was thinking about seeking right byte by byte and trying to read this byte, but then I have to know the number of holes in advance.

推荐答案

如果通过表示稀疏文件,则必须 lseek 重新创建它们.从Linux 3.1开始,您甚至可以使用 lseek 跳转到孔的开头或结尾,如

If by holes you mean sparse files, then you have to find the holes in the input file and recreate them using lseek when writing the output file. Since Linux 3.1, you can even use lseek to jump to the beginning or end of a hole, as described in great detail in the man page.

正如ThiefMaster 已指出,正常的文件操作会将空洞简单地视为零序列个字节,因此您看不到您提到的EOF.

As ThiefMaster already pointed out, normal file operations will treat holes simply as sequences of zero bytes, so you won't see the EOF you mention.

这篇关于复制稀疏文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆