| 42333 |
ripley |
1 |
% File src/library/utils/man/normalizePath.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 70519 |
ripley |
3 |
% Copyright 1995-2016 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 33233 |
ripley |
6 |
\name{normalizePath}
|
| 56186 |
murdoch |
7 |
\alias{normalizePath}
|
| 33233 |
ripley |
8 |
\title{Express File Paths in Canonical Form}
|
|
|
9 |
\description{
|
| 33237 |
ripley |
10 |
Convert file paths to canonical form for the platform, to display them
|
| 53156 |
ripley |
11 |
in a user-understandable form and so that relative and absolute paths can
|
| 53128 |
ripley |
12 |
be compared.
|
| 33233 |
ripley |
13 |
}
|
|
|
14 |
\usage{
|
| 53131 |
ripley |
15 |
normalizePath(path, winslash = "\\\\", mustWork = NA)
|
| 33233 |
ripley |
16 |
}
|
|
|
17 |
\arguments{
|
|
|
18 |
\item{path}{character vector of file paths.}
|
| 53134 |
ripley |
19 |
\item{winslash}{the separator to be used on Windows -- ignored
|
| 53131 |
ripley |
20 |
elsewhere. Must be one of \code{c("/", "\\\\")}.}
|
|
|
21 |
\item{mustWork}{logical: if \code{TRUE} then an error is given if the result
|
|
|
22 |
cannot be determined; if \code{NA} then a warning.}
|
| 33233 |
ripley |
23 |
}
|
| 53252 |
ripley |
24 |
\details{
|
|
|
25 |
Tilde-expansion (see \code{\link{path.expand}}) is first done on
|
| 65147 |
ripley |
26 |
\code{paths}.
|
| 61433 |
ripley |
27 |
|
| 53134 |
ripley |
28 |
Where the Unix-alike platform supports it attempts to turn paths into
|
| 50567 |
ripley |
29 |
absolute paths in their canonical form (no \samp{./}, \samp{../} nor
|
| 53134 |
ripley |
30 |
symbolic links). It relies on the POSIX system function
|
|
|
31 |
\code{realpath}: if the platform does not have that (we know of no
|
|
|
32 |
current example) then the result will be an absolute path but might
|
| 53156 |
ripley |
33 |
not be canonical. Even where \code{realpath} is used the canonical
|
|
|
34 |
path need not be unique, for example \emph{via} hard links or
|
|
|
35 |
multiple mounts.
|
| 33237 |
ripley |
36 |
|
| 77324 |
kalibera |
37 |
On Windows it converts relative paths to absolute paths, resolves symbolic
|
|
|
38 |
links, converts short names for path elements to long names and ensures the
|
|
|
39 |
separator is that specified by \code{winslash}. It will match each path
|
|
|
40 |
element case-insensitively or case-sensitively as during the usual name
|
|
|
41 |
lookup and return the canonical case. It relies on Windows API function
|
|
|
42 |
\code{GetFinalPathNameByHandle} and in case of an error (such as
|
|
|
43 |
insufficient permissions) it currently falls back to the \R 3.6 (and
|
|
|
44 |
older) implementation, which relies on \code{GetFullPathName} and
|
|
|
45 |
\code{GetLongPathName} with limitations described in the Notes section.
|
| 85908 |
hornik |
46 |
An attempt is made not to introduce \abbr{UNC} paths in presence of mapped drives
|
|
|
47 |
or symbolic links: if \code{GetFinalPathNameByHandle} returns a \abbr{UNC} path,
|
| 77460 |
kalibera |
48 |
but \code{GetLongPathName} returns a path starting with a drive letter, R
|
|
|
49 |
falls back to the \R 3.6 (and older) implementation.
|
| 54551 |
ripley |
50 |
UTF-8-encoded paths not valid in the current locale can be used.
|
| 53136 |
ripley |
51 |
|
|
|
52 |
\code{mustWork = FALSE} is useful for expressing paths for use in
|
|
|
53 |
messages.
|
| 33233 |
ripley |
54 |
}
|
| 53134 |
ripley |
55 |
|
| 70519 |
ripley |
56 |
\note{
|
|
|
57 |
The canonical form of paths may not be what you expect. For example,
|
| 70779 |
ripley |
58 |
on macOS absolute paths such as \file{/tmp} and \file{/var} are
|
| 74632 |
kalibera |
59 |
symbolic links. On Linux, a path produced by bash process substitution is
|
|
|
60 |
a symbolic link (such as \file{/proc/fd/63}) to a pipe and there is no
|
| 77324 |
kalibera |
61 |
canonical form of such path. In \R 3.6 and older on Windows, symlinks will
|
|
|
62 |
not be resolved and the long names for path elements will be returned with
|
|
|
63 |
the case in which they are in \code{path}, which may not be canonical in
|
|
|
64 |
case-insensitive folders.
|
| 70519 |
ripley |
65 |
}
|
|
|
66 |
|
| 33233 |
ripley |
67 |
\value{
|
|
|
68 |
A character vector.
|
| 50567 |
ripley |
69 |
|
| 53134 |
ripley |
70 |
If an input is not a real path the result is system-dependent (unless
|
|
|
71 |
\code{mustWork = TRUE}, when this should be an error). It will be
|
|
|
72 |
either the corresponding input element or a transformation of it into
|
|
|
73 |
an absolute path.
|
|
|
74 |
|
|
|
75 |
Converting to an absolute file path can fail for a large number of
|
|
|
76 |
reasons. The most common are
|
|
|
77 |
\itemize{
|
|
|
78 |
\item One of more components of the file path does not exist.
|
|
|
79 |
\item A component before the last is not a directory, or there is
|
|
|
80 |
insufficient permission to read the directory.
|
|
|
81 |
\item For a relative path, the current directory cannot be
|
|
|
82 |
determined.
|
|
|
83 |
\item A symbolic link points to a non-existent place or links form a
|
|
|
84 |
loop.
|
| 53135 |
ripley |
85 |
\item The canonicalized path would be exceed the maximum supported
|
|
|
86 |
length of a file path.
|
| 53134 |
ripley |
87 |
}
|
| 33233 |
ripley |
88 |
}
|
| 53291 |
ripley |
89 |
|
| 35920 |
ripley |
90 |
#ifdef windows
|
| 61433 |
ripley |
91 |
\seealso{
|
| 35920 |
ripley |
92 |
\code{\link{shortPathName}}
|
| 53291 |
ripley |
93 |
}
|
| 53120 |
ripley |
94 |
#endif
|
| 53291 |
ripley |
95 |
|
| 86027 |
smeyer |
96 |
\examples{\dontdiff{% random tempdir
|
| 33233 |
ripley |
97 |
cat(normalizePath(c(R.home(), tempdir())), sep = "\n")
|
| 51725 |
ripley |
98 |
}}
|
| 33233 |
ripley |
99 |
\keyword{ utilities }
|