我正在尝试访问linux/fs.h中定义的超级块对象。而是如何初始化对象,以便我们可以访问它的属性。我发现alloc_super()是用来初始化超级的,但是它是如何调用的呢?
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <linux/fs.h>
int main(){
printf("hello there");
struct super_block *sb;
return 0;
}发布于 2015-11-08 20:45:48
答案在很大程度上取决于文件系统,因为不同的文件系统会有不同的超级数据块布局,实际上也会有不同的数据块排列。
例如,ext2文件系统超级块位于磁盘上的已知位置(字节1024),并且具有已知大小(sizeof(结构超级块)字节)。
因此,您想要的典型实现(这不是一个工作代码,但只需稍加修改即可工作)将是:
struct superblock *read_superblock(int fd) {
struct superblock *sb = malloc(sizeof(struct superblock));
assert(sb != NULL);
lseek(fd, (off_t) 1024, SEEK_SET));
read(fd, (void *) sb, sizeof(struct superblock));
return sb;
}现在,您可以使用linux/headers分配超级块,或者编写与ext2/ext3/etc/etc文件系统超级块完全匹配的结构。
那么你必须知道在哪里可以找到超级块(这里有lseek() )。
此外,您还需要将磁盘文件名file_descriptor传递给函数。
因此,您可以使用
int fd = open(argv[1], O_RDONLY);
struct superblock * sb = read_superblock(fd);https://stackoverflow.com/questions/33584018
复制相似问题