mkdir, mkdirat - create a directory
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>
int mkdirat(int dirfd, const char *pathname, mode_t mode);
//Return: 0 if OK, -1 on error
These functions create a new, empty directory. The entries for dot and dot-dot are created automatically. The specified file access permissions, mode, are modified by the file mode creation mask of the process.
(这些函数创建新的空目录,’.’ ‘..’entry自动创建。mode
访问权限由进程的mode creation mask
修改)
普遍的错误就是指定给目录的访问权限
mode
和一般文件一样。文件拥有read
和write
权限。这对于目录是不够的,因为如果想要读取目录中的文件名,目录必须要拥有execute
权限Linux 3.2.0 also have the new directory inherit the set-group-ID bit from the parent directory. Files created in the new directory will then inherit the group ID of that directory. With Linux, the file system implementation determines whether this behavior is supported. For example, the ext2, ext3, and ext4 file systems allow this behavior to be controlled by an option to the
mount(1)
command. With the Linux implementation of the UFS file system, however, the behavior is not selectable; it inherits the set-group-ID bit tomimic(模仿的)
the historical BSD implementation, where the group ID of a directory is inherited from the parent directory.(ext2/3/4可通过mount来选择是否新目录继承父目录的set-group-ID
bit,而UFS文件系统中是不可选的,一定会继承)
mkdirat
The mkdirat function is similar to the mkdir function. When the dirfd argument has the special value AT_FDCWD, or when the pathname argument specifies an absolute pathname, mkdirat behaves exactly like mkdir. Otherwise, the dirfd argument is an open directory from which relative pathnames will be evaluated.
rmdir - delete a directory
An empty directory is deleted with the rmdir function. Recall that an empty directory is one that contains entries only for dot and dot-dot.
#include <unistd.h>
int rmdir(const char *pathname);
//Returns: 0 if OK, -1 on error
如果通过该调用rmdir
使该目录的link count
为0,并且没有其他进程打开该目录,那么该目录占据的空间就会被释放。If one or more processes have the directory open when the link count reaches 0, the last link is removed and the dot and dot-dot entries are removed before this function returns.
Additionally, no new files can be created in the directory. The directory is not freed, however, until the last process closes it. (Even though some other process has the directory open, it can’t be doing much in the directory, as the directory had to be empty for the rmdir function to succeed.)