我的問題是處理稀疏文件讀取並理解文件的範圍在哪裏執行某些邏輯。如何使用帶有FS_IOC_FIEMAP的ioctl
因爲沒有直接的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;
}
初始化fiemap工作!非常感謝你! – Aila