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/>.
24 #include <sys/types.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(),
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>"));
63 static void DPKG_ATTR_NORET
64 usage(const struct cmdinfo
*cip
, const char *value
)
67 "Usage: %s [<option>...] <pathname>\n"
68 "\n"), dpkg_get_progname());
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"
79 m_output(stdout
, _("<standard output>"));
84 static int opt_eol
= '\n';
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
;
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
, '/');
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
, '/');
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
)) {
153 ohshit(_("too many levels of symbolic links"));
155 /* Resolve the symlink within result. */
156 linksize
= file_readlink(slink
.buf
, &dst
, st
.st_size
);
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
166 varbuf_set_varbuf(&result
, &root
);
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
, '/');
177 /* Relative pathname. */
179 varbuf_add_char(&dst
, '/');
180 varbuf_add_varbuf(&dst
, &src
);
182 varbuf_set_varbuf(&src
, &dst
);
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
},
213 main(int argc
, const char *const *argv
)
216 const char *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());
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'"),
234 real_pathname
= realpath_relative_to(pathname
, instdir
);
235 printf("%s%c", real_pathname
, opt_eol
);