|
我最近打算做个搜索引擎,涉及到通过散列表快速定位文件在磁盘中位置的问题,为此需要了解linux下如何直接定位和读取文件在磁盘上的位置
为此我查找了一下并找到了linux提供的两个struct:
//file 每个打开的文件, socket 接口都对应一个文件数据结构。
[code:1]
struct file
{
mode_t f_mode;
loff_t f_pos;
unsigned short f_flags;
unsigned short f_count;
unsigned long f_reada, f_ramax, f_raend, f_ralen,f_rawin;
struct file *f_next, *f_prev;
int f_owner;
/* pid or -pgrp where SIGIO should be sent */
struct inode * f_inode;
//~~~~~~~~~~~~~~~~
struct file_operations * f_op;
unsigned long f_version;
void *private_data;
/* needed for tty driver, and maybeothers */
};
[/code:1]
//inode VFSinode 数据结构包含一个磁盘中的文件或目录的信息。(从逻辑概念 “文件到物理上的”数据块“映射。)
[code:1]
struct inode
{
kdev_t i_dev;
unsigned long i_ino;
umode_t i_mode;
nlink_t i_nlink;
user_id_t i_uid;
gid_t i_gid;
kdev_t i_rdev;
off_t i_size;
time_t i_atime;
time_t i_mtime;
time_t i_ctime;
unsigned longi_blksize;
unsigned long i_blocks;
~~~~~~~~~~~~~~~~~~~
unsigned long i_version;
unsigned long i_nrpages;
struct semaphore i_sem;
struct inode_operations *i_op;
struct super_block *i_sb;
struct wait_queue *i_wait;
struct file_lock *i_flock;
structvm_area_struct *i_mmap;
struct page *i_pages;
struct dquot *i_dquot[MAXQUOTAS];
struct inode *i_next, *i_prev;
struct inode *i_hash_next, *i_hash_prev;struct inode *i_bound_to,*i_bound_by;
struct inode *i_mount;
unsigned shorti_count;
unsigned char i_lock;
unsigned char i_dirt;
unsigned char i_pipe;
unsigned char i_sock;
unsigned char i_seek;
unsigned char i_update;
unsigned short i_writecount;
union
{
struct pipe_inode_info pipe_i;
struct minix_inode_info minix_i;
struct ext_inode_info ext_i;
struct ext2_inode_info ext2_i;
struct hpfs_inode_info hpfs_i;
struct msdos_inode_info msdos_i;
struct umsdos_inode_info umsdos_i;
struct iso_inode_info isofs_i;
struct nfs_inode_info nfs_i;
struct xiafs_inode_info xiafs_i;
struct sysv_inode_infosysv_i;
struct affs_inode_info affs_i;
struct ufs_inode_info ufs_i;
struct socket socket_i;
void *generic_ip;
} u;
};
[/code:1]
我个人以为这个struct file可能是<stdio.h>里提供的FILE,这样通过访问FILE::f_inode->l_blocks就可以记录文件在磁盘中的位置,然后就可以直接访问文件了
但是写了一个测试的代码 :
[code:1]
/*test.cpp*/
#include<stdio.h>
#include<iostream.h>
int main()
{
FILE* fp;
fp=fopen("a.txt","w");
fputs("hello",fp);
fclose(fp);
fp=fopen("a.txt","r");
struct inode * inode;
inode=fp->f_inode;
unsigned long blksize=inode->i_blksize;
unsigned long blocks=inode->i_blocks;
struct inode_operations *op=inode->i_op;
cout<<"block size:"<<blksize<<endl;
return 1;
}
[/code:1]
发现有点问题
编译器报错:
test.cpp: In function `int main()':
test.cpp:11: `struct _IO_FILE' has no member named `f_inode'
test.cpp:12: invalid use of undefined type `struct inode'
test.cpp:10: forward declaration of `struct inode'
test.cpp:13: invalid use of undefined type `struct inode'
test.cpp:10: forward declaration of `struct inode'
test.cpp:14: invalid use of undefined type `struct inode'
test.cpp:10: forward declaration of `struct inode'
发现原来的猜想可能有点错误,就是说struct file 和 struct inode 如何能访问到的问题
不知道哪位做过类似的工作,可否提示一二 |
|