]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/canonicalize.c
2 * canonicalize.c -- canonicalize pathname by removing symlinks
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library Public License for more details.
18 * This routine is part of libc. We include it nevertheless,
19 * since the libc version has some security flaws.
21 * TODO: use canonicalize_file_name() when exist in glibc
30 #include "canonicalize.h"
33 # define MAXSYMLINKS 256
37 myrealpath(const char *path
, char *resolved_path
, int maxreslth
) {
40 char link_path
[PATH_MAX
+1];
44 npath
= resolved_path
;
46 /* If it's a relative pathname use getcwd for starters. */
48 if (!getcwd(npath
, maxreslth
-2))
50 npath
+= strlen(npath
);
58 /* Expand each slash-separated pathname component. */
59 while (*path
!= '\0') {
60 /* Ignore stray "/" */
65 if (*path
== '.' && (path
[1] == '\0' || path
[1] == '/')) {
70 if (*path
== '.' && path
[1] == '.' &&
71 (path
[2] == '\0' || path
[2] == '/')) {
74 while (npath
> resolved_path
+1 &&
79 /* Safely copy the next pathname component. */
80 while (*path
!= '\0' && *path
!= '/') {
81 if (npath
-resolved_path
> maxreslth
-2) {
88 /* Protect against infinite loops. */
89 if (readlinks
++ > MAXSYMLINKS
) {
94 /* See if last pathname component is a symlink. */
96 n
= readlink(resolved_path
, link_path
, PATH_MAX
);
98 /* EINVAL means the file exists but isn't a symlink. */
105 /* Note: readlink doesn't add the null byte. */
107 if (*link_path
== '/')
108 /* Start over for an absolute symlink. */
109 npath
= resolved_path
;
111 /* Otherwise back up over this component. */
112 while (*(--npath
) != '/')
115 /* Insert symlink contents into path. */
117 newbuf
= malloc(m
+ n
+ 1);
120 memcpy(newbuf
, link_path
, n
);
121 memcpy(newbuf
+ n
, path
, m
+ 1);
127 /* Delete trailing slash but don't whomp a lone slash. */
128 if (npath
!= resolved_path
+1 && npath
[-1] == '/')
130 /* Make sure it's null terminated. */
134 return resolved_path
;
142 * Converts private "dm-N" names to "/dev/mapper/<name>"
144 * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
145 * provides the real DM device names in /sys/block/<ptname>/dm/name
148 canonicalize_dm_name(const char *ptname
)
152 char path
[256], name
[256], *res
= NULL
;
154 snprintf(path
, sizeof(path
), "/sys/block/%s/dm/name", ptname
);
155 if (!(f
= fopen(path
, "r")))
158 /* read "<name>\n" from sysfs */
159 if (fgets(name
, sizeof(name
), f
) && (sz
= strlen(name
)) > 1) {
161 snprintf(path
, sizeof(path
), "/dev/mapper/%s", name
);
169 canonicalize_path(const char *path
)
171 char canonical
[PATH_MAX
+2];
177 if (!myrealpath(path
, canonical
, PATH_MAX
+1))
181 p
= strrchr(canonical
, '/');
182 if (p
&& strncmp(p
, "/dm-", 4) == 0 && isdigit(*(p
+ 4))) {
183 p
= canonicalize_dm_name(p
+1);
188 return strdup(canonical
);
192 #ifdef TEST_PROGRAM_CANONICALIZE
193 int main(int argc
, char **argv
)
196 fprintf(stderr
, "usage: %s <device>\n", argv
[0]);
200 fprintf(stdout
, "orig: %s\n", argv
[1]);
201 fprintf(stdout
, "real: %s\n", canonicalize_path(argv
[1]));