Detailed explanation and application analysis of fs.symlink, LN and LN – S

Time:2021-10-4

inode
Before explaining the soft / hard link, first understand an important concept inode in Linux system. As we all know, files are stored on the hard disk. The smallest storage unit of the hard disk is called “sector” (512 bytes stored in each sector). When the operating system reads the hard disk, it will not read sectors one by one, because this is too inefficient. Instead, it reads multiple sectors continuously at one time. This continuous multiple sectors read at one time are “blocks”. This “block” composed of multiple sectors is the smallest unit of file access. The most common size of “block” is 4KB, that is, eight consecutive sectors form a block.
The file data is stored in “blocks”, so obviously,Front end trainingWe must also find a place to store the meta information of the file, such as the creator of the file, the creation date of the file, the size of the file, and so on. This area for storing file meta information is called inode, which is translated into Chinese as “index node”.
Note: when we open a file, the system first finds the inode number corresponding to the file name, then obtains the inode information through the inode number, and then reads the data according to the block where the file data in the inode information is located.
There are many articles about the above concepts, which are not easy to remember. Look at the picture!
Detailed explanation and application analysis of fs.symlink, LN and LN - S
What is included in inode?
Inode contains the meta information of the file, specifically the following:

  1. Bytes of file
  2. User ID of the file owner
  3. Group ID of the file
  4. Read, write and execute permissions of files
  5. There are three timestamps for files: CTime refers to the last change time of inode, mtime refers to the last change time of file content, and atime refers to the last opening time of file.
  6. The number of links, that is, how many file names point to this inode
  7. Location of file data block

How do linux commands get inode information of a file
You can directly use the Linux command stat to view the inode information of a file
stat example.js
Output information
koala node % stat example.js
16777223 11904170 -rw-r–r– 1 koala staff 0 0 “Mar 24 13:46:08 2021” “Mar 24 13:46:08 2021” “Mar 24 13:46:26 2021” “Mar 24 13:46:08 2021” 4096 0 0x40 example.js
How to get inode information of a file in node.js
In node.js, FS provides the stat function to view relevant information
fs.statSync(‘./example.js’);
Output information
File information stats{
dev: 16777223,
mode: 33188,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 11904170,
size: 0,
blocks: 0,
atimeMs: 1616564768255.48,
mtimeMs: 1616564768255.48,
ctimeMs: 1616564786778.5532,
birthtimeMs: 1616564768255.48,
atime: 2021-03-24T05:46:08.255Z,
mtime: 2021-03-24T05:46:08.255Z,
ctime: 2021-03-24T05:46:26.779Z,
birthtime: 2021-03-24T05:46:08.255Z
}
Each inode has a unique identification code. In the above output information, the inode is the unique identification code of the inode. In the Linux system, the inode identification code is used to identify the file instead of the file name. Previously
In Linux system, directory is also a kind of file. The directory file contains a series of directory entries. Each directory entry consists of two parts: the file name of the contained file and the inode identification code corresponding to the file name. We can use LS – I to list the files in the directory and all inde identification codes. Here we can also explain the problem that the partners may find it unreasonable. Only modifying the read permission of the directory can not read the contents of all files in the directory. Finally, we need to modify the files in the recursive directory.
Soft link and hard link
What is soft link (also known as symbolic link)
Detailed explanation and application analysis of fs.symlink, LN and LN - S
Soft links are similar to “shortcuts” in window. Creating a soft link will create a new inode. For example, a soft link file B is created for file a, and the inside of file B will point to the inode of A. When we read file B, the system will automatically guide to file A. file B is the soft connection (or symbolic link) of file a.
• access: after creating a soft link, we can access the same content with different file names,
• modification: the content of document a will be modified, and the content of document B will also be changed. The modification of document content will be shown to all documents.
• delete: when we delete the source file a, we will report an error “no such file or directory” when accessing the soft connection file B

You can directly use the Linux command ln – s source target to create a soft link (Note: it means that the target “points to” source)
ln -s ./target/a.js b.js
After executing the shell command, the B.js file will appear, and the soft link is created successfully.
Detailed explanation and application analysis of fs.symlink, LN and LN - S
Create a soft link in node.js
Basic usage
The official node.js document provides the symlinksync function to create soft links.
fs.symlinkSync(target,path,type)

Target < string > | < buffer > | < URL > / / target file
Path < string > | < buffer > | < URL > / / create the address corresponding to the soft chain
type <string>
It creates a link named path that points to the target. The type parameter is only available on windows and is ignored on other platforms. It can be set to ‘dir’, ‘file’, or ‘junction’. If the type parameter is not set, node.js will automatically detect the type of target and use ‘file’ or ‘dir’. If the target does not exist, ‘file’ will be used. The connection point on Windows requires that the destination path be an absolute path. When ‘junction’ is used, the target parameter is automatically normalized to an absolute path.
• use examples

const res = fs.symlinkSync(‘./target/a.js’,’./b.js’);
Detailed explanation and application analysis of fs.symlink, LN and LN - S
This code means that to create a soft link, B.js points to the file. / targert / A.js. When the content in A.js changes, the B.js file will also change.
⚠️ Note: if you create a soft link to the directory, the third parameter in the method needs to be passed ‘dir’ (although the third parameter only takes effect under windows, this can ensure that cross platform problems will not occur): fs.symlink (target, path, ‘dir’)
The above explains the two ways of creating soft links in Linux and node.js. If you look carefully, you may find problems. Why do the two parameters fs.symlink (target, path) in node.js and LN – s source target in shell command seem to be opposite? Is there such a question?
In fact, if you try the previous small examples, you will find that the order of the two parameters passed later is actually the same, which makes the new “point = >” in front of Oh!
What is a hard link
Detailed explanation and application analysis of fs.symlink, LN and LN - S
Generally, a file name “unique” corresponds to an inode. However, Linux allows multiple file names to point to the same inode. Indicates that we can use different file names to access the same content; Modify the content of the document and show it to all documents; Deleting one file does not affect access to another file. This mechanism is called “hard link”
Hard link creation
You can directly use the Linux command ln source target to create a hard link (Note: source is an existing file, and target is the link to be established)
ln ./target/a.js c.js
After executing the shell command, the c.js file will appear, and the hard link is created successfully.
Detailed explanation and application analysis of fs.symlink, LN and LN - S
Unlike soft connection, hard links can only be established for files, not directories. And the source file must exist, otherwise an error will be reported when creating a hard link.
Deleting one file does not affect access to another. Why? In fact, there is also information about the number of links in the file inode. For every more file pointing to this inode, the number will increase by 1. For every less file pointing to this inode, the number will decrease by 1. When the value is reduced to 0, the system will automatically recycle the inode and its corresponding block area. Much like a garbage collection mechanism for reference counting.
When we establish a hard link to a file, the number of links of the corresponding inode will be`2 (the original file already has a point). When a file is deleted, the number of links becomes 1, the conditions for recycling are not met, so we can still access the file.
Comparative analysis and summary of soft link and hard link

  1. Use ln source target to establish a hard link; Use ln – s source target to establish a soft link
  2. Hard links do not create additional inodes and share the same inode with the source file; The soft link creates an additional file (an additional inode) that points to the inode of the source file
  3. When establishing a hard link, the source must exist and can only be a file; When establishing a soft link, the source may not exist and may be a directory
  4. Deleting the source file will not affect the access to the hard link file (because the inode is still there); Deleting the source file will affect the access of the soft link file (because the inode pointed to no longer exists)
  5. A link with the same name that has been established cannot be established again unless it is deleted or the – f parameter is used

Application scenario
npm link
The principle of NPM link is also realized through soft chain. When we want to debug the locally developed NPM module package (some contents have not been released or modified), we need to use NPM link for debugging. For example:
Detailed explanation and application analysis of fs.symlink, LN and LN - S
There are two projects, create mono repo and create mono repo testcli. After NPM link is successfully executed under the create mono repo project node, execute NPM link create mono repo under the create mono repo testcli project directory, so that debugging can be completed
lerna
For the project created by lerna, the modules in the packages directory depend on each other, which is also realized by creating soft links based on fs.symlinksync.
The specific implementation code and address are as follows:
function createSymbolicLink(src, dest, type) {
log.silly(“createSymbolicLink”, [src, dest, type]);

return fs

.lstat(dest)
.then(() => fs.unlink(dest))
.catch(() => {
  /* nothing exists at destination */
})
.then(() => fs.symlink(src, dest, type));

}