Debian Codegraph @

src/rel2absat.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2013-2015 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_FCHDIR
24
25 #define _BSD_SOURCE
26 #define _GNU_SOURCE
27 #define _DEFAULT_SOURCE
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32
33 #include "libfakechroot.h"
34 #include "strlcpy.h"
35 #include "dedotdot.h"
36 #include "open.h"
37
38
39 LOCAL char * rel2absat(int dirfd, const char * name, char * resolved)
40 {
41 int cwdfd = 0;
42 char cwd[FAKECHROOT_PATH_MAX - 1];
43
44 debug("rel2absat(%d, \"%s\", &resolved)", dirfd, name);
45
46 if (name == NULL) {
47 resolved = NULL;
48 goto end;
49 }
50
51 if (*name == '\0') {
52 *resolved = '\0';
53 goto end;
54 }
55
56 if (*name == '/') {
57 strlcpy(resolved, name, FAKECHROOT_PATH_MAX);
58 } else if(dirfd == AT_FDCWD) {
59 if (! getcwd(cwd, FAKECHROOT_PATH_MAX - 1)) {
60 goto error;
61 }
62 snprintf(resolved, FAKECHROOT_PATH_MAX, "%s/%s", cwd, name);
63 } else {
64 if ((cwdfd = nextcall(open)(".", O_RDONLY|O_DIRECTORY)) == -1) {
65 goto error;
66 }
67
68 if (fchdir(dirfd) == -1) {
69 goto error;
70 }
71 if (! getcwd(cwd, FAKECHROOT_PATH_MAX - 1)) {
72 goto error;
73 }
74 if (fchdir(cwdfd) == -1) {
75 goto error;
76 }
77 (void)close(cwdfd);
78
79 snprintf(resolved, FAKECHROOT_PATH_MAX, "%s/%s", cwd, name);
80 }
81
82 dedotdot(resolved);
83
84 end:
85 debug("rel2absat(%d, \"%s\", \"%s\")", dirfd, name, resolved);
86 return resolved;
87
88 error:
89 if (cwdfd) {
90 (void)close(cwdfd);
91 }
92 resolved = NULL;
93 debug("rel2absat(%d, \"%s\", NULL)", dirfd, name);
94 return resolved;
95 }
96
97 #else
98 typedef int empty_translation_unit;
99 #endif