1 个不稳定版本
| 0.1.0 | 2023年4月2日 | 
|---|
#890 在 文件系统
24 每月下载量
用于 repro-env
10KB
150 行
clone-file
简单使用文件系统的 reflink 功能。
// Clone a file using a reflink, or error if it can not be done.
clone_file("src.bin", "dest.bin");
// Try to clone a file, falling back to a regular copy.
clone_or_copy_file("src.bin", "dest.bin");
// Clone a sub-range of a file using a reflink, or error if it can not be done.
clone_file_range(
  "src.bin",
  /* offset: */ 4 << 10,
  /* length: */ 2 << 20,
  "dest.bin",
  /* dest offset: */ 0
);
// Try to clone a sub-range of a file, falling back to a naive copy.
clone_or_copy_file_range(
  "src.bin",
  /* offset: */ 4 << 10,
  /* length: */ 2 << 20,
  "dest.bin",
  /* dest offset: */ 0
);
实现细节
Linux
在 Linux 上,使用 FICLONE 和 FICLONERANGE ioctls。请参阅 man 2 ioctl_ficlonerange 获取详细信息及限制。
在 btrfs 文件系统中进行了测试。
其他
目前尚未为其他平台实现 clone_file 和 clone_file_range 函数。
但是,回退函数 clone_or_copy_file 和 clone_or_copy_file_range 仍然可以工作,回退到原始复制。
运行测试
要测试克隆,我们需要一个支持 reflinks 的文件系统。这需要一些设置,在 test.sh 脚本中实现。
它期望一个安装了 btrfs-progs 软件包的 Linux 系统。它创建一个 200MiB 的循环设备,将其格式化为 btrfs,然后创建必要的测试数据并运行 cargo tests。然后清理循环设备和挂载点。
测试故意设置为仅通过 test.sh 运行
依赖项
~0–385KB