我的问题是处理稀疏文件读取并了解文件的范围在哪里围绕它执行一些逻辑。
由于没有直接的 API 调用来解决这些问题,我决定使用 ioctl api 来执行此操作。我从 cp 命令如何通过查看他们的代码来处理复制稀疏文件的问题得到了这个想法,并最终看到了这一点。
因此,我尝试在用户空间中运行的示例程序中执行相同的操作,但出现“无效参数”错误。我不确定我错过了什么,或者这是否可能来自用户空间。我在 ext4 文件系统上的 ubuntu 14.04 上运行。这可能是设备驱动程序在下面支持这些请求模式的问题吗?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include "fiemap.h" //This is from https://github.com/coreutils/coreutils/blob/df88fce71651afb2c3456967a142db0ae4bf9906/src/fiemap.h
int main(int argc, char* argv[]) {
int input_fd;
if(argc != 2){
printf ("Usage: ioctl file1");
return 1;
}
/* Create input file descriptor */
input_fd = open (argv [1], O_RDWR);
if (input_fd < 0) {
perror ("open");
return 2;
}
union { struct fiemap f; char c[4096]; } fiemap_buf;
struct fiemap *fiemap = &fiemap_buf.f;
int s = ioctl(input_fd, FS_IOC_FIEMAP, fiemap);
if (s == 0) {
printf("ioctl success\n");
} else {
printf("ioctl failure\n");
char * errmsg = strerror(errno);
printf("error: %d %s\n", errno, errmsg);
}
/* Close file descriptors */
close (input_fd);
return s;
}