Symbolic Links and loops because of links (4.16)

Source: Internet
Author: User
Tags lstat

A symbolic link is an indirect pointer to a file, unlike the hard links from the previous section, which pointed directly to the I-node of the file. symbolic Links were introduced to get around the limitations of hard links.

  • Hard links normally require that the link and the file reside in the same file system

  • Only the superuser can create a hard link to a directory

There are no file system limitations on a symbolic link and what it points to, and anyone can create a symbolic link to a directory. symbolic Links are typically used to move a file or an entire directory hierarchy to another location on a system.

When using functions that refer to a file by name, we always need to know whether the function follows a symbolic link. if the function follows a symbolic link, a pathname argument to the function refers to the file pointed to by the symbolic link. otherwise, a pathname argument refers to the link itself, not the file pointed to by the link. figure 4.17 summarizes whether the functions described in this chapter follow a symbolic link. the FunctionsMkdir,Mkfifo,Mknod, AndRmdirAre not in this figure, as they return an error when the pathname is a symbolic link. Also, the functions that take a file descriptor argument, suchFstatAndFchmod, Are not listed, as the handling of a symbolic link is done by the function that returns the file descriptor (usuallyOpen). Whether or notChownFollows a symbolic link depends on the implementation.

One exception to figure 4.17 is whenOpenFunction is called with bothO_creatAndO_exclSet. In this case, if the pathname refers to a symbolic link,OpenWill failErrnoSetEexist. This behavior is intended to close a security hole so that privileged processes can't be fooled into writing to the wrong files.

Figure 4.17. Treatment of symbolic links by varous Functions

Function

Does not follow symbolic link

Follows symbolic link

Access

 

Chdir

 

Chmod

 

Chown

Creat

 

Exec

 

Lchown

 

Link

 

Lstat

 

Open

 

Opendir

 

Pathconf

 

Readlink

 

Remove

 

Rename

 

Stat

 

Truncate

 

Unlink

 

Example

It is possible to introduce Loops
Into the File System by Using symbolic links. Most functions that look up
Pathname returnErrnoOfEloopWhen this occurs. Consider
The following commands:

     $ mkdir foo                   make a new directory     $ touch foo/a                 create a 0-length file     $ ln -s ../foo foo/testdir    create a symbolic link("../foo" is the content written into foo/testdir)     $ ls -l foo     total 0     -rw-r----- 1 sar            0 Jan 22 00:16 a     lrwxrwxrwx 1 sar            6 Jan 22 00:16 testdir -> ../foo

 

This creates a directoryFooThat contains the file
AAnd a symbolic link that pointsFoo. If we write a simple program that
Uses the standard functionFTW(3) On Solaris to descend through a file
Hierarchy, printing each pathname encountered, the output is

    foo    foo/a    foo/testdir    foo/testdir/a    foo/testdir/testdir    foo/testdir/testdir/a    foo/testdir/testdir/testdir    foo/testdir/testdir/testdir/a

  (many more lines until we encounter an ELOOP error)

In section 4.21, we provide our own version ofFTWFunction that usesLstatInsteadStat, To prevent it from following symbolic links.

Note that on Linux,FTWFunction usesLstat, So it doesn' t display this behavior.

A loop of this form is easy to remove. We are ableUnlinkThe fileFoo/testdir,UnlinkDoes not follow a symbolic link. But if we create a hard link that forms a loop of this type, its removal is much more difficult. This is whyLinkFunction will not form a hard link to a directory unless the process has superuser privileges (in centos, even you have superuser privileges, hardlink to a directory is still not allowed ).

Indeed, rich Steven s did this on his own system as an experiment while writing the original version of this section. The file system got has upted and the normalFsck(1) Utility couldn't fix things. The deprecated toolsClri(8) andDcheck(8) were needed to repair the file system.

The need for hard links to directories has long since passed. with symbolic links andMkdirFunction, there is no longer any need for users to create hard links to directories.

When we open a file, if the pathname passedOpenSpecifies a symbolic link,OpenFollows the link to the specified file. If the file pointed to by the symbolic link doesn't exist,OpenReturns an error saying that it can't open the file. This can confuse users who aren't familiar with symbolic links. For example,

     $ ln -s /no/such/file myfile            create a symbolic link     $ ls myfile     myfile                                  ls says it's there     $ cat myfile                            so we try to look at it     cat: myfile: No such file or directory     $ ls -l myfile                          try -l option     lrwxrwxrwx 1 sar        13 Jan 22 00:26 myfile -> /no/such/file

 

The fileMyfileDoes exist, yetCatSays
There is no such file, becauseMyfileIs a symbolic link and the file
Pointed to by the symbolic link doesn't exist.-LOption
LsGives us two hints: the first character isL, Which
Means a symbolic link, and the sequence->Also indicates a symbolic
Link.LsCommand has another option (-F) That appends
At-sign (@) to filenames that are symbolic links, which can help spot Symbolic Links
In a directory listing without-LOption.

 

 

 

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.