gpt4 book ai didi

c - ioctl 失败 : Operation not permitted - block layout of my files

转载 作者:行者123 更新时间:2023-12-03 09:51:38 75 4
gpt4 key购买 nike

我想在 block 级别查看文件的内容。给定一个文件,我想知道它有多少个 block ,每个 block 的大小是多少。

我的问题是当我运行它时出现这个错误:

FIBMAP ioctl failed: Operation not permitted

此外,当我进行编译时,我收到以下警告:

warning: implicit declaration of function ‘ioctl’ [-Wimplicit-function-declaration]
if (ioctl(fd, FIGETBSZ, &blocksize)) {

我正在使用以下代码。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <assert.h>
#include <linux/fs.h>



int main(int argc, char **argv)
{

int fd, i, block, blocksize, blkcnt;
struct stat st;

assert(argv[1] != NULL);

fd = open(argv[1], O_RDONLY);
if (fd <= 0) {
perror("error opening file");
exit(EXIT_FAILURE);
}

if (ioctl(fd, FIGETBSZ, &blocksize)) {
perror("FIBMAP ioctl failed");
exit(EXIT_FAILURE);
}

if (fstat(fd, &st)) {
perror("fstat error");
exit(EXIT_FAILURE);
}

blkcnt = (st.st_size + blocksize - 1) / blocksize;

for (i = 0; i < blkcnt; i++) {
block = i;
if (ioctl(fd, FIBMAP, &block)) {
perror("FIBMAP ioctl failed");
}
printf("%3d %10d\n", i, block);
}

close(fd);
return 0;
}

任何人都可以向我解释这段代码中的问题是什么。

最佳答案

您应该以 root 身份运行该程序。

ioctl(FIIBMAP) 需要 CAP_SYS_RAWIO capability .

关于c - ioctl 失败 : Operation not permitted - block layout of my files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37401944/

75 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com