![如何找到 BTRFS 中文件的物理偏移量?](https://linux22.com/image/184781/%E5%A6%82%E4%BD%95%E6%89%BE%E5%88%B0%20BTRFS%20%E4%B8%AD%E6%96%87%E4%BB%B6%E7%9A%84%E7%89%A9%E7%90%86%E5%81%8F%E7%A7%BB%E9%87%8F%EF%BC%9F.png)
答案1
下面是一个 C 程序,用于计算从分区开始处的起始偏移量:一些Btrfs 文件系统上的文件。 Btrfs 似乎没有可靠的方法来做到这一点。
#include <unistd.h>
#include <linux/fs.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/fiemap.h>
int main(int argc, char **argv)
{
char buffer[sizeof (struct fiemap) + sizeof (struct fiemap_extent)];
struct fiemap *map = (struct fiemap *)buffer;
map->fm_start = 0;
map->fm_length = 4096;
map->fm_flags = FIEMAP_FLAG_SYNC;
map->fm_extent_count = 1; /* If you change this, you'll need to enlarge `buffer´. */
map->fm_reserved = 0;
int fd;
if (argc < 2) {
fprintf(stderr, "Usage %s filename\n", argv[0]);
return 1;
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("Error opening file");
return 1;
}
int block = 0;
int ret = ioctl(fd, FS_IOC_FIEMAP, map);
if (ret < 0) {
perror("ioctl");
close(fd);
return 1;
}
close(fd);
printf("Number of extents returned: %ld\n", map->fm_mapped_extents);
printf("File %s starts at byte offset %lu\n", argv[1], map->fm_extents[0].fe_physical);
return 0;
}