forked from LINBIT/csync2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getrealfn.c
153 lines (138 loc) · 3.79 KB
/
getrealfn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* csync2 - cluster synchronization tool, 2nd generation
* Copyright (C) 2004 - 2015 LINBIT Information Technologies GmbH
* http://www.linbit.com; see also AUTHORS
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "csync2.h"
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
static char *my_get_current_dir_name()
{
#if defined __CYGWIN__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
char *r = malloc(1024);
if (!getcwd(r, 1024))
strcpy(r, "/__PATH_TO_LONG__");
return r;
#else
return get_current_dir_name();
#endif
}
/*
* glibc's realpath() is broken - so don't use it!
*/
char *getrealfn(const char *filename)
{
static char *ret = 0;
char *st_mark = 0;
struct stat st;
char *tempfn;
/* create working copy of filename */
tempfn = strdup(filename);
/* make the path absolute */
if ( *tempfn != '/' ) {
char *t2, *t1 = my_get_current_dir_name();
ASPRINTF(&t2, "%s/%s", t1, tempfn);
free(t1);
free(tempfn);
tempfn = t2;
}
/* remove leading slashes from tempfn */
{
char *tmp = tempfn + strlen(tempfn) - 1;
while (tmp > tempfn && *tmp == '/') *(tmp--)=0;
}
/* get rid of the .. and // entries */
{
char *source = tempfn, *target = tempfn;
for (; *source; source++) {
if ( *source == '/' ) {
if ( *(source+1) == '/' ) continue;
if ( !strncmp(source, "/../", 4) ||
!strcmp(source, "/..") ) {
while (1) {
if ( target == tempfn ) break;
if ( *(--target) == '/' ) break;
}
source += 2;
continue;
} else
if ( !strncmp(source, "/./", 3) ) {
source += 2;
}
}
*(target++) = *source;
}
*target = 0;
}
/* this case is trivial */
if ( !strcmp(tempfn, "/") )
goto return_filename;
/* find the last stat-able directory element, but don't use the */
/* leaf-node because we do not want to resolve a symlink there. */
do {
char *tmp = st_mark;
st_mark = strrchr(tempfn, '/');
if ( tmp ) *tmp = '/';
assert( st_mark != 0 );
if ( st_mark == tempfn ) goto return_filename;
*st_mark = 0;
} while ( stat(tempfn, &st) || !S_ISDIR(st.st_mode) );
/* ok - this might be ugly, but who cares .. */
{
int cwdfd = open(".", O_RDONLY);
if (cwdfd < 0)
csync_fatal("Cannot open current directory!\n");
if ( !chdir(tempfn) ) {
char *t2, *t1 = my_get_current_dir_name();
if ( st_mark ) {
ASPRINTF(&t2, "%s/%s", t1, st_mark+1);
free(tempfn); free(t1); tempfn = t2;
} else {
free(tempfn); tempfn = t1;
}
if (fchdir(cwdfd))
csync_fatal("Cannot chdir back to previous directory!\n");
close(cwdfd);
} else
if ( st_mark ) *st_mark = '/';
}
return_filename:
/* remove a possible "/." from the end */
{
int len = strlen(tempfn);
if ( len >= 2 && !strcmp(tempfn+len-2, "/.") ) {
if (len == 2) len++;
*(tempfn+len-2) = 0;
}
}
if (ret) free(ret);
return (ret=tempfn);
}
#ifdef DEBUG_GETREALFN_MAIN
/* debugging main function to debug this code stand-alone */
int main(int argc, char ** argv)
{
int i;
for (i=1; i<argc; i++)
printf("%s -> %s\n", argv[i], getrealfn(argv[i]));
return 0;
}
#endif