符号链接背后有什么?

时间:2021-07-28 16:27:01

How are symbolic links managed internally by UNIX/Linux systems. It is known that a symbolic link may exist even without an actual target file (Dangling link). So what is that which represents a symbolic link internally.

如何通过UNIX / Linux系统在内部管理符号链接。众所周知,即使没有实际的目标文件(悬空链接),也可能存在符号链接。那么内部代表符号链接的是什么呢?

In Windows, the answer is a reparse point.

在Windows中,答案是重新分析点。

Questions:

问题:

Is the answer an inode in UNIX/Linux?

答案是UNIX / Linux中的inode吗?

If yes, then will the inode number be same for target and links?

如果是,那么目标和链接的inode编号是否相同?

If yes, can the link inode can have permissions different from that of target's inode (if one exists)?

如果是,链接inode可以具有与目标的inode(如果存在)不同的权限吗?

2 个解决方案

#1


14  

It is not about UNIX/Linux but about filesystem implementation - but yes, Unix/Linux uses inodes at kernel level and filesystem implementations have inodes (at least virtual ones).

它不是关于UNIX / Linux而是关于文件系统实现 - 但是,Unix / Linux在内核级别使用inode而文件系统实现具有inode(至少是虚拟的)。

In the general, symbolic links are simply files (btw, directories are also files), that have:

通常,符号链接只是文件(顺便说一句,目录也是文件),它们具有:

  • the flag file-type in the "inode" that tells to the system this file is a "symbolic link"
  • “inode”中的标志文件类型告诉系统该文件是“符号链接”
  • file-content: path to the target - in other words: a symbolic link is simply a file which contains a filename with a flag in the inode.
  • file-content:目标的路径 - 换句话说:符号链接只是一个文件,其中包含inode中带有标志的文件名。

Virtual filesystems can have symbolic links too, so, check FUSE or some other filesystem implementation sources. (ext2/ext3/ufs..etc)

虚拟文件系统也可以具有符号链接,因此,请检查FUSE或其他一些文件系统实现源。 (EXT2 / EXT3 / ufs..etc)

So,

所以,

Is the answer an inode in UNIX/Linux?

答案是UNIX / Linux中的inode吗?

depends on filesystem implementation, but yes, generally the inode contains a "file-type" (and owners, access rights, timestamps, size, pointers to data blocks). There are filesystems that don't have inodes (in a physical implementation) but have only "virtual inodes" for maintaining compatibility with the kernel.

取决于文件系统实现,但是,通常inode包含“文件类型”(以及所有者,访问权限,时间戳,大小,指向数据块的指针)。有些文件系统没有inode(在物理实现中),但只有“虚拟inode”用于维护与内核的兼容性。

If yes, then will the inode number be same for target and links?

如果是,那么目标和链接的inode编号是否相同?

No. Usually, the symlink is a file with its own inode, (with file-type, own data blocks, etc.)

通常,符号链接是具有自己的inode的文件(具有文件类型,自己的数据块等)

If yes, can the link inode can have permissions different from that of target's inode(if one exists)?

如果是,链接inode可以具有与目标的inode(如果存在)不同的权限吗?

This is about how symlink files are handled. Usually, the kernel doesn't allow changes to the symlink permissions - and symlinks always have default permissions. You could write your own filesystem that would allow different permissions for symlinks, but you would get into trouble because common programs like chmod don't change permissions on symlinks themselves, so making such a filesystem would be pointless anyway)

这是关于如何处理符号链接文件的。通常,内核不允许更改符号链接权限 - 并且符号链接始终具有默认权限。您可以编写自己的文件系统,允许符号链接的不同权限,但是您会遇到麻烦,因为像chmod这样的常见程序本身不会更改符号链接的权限,所以制作这样的文件系统无论如何都会毫无意义)

To understand the difference between hard links and symlinks, you should understand directories first.

要理解硬链接和符号链接之间的区别,您应该首先了解目录。

Directories are files (with differentiated by a flag in the inode) that tell the kernel, "handle this file as a map of file-name to inode_number". Hard-links are simply file names that map to the same inode. So if the directory-file contains:

目录是文件(由inode中的标志区分)告诉内核“将此文件作为文件名映射到inode_number”。硬链接只是映射到同一inode的文件名。所以如果目录文件包含:

file_a: 1000
file_b: 1001
file_c: 1000

the above means, in this directory, are 3 files:

上述方法,在此目录中,是3个文件:

  • file_a described by inode 1000
  • file_a由inode 1000描述
  • file_b described by inode 1001 and
  • file1由inode 1001和
  • file_c again described by inode 1000 (so it is a hard link with file_a, not hardlink to file_a - because it is impossible to tell which filename came first - they are identical).
  • file_c再次由inode 1000描述(因此它是与file_a的硬链接,而不是与file_a的硬链接 - 因为无法确定哪个文件名首先出现 - 它们是相同的)。

This is the main difference to symlinks, where the inode of file_b (inode 1001) could have content "file_a" and a flag meaning "this is a symlink". In this case, file_b would be a symlink pointing to file_a.

这是符号链接的主要区别,其中file_b(inode 1001)的inode可以包含内容“file_a”,而标志意味着“这是符号链接”。在这种情况下,file_b将是指向file_a的符号链接。

#2


2  

You can also easily explore this on your own:

您也可以自己轻松探索:

$ touch a
$ ln -s a b
$ ln a c
$ ls -li
total 0
95905 -rw-r--r-- 1 regnarg regnarg 0 Jun 19 19:01 a
96990 lrwxrwxrwx 1 regnarg regnarg 1 Jun 19 19:01 b -> a
95905 -rw-r--r-- 2 regnarg regnarg 0 Jun 19 19:01 c

The -i option to ls shows inode numbers in the first column. You can see that the symlink has a different inode number while the hardlink has the same. You can also use the stat(1) command:

ls的-i选项显示第一列中的inode编号。您可以看到符号链接具有不同的inode编号,而硬链接具有相同的编号。您还可以使用stat(1)命令:

$ stat a
  File: 'a'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 28h/40d Inode: 95905       Links: 2
[...]

$ stat b
  File: 'b' -> 'a'
  Size: 1           Blocks: 0          IO Block: 4096   symbolic link
Device: 28h/40d Inode: 96990       Links: 1
[...]

If you want to do this programmatically, you can use the lstat(2) system call to find information about the symlink itself (its inode number etc.), while stat(2) shows information about the target of the symlink, if it exists. Example in Python:

如果要以编程方式执行此操作,可以使用lstat(2)系统调用来查找有关符号链接本身的信息(其inode编号等),而stat(2)显示有关符号链接的目标的信息(如果存在) 。 Python中的示例:

>>> import os
>>> os.stat("b").st_ino
95905
>>> os.lstat("b").st_ino
96990

#1


14  

It is not about UNIX/Linux but about filesystem implementation - but yes, Unix/Linux uses inodes at kernel level and filesystem implementations have inodes (at least virtual ones).

它不是关于UNIX / Linux而是关于文件系统实现 - 但是,Unix / Linux在内核级别使用inode而文件系统实现具有inode(至少是虚拟的)。

In the general, symbolic links are simply files (btw, directories are also files), that have:

通常,符号链接只是文件(顺便说一句,目录也是文件),它们具有:

  • the flag file-type in the "inode" that tells to the system this file is a "symbolic link"
  • “inode”中的标志文件类型告诉系统该文件是“符号链接”
  • file-content: path to the target - in other words: a symbolic link is simply a file which contains a filename with a flag in the inode.
  • file-content:目标的路径 - 换句话说:符号链接只是一个文件,其中包含inode中带有标志的文件名。

Virtual filesystems can have symbolic links too, so, check FUSE or some other filesystem implementation sources. (ext2/ext3/ufs..etc)

虚拟文件系统也可以具有符号链接,因此,请检查FUSE或其他一些文件系统实现源。 (EXT2 / EXT3 / ufs..etc)

So,

所以,

Is the answer an inode in UNIX/Linux?

答案是UNIX / Linux中的inode吗?

depends on filesystem implementation, but yes, generally the inode contains a "file-type" (and owners, access rights, timestamps, size, pointers to data blocks). There are filesystems that don't have inodes (in a physical implementation) but have only "virtual inodes" for maintaining compatibility with the kernel.

取决于文件系统实现,但是,通常inode包含“文件类型”(以及所有者,访问权限,时间戳,大小,指向数据块的指针)。有些文件系统没有inode(在物理实现中),但只有“虚拟inode”用于维护与内核的兼容性。

If yes, then will the inode number be same for target and links?

如果是,那么目标和链接的inode编号是否相同?

No. Usually, the symlink is a file with its own inode, (with file-type, own data blocks, etc.)

通常,符号链接是具有自己的inode的文件(具有文件类型,自己的数据块等)

If yes, can the link inode can have permissions different from that of target's inode(if one exists)?

如果是,链接inode可以具有与目标的inode(如果存在)不同的权限吗?

This is about how symlink files are handled. Usually, the kernel doesn't allow changes to the symlink permissions - and symlinks always have default permissions. You could write your own filesystem that would allow different permissions for symlinks, but you would get into trouble because common programs like chmod don't change permissions on symlinks themselves, so making such a filesystem would be pointless anyway)

这是关于如何处理符号链接文件的。通常,内核不允许更改符号链接权限 - 并且符号链接始终具有默认权限。您可以编写自己的文件系统,允许符号链接的不同权限,但是您会遇到麻烦,因为像chmod这样的常见程序本身不会更改符号链接的权限,所以制作这样的文件系统无论如何都会毫无意义)

To understand the difference between hard links and symlinks, you should understand directories first.

要理解硬链接和符号链接之间的区别,您应该首先了解目录。

Directories are files (with differentiated by a flag in the inode) that tell the kernel, "handle this file as a map of file-name to inode_number". Hard-links are simply file names that map to the same inode. So if the directory-file contains:

目录是文件(由inode中的标志区分)告诉内核“将此文件作为文件名映射到inode_number”。硬链接只是映射到同一inode的文件名。所以如果目录文件包含:

file_a: 1000
file_b: 1001
file_c: 1000

the above means, in this directory, are 3 files:

上述方法,在此目录中,是3个文件:

  • file_a described by inode 1000
  • file_a由inode 1000描述
  • file_b described by inode 1001 and
  • file1由inode 1001和
  • file_c again described by inode 1000 (so it is a hard link with file_a, not hardlink to file_a - because it is impossible to tell which filename came first - they are identical).
  • file_c再次由inode 1000描述(因此它是与file_a的硬链接,而不是与file_a的硬链接 - 因为无法确定哪个文件名首先出现 - 它们是相同的)。

This is the main difference to symlinks, where the inode of file_b (inode 1001) could have content "file_a" and a flag meaning "this is a symlink". In this case, file_b would be a symlink pointing to file_a.

这是符号链接的主要区别,其中file_b(inode 1001)的inode可以包含内容“file_a”,而标志意味着“这是符号链接”。在这种情况下,file_b将是指向file_a的符号链接。

#2


2  

You can also easily explore this on your own:

您也可以自己轻松探索:

$ touch a
$ ln -s a b
$ ln a c
$ ls -li
total 0
95905 -rw-r--r-- 1 regnarg regnarg 0 Jun 19 19:01 a
96990 lrwxrwxrwx 1 regnarg regnarg 1 Jun 19 19:01 b -> a
95905 -rw-r--r-- 2 regnarg regnarg 0 Jun 19 19:01 c

The -i option to ls shows inode numbers in the first column. You can see that the symlink has a different inode number while the hardlink has the same. You can also use the stat(1) command:

ls的-i选项显示第一列中的inode编号。您可以看到符号链接具有不同的inode编号,而硬链接具有相同的编号。您还可以使用stat(1)命令:

$ stat a
  File: 'a'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 28h/40d Inode: 95905       Links: 2
[...]

$ stat b
  File: 'b' -> 'a'
  Size: 1           Blocks: 0          IO Block: 4096   symbolic link
Device: 28h/40d Inode: 96990       Links: 1
[...]

If you want to do this programmatically, you can use the lstat(2) system call to find information about the symlink itself (its inode number etc.), while stat(2) shows information about the target of the symlink, if it exists. Example in Python:

如果要以编程方式执行此操作,可以使用lstat(2)系统调用来查找有关符号链接本身的信息(其inode编号等),而stat(2)显示有关符号链接的目标的信息(如果存在) 。 Python中的示例:

>>> import os
>>> os.stat("b").st_ino
95905
>>> os.lstat("b").st_ino
96990