Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

120 Files and Directories Chapter 4



  1. If eitheroldnameornewnamerefers to a symbolic link, then the link itself is
    processed, not the file to which it resolves.

  2. Wecan’t rename dot or dot-dot. Moreprecisely,neither dot nor dot-dot can
    appear as the last component ofoldnameornewname.

  3. Asaspecial case, ifoldnameandnewnamerefer to the same file, the function
    returns successfully without changing anything.


Ifnewnamealready exists, we need permissions as if we weredeleting it. Also, because
we’reremoving the directory entry foroldnameand possibly creating a directory entry
fornewname, we need write permission and execute permission in the directory
containingoldnameand in the directory containingnewname.
Therenameatfunction provides the same functionality as therenamefunction,
except when eitheroldname or newname refers to a relative pathname. If oldname
specifies a relative pathname, it is evaluated relative to the directory referenced byoldfd.
Similarly,newnameis evaluated relative to the directory referenced bynewfdifnewname
specifies a relative pathname. Either theoldfdornewfdarguments (or both) can be set to
AT_FDCWDto evaluate the corresponding pathname relative to the current directory.

4.17 Symbolic Links


Asymbolic link is an indirect pointer to a file, unlike the hardlinks described in the
previous section, which pointed directly to the i-node of the file. Symbolic links were
introduced to get around the limitations of hardlinks.

•Hardlinks normally requirethat the link and the file reside in the same file
system.
•Only the superuser can create a hardlink to a directory (when supported by the
underlying file system).

Thereare nofile system limitations on a symbolic link and what it points to, and anyone
can create a symbolic link to a directory.Symbolic links aretypically used to ‘‘move’’a
file or an entiredirectory 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. Figure4.17 summarizes whether the functions described in this chapter follow a
symbolic link. The functionsmkdir,mkfifo,mknod,andrmdirdo not appear in this
figure, as they return an error when the pathname is a symbolic link. Also, the
functions that take a file descriptor argument, such asfstatandfchmod,are not
listed, as the function that returns the file descriptor (usuallyopen)handles the
symbolic link. Historically,implementations have differed in whetherchownfollows
symbolic links. In all modern systems, however,chowndoes follow symbolic links.
Free download pdf