答案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;
}