readdir函数解析

article/2025/10/9 2:20:58

函数原型:

struct dirent *readdir(DIR *dirp);

首先纠正一个很多人都错误理解的事实,readdir不是系统调用,它是glibc的封装函数,而且readdir系统调用是存在的,原型如下:

int readdir(unsigend int fd, struct old_linux_dirent *dirp, unsigned int count);

glibc的readdir所调用的系统调用不是readdir而是getdents系统调用。此处说明一下为什么采用封装getdents而不是readdir系统调用,最重要的一个理由是readdir系统调用每次只会读入一个目录项,而getdents会一下子读入尽可能多的目录项至缓冲。我先分析readdir系统调用的实现,具体的代码如下:

复制代码

 1 SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
 2                 struct old_linux_dirent __user *, dirent, unsigned int, count)
 3 {
 4         int error;
 5         struct fd f = fdget(fd);
 6         struct readdir_callback buf = {
 7                 .ctx.actor = fillonedir,
 8                 .dirent = dirent
 9         };
10 
11         if (!f.file)
12                 return -EBADF;
13 
14         error = iterate_dir(f.file, &buf.ctx);
15         if (buf.result)
16                 error = buf.result;
17 
18         fdput(f);
19         return error;
20 }

复制代码

6-9行:设置目录项填充函数为fillonedir,filonedir的具体实现不分析,只需知道每次只填充一个目录项即可(作为替代,将会分析更加复杂的filldir函数)

14行:iterate_dir是vfs的封装函数,该函数调用具体的文件系统的iterate函数填充目录

    注: 3.11之前并不使用iterate作为读目录的函数而是使用readdir函数

总结:readdir系统调用忽略了count参数并且每次只读一个目录项

接下来分析glibc的readdir函数实现,这个过程可能比较复杂,有兴趣的可以看看,首先给出readdir的实现:

复制代码

 1 DIRENT_TYPE *
 2 __READDIR (DIR *dirp)
 3 {
 4   DIRENT_TYPE *dp;
 5   int saved_errno = errno;
 6 
 7 #ifndef NOT_IN_libc
 8   __libc_lock_lock (dirp->lock);
 9 #endif
10 
11   do
12     {
13       size_t reclen;
14 
15       if (dirp->offset >= dirp->size) 
16         {
17           /* We've emptied out our buffer.  Refill it.  */
18 
19           size_t maxread;
20           ssize_t bytes;
21 
22 #ifndef _DIRENT_HAVE_D_RECLEN
23           /* Fixed-size struct; must read one at a time (see below).  */
24           maxread = sizeof *dp;
25 #else
26           maxread = dirp->allocation;

http://chatgpt.dhexx.cn/article/7GYpks7S.shtml

相关文章

C/C++的readdir和readdir_r函数(遍历目录)

1.首先要打开目录文件 DIR *opendir( const char *name); DIR *fdopendir( int fd); 2.读取目录文件信息的函数 注意:这是个库函数 struct dirent *readdir( DIR *dirp); int readdir_r( DIR *dirp, struct dirent *entry, struct dirent **res…

Linux下 C 遍历目录(opendir,readdir函数)

opendir()函数: 头文件&#xff1a; #include <sys/types.h> #include <dirent.h> 函数原型&#xff1a; Dir* opendir(const char* pathname); 函数功能&#xff1a; 获取pathname目录下的所有文件和目录的列表&#xff0c;如果pathname是个文件或失败则返…

linux——读取文件(read)

ssize_t read(int fd, void *buf, size_t count); 将fd中的内容读取到buf中。 代码&#xff1a; #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <string.h> #inc…

linux的readdir和readdir_r函数

1.首先要打开目录文件 DIR *opendir( const char *name); DIR *fdopendir( int fd); 2.读取目录文件信息的函数 注意&#xff1a;这是个库函数 struct dirent *readdir( DIR *dirp); int readdir_r( DIR *dirp, struct dirent *entry, struct dirent **res…

readdir函数

readdir会不断读取目中的文件及目录&#xff0c;但不会读子目录中的文件。 #include <sys/types.h> #include <dirent.h> #include <stdio.h> #include <stdlib.h> #include <dirent.h> int main() {DIR *dirp opendir("/home/python/Des…

readdir不保证读取的文件顺序

readdir用于读取某个文件夹中的全部文件或文件夹&#xff0c;相当于ls。 但是readdir并不保证读取后的文件顺序&#xff0c;在不同的操作系统上可能有不同的顺序。 在某些场景下需要注意&#xff0c;比如读取配置文件时&#xff0c;可能会根据配置文件进行一些初始化&#xf…

Ubuntu 18.04安装远程桌面

Ubuntu 18.04安装远程桌面 陈拓 2021/08/05-2020/08/08 1. Putty登录 IP地址 192.168.0.103 登录账户 ccdc xxxxxxxx 2. Ubuntu 18.04安装桌面 如果安装的系统已经带桌面跳过这一步。 2.1 查看linux系统版本 lsb_release -a 2.2 安装桌面 sudo apt-get install ubun…

ubuntu远程桌面连接windows

ubuntu远程桌面连接windows 1&#xff1a;使用ubuntu自带软件remmina 2&#xff1a;打开该软件后&#xff0c;点击长方形 3&#xff1a; 服务器&#xff1a;windows电脑内网ip 用户名&#xff1a;电脑用户名 密码&#xff1a;电脑用户名的登陆密码 点击右下角&#xff1a;保存…

Ubuntu 远程桌面的方式

提示&#xff1a;仅仅是按照记忆所写的笔记&#xff0c;如果你看到这篇笔记&#xff0c;按照操作出了问题&#xff0c;评论就好了&#xff0c;我会完善一下。笔记内容以外的问题不要评论&#xff0c;我不管。 vino & dconf-editor 该方式适用于ubuntu desktop 18.04 及以后…

Ubuntu18.04远程桌面连接

一、安装 xrdp、tightvncserver sudo apt-get install tightvncserver xrdp二、安装xubuntu-desktop sudo apt-get install xubuntu-desktop三、修改配置文件 echo xfce4-session >~/.xsession sudo vi /etc/xrdp/startwm.sh在下图位置加入"xfce4-session" 在…

多ubuntu主机远程桌面连接方案

一、需求背景 公司有一批ubuntu的主机&#xff0c;需要研发远程上去进行代码调试&#xff0c;普通的远程桌面方式不易于管理&#xff0c;并且无法进行连接控制。 二、方案制定 基于web的远程方案有Guacamole、NoVNC两种方案&#xff0c;但都不利于后期工具与公司整体的SSO进行对…

Ubuntu Server 18.04安装远程桌面并连接

文章目录 一、安装桌面环境Xfce二、安装 Xrdp三、设置root密码四、连接Xrdp服务器五、设置终端 尝试了很多种方法&#xff0c;折腾了一晚上终于搞出来了呜呜…顺便记录一下,以免下次忘记! 一、安装桌面环境Xfce Ubuntu 服务器通常使用命令行进行管理&#xff0c;并且默认没有安…

Windows10远程桌面Ubuntu16.04

自己的笔记本配置太低&#xff0c;有很多图形界面的软件&#xff0c;需要在服务器上运行&#xff0c;通常只用SSH方式访问的命令行方式是无法实现的。 虽然配置XShell XManager可以实现打开图形程序&#xff0c;但速度之慢&#xff0c;即使内网也无法忍受。 今天来推荐一个更…

Ubuntu 安装远程桌面

转自&#xff1a;https://blog.csdn.net/heyangyi_19940703/article/details/77994416 1.安装xrdp软件: 运行Terminal,执行以下命令&#xff1a; sudo apt-get -y install xfce4 xrdp vnc4server 2.安装完成&#xff0c;查看下相关软件包 执行命令&#xff1a; dpkg -L xrdp…

Ubuntu 系统下如何远程访问 Windows 桌面 ?

你一定听说过 Windows 应用程序远程桌面连接。该应用程序系统自带不用安装&#xff0c;并允许您远程访问另一台 PC 或服务器。它使用远程桌面协议建立远程桌面连接会话。 一些 Linux 发行版可能会提供 RDP 客户端来连接到 Windows 系统。但是&#xff0c;对于某些 linux 发行版…

ubuntu20.04远程桌面这样装

我的记录本 安装了新系统之后&#xff0c;直接就进行了远程桌面的测试1.系统设置2.安装xrdp3.安装dconf-editor并设置4.切换windows系统打开远程桌面 安装了新系统之后&#xff0c;直接就进行了远程桌面的测试 更新源都没有动&#xff0c;直接进行远程桌面的测试。 参考文献&a…

Windows 远程桌面 Ubuntu

参考 Windows远程桌面工具连接Ubuntu系统使用总结_CHH3213的博客-CSDN博客_远程连接ubuntu 开启ssh服务&#xff08;非必须 查看ssh是否已经开启 sudo ps -e | grep ssh 如果最后返回是sshd&#xff0c;证明ssh已经开启&#xff0c;跳到第四步 第二步&#xff0c;如果没有…

ubuntu使用VNC实现远程桌面

2022.11更新 目前我已经放弃VNC了&#xff0c;虽然局域网连着画面还行&#xff0c;但是太容易出bug了。 现在更推荐向日葵或Todesk这些远程控制软件&#xff0c;虽然画面糊一点&#xff0c;但是稳定&#xff0c;而且不受局域网限制。 前言 我是在树莓派4B上安装的Ubuntu20.10&…

Windows10远程登陆Ubuntu桌面

简介 我们在使用Ubuntu系统有时可能需要看Ubuntu上的仿真界面和可视化数据&#xff0c;可能会有这样一个需求&#xff1a;使用Windows系统接入Ubuntu&#xff0c;本文章提供一种方法&#xff0c;使用Windows自带的远程桌面Ubuntu安装VNC解决问题。 Ubuntu上的配置 1.下载配置…

window10远程桌面控制Ubuntu系统

Windows操作系统作为全球使用最多的个人操作系统&#xff0c;在我们身边随处可见&#xff0c;但放眼各类电子设备的操作系统&#xff0c;windows并不是一家独大&#xff0c;服务器系统大多基于Linux系统开发、手机操作系统几乎都是安卓、更不用说还有苹果的iOS、树莓派、Ubuntu…