`
yes1983
  • 浏览: 37844 次
  • 性别: 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.

分享到:
评论

相关推荐

    hardlink:使用硬链接复制文件并合并目录

    概述该软件包提供了一个hardlink ,使用硬链接复制文件和目录合并模块和命令行脚本。 为什么? 因为OS X不支持cp -lR 。安装直接从资源库获取最新代码: $ pip install hardlink用法通过命令行脚本: $ hardlink -...

    硬链壳扩展 HardLink ShellExtension 3.9.3.5 + x64 中文.rar

    通过右键菜单创建硬链接 安装非常简单,因为安装程序仅包含熟悉的选项。要创建新的硬链接,您要做的...总而言之,当以最小的努力将硬链接和符号链接放在一起时,HardLink ShellExtension 简化了一个相当复杂的问题。

    Linux软连接和硬链接

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

    Hard Hat-Linux开发资料

    Hard Hat-Linux开发资料 Hard Hat-Linux开发资料

    Linux操作系统的硬链接与软链接讲解

    在Linux系统中,内核为每一个新创建的文件分配一个Inode(索引结点),每个文件都有一个惟一的inode号。...Linux中包括两种链接:硬链接(Hard Link)和软链接(Soft Link),软链接又称为符号链接(Symbolic link)。

    详解linux软连接和硬链接

    本篇文章详细介绍了linux软连接和硬链接,废话不多说,接着往下看把。 一 链接文件 Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link)。默认情况下,ln命令产生硬链接。 ...

    mac-hardlink:macOS上的硬链接

    macOS上的硬链接 安装 npm i -g mac-hardlink 用法 hardlink -h hardlink <src> 或取消链接: hardlink <dir> -u

    区分Linux硬链接与软链接

    在Linux中,连接文件有两种,一种类似于Windows的快捷方式,可以让你快速地链接到目标文件(或目录),这种称为软链接(soft link),也叫作符号链接(symbolic link);另一种则是通过文件系统的 inode 连接来产生...

    linux教程——很使用的那种

    linux中的文件(普通文件或设备文件等)都必须有挂在点,只有挂载到一个目录下才能访问,swap分区是不需要挂载点的,在分区的时候,将文件系统类型选为swap,挂载点自动会变为灰色。一般linux中的swap大小为内存的两倍。...

    详解Linux ln 命令

    Linux 中的文件分为 Hard Link 和 Symbolic Link 两种。Hard Link 文件又被称为硬链接文件、实体链接文件,Symbolic Link 文件则常被称为符号链接、软链接文件。 ln 命令用来在文件之间建立链接。在介绍 ln 命令前,...

    Linux 硬链接和软链接详细介绍

     硬链接(hard link)是Unix系统最早的创建链接的方式。  默认情况下每个文件都有一个硬链接,创建硬链接时,实际上是创建了附加的入口,当且仅当指向文件的所有硬链接都被删除之后文件才被真正删除,即数据块被清理...

    hp1001打印驱动文件

    hp1001打印驱动文件,工具类资源 hardlink_HP1001_fir22.zip

    PyPI 官网下载 | git_export_hardlink-0.1.0.tar.gz

    资源来自pypi官网。 资源全名:git_export_hardlink-0.1.0.tar.gz

    Linux 硬链接与软链接

     Linux 中有两种链接:硬链接(Hard Link)和软链接(Soft Link),软链接也称为符号链接(Symbolic Link)。  硬链接  硬链接其实已存在文件的另一个名字,链接与原来的文件并没有什么区别

    剖析Linux系统中硬链接与软链接的区别

    首先要弄清楚,在Linux系统中,内核为每一个新创建的文件分配一个Inode(索引结点),...Linux中包括两种链接:硬链接(Hard Link)和软链接(Soft Link),软链接又称为符号链接(Symbolic link)。 本文对这两种链接作了介绍。

    入门学习Linux常用必会60个命令实例详解doc/txt

    Linux挂装CD-ROM后,会锁定CD—ROM,这样就不能用CD- ROM面板上的Eject按钮弹出它。但是,当不再需要光盘时,如果已将/cdrom作为符号链接,请使用umount/cdrom来卸装它。仅当无用户正在使用光盘时,该命令才会成功。...

    Linux硬链接与软链接原理及用法解析

    链接的方式可以分为两种,一种是硬链接(Hard Link),另一种是软链接或者也称为符号链接(Symbolic Link)。 硬链接概念 硬链接(hard link, 也称链接)就是一个文件的一个或多个文件名 硬链接是指通过索引节点来...

    硬盘安装linux(loadlin法)

    这样G:\linux\dos下的文件列表为: autoboot.bat loadlin.exe vmlinuz initrd.img 第三步:启动进入纯DOS 重新启动计算机,借助DOS启动盘进入纯DOS。 A:\>G: G:\>cd \linux\dos G:\linux\dos>autoboot.bat Linux...

    Linux系统中硬链接与软链接的区别

    Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link)。默认情况下,ln命令产生硬链接。

    Hardcode-Tray:修复Linux中的硬编码托盘图标

    修复Linux中的硬编码托盘图标 该脚本将自动检测您的默认主题,正确的图标大小,硬编码的应用程序,每个指示器的正确图标并进行修复。 所有这些都可以还原为原始图标。 主题 以下是支持Hardcode-Tray的主题列表: ...

Global site tag (gtag.js) - Google Analytics