关于硬链接 [英] Regarding Hard Link
问题描述
mkdir(2)
和 rmdir(2)
。 mkdir(1)
程序是SUID根目录,并使用 mknod(2)
系统调用来创建目录,系统调用。和。 code>在新目录中。 链接(2)
系统调用只允许root做这件事。因此,当时(大约在1978年),超级用户有可能创建到目录的链接,但只有超级用户才允许这样做,以确保循环或其他缺失链接没有问题。例如,如果系统在目录部分创建时崩溃,那么就有诊断程序可以提取这些碎片。
您可以在贝尔实验室上找到Unix 7th Edition手册。第2节和第3节没有 mkdir(2)
和 rmdir(2)
。您使用 mknod(2)
系统调用来创建目录:
$ b
NAME
mknod - 创建目录或特殊文件
概要
mknod(name,mode,addr)
char * name;
描述
Mknod创建一个新文件其名称是由名称指向的以空字符结尾的字符串。新文件(包括目录和特殊文件位)的模式
是从模式初始化的。 (
的保护部分模式被进程的模式掩码修改;请参阅umask(2))。 i节点
的第一个块指针是从addr初始化的。对于普通的文件和目录,addr通常是零。在特殊的
文件的情况下,addr指定哪个特殊文件。
Mknod只能由超级用户调用。
另见
mkdir(1),mknod(1),filsys(5)
$ b $ h3> DIAGNOSTICS
如果文件已经被创建,则返回零; - 1如果文件已经存在或用户不是超级用户。
链接2)
指出:
诊断
链接时返回; - 当name1找不到时返回-1;当name2已经存在
;当name2的目录不能被写入时;当试图通过
链接到一个目录时,超级用户以外的用户;当试图链接到另一个文件系统上的文件时;当
文件有太多的链接。
unlink(2)
诊断
通常返回零; - 1表示该文件不存在,其目录不能写入,
或文件包含当前正在使用的纯过程文本。
文件本身不需要写入权限。
ln(1)
命令记录:
禁止链接到目录或链接到文件系统。
mkdir(1)
命令的手册页注意: b
$ b
目录本身的标准条目。,父目录的..
是自动创建的
如果没有这些链接,就不可能创建目录。 b
$ b
现在, mkdir(2)
和 rmdir 2)
系统调用是标准的,允许任何用户创建和删除目录,保留正确的语义。不再需要允许用户创建硬链接到目录。由于引入了符号链接,所以这是非常正确的 - 它们不在UNIX的第7版中,而是在很早的时候就在UNIX的BSD版本中。
$ b
对于普通的目录, ..
条目明确地链接到 (单独的)父目录。如果在不同的目录中有相同目录的两个硬链接(两个名称),那么 ..
入口点在哪里?据推测,原来的父目录 - 大概没有办法从链接目录到达其他父目录。这是一个不对称,可能会造成麻烦。通常,如果你这样做:
chdir(./subdir);
chdir(..);
(其中 ./ subdir
不是符号链接),那么你将回到你开始的目录。如果 ./subdir
是一个硬链接到某个目录的其他目录,那么你将在第二个 chdir( )
。您必须在显示 chdir()
操作之前和之后显示一对 stat()
。
Can somebody please explain me why the kernel doesn't allow us to make a hard link to a directory. Whether it is because it breaks the rule of directed acyclic graph structure of the file-system or it is because of some other reason. What other complications come if it allows that?
解决方案 Back in the days of 7th Edition (or Version 7) UNIX, there were no system calls mkdir(2)
and rmdir(2)
. The mkdir(1)
program was SUID root, and used the mknod(2)
system call to create the directory and the link(2)
system call to make the entries for .
and ..
in the new directory. The link(2)
system call only allowed root to do that. Consequently, way back then (circa 1978), it was possible for the superuser to create links to directories, but only the superuser was permitted to do so to ensure that there were no problems with cycles or other missing links. There were diagnostic programs to pick up the pieces if the system crashed while a directory was partly created, for example.
You can find the Unix 7th Edition manuals at Bell Labs. Sections 2 and 3 are devoid of mkdir(2)
and rmdir(2)
. You used the mknod(2)
system call to make the directory:
NAME
mknod – make a directory or a special file
SYNOPSIS
mknod(name, mode, addr)
char *name;
DESCRIPTION
Mknod creates a new file whose name is the null-terminated string pointed to by name. The mode of
the new file (including directory and special file bits) is initialized from mode. (The protection part of
the mode is modified by the process’s mode mask; see umask(2)). The first block pointer of the i-node
is initialized from addr. For ordinary files and directories addr is normally zero. In the case of a special
file, addr specifies which special file.
Mknod may be invoked only by the super-user.
SEE ALSO
mkdir(1), mknod(1), filsys(5)
DIAGNOSTICS
Zero is returned if the file has been made; – 1 if the file already exists or if the user is not the superuser.
The entry for link(2)
states:
DIAGNOSTICS
Zero is returned when a link is made; – 1 is returned when name1 cannot be found; when name2 already
exists; when the directory of name2 cannot be written; when an attempt is made to link to a directory by
a user other than the super-user; when an attempt is made to link to a file on another file system; when a
file has too many links.
The entry for unlink(2)
states:
DIAGNOSTICS
Zero is normally returned; – 1 indicates that the file does not exist, that its directory cannot be written,
or that the file contains pure procedure text that is currently in use. Write permission is not required on
the file itself. It is also illegal to unlink a directory (except for the super-user).
The manual page for the ln(1)
command noted:
It is forbidden to link to a directory or to link across file systems.
The manual page for the mkdir(1)
command notes:
Standard entries, '.', for the directory itself, and '..'
for its parent, are made automatically.
This would not be worthy of comment were it not that it was possible to create directories without those links.
Nowadays, the mkdir(2)
and rmdir(2)
system calls are standard and permit any user to create and remove directories, preserving the correct semantics. There is no longer a need to permit users to create hard links to directories. This is doubly true since symbolic links were introduced - they were not in 7th Edition UNIX, but were in the BSD versions of UNIX from quite early on.
With normal directories, the ..
entry unambiguously links back to the (single, solitary) parent directory. If you have two hard links (two names) for the same directory in different directories, where does the ..
entry point? Presumably, to the original parent directory - and presumably there is no way to get to the 'other' parent directory from the linked directory. That's an asymmetry that can cause trouble. Normally, if you do:
chdir("./subdir");
chdir("..");
(where ./subdir
is not a symbolic link), then you will be back in the directory you started from. If ./subdir
is a hard link to a directory somewhere else, then you will be in a different directory from where you started after the second chdir()
. You'd have to show that with a pair of stat()
calls before and after the chdir()
operations shown.
这篇关于关于硬链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!