`
yes1983
  • 浏览: 38487 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

为什么linux的硬连接(hard link)不能指向目录

阅读更多

This is just a bad idea, as there is no way to tell the difference between a hard link and original name.

Allowing hard links to directories would break the directed acyclic graph structure of the filesystem, possibly creating directory loops and dangling directory subtrees, which would make fsck and any other file tree walkers error prone.

First, to understand this, let's talk about inodes. The data in the filesystem is held in blocks on the disk, and those blocks are collected together by an inode. You can think of the inode as THE file. Inodes lack filenames though. That's where links come in.

A link is just a pointer to an inode. A directory is an inode that holds links. Each filename in a directory is just a link to an inode. Opening a file in UNIX also creates a link, but it's a different type of link (it's not a named link).

A hard link is just an extra directory entry pointing to that inode. When you ls -l, the number after the permissions is the named link count. Most regular files will have one link. Creating a new hard link to a file will make both filenames point to the same inode. Note:

% ls -l test
ls: test: No such file or directory
% touch test
% ls -l test
-rw-r--r--  1 danny  staff  0 Oct 13 17:58 test
% ln test test2
% ls -l test*
-rw-r--r--  2 danny  staff  0 Oct 13 17:58 test
-rw-r--r--  2 danny  staff  0 Oct 13 17:58 test2
% touch test3
% ls -l test*
-rw-r--r--  2 danny  staff  0 Oct 13 17:58 test
-rw-r--r--  2 danny  staff  0 Oct 13 17:58 test2
-rw-r--r--  1 danny  staff  0 Oct 13 17:59 test3
            ^
            ^ this is the link count

Now, you can clearly see that there is no such think as a hard link. A hard link is the same as a regular name. In the above example, test or test2, which is the original file and which is the hard link? By the end, you cant really tell (ignoring timestamps) because both names point to the same contents, the same inode:

% ls -li test*  
14445750 -rw-r--r--  2 danny  staff  0 Oct 13 17:58 test
14445750 -rw-r--r--  2 danny  staff  0 Oct 13 17:58 test2
14445892 -rw-r--r--  1 danny  staff  0 Oct 13 17:59 test3

The -i flag to ls shows you inode numbers in the beginning of the line. Note how test and test2 have the same inode number.

Now, if you were allowed to do this for directories, two different directories in different points in the filesystem could point to the same thing. In fact, a subdir could point back to its grandparent, creating a loop.

Why is this loop a concern? Because when you are traversing, there is no way to detect you are looping (without keeping track of inode numbers as you traverse). Imagine you are writing the du command, which needs to recurse through subdirs to find out about disk usage. How would du know when it hit a loop? It is error prone and a lot of bookkeeping that du would have to do, just to pull off this simple task.

Symlinks are a whole different beast, in that they are a special type of "file" that many file filesystem APIs tend to automatically follow. Note, a symlink can point to an nonexistent destination, because they point by name, and not directly to an inode. That concept doesn't make sense with hard links, because the mere existance of a "hard link" means the file exists.

So why can du deal with symlinks easily and not hard links? We were able to see above that hard links are indistinguishable from normal directory entries. Symlinks however are special, detectable, and skippable! Du notices that the symlink is a symlink, and skips it completely!

% ls -l 
total 4
drwxr-xr-x  3 danny  staff  102 Oct 13 18:14 test1/
lrwxr-xr-x  1 danny  staff    5 Oct 13 18:13 test2@ -> test1
% du -ah
242M    ./test1/bigfile
242M    ./test1
4.0K    ./test2
242M    .


With the exception of mount points, each directory has one and only parent: ...

One way to do pwd is to check the device:inode for '.' and '..'. If they are the same, you have reached the root of the file system. Otherwise, find the name of the current directory in the parent, push that on a stack, and start comparing '../.' with '../..', then '../../.' with '../../..', etc. Once you've hit the root, start popping and printing the names from the stack. This algorithm relies on the fact that each directory has one and only one parent.

If hard links to directories were allowed, which one of the multiple parents should .. point to? That is one compelling reason why hardlinks to directories are not allowed.

Symlinks to directories don't cause that problem. If a program wants to, it could do an lstat() on each part of the pathname and detect when a symlink is encountered. The pwd algorithm will return the true absolute pathname for a target directory. The fact that there is a piece of text somewhere (the symlink) that points to the target directory is pretty much irrelevant. The existence of such a symlink does not create a loop in the graph.

分享到:
评论

相关推荐

    Linux 软连接与硬链接.pdf

    Linux 操作系统中有两种类型的连接:硬连接(Hard Link)和软连接(Symbolic Link)。这两种连接类型都可以实现文件的共享,但它们的实现机制和应用场景不同。 一、硬连接(Hard Link) 硬连接是一种文件系统级别...

    Linux2 建立硬连接.pdf

    _hard link_ 是指向同一个索引节点的多个文件名,硬连接文件和原文件是同一个文件(但却不是复制),它们占有相同的索引节点编号。 二、硬连接的建立 创建硬连接的命令是 `ln`,使用格式为:`ln [真实文件路径] ...

    Linux2 建立硬连接.docx

    在Linux操作系统中,文件的管理是一个重要的概念,而硬连接(Hard Link)是Linux文件系统提供的一种独特功能。硬连接允许用户创建一个与原始文件共享相同数据块的“连接”,而不是创建文件的完整副本。这样,多个...

    硬连接和符号连接

    ### 硬连接和符号连接在Linux系统中的深入解析 #### 一、概述 在Linux系统中,连接(Links)是一种非常重要的文件管理机制。它允许用户通过不同的名称访问同一个文件,这对于文件共享和组织非常有用。根据实现方式...

    linux软链接 硬链接区别

    Linux 链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link)。 硬链接(Hard Link) 硬链接是通过索引节点来进行连接。在 Linux 的文件系统中,保存在磁盘分区中的文件不管是什么...

    Linux软连接和硬链接-实际操作一遍你就会懂

    Linux操作系统中,文件的链接是一种将文件的不同名称关联起来的技术,这种技术分为两种主要类型:硬链接(HardLink)和符号链接(SymbolicLink),也就是我们常说的软链接。通过实际操作演示,可以更直观地理解这两...

    node-04-硬连接.ev4.rar

    然而,由于其特性,硬连接并不适用于所有情况,例如当需要跨文件系统创建链接或者源文件是目录时,硬连接通常不适用。 在视频文件"node-04-硬连接.ev4.mp4"中,可能详细演示了如何在Node.js环境中创建、管理和使用...

    Linux软连接和硬链接

     Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link)。默认情况下,ln命令产生硬链接。  【硬连接】  硬连接指通过索引节点来进行连接。在Linux的文件系统中,保存在...

    linux 连接与索引节点

    硬链接的一个关键特性是,它们不能跨文件系统创建,且不能针对目录创建硬链接。硬链接的创建通常使用`ln`命令完成。 - **软链接**:软链接,也称为符号链接,是一种特殊的文件,其中包含指向另一个文件或目录的引用...

    ln的软硬连接区别

    在Linux操作系统中,链接主要分为两类:硬链接(hard link)和软链接(又称为符号链接,symbolic link)。下面我们将从多个维度详细分析这两种链接的特点及其差异。 ### 软链接(Symbolic Link) 软链接在功能上...

    linux 硬链接和软链接深入理解.docx

    可以看到,file 和 file1 的 inode 是一样的,因此可以说硬链接是指针指向同一个 inode,系统并不为它重新分配 inode。file2 指向 file,说明 file2 是软链接,inode 号也发生变化。 如果删除原始文件 file,硬链接...

    11.linux8.0-软硬链接.pdf

    硬链接不能跨文件系统,也不能链接到目录。创建硬链接使用不带`-s`选项的`ln`命令: ```bash ln 源文件路径 目标路径 ``` 同样,如果源文件被删除,硬链接仍然有效,因为它直接指向文件的i节点,而不是文件的名称...

    hp1001打印驱动文件

    "hardlink_HP"标签可能表示这是HP品牌的一个特殊版本或系列,可能涉及到了硬盘链接技术,这是一种在文件系统中创建指向同一数据块的不同文件名的技术,用于节省存储空间或便于管理。 在压缩包子文件的文件名称列表...

    linux面试问题及回答方案.doc

    软连接(Symbolic Link)和硬连接(Hard Link)是 Linux 文件系统中两种不同类型的连接方式: * 软连接:软连接是一个指向原始文件的指针,它可以跨越文件系统的边界,但如果原始文件被删除,软连接将变得无效。 * ...

    linux目录.pdf

    硬链接(hard link)则创建了指向同一i节点的不同文件名,两个链接文件具有相同的物理内容。 七、挂载和文件系统 Linux支持多种文件系统,如EXT4、XFS、FAT32、NTFS等。通过`mount`命令可以将不同的磁盘分区挂载到...

    linux文件系统结构分析

    Linux还支持软链接(symbolic link)和硬链接(hard link)。软链接类似于Windows的快捷方式,指向另一个文件的路径;而硬链接则是在文件系统的i节点表中添加一个额外的引用,两个链接都指向同一个文件。 了解了...

    浅析Linux下的链接文件.pdf

    链接文件有两种主要类型:硬链接(hard link)和软链接(symbolic link),也称为符号链接。 1. **硬链接**: - **本质**:硬链接是现有文件在目录树中的另一个入口。它们并不创建新的文件副本,而是共享同一索引...

    详解linux软连接和硬链接

    硬链接的一个限制是不能用于目录,且只能在同一文件系统内创建。 软链接,又称符号链接,更像Windows系统中的快捷方式。它是一个特殊的文件,包含了一个指向另一个文件的路径信息。创建软链接时,系统会在磁盘上...

    Linux 关机命令

    Linux 中的文件系统管理是非常重要的一部分,硬链接(hard link)和软链接(symbolic link)是其中两个重要的概念。今天,我们将详细地介绍硬链接和软链接的概念、使用方法和差异。 硬链接(Hard Link) 硬链接是...

Global site tag (gtag.js) - Google Analytics