Directories can be read by anyone who has access permission to read the directory. But only the kernel can write to a directory, to preserve file system sanity(心智健康)(只有内核可以写如目录来保证文件系统的正常运作).
目录的写权限和执行权限可以让我们在目录中创建新文件和移除文件。但是并不能让我们写入目录它本身。
目录的实际形式取决于Unix系统实现和文件系统的设计。早期的系统中,使用简单的结构:目录entry有16字节,14byte for filename, 2byte for inode number。随着更长的文件名加入,每个entry都是可变的长度。
为简化读目录操作,POSIX1规定了一系列directory routines。许多实现访问应用程序使用read来访问目录的内容。
opendir, fdopendir - open a directory
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
//Return: pointer if OK, NULL on error

这篇博客介绍了Linux中读取目录的相关函数,如opendir和fdopendir,以及dirent结构体。文章阐述了目录的访问权限和其在不同Unix系统中的实现,并通过示例代码展示了如何使用这些函数遍历文件系统,同时提到了在处理符号链接时的注意事项。
最低0.47元/天 解锁文章
924

被折叠的 条评论
为什么被折叠?



