| 1 |
/* |
| 2 |
* dpkg-realpath - print the resolved pathname with DPKG_ROOT support |
| 3 |
* |
| 4 |
* Copyright © 2020 Helmut Grohne <[email protected]> |
| 5 |
* Copyright © 2020-2024 Guillem Jover <[email protected]> |
| 6 |
* |
| 7 |
* This is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
#include <compat.h> |
| 23 |
|
| 24 |
#include <sys/types.h> |
| 25 |
#include <sys/stat.h> |
| 26 |
|
| 27 |
#include <errno.h> |
| 28 |
#ifdef HAVE_LOCALE_H |
| 29 |
#include <locale.h> |
| 30 |
#endif |
| 31 |
#include <string.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <stdint.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <stdio.h> |
| 36 |
|
| 37 |
#include <dpkg/i18n.h> |
| 38 |
#include <dpkg/dpkg.h> |
| 39 |
#include <dpkg/dpkg-db.h> |
| 40 |
#include <dpkg/debug.h> |
| 41 |
#include <dpkg/string.h> |
| 42 |
#include <dpkg/db-fsys.h> |
| 43 |
#include <dpkg/options.h> |
| 44 |
|
| 45 |
static const char printforhelp[] = N_( |
| 46 |
"Use --help for help about this utility."); |
| 47 |
|
| 48 |
static void DPKG_ATTR_NORET |
| 49 |
printversion(const struct cmdinfo *cip, const char *value) |
| 50 |
{ |
| 51 |
printf(_("Debian %s version %s.\n"), dpkg_get_progname(), |
| 52 |
PACKAGE_RELEASE); |
| 53 |
|
| 54 |
printf(_( |
| 55 |
"This is free software; see the GNU General Public License version 2 or\n" |
| 56 |
"later for copying conditions. There is NO warranty.\n")); |
| 57 |
|
| 58 |
m_output(stdout, _("<standard output>")); |
| 59 |
|
| 60 |
exit(0); |
| 61 |
} |
| 62 |
|
| 63 |
static void DPKG_ATTR_NORET |
| 64 |
usage(const struct cmdinfo *cip, const char *value) |
| 65 |
{ |
| 66 |
printf(_( |
| 67 |
"Usage: %s [<option>...] <pathname>\n" |
| 68 |
"\n"), dpkg_get_progname()); |
| 69 |
|
| 70 |
printf(_( |
| 71 |
"Options:\n" |
| 72 |
" -z, --zero end output line with NUL, not newline.\n" |
| 73 |
" --instdir <directory> set the root directory.\n" |
| 74 |
" --root <directory> set the root directory.\n" |
| 75 |
" --version show the version.\n" |
| 76 |
" --help show this help message.\n" |
| 77 |
"\n")); |
| 78 |
|
| 79 |
m_output(stdout, _("<standard output>")); |
| 80 |
|
| 81 |
exit(0); |
| 82 |
} |
| 83 |
|
| 84 |
static int opt_eol = '\n'; |
| 85 |
|
| 86 |
static char * |
| 87 |
realpath_relative_to(const char *pathname, const char *rootdir) |
| 88 |
{ |
| 89 |
struct varbuf root = VARBUF_INIT; |
| 90 |
struct varbuf src = VARBUF_INIT; |
| 91 |
struct varbuf dst = VARBUF_INIT; |
| 92 |
struct varbuf slink = VARBUF_INIT; |
| 93 |
struct varbuf result = VARBUF_INIT; |
| 94 |
struct varbuf prefix = VARBUF_INIT; |
| 95 |
int loop = 0; |
| 96 |
|
| 97 |
varbuf_init(&root, 32); |
| 98 |
varbuf_init(&src, 32); |
| 99 |
varbuf_init(&dst, 32); |
| 100 |
varbuf_init(&result, 32); |
| 101 |
|
| 102 |
varbuf_set_str(&root, rootdir); |
| 103 |
varbuf_set_str(&src, pathname); |
| 104 |
varbuf_set_str(&result, rootdir); |
| 105 |
|
| 106 |
/* Check whether the path is relative, make it absolute otherwise. */ |
| 107 |
if (src.buf[0] != '/') { |
| 108 |
struct varbuf abs_src = VARBUF_INIT; |
| 109 |
|
| 110 |
file_getcwd(&abs_src); |
| 111 |
varbuf_add_char(&abs_src, '/'); |
| 112 |
varbuf_add_varbuf(&abs_src, &src); |
| 113 |
|
| 114 |
varbuf_set_varbuf(&src, &abs_src); |
| 115 |
varbuf_destroy(&abs_src); |
| 116 |
|
| 117 |
varbuf_trim_varbuf_prefix(&src, &root); |
| 118 |
} |
| 119 |
|
| 120 |
/* Remove prefixed slashes. */ |
| 121 |
varbuf_trim_char_prefix(&src, '/'); |
| 122 |
|
| 123 |
while (src.used) { |
| 124 |
struct stat st; |
| 125 |
const char *slash; |
| 126 |
|
| 127 |
/* Get the first directory component. */ |
| 128 |
varbuf_set_varbuf(&prefix, &src); |
| 129 |
slash = strchrnul(prefix.buf, '/'); |
| 130 |
varbuf_trunc(&prefix, slash - prefix.buf); |
| 131 |
|
| 132 |
/* Remove the first directory component from src. */ |
| 133 |
varbuf_trim_varbuf_prefix(&src, &prefix); |
| 134 |
|
| 135 |
/* Remove prefixed slashes. */ |
| 136 |
varbuf_trim_char_prefix(&src, '/'); |
| 137 |
|
| 138 |
varbuf_set_varbuf(&slink, &result); |
| 139 |
varbuf_add_char(&slink, '/'); |
| 140 |
varbuf_add_varbuf(&slink, &prefix); |
| 141 |
|
| 142 |
/* Resolve the first directory component. */ |
| 143 |
if (strcmp(prefix.buf, ".") == 0) { |
| 144 |
/* Ignore, stay at the same directory. */ |
| 145 |
} else if (strcmp(prefix.buf, "..") == 0) { |
| 146 |
/* Go up one directory. */ |
| 147 |
slash = strrchr(result.buf, '/'); |
| 148 |
if (slash != NULL) |
| 149 |
varbuf_trunc(&result, slash - result.buf); |
| 150 |
|
| 151 |
if (root.used && !varbuf_has_prefix(&result, &root)) |
| 152 |
varbuf_set_varbuf(&result, &root); |
| 153 |
} else if (lstat(slink.buf, &st) == 0 && S_ISLNK(st.st_mode)) { |
| 154 |
ssize_t linksize; |
| 155 |
|
| 156 |
loop++; |
| 157 |
if (loop > 25) |
| 158 |
ohshit(_("too many levels of symbolic links")); |
| 159 |
|
| 160 |
/* Resolve the symlink within result. */ |
| 161 |
linksize = file_readlink(slink.buf, &dst, st.st_size); |
| 162 |
if (linksize < 0) |
| 163 |
ohshite(_("cannot read link '%s'"), slink.buf); |
| 164 |
else if ((off_t)linksize != st.st_size) |
| 165 |
ohshit(_("symbolic link '%s' size has changed from %jd to %zd"), |
| 166 |
slink.buf, (intmax_t)st.st_size, linksize); |
| 167 |
|
| 168 |
if (dst.buf[0] == '/') { |
| 169 |
/* Absolute pathname, reset result back to |
| 170 |
* root. */ |
| 171 |
varbuf_set_varbuf(&result, &root); |
| 172 |
|
| 173 |
if (src.used) { |
| 174 |
varbuf_add_char(&dst, '/'); |
| 175 |
varbuf_add_varbuf(&dst, &src); |
| 176 |
} |
| 177 |
varbuf_set_varbuf(&src, &dst); |
| 178 |
|
| 179 |
/* Remove prefixed slashes. */ |
| 180 |
varbuf_trim_char_prefix(&src, '/'); |
| 181 |
} else { |
| 182 |
/* Relative pathname. */ |
| 183 |
if (src.used) { |
| 184 |
varbuf_add_char(&dst, '/'); |
| 185 |
varbuf_add_varbuf(&dst, &src); |
| 186 |
} |
| 187 |
varbuf_set_varbuf(&src, &dst); |
| 188 |
} |
| 189 |
} else { |
| 190 |
/* Otherwise append the prefix. */ |
| 191 |
varbuf_add_char(&result, '/'); |
| 192 |
varbuf_add_varbuf(&result, &prefix); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/* We are done, return the resolved pathname, w/o root. */ |
| 197 |
varbuf_trim_varbuf_prefix(&result, &root); |
| 198 |
|
| 199 |
varbuf_destroy(&root); |
| 200 |
varbuf_destroy(&src); |
| 201 |
varbuf_destroy(&dst); |
| 202 |
varbuf_destroy(&slink); |
| 203 |
varbuf_destroy(&prefix); |
| 204 |
|
| 205 |
return varbuf_detach(&result); |
| 206 |
} |
| 207 |
|
| 208 |
static const struct cmdinfo cmdinfos[] = { |
| 209 |
{ "zero", 'z', 0, &opt_eol, NULL, NULL, '\0' }, |
| 210 |
{ "instdir", 0, 1, NULL, NULL, set_instdir, 0 }, |
| 211 |
{ "root", 0, 1, NULL, NULL, set_instdir, 0 }, |
| 212 |
{ "version", 0, 0, NULL, NULL, printversion }, |
| 213 |
{ "help", '?', 0, NULL, NULL, usage }, |
| 214 |
{ NULL, 0 } |
| 215 |
}; |
| 216 |
|
| 217 |
int |
| 218 |
main(int argc, const char *const *argv) |
| 219 |
{ |
| 220 |
const char *instdir; |
| 221 |
const char *pathname; |
| 222 |
char *real_pathname; |
| 223 |
|
| 224 |
dpkg_locales_init(PACKAGE); |
| 225 |
dpkg_program_init("dpkg-realpath"); |
| 226 |
dpkg_options_parse(&argv, cmdinfos, printforhelp); |
| 227 |
|
| 228 |
debug(dbg_general, "root=%s admindir=%s", |
| 229 |
dpkg_fsys_get_dir(), dpkg_db_get_dir()); |
| 230 |
|
| 231 |
pathname = argv[0]; |
| 232 |
if (str_is_unset(pathname)) |
| 233 |
badusage(_("need a pathname argument")); |
| 234 |
|
| 235 |
instdir = dpkg_fsys_get_dir(); |
| 236 |
if (strlen(instdir) && strncmp(pathname, instdir, strlen(instdir)) == 0) |
| 237 |
badusage(_("link '%s' includes root prefix '%s'"), |
| 238 |
pathname, instdir); |
| 239 |
|
| 240 |
real_pathname = realpath_relative_to(pathname, instdir); |
| 241 |
printf("%s%c", real_pathname, opt_eol); |
| 242 |
free(real_pathname); |
| 243 |
|
| 244 |
dpkg_program_done(); |
| 245 |
dpkg_locales_done(); |
| 246 |
|
| 247 |
return 0; |
| 248 |
} |