aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2017-08-19 00:48:28 -0700
committerPaul Eggert2017-08-19 00:49:39 -0700
commite58b3ef78b7db5190bd940495da6de0b6737f955 (patch)
tree2e6c43e0b52a63f2a43cec4dcfe7124c39197927
parent6763399ef3f268269fefd75d7c7a4ac012f66833 (diff)
downloademacs-e58b3ef78b7db5190bd940495da6de0b6737f955.tar.gz
emacs-e58b3ef78b7db5190bd940495da6de0b6737f955.zip
Clarify behavior of symlinks and directories
* doc/lispref/files.texi (Saving Buffers): Document how functions like rename-file work with symlinks and directories. This patch attempts to document the current behavior better, in preparation for possibly changing it. See Bug#27986.
-rw-r--r--doc/lispref/files.texi123
1 files changed, 80 insertions, 43 deletions
diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi
index 9359d3eaa09..5a52765131c 100644
--- a/doc/lispref/files.texi
+++ b/doc/lispref/files.texi
@@ -401,6 +401,8 @@ If @var{confirm} is non-@code{nil}, that means to ask for confirmation
401before overwriting an existing file. Interactively, confirmation is 401before overwriting an existing file. Interactively, confirmation is
402required, unless the user supplies a prefix argument. 402required, unless the user supplies a prefix argument.
403 403
404@c FIXME: This disagrees with the doc string, which talks about
405@c directory names, not directories. See Bug#27986.
404If @var{filename} is an existing directory, or a symbolic link to one, 406If @var{filename} is an existing directory, or a symbolic link to one,
405@code{write-file} uses the name of the visited file, in directory 407@code{write-file} uses the name of the visited file, in directory
406@var{filename}. If the buffer is not visiting a file, it uses the 408@var{filename}. If the buffer is not visiting a file, it uses the
@@ -628,7 +630,10 @@ If @var{mustbenew} is non-@code{nil}, then @code{write-region} asks
628for confirmation if @var{filename} names an existing file. If 630for confirmation if @var{filename} names an existing file. If
629@var{mustbenew} is the symbol @code{excl}, then @code{write-region} 631@var{mustbenew} is the symbol @code{excl}, then @code{write-region}
630does not ask for confirmation, but instead it signals an error 632does not ask for confirmation, but instead it signals an error
631@code{file-already-exists} if the file already exists. 633@code{file-already-exists} if the file already exists. Although
634@code{write-region} normally follows a symbolic link and creates the
635pointed-to file if the symbolic link is dangling, it does not follow
636symbolic links if @var{mustbenew} is @code{excl}.
632 637
633The test for an existing file, when @var{mustbenew} is @code{excl}, uses 638The test for an existing file, when @var{mustbenew} is @code{excl}, uses
634a special system feature. At least for files on a local disk, there is 639a special system feature. At least for files on a local disk, there is
@@ -817,9 +822,7 @@ are silently and automatically ignored.
817 822
818 These functions test for permission to access a file for reading, 823 These functions test for permission to access a file for reading,
819writing, or execution. Unless explicitly stated otherwise, they 824writing, or execution. Unless explicitly stated otherwise, they
820recursively follow symbolic links for their file name arguments, at 825follow symbolic links. @xref{Kinds of Files}.
821all levels (at the level of the file itself and at all levels of
822parent directories).
823 826
824 On some operating systems, more complex sets of access permissions 827 On some operating systems, more complex sets of access permissions
825can be specified, via mechanisms such as Access Control Lists (ACLs). 828can be specified, via mechanisms such as Access Control Lists (ACLs).
@@ -838,8 +841,8 @@ If the file does not exist, or if access control policies prevent you
838from finding its attributes, this function returns @code{nil}. 841from finding its attributes, this function returns @code{nil}.
839 842
840Directories are files, so @code{file-exists-p} returns @code{t} when 843Directories are files, so @code{file-exists-p} returns @code{t} when
841given a directory name. However, symbolic links are treated 844given a directory name. However, because @code{file-exists-p} follows
842specially; @code{file-exists-p} returns @code{t} for a symbolic link 845symbolic links, it returns @code{t} for a symbolic link
843name only if the target file exists. 846name only if the target file exists.
844@end defun 847@end defun
845 848
@@ -906,10 +909,7 @@ returns @code{t} for nonexistent files.
906If the optional argument @var{group} is non-@code{nil}, this function 909If the optional argument @var{group} is non-@code{nil}, this function
907also checks that the file's group would be unchanged. 910also checks that the file's group would be unchanged.
908 911
909If @var{filename} is a symbolic link, then, unlike the other functions 912This function does not follow symbolic links.
910discussed here, @code{file-ownership-preserved-p} does @emph{not}
911replace @var{filename} with its target. However, it does recursively
912follow symbolic links at all levels of parent directories.
913@end defun 913@end defun
914 914
915@defun file-modes filename 915@defun file-modes filename
@@ -919,8 +919,8 @@ follow symbolic links at all levels of parent directories.
919@cindex file modes 919@cindex file modes
920This function returns the @dfn{mode bits} of @var{filename}---an 920This function returns the @dfn{mode bits} of @var{filename}---an
921integer summarizing its read, write, and execution permissions. 921integer summarizing its read, write, and execution permissions.
922Symbolic links in @var{filename} are recursively followed at all 922This function follows symbolic links. If the file does not exist, the
923levels. If the file does not exist, the return value is @code{nil}. 923return value is @code{nil}.
924 924
925@xref{File permissions,,, coreutils, The @sc{gnu} @code{Coreutils} 925@xref{File permissions,,, coreutils, The @sc{gnu} @code{Coreutils}
926Manual}, for a description of mode bits. For example, if the 926Manual}, for a description of mode bits. For example, if the
@@ -971,19 +971,26 @@ Unix. These conventions are also followed by @code{file-attributes}
971@subsection Distinguishing Kinds of Files 971@subsection Distinguishing Kinds of Files
972@cindex file classification 972@cindex file classification
973@cindex classification of file types 973@cindex classification of file types
974@cindex symbolic links
974 975
975 This section describes how to distinguish various kinds of files, such 976 This section describes how to distinguish various kinds of files, such
976as directories, symbolic links, and ordinary files. 977as directories, symbolic links, and ordinary files.
977 978
979 Symbolic links are ordinarily followed wherever they appear. For
980example, to interpret the file name @file{a/b/c}, any of @file{a},
981@file{a/b}, and @file{a/b/c} can be symbolic links that are followed,
982possibly recursively if the link targets are themselves symbolic
983links. However, a few functions do not follow symbolic links at the
984end of a file name (@file{a/b/c} in this example). Such a function
985is said to @dfn{not follow symbolic links}.
986
978@defun file-symlink-p filename 987@defun file-symlink-p filename
979@cindex file symbolic links 988@cindex symbolic links
980If the file @var{filename} is a symbolic link, the 989If the file @var{filename} is a symbolic link, this function does not
981@code{file-symlink-p} function returns its (non-recursive) link target 990follow it and instead returns its link target
982as a string. (The link target string is not necessarily the full 991as a string. (The link target string is not necessarily the full
983absolute file name of the target; determining the full file name that 992absolute file name of the target; determining the full file name that
984the link points to is nontrivial, see below.) If the leading 993the link points to is nontrivial, see below.)
985directories of @var{filename} include symbolic links, this function
986recursively follows them.
987 994
988If the file @var{filename} is not a symbolic link, or does not exist, 995If the file @var{filename} is not a symbolic link, or does not exist,
989@code{file-symlink-p} returns @code{nil}. 996@code{file-symlink-p} returns @code{nil}.
@@ -1011,9 +1018,9 @@ Here are a few examples of using this function:
1011 1018
1012Note that in the third example, the function returned @file{sym-link}, 1019Note that in the third example, the function returned @file{sym-link},
1013but did not proceed to resolve it, although that file is itself a 1020but did not proceed to resolve it, although that file is itself a
1014symbolic link. This is what we meant by ``non-recursive'' above---the 1021symbolic link. That is because this function does not follow symbolic
1015process of following the symbolic links does not recurse if the link 1022links---the process of following the symbolic links does not apply to
1016target is itself a link. 1023the last component of the file name.
1017 1024
1018The string that this function returns is what is recorded in the 1025The string that this function returns is what is recorded in the
1019symbolic link; it may or may not include any leading directories. 1026symbolic link; it may or may not include any leading directories.
@@ -1044,12 +1051,10 @@ link. If you actually need the file name of the link target, use
1044@ref{Truenames}. 1051@ref{Truenames}.
1045@end defun 1052@end defun
1046 1053
1047The next two functions recursively follow symbolic links at
1048all levels for @var{filename}.
1049
1050@defun file-directory-p filename 1054@defun file-directory-p filename
1051This function returns @code{t} if @var{filename} is the name of an 1055This function returns @code{t} if @var{filename} is the name of an
1052existing directory, @code{nil} otherwise. 1056existing directory, @code{nil} otherwise.
1057This function follows symbolic links.
1053 1058
1054@example 1059@example
1055@group 1060@group
@@ -1080,6 +1085,7 @@ existing directory, @code{nil} otherwise.
1080This function returns @code{t} if the file @var{filename} exists and is 1085This function returns @code{t} if the file @var{filename} exists and is
1081a regular file (not a directory, named pipe, terminal, or 1086a regular file (not a directory, named pipe, terminal, or
1082other I/O device). 1087other I/O device).
1088This function follows symbolic links.
1083@end defun 1089@end defun
1084 1090
1085@node Truenames 1091@node Truenames
@@ -1231,15 +1237,11 @@ on the 19th, @file{aug-20} was written on the 20th, and the file
1231@end example 1237@end example
1232@end defun 1238@end defun
1233 1239
1234 If the @var{filename} argument to the next two functions is a
1235symbolic link, then these function do @emph{not} replace it with its
1236target. However, they both recursively follow symbolic links at all
1237levels of parent directories.
1238
1239@defun file-attributes filename &optional id-format 1240@defun file-attributes filename &optional id-format
1240@anchor{Definition of file-attributes} 1241@anchor{Definition of file-attributes}
1241This function returns a list of attributes of file @var{filename}. If 1242This function returns a list of attributes of file @var{filename}. If
1242the specified file cannot be opened, it returns @code{nil}. 1243the specified file's attributes cannot be accessed, it returns @code{nil}.
1244This function does not follow symbolic links.
1243The optional parameter @var{id-format} specifies the preferred format 1245The optional parameter @var{id-format} specifies the preferred format
1244of attributes @acronym{UID} and @acronym{GID} (see below)---the 1246of attributes @acronym{UID} and @acronym{GID} (see below)---the
1245valid values are @code{'string} and @code{'integer}. The latter is 1247valid values are @code{'string} and @code{'integer}. The latter is
@@ -1391,7 +1393,7 @@ This function returns the number of names (i.e., hard links) that
1391file @var{filename} has. If the file does not exist, this function 1393file @var{filename} has. If the file does not exist, this function
1392returns @code{nil}. Note that symbolic links have no effect on this 1394returns @code{nil}. Note that symbolic links have no effect on this
1393function, because they are not considered to be names of the files 1395function, because they are not considered to be names of the files
1394they link to. 1396they link to. This function does not follow symbolic links.
1395 1397
1396@example 1398@example
1397@group 1399@group
@@ -1553,6 +1555,16 @@ a @code{file-missing} error instead.
1553made by these functions instead of writing them immediately to 1555made by these functions instead of writing them immediately to
1554secondary storage. @xref{Files and Storage}. 1556secondary storage. @xref{Files and Storage}.
1555 1557
1558@c FIXME: This paragraph is purposely silent on what happens if
1559@c @var{newname} is not a directory name but happens to name a
1560@c directory. See Bug#27986 for discussion on how to clear this up.
1561 In the functions that have an argument @var{newname}, if this
1562argument is a directory name it is treated as if the nondirectory part
1563of the source name were appended. Typically, a directory name is one
1564that ends in @samp{/} (@pxref{Directory Names}). For example, if the
1565old name is @file{a/b/c}, the @var{newname} @file{d/e/f/} is treated
1566as if it were @file{d/e/f/c}.
1567
1556 In the functions that have an argument @var{newname}, if a file by the 1568 In the functions that have an argument @var{newname}, if a file by the
1557name of @var{newname} already exists, the actions taken depend on the 1569name of @var{newname} already exists, the actions taken depend on the
1558value of the argument @var{ok-if-already-exists}: 1570value of the argument @var{ok-if-already-exists}:
@@ -1570,11 +1582,6 @@ Replace the old file without confirmation if @var{ok-if-already-exists}
1570is any other value. 1582is any other value.
1571@end itemize 1583@end itemize
1572 1584
1573The next four commands all recursively follow symbolic links at all
1574levels of parent directories for their first argument, but, if that
1575argument is itself a symbolic link, then only @code{copy-file}
1576replaces it with its (recursive) target.
1577
1578@deffn Command add-name-to-file oldname newname &optional ok-if-already-exists 1585@deffn Command add-name-to-file oldname newname &optional ok-if-already-exists
1579@cindex file with multiple names 1586@cindex file with multiple names
1580@cindex file hard link 1587@cindex file hard link
@@ -1582,6 +1589,14 @@ This function gives the file named @var{oldname} the additional name
1582@var{newname}. This means that @var{newname} becomes a new hard 1589@var{newname}. This means that @var{newname} becomes a new hard
1583link to @var{oldname}. 1590link to @var{oldname}.
1584 1591
1592If @var{newname} is a symbolic link, its directory entry is replaced,
1593not the directory entry it points to. If @var{oldname} is a symbolic
1594link, this function might or might not follow the link; it does not
1595follow the link on GNU platforms. If @var{oldname} is a directory,
1596this function typically fails, although for the superuser on a few
1597old-fashioned non-GNU platforms it can succeed and create a filesystem
1598that is not tree-structured.
1599
1585In the first part of the following example, we list two files, 1600In the first part of the following example, we list two files,
1586@file{foo} and @file{foo3}. 1601@file{foo} and @file{foo3}.
1587 1602
@@ -1649,14 +1664,34 @@ This command renames the file @var{filename} as @var{newname}.
1649If @var{filename} has additional names aside from @var{filename}, it 1664If @var{filename} has additional names aside from @var{filename}, it
1650continues to have those names. In fact, adding the name @var{newname} 1665continues to have those names. In fact, adding the name @var{newname}
1651with @code{add-name-to-file} and then deleting @var{filename} has the 1666with @code{add-name-to-file} and then deleting @var{filename} has the
1652same effect as renaming, aside from momentary intermediate states. 1667same effect as renaming, aside from momentary intermediate states and
1668treatment of errors, directories and symbolic links.
1669
1670This command does not follow symbolic links. If @var{filename} is a
1671symbolic link, this command renames the symbolic link, not the file it
1672points to. If @var{newname} is a symbolic link, its directory entry
1673is replaced, not the directory entry it points to.
1674
1675This command does nothing if @var{filename} and @var{newname} are the
1676same directory entry, i.e., if they refer to the same parent directory
1677and give the same name within that directory. Otherwise, if
1678@var{filename} and @var{newname} name the same file, this command does
1679nothing on POSIX-conforming systems, and removes @var{filename} on
1680some non-POSIX systems.
1681
1682If @var{newname} exists, then it must be an empty directory if
1683@var{oldname} is a directory and a non-directory otherwise.
1653@end deffn 1684@end deffn
1654 1685
1655@deffn Command copy-file oldname newname &optional ok-if-already-exists time preserve-uid-gid preserve-extended-attributes 1686@deffn Command copy-file oldname newname &optional ok-if-already-exists time preserve-uid-gid preserve-extended-attributes
1656This command copies the file @var{oldname} to @var{newname}. An 1687This command copies the file @var{oldname} to @var{newname}. An
1657error is signaled if @var{oldname} does not exist. If @var{newname} 1688error is signaled if @var{oldname} is not a regular file. If @var{newname}
1658names a directory, it copies @var{oldname} into that directory, 1689names a directory, it copies @var{oldname} into that directory,
1659preserving its final name component. 1690preserving its final name component.
1691@c FIXME: See Bug#27986 for how the previous sentence might change.
1692
1693This function follows symbolic links, except that it does not follow a
1694dangling symbolic link to create @var{newname}.
1660 1695
1661If @var{time} is non-@code{nil}, then this function gives the new file 1696If @var{time} is non-@code{nil}, then this function gives the new file
1662the same last-modified time that the old one has. (This works on only 1697the same last-modified time that the old one has. (This works on only
@@ -1689,7 +1724,11 @@ SELinux context are not copied over in either case.
1689@kindex file-already-exists 1724@kindex file-already-exists
1690This command makes a symbolic link to @var{filename}, named 1725This command makes a symbolic link to @var{filename}, named
1691@var{newname}. This is like the shell command @samp{ln -s 1726@var{newname}. This is like the shell command @samp{ln -s
1692@var{filename} @var{newname}}. 1727@var{filename} @var{newname}}. The @var{filename} argument
1728is treated only as a string; it need not name an existing file.
1729If @var{filename} is a relative file name, the resulting symbolic link
1730is interpreted relative to the directory containing the symbolic link.
1731@xref{Relative File Names}.
1693 1732
1694This function is not available on systems that don't support symbolic 1733This function is not available on systems that don't support symbolic
1695links. 1734links.
@@ -1702,8 +1741,7 @@ links.
1702This command deletes the file @var{filename}. If the file has 1741This command deletes the file @var{filename}. If the file has
1703multiple names, it continues to exist under the other names. If 1742multiple names, it continues to exist under the other names. If
1704@var{filename} is a symbolic link, @code{delete-file} deletes only the 1743@var{filename} is a symbolic link, @code{delete-file} deletes only the
1705symbolic link and not its target (though it does follow symbolic links 1744symbolic link and not its target.
1706at all levels of parent directories).
1707 1745
1708A suitable kind of @code{file-error} error is signaled if the file 1746A suitable kind of @code{file-error} error is signaled if the file
1709does not exist, or is not deletable. (On Unix and GNU/Linux, a file 1747does not exist, or is not deletable. (On Unix and GNU/Linux, a file
@@ -1724,8 +1762,7 @@ See also @code{delete-directory} in @ref{Create/Delete Dirs}.
1724@cindex file modes, setting 1762@cindex file modes, setting
1725@deffn Command set-file-modes filename mode 1763@deffn Command set-file-modes filename mode
1726This function sets the @dfn{file mode} (or @dfn{permissions}) of 1764This function sets the @dfn{file mode} (or @dfn{permissions}) of
1727@var{filename} to @var{mode}. It recursively follows symbolic links 1765@var{filename} to @var{mode}. This function follows symbolic links.
1728at all levels for @var{filename}.
1729 1766
1730If called non-interactively, @var{mode} must be an integer. Only the 1767If called non-interactively, @var{mode} must be an integer. Only the
1731lowest 12 bits of the integer are used; on most systems, only the 1768lowest 12 bits of the integer are used; on most systems, only the