]>
git.proxmox.com Git - ceph.git/blob - ceph/src/mount/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
32 # define MAXSYMLINKS 256
36 myrealpath(const char *path
, char *resolved_path
, int maxreslth
) {
43 npath
= resolved_path
;
45 /* If it's a relative pathname use getcwd for starters. */
47 if (!getcwd(npath
, maxreslth
-2))
49 npath
+= strlen(npath
);
57 /* Expand each slash-separated pathname component. */
58 link_path
= malloc(PATH_MAX
+1);
61 while (*path
!= '\0') {
62 /* Ignore stray "/" */
67 if (*path
== '.' && (path
[1] == '\0' || path
[1] == '/')) {
72 if (*path
== '.' && path
[1] == '.' &&
73 (path
[2] == '\0' || path
[2] == '/')) {
76 while (npath
> resolved_path
+1 &&
81 /* Safely copy the next pathname component. */
82 while (*path
!= '\0' && *path
!= '/') {
83 if (npath
-resolved_path
> maxreslth
-2) {
90 /* Protect against infinite loops. */
91 if (readlinks
++ > MAXSYMLINKS
) {
96 /* See if last pathname component is a symlink. */
99 n
= readlink(resolved_path
, link_path
, PATH_MAX
);
101 /* EINVAL means the file exists but isn't a symlink. */
108 /* Note: readlink doesn't add the null byte. */
110 if (*link_path
== '/')
111 /* Start over for an absolute symlink. */
112 npath
= resolved_path
;
114 /* Otherwise back up over this component. */
115 while (*(--npath
) != '/')
118 /* Insert symlink contents into path. */
120 newbuf
= malloc(m
+ n
+ 1);
123 memcpy(newbuf
, link_path
, n
);
124 memcpy(newbuf
+ n
, path
, m
+ 1);
130 /* Delete trailing slash but don't whomp a lone slash. */
131 if (npath
!= resolved_path
+1 && npath
[-1] == '/')
133 /* Make sure it's null terminated. */
138 return resolved_path
;
147 * Converts private "dm-N" names to "/dev/mapper/<name>"
149 * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
150 * provides the real DM device names in /sys/block/<ptname>/dm/name
153 canonicalize_dm_name(const char *ptname
)
157 char path
[268], name
[256], *res
= NULL
;
159 snprintf(path
, sizeof(path
), "/sys/block/%s/dm/name", ptname
);
160 if (!(f
= fopen(path
, "r")))
163 /* read "<name>\n" from sysfs */
164 if (fgets(name
, sizeof(name
), f
) && (sz
= strlen(name
)) > 1) {
166 snprintf(path
, sizeof(path
), "/dev/mapper/%s", name
);
174 canonicalize_path(const char *path
)
182 canonical
= malloc(PATH_MAX
+2);
185 if (!myrealpath(path
, canonical
, PATH_MAX
+1)) {
191 p
= strrchr(canonical
, '/');
192 if (p
&& strncmp(p
, "/dm-", 4) == 0 && isdigit(*(p
+ 4))) {
193 p
= canonicalize_dm_name(p
+1);