Debian Codegraph @

src/readlinkat.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2010, 2013 Piotr Roszatycki <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library 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 GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21 #include <config.h>
22
23 #ifdef HAVE_READLINKAT
24
25 #define _ATFILE_SOURCE
26 #include <sys/types.h>
27 #include <stddef.h>
28 #include "libfakechroot.h"
29
30
31 wrapper(readlinkat, ssize_t, (int dirfd, const char * path, char * buf, size_t bufsiz))
32 {
33 int linksize;
34 char tmp[FAKECHROOT_PATH_MAX], *tmpptr;
35 const char *fakechroot_base = getenv("FAKECHROOT_BASE");
36 char fakechroot_abspath[FAKECHROOT_PATH_MAX];
37 char fakechroot_buf[FAKECHROOT_PATH_MAX];
38
39 debug("readlinkat(%d, \"%s\", &buf, %zd)", dirfd, path, bufsiz);
40 expand_chroot_path_at(dirfd, path);
41
42 if ((linksize = nextcall(readlinkat)(dirfd, path, tmp, FAKECHROOT_PATH_MAX-1)) == -1) {
43 return -1;
44 }
45 tmp[linksize] = '\0';
46
47 if (fakechroot_base != NULL) {
48 tmpptr = strstr(tmp, fakechroot_base);
49 if (tmpptr != tmp) {
50 tmpptr = tmp;
51 }
52 else if (tmp[strlen(fakechroot_base)] == '\0') {
53 tmpptr = "/";
54 linksize = strlen(tmpptr);
55 }
56 else if (tmp[strlen(fakechroot_base)] == '/') {
57 tmpptr = tmp + strlen(fakechroot_base);
58 linksize -= strlen(fakechroot_base);
59 }
60 else {
61 tmpptr = tmp;
62 }
63 if (strlen(tmpptr) > bufsiz) {
64 linksize = bufsiz;
65 }
66 strncpy(buf, tmpptr, linksize);
67 }
68 else {
69 strncpy(buf, tmp, linksize);
70 }
71 return linksize;
72 }
73
74 #else
75 typedef int empty_translation_unit;
76 #endif