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