preface

The front-end package manager (PNPM) is really hot these days, with a lot of articles analyzing the principles of PNPM. After understanding it, I found that the whole structure of PNPM is organized based on hard link and soft link. However, I am vague about these two concepts, so I want to study them. As you know, everything in Unix/Linux is a file. As you can see, files are very important in Linux systems. We usually more intuitive feelings for the file must be the file name and file content. However, in Linux file systems, in addition to file names and file contents, there is another important concept, the inode.

inode

Wikipedia describes the inode as follows:

The inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. Each inode stores the attributes and disk block locations of the object’s data.File-system object attributes may include metadata (times of last change,access, modification), as well as owner and permission data. A directory is a list of inodes with their assigned names. The list includes an entry for itself, its parent, and each of its children.

The inode is a data structure used in unix-like file systems to describe file system objects, such as files or folders. It stores various attributes of the file (meta-information such as the time of the last inode change, the time of the last access, the time of the last modification, and permission information, etc.). A folder is a set of inodes containing its own entry, the entry of its parent, and all of its children.

The inode contains more than this:

  1. The number of bytes of the file
  2. The User ID of the file
  3. The Group ID of the file
  4. Read, write, and execute permissions on files
  5. Timestamp: ctime, the last time the inode changed; Mtime, the time when the file contents were last changed; Atime, time when the file was last opened
  6. The link count is how many file names point to the inode
  7. Location of the file data block

In the ext2/ext3 file system used by Linux, different types of data are stored in different areas. An inode table consisting of inodes is stored in one location, and file data blocks are stored in another location.

Inodes do not contain file names, which are stored in the structure of folder information. A file name is an alias for an inode, which is easy to manage and remember. Linux uses inodes to operate files. When a file is modified, the system finds the inode corresponding to the file name from the information structure of the folder, and then finds the corresponding hard disk location based on the block address of the file data stored in the inode for reading and writing operations.

Hard links

In general, inodes have a one-to-one relationship with file names and file data. However, we can use shell commands to make multiple file names point to the same inode, which is called hard link.

To create a hard link, run the ln


command

ln test.txt test_hard.txt
Copy the code

The fs.link method corresponds to nodejs.

Before creating a hard link,test.txtIt can be expressed like this:



After creating a hard link:



As you can see,test_hard.txtInode and source filetest.txtI’m using the same thing, except now I have 2 links.

We can implementls -liCheck it out.



The first column is the inode number, as you can see13029546, so both files use the same inode. The second column is the permission information, the fourth column is the owner, and the sixth column is the file content size. As you can see, the hard link creates a file with exactly the same meta-information as the source file, except for the filename. The third column represents the number of links, and you can see that the number of links is currently 2.

Because hard-linked files and source files use the same inode and point to the same block of file data, all information is the same except the file name. So these two files are equivalent, so to speak, hard-linked to each other. If you modify one file, you can see that the contents of the other file change synchronously.

Soft links

To be precise, it is called symbolic link, and generally also called soft link. Instead of sharing one inode with a hard link, a soft link creates a new inode and points to the source file. Soft links can be understood as desktop shortcuts in Windows.

-s: ln -s


:

ln -s test.txt test_symbolic.txt
Copy the code

The corresponding nodejs fs.symlink method.

After creating a soft link:



The number of links in the source file inode is still 1. A new inode is created and the soft link points to the source file.

Run ls -li to see:



The inode number of the soft link is different from that of the source file. The permission column starts with a lowercase L, indicating the soft link. The number of links is 1, and the size is 8 bytes. Yes, the soft link file has a size, but it is usually very small, after all, just a shortcut.

contrast

File rename or file move

File renaming and file moving are both changes to the absolute path of a file on Linux. For hard links, file renaming or file movement does not change the link pointing, while for soft links, file renaming or file movement disconnects the link. In this case, when the file content is modified through the soft link, a new inode is created, associated with the original file name and file data block.

File deletion

The rm command or nodejs unlink actually subtracts the number of links in the inode by one. For hard links, deleting test_hard. TXT causes the inode1 link count to be 1. When the link count becomes 0, the inode number is released and the inode number can be used for new files. There is no inode pointing to the file data block, so the file cannot be found. But the file data is still on the hard drive, so it’s not uncommon to see online tools that can help you recover files that have been deleted by mistake. The number of soft link inodes is 1. If the soft link is deleted, the system releases the soft link.

Link files and folders

Soft links can link files and folders, but hard links can only link files.

Create links for different file systems

Soft links can be created across different file systems, but hard links cannot be created because hard links share one inode, and different file systems have different inode tables.

Application scenarios

Hard links

  1. Backup files: Backup files are a good way to prevent important files from being deleted, but copying files consumes disk space. Hard links can take up no disk space to achieve file backup.
  2. File sharing: When multiple users maintain the same file, you can create a hard link in a private directory. In this way, the modification of each user can be synchronized to the source file, but the deletion of the file by one user is avoided.
  3. File classification: different file resources need to be classified, such as a movie is classified as foreign, suspense, then we can create hard links in the foreign folder and suspense folder respectively, so that you can avoid duplicate copy of the movie waste disk space. Some might say, isn’t it ok to use soft links? Yes, but not very well. As soon as the source file is moved or renamed, the soft link is broken.

Soft links

  1. Shortcuts: For files with deep paths, it is not very convenient to find them. Use soft links to create shortcuts on your desktop to quickly open and edit files.
  2. Flexible program version switching: For programs with multiple versions on the machine, you can change the point of the soft link to quickly switch program versions. It is mentioned here that switching the Python version can do this.
  3. Dynamic library versioning: not really, see here.

conclusion

  1. Linux uses inodes to manage files. Inodes store information such as the number of bytes, file permissions, number of links, and location of data blocks.
  2. Hard links share the inode with the source file and are the same as the source file except for the file name. Hard links cannot be created for folders or files on different file systems.
  3. Soft links are similar to Windows shortcuts, with separate inodes. You can create soft links to folders or files on different file systems.
  4. Both hard and soft link modification file contents are synchronized to the source file because they are essentially data blocks pointing to the source file.

The resources

  1. en.wikipedia.org/wiki/Inode
  2. www.ruanyifeng.com/blog/2011/1…
  3. Man7.org/linux/man-p…
  4. www.eet-china.com/mp/a76055.h…