Detailed explanation of hard link and symbolic link in OSX (UNIX) (hard connection and soft connection)

Source: Internet
Author: User

A few days ago, a company's project required recursive File Retrieval. Due to my temporary negligence, I did not consider the existence of Connection Files, as a result, the released program may fall into the major bug of an endless loop, without getting too much white-eye from the boss. The so-called where to fall and where to climb, so today I will explain in detail the next life of "Connection Files" and share my experiment results.

Those familiar with Unix are certainly not unfamiliar with hard link and soft link. OSX comes from UNIX lineage and naturally inherits such a feature perfectly, hard link and symbolic link are generally referred to in the official documents of OSX.

File System Management and memory management are very similar in some files. For example, we want to operate data in the memory and execute corresponding operations through pointers, it is no exception in the file system. When creating a file, we first configure a certain disk sector in the hard disk and related attributes of the file (such as permissions). The information is ultimately stored in one node (inode, in the future, we can access, modify, and append information through the address of this node.

Hard connection

Hard connection is actually an address pointing to inode. Strictly speaking, every file we see in file management is a hard connection. We often say that creating a hard connection to a file is a copy of the address. An inode has one or more such connections pointing to it, just as some pointers used in our program, modifying a file with a pointer will affect all connections directed to the node. In addition, hard connections have a mechanism similar to "Automatic Memory Management". When you delete any hard connection, as long as there are other connections pointing to the inode node, the information of the block file will not be released. It will be set to idle only when all connections pointing to it have been deleted.

Soft connection

If you have a deep understanding of the principles of memory management, of course you will not have any difficulties in understanding the above hard connections, and you will not have to bother with understanding soft connections, because the soft connection is just like the "pointer to Pointer" in memory management, the soft connection is essentially an address pointing to a hard connection. Naturally, it will only be valid for this hard connection, once the hard connection to which the soft connection points is deleted, the soft connection becomes invalid. Of course, this is also a very subtle difference with "pointer Pointer", that is, your operations on soft links are mapped to the node operations by redirecting to the hard connection, do you see this subtle difference?

There are two methods to create a hard link in OSX:

12 - (BOOL)linkItemAtPath:(NSString
*)srcPath toPath:(NSString
*)dstPath error:(NSError
**)error;- (BOOL)linkItemAtURL:(NSURL
*)srcURL toURL:(NSURL
*)dstURL error:(NSError
**)error;

You can create a soft link using either of the following methods:

12 - (BOOL)createSymbolicLinkAtPath:(NSString
*)path withDestinationPath:(NSString
*)destPath error:(NSError
**)error ;- (BOOL)createSymbolicLinkAtURL:(NSURL
*)url withDestinationURL:(NSURL
*)destURL error:(NSError
**)error;

The following describes how to verify these theories!

Experiment 1: The implementation of soft links is very simple. I directly create a symboliclink for a file (the file you see is actually a hard connection, then, when I modify the file name, The symboliclink still can find the file correctly, but when I delete the file, the symboliclink will always expire.

Experiment 2: token has not changed. Do I understand it wrong?

Experiment 3: the results of experiment 2 make me unwilling, and I guess that using the file editor to open and save the changes again is not to modify the original data, but to delete the original file, and re-create a file (in the true sense, create, allocate sectors, nodes ......). To verify my guess, I still created test1.txt and created a hard connection test2.txt. However, in this modification, I no longer use the file editor. Instead, I can use the command line: printf "Hello"> pipeline to see the hello text that I appended!

Supplement: In OSX, the difference between hard connection and soft connection is that hard connection is the same as the file method I used to operate on (as I mentioned above, previously, the file itself is a hard connection), and the file system calculates the same space, while the size of the soft connection file will be a very small value, usually only a few bytes. In addition, the operations on the soft connection will eventually be mapped to the operations on the corresponding file. Of course, we can also use the nsfilemanager destinationofsymboliclinkatpath: Error: method and the stringbyresolvingsymlinksinpath method of nsstring to convert to the original file path.

The above is my explanation of hard link and symbolic link. If you are passing by, please ignore it!

The experiment has already made me understand and confirm the difference between hard link and symbolic link, in OSX, right-click to create a proxy. In this way, the connection type is Shenma connection ?? Experiment 1 eliminates the possibility of hard connection, but the nsfiletype obtained by nsfilemanager is nsfiletyperegular (common file type), which is not a soft-connection nsfiletypesymboliclink type, in addition, if it is a folder replacement, using the nsfilemanager syntax contentsofdirectoryatpath: Error: In the Code does not produce any results, which is also in conflict with the soft connection type, so I guess this is a connection file type created by OSX, and there is no experiment!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.