dpkg-realpath: Rewrite in C
[dpkg/guillem.git] / src / realpath / main.c
blob387294a614a513daa18b3573fce9e4a231635ae3
1 /*
2 * dpkg-realpath - print the resolved pathname with DPKG_ROOT support
4 * Copyright © 2020 Helmut Grohne <helmut@subdivi.de>
5 * Copyright © 2020-2024 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
21 #include <config.h>
22 #include <compat.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
27 #include <errno.h>
28 #if 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>
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>
45 static const char printforhelp[] = N_(
46 "Use --help for help about this utility.");
48 static void DPKG_ATTR_NORET
49 printversion(const struct cmdinfo *cip, const char *value)
51 printf(_("Debian %s version %s.\n"), dpkg_get_progname(),
52 PACKAGE_RELEASE);
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"));
58 m_output(stdout, _("<standard output>"));
60 exit(0);
63 static void DPKG_ATTR_NORET
64 usage(const struct cmdinfo *cip, const char *value)
66 printf(_(
67 "Usage: %s [<option>...] <pathname>\n"
68 "\n"), dpkg_get_progname());
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"));
79 m_output(stdout, _("<standard output>"));
81 exit(0);
84 static int opt_eol = '\n';
86 static char *
87 realpath_relative_to(const char *pathname, const char *rootdir)
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;
97 varbuf_set_str(&root, rootdir);
98 varbuf_set_str(&src, pathname);
99 varbuf_set_str(&result, rootdir);
101 /* Check whether the path is relative, make it absolute otherwise. */
102 if (src.buf[0] != '/') {
103 struct varbuf abs_src = VARBUF_INIT;
105 file_getcwd(&abs_src);
106 varbuf_add_char(&abs_src, '/');
107 varbuf_add_varbuf(&abs_src, &src);
109 varbuf_set_varbuf(&src, &abs_src);
110 varbuf_destroy(&abs_src);
112 varbuf_trim_varbuf_prefix(&src, &root);
115 /* Remove prefixed slashes. */
116 varbuf_trim_char_prefix(&src, '/');
118 while (src.used) {
119 struct stat st;
120 const char *slash;
122 /* Get the first directory component. */
123 varbuf_set_varbuf(&prefix, &src);
124 slash = strchrnul(prefix.buf, '/');
125 varbuf_trunc(&prefix, slash - prefix.buf);
127 /* Remove the first directory component from src. */
128 varbuf_trim_varbuf_prefix(&src, &prefix);
130 /* Remove prefixed slashes. */
131 varbuf_trim_char_prefix(&src, '/');
133 varbuf_set_varbuf(&slink, &result);
134 varbuf_add_char(&slink, '/');
135 varbuf_add_varbuf(&slink, &prefix);
137 /* Resolve the first directory component. */
138 if (strcmp(prefix.buf, ".") == 0) {
139 /* Ignore, stay at the same directory. */
140 } else if (strcmp(prefix.buf, "..") == 0) {
141 /* Go up one directory. */
142 slash = strrchr(result.buf, '/');
143 if (slash != NULL)
144 varbuf_trunc(&result, slash - result.buf);
146 if (root.used && !varbuf_has_prefix(&result, &root))
147 varbuf_set_varbuf(&result, &root);
148 } else if (lstat(slink.buf, &st) == 0 && S_ISLNK(st.st_mode)) {
149 ssize_t linksize;
151 loop++;
152 if (loop > 25)
153 ohshit(_("too many levels of symbolic links"));
155 /* Resolve the symlink within result. */
156 linksize = file_readlink(slink.buf, &dst, st.st_size);
157 if (linksize < 0)
158 ohshite(_("cannot read link '%s'"), slink.buf);
159 else if ((off_t)linksize != st.st_size)
160 ohshit(_("symbolic link '%s' size has changed from %jd to %zd"),
161 slink.buf, (intmax_t)st.st_size, linksize);
163 if (dst.buf[0] == '/') {
164 /* Absolute pathname, reset result back to
165 * root. */
166 varbuf_set_varbuf(&result, &root);
168 if (src.used) {
169 varbuf_add_char(&dst, '/');
170 varbuf_add_varbuf(&dst, &src);
172 varbuf_set_varbuf(&src, &dst);
174 /* Remove prefixed slashes. */
175 varbuf_trim_char_prefix(&src, '/');
176 } else {
177 /* Relative pathname. */
178 if (src.used) {
179 varbuf_add_char(&dst, '/');
180 varbuf_add_varbuf(&dst, &src);
182 varbuf_set_varbuf(&src, &dst);
184 } else {
185 /* Otherwise append the prefix. */
186 varbuf_add_char(&result, '/');
187 varbuf_add_varbuf(&result, &prefix);
191 /* We are done, return the resolved pathname, w/o root. */
192 varbuf_trim_varbuf_prefix(&result, &root);
194 varbuf_destroy(&root);
195 varbuf_destroy(&src);
196 varbuf_destroy(&dst);
197 varbuf_destroy(&slink);
198 varbuf_destroy(&prefix);
200 return varbuf_detach(&result);
203 static const struct cmdinfo cmdinfos[] = {
204 { "zero", 'z', 0, &opt_eol, NULL, NULL, '\0' },
205 { "instdir", 0, 1, NULL, NULL, set_instdir, 0 },
206 { "root", 0, 1, NULL, NULL, set_instdir, 0 },
207 { "version", 0, 0, NULL, NULL, printversion },
208 { "help", '?', 0, NULL, NULL, usage },
209 { NULL, 0 }
213 main(int argc, const char *const *argv)
215 const char *instdir;
216 const char *pathname;
217 char *real_pathname;
219 dpkg_locales_init(PACKAGE);
220 dpkg_program_init("dpkg-realpath");
221 dpkg_options_parse(&argv, cmdinfos, printforhelp);
223 debug(dbg_general, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());
225 pathname = argv[0];
226 if (pathname == NULL)
227 badusage(_("need a pathname argument"));
229 instdir = dpkg_fsys_get_dir();
230 if (strlen(instdir) && strncmp(pathname, instdir, strlen(instdir)) == 0)
231 badusage(_("link '%s' includes root prefix '%s'"),
232 pathname, instdir);
234 real_pathname = realpath_relative_to(pathname, instdir);
235 printf("%s%c", real_pathname, opt_eol);
236 free(real_pathname);
238 dpkg_program_done();
239 dpkg_locales_done();
241 return 0;