【问题标题】:Linux: Support File OperationsLinux:支持文件操作
【发布时间】:2026-01-09 21:10:02
【问题描述】:

是否有人可能是一个很好的来源,其中描述了所有可用的文件操作,如 fopen、fread、mkdir 等?当我搜索 Linux 文件操作时,大多数页面都会向我解释文件系统层次结构的样子。

【问题讨论】:

  • linux 或 unix 系统调用与 stio 函数不同。你要买什么 ?打开、关闭、取消链接、写入等...或 fopen、fwrite、fread 等...?

标签: linux filesystems


【解决方案1】:

您所询问的函数实际上分为几类 - 文件流 I/O(fopenfread 等)、低级文件描述符 I/O(openread等)和文件系统/目录操作(chownmkdir 等)。

有关文件流 I/O 功能的概述,请参阅man stdio

要在 Google 上搜索,请尝试使用“posix 文件 api”而不是“linux 文件操作”。

您也可以查看GNU C Libary Manual

【讨论】:

    【解决方案2】:

    我不确定这是否有帮助,但这直接来自内核源代码:

    struct file_operations {
        struct module *owner;
        loff_t (*llseek) (struct file *, loff_t, int);
        ssize_t (*read) (struct file *, char *, size_t, loff_t *);
        ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
        int (*readdir) (struct file *, void *, filldir_t);
        unsigned int (*poll) (struct file *, struct poll_table_struct *);
        int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
        int (*mmap) (struct file *, struct vm_area_struct *);
        int (*open) (struct inode *, struct file *);
        int (*flush) (struct file *);
        int (*release) (struct inode *, struct file *);
        int (*fsync) (struct file *, struct dentry *, int datasync);
        int (*fasync) (int, struct file *, int);
        int (*lock) (struct file *, int, struct file_lock *);
        ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
        ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
        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);
    };
    

    文件系统通常将其所有实现注册到这些回调中。

    【讨论】:

    • 这些级别低于您通常想要的级别。其中一些函数似乎无法从用户空间访问,作为用户空间开发人员,您经常会使用在 libc 而不是内核中实现的 fopen 和 fread 等函数。
    • 是的,可能有点忘乎所以。但至少它以某种小的方式为答案做出了贡献:)
    【解决方案3】:

    是的——使用手册页。 man fopenman freadman mkdir 等,将描述这些函数的用法。许多手册页也有一个“另见”部分,它将引导您访问相关功能的手册页,有点像原始的 Wikipedia。 :)

    【讨论】:

    • 谢谢,但我想了解一下所有现有的 linux 文件系统函数,如 fopen、fread、mkdir、chown 等
    【解决方案4】:

    在堆栈的不同级别上有几个文件操作 API,例如POSIX API、Standard C API、Linux VFS API(正如 Jeremy 提到的)和 FUSE API。所有的 API 都或多或少做着相同的事情,但细节却大不相同。

    这两个API对于普通用户来说是最重要的。

    一本关于该主题的好书是“Advanced Programming in the UNIX Environment" Stevens 和 Rago”

    【讨论】:

      【解决方案5】:

      使用man 2 openman 2 mkdir。本手册页底部是相关命令的名称。

      或者,如果您搜索此手册页的可浏览版本,您可以尝试here

      【讨论】: