用户空间和内核空间文件操作 file_operations

内核空间:

struct file_operations {

struct module *owner;

loff_t (*llseek) (struct file *, loff_t, int);

ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);

ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);

ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);

int (*readdir) (struct file *, void *, filldir_t);

unsigned int (*poll) (struct file *, struct poll_table_struct *);

long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);

long (*compat_ioctl) (struct file *, unsigned int, unsigned long);         (对应的用户空间函数是ioctl(int fd, int command, (char *) argstruct);

int (*mmap) (struct file *, struct vm_area_struct *);

int (*open) (struct inode *, struct file *);

int (*flush) (struct file *, fl_owner_t id);

int (*release) (struct inode *, struct file *);

int (*fsync) (struct file *, loff_t, loff_t, int datasync);

int (*aio_fsync) (struct kiocb *, int datasync);

int (*fasync) (int, struct file *, int);

int (*lock) (struct file *, int, struct file_lock *);

ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);

unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);

int (*check_flags)(int);

int (*flock) (struct file *, int, struct file_lock *);

ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);

ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);

int (*setlease)(struct file *, long, struct file_lock **);

long (*fallocate)(struct file *file, int mode, loff_t offset,

  loff_t len);

};

 

 

用户空间对应的操作接口:

1、打开文件

int open( const char *pathname,int flags, int mode);  

 

返回值:为一个文件句柄(fd),供readwrite等操作。


参数:

pathname打开的文件所在路径字符串。如

String filename = "/sdcard/test.txt";  

flags文件打开的方式

flag之间可以作运算,如

open(filename, O_CREAT  | O_RDWRmode);  

常用flags
O_RDONLY 以只读方式打开文件
O_WRONLY 以只写方式打开文件
O_RDWR 以可读写方式打开文件。上述三种旗标是互斥的,也就是不可同时使用,但可与下列的旗标利用OR(|)运算符组合。
O_CREAT 若欲打开的文件不存在则自动建立该文件。
O_TRUNC 若文件存在并且以可写的方式打开时,此标志位会令文件长度重新清为0,也就是说文件内容清空。
O_APPEND 当读写文件时会从文件尾开始移动,也就是所写入的数据会以附加的方式加入到文件后面。
O_NONBLOCK 以不可阻断的方式打开文件,也就是无论有无数据读取或等待,都会立即返回进程之中。
O_SYNC 以同步的方式打开文件。
O_NOFOLLOW 如果参数pathname所指的文件为一符号连接,则会令打开文件失败。
O_DIRECTORY 如果参数pathname所指的文件并非为一目录,则会令打开文件失败。

 

 

mode文件存储权限

S_IRWXU00700 权限,代表该文件所有者具有可读、可写及可执行的权限。
S_IRUSR S_IREAD00400权限,代表该文件所有者具有可读取的权限。
S_IWUSR S_IWRITE00200权限,代表该文件所有者具有可写入的权限。
S_IXUSR S_IEXEC00100权限,代表该文件所有者具有可执行的权限。
S_IRWXG 00070权限,代表该文件用户组具有可读、可写及可执行的权限。
S_IRGRP 00040 权限,代表该文件用户组具有可读的权限。
S_IWGRP 00020权限,代表该文件用户组具有可写入的权限。
S_IXGRP 00010 权限,代表该文件用户组具有可执行的权限。
S_IRWXO 00007权限,代表其他用户具有可读、可写及可执行的权限。
S_IROTH 00004 权限,代表其他用户具有可读的权限
S_IWOTH 00002权限,代表其他用户具有可写入的权限。
S_IXOTH 00001 权限,代表其他用户具有可执行的权限。


3、文件的读(read)操作

int read(int fd, unsigned char *buf, int size);   

 

返回值:返回实际读取到的字节数,如果返回0,表示已到达文件尾或是无可读取的数据,此外文件读写位置会随读取到的字节移动。

参数

fd:表示文件句柄,是由open函数得到

bufread()函数会把fd所指的文件传送count个字节到buf指针所指的内存中

size:要读取的字节数

 


4、写入操作

int write (int fd, const unsigned char *buf, int size);   


返回值 :如果成功write(),就会返回实际写入的字节数。当有错误发生时则返回-1

参数

fd:同上

buf:将要写入到文件里面的内容。

size:要写入的字节数

 

5、跳转操作

int64_t seek(int fd, int64_t pos, int whence)  


返回值:成功时则返回目前的读写位置,也就是距离文件开头多少个字节,若有错误则返回-1

参数

fd:同上

pos:跳转的相对量,可正可负,表示相对位置的前后关系

whence:跳转的方向,whence取值如下所示

int SEEK_SET   =        0;//将读写位置指向文件头后再增加offset个位移量。  

int SEEK_CUR   =        1;//以目前的读写位置往后增加offset个位移量。  

int EEK_END    =        2;//将读写位置指向文件尾后再增加offset个位移量。  


注:当size参数=0;whence = SEEK_END;时返回值即为文件大小。

 

6、关闭操作

int close(int fd)  

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
file_operations是Linux内核中的一个结构体,用于定义文件操作相关的函数指针,包括文件的打开、关闭、读取、写入等操作。 该结构体的定义如下: ```c struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); int (*open) (struct inode *, struct file *); int (*release) (struct inode *, struct file *); int (*flush) (struct file *, fl_owner_t id); int (*fsync) (struct file *, int datasync); int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); int (*check_flags)(int); int (*flock) (struct file *, int, struct file_lock *); ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int); ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int); int (*setlease)(struct file *, long, struct file_lock **, void **); long (*fallocate)(struct file *, int, loff_t, loff_t); }; ``` 其中,各个函数指针的含义如下: - `owner`:指向模块的指针,用于内存管理。 - `llseek`:将文件指针定位到给定的偏移量。 - `read`:从文件中读取数据。 - `write`:向文件中写入数据。 - `open`:打开文件。 - `release`:关闭文件。 - `flush`:刷新文件缓存。 - `fsync`:将文件同步到磁盘。 - `fasync`:设置异步通知。 - `lock`:对文件进行加锁。 - `sendpage`:将文件中的一页发送给网络。 - `get_unmapped_area`:为文件映射内存。 - `check_flags`:检查文件打开标志。 - `flock`:锁定文件。 - `splice_write`:将数据从文件传输到管道。 - `splice_read`:将数据从管道传输到文件。 - `setlease`:设置文件租约。 - `fallocate`:为文件预分配磁盘空间file_operations结构体定义了Linux内核文件系统的接口,让不同的文件系统能够共用相同的接口,从而实现更好的兼容性和可移植性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值