Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 149 lines (133 sloc) 3.731 kb
d3ec731c »
2004-03-17 Added brief README file, added copyright notes
1 /*
a111585b »
2005-09-15 s/synchronisation/synchronization/
2 * csync2 - cluster synchronization tool, 2nd generation
5f828fce »
2004-03-26 Fixed version number in help text
3 * LINBIT Information Technologies GmbH <http://www.linbit.com>
e315a18e »
2005-06-07 Added 2005 to copyright messages
4 * Copyright (C) 2004, 2005 Clifford Wolf <clifford@clifford.at>
d3ec731c »
2004-03-17 Added brief README file, added copyright notes
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
21 #include "csync2.h"
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <assert.h>
27
82ba047b »
2005-04-27 Little hotfix for building on cygwin
28 static char *my_get_current_dir_name()
29 {
7d1779da »
2007-05-16 Do not use get_current_dir_name() on *BSD systems
30 #if defined __CYGWIN__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
82ba047b »
2005-04-27 Little hotfix for building on cygwin
31 char *r = malloc(1024);
32 if (!getcwd(r, 1024))
33 strcpy(r, "/__PATH_TO_LONG__");
34 return r;
35 #else
36 return get_current_dir_name();
37 #endif
38 }
39
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
40 /*
41 * glibc's realpath() is broken - so don't use it!
42 */
43 char *getrealfn(const char *filename)
44 {
45 static char *ret = 0;
46 char *st_mark = 0;
47 struct stat st;
48 char *tempfn;
49
50 /* create working copy of filename */
51 tempfn = strdup(filename);
52
53 /* make the path absolute */
54 if ( *tempfn != '/' ) {
82ba047b »
2005-04-27 Little hotfix for building on cygwin
55 char *t2, *t1 = my_get_current_dir_name();
92ba38f1 »
2010-09-15 ASPRINTF with check for out of memory.
56
aec04388 »
2011-02-09 fix compiler warnings about statement without effect (ASPRINTF macro)
57 ASPRINTF(&t2, "%s/%s", t1, tempfn);
92ba38f1 »
2010-09-15 ASPRINTF with check for out of memory.
58 free(t1);
59 free(tempfn);
7f9d23d5 »
2010-09-01 Database abstraction layer.
60 tempfn = t2;
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
61 }
62
63 /* remove leading slashes from tempfn */
64 {
65 char *tmp = tempfn + strlen(tempfn) - 1;
66 while (tmp > tempfn && *tmp == '/') *(tmp--)=0;
67 }
68
69 /* get rid of the .. and // entries */
70 {
71 char *source = tempfn, *target = tempfn;
72 for (; *source; source++) {
73 if ( *source == '/' ) {
74 if ( *(source+1) == '/' ) continue;
75 if ( !strncmp(source, "/../", 4) ||
76 !strcmp(source, "/..") ) {
77 while (1) {
78 if ( target == tempfn ) break;
79 if ( *(--target) == '/' ) break;
80 }
81 source += 2;
82 continue;
32856094 »
2004-04-02 Some getrealfn.c improvements for symlinks and /./././. crap.
83 } else
84 if ( !strncmp(source, "/./", 3) ) {
85 source += 2;
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
86 }
87 }
88 *(target++) = *source;
89 }
90 *target = 0;
91 }
92
93 /* this case is trivial */
94 if ( !strcmp(tempfn, "/") )
95 goto return_filename;
96
32856094 »
2004-04-02 Some getrealfn.c improvements for symlinks and /./././. crap.
97 /* find the last stat-able directory element, but don't use the */
98 /* leaf-node because we do not want to resolve a symlink there. */
99 do {
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
100 char *tmp = st_mark;
101 st_mark = strrchr(tempfn, '/');
102 if ( tmp ) *tmp = '/';
103 assert( st_mark != 0 );
104 if ( st_mark == tempfn ) goto return_filename;
105 *st_mark = 0;
32856094 »
2004-04-02 Some getrealfn.c improvements for symlinks and /./././. crap.
106 } while ( stat(tempfn, &st) || !S_ISDIR(st.st_mode) );
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
107
108 /* ok - this might be ugly, but who cares .. */
109 {
82ba047b »
2005-04-27 Little hotfix for building on cygwin
110 char *oldpwd = my_get_current_dir_name();
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
111 if ( !chdir(tempfn) ) {
82ba047b »
2005-04-27 Little hotfix for building on cygwin
112 char *t2, *t1 = my_get_current_dir_name();
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
113 if ( st_mark ) {
aec04388 »
2011-02-09 fix compiler warnings about statement without effect (ASPRINTF macro)
114 ASPRINTF(&t2, "%s/%s", t1, st_mark+1);
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
115 free(tempfn); free(t1); tempfn = t2;
116 } else {
117 free(tempfn); tempfn = t1;
118 }
119 chdir(oldpwd);
120 } else
121 if ( st_mark ) *st_mark = '/';
122 }
123
124 return_filename:
32856094 »
2004-04-02 Some getrealfn.c improvements for symlinks and /./././. crap.
125 /* remove a possible "/." from the end */
126 {
127 int len = strlen(tempfn);
128 if ( len >= 2 && !strcmp(tempfn+len-2, "/.") ) {
129 if (len == 2) len++;
130 *(tempfn+len-2) = 0;
131 }
132 }
133
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
134 if (ret) free(ret);
135 return (ret=tempfn);
136 }
137
32856094 »
2004-04-02 Some getrealfn.c improvements for symlinks and /./././. crap.
138 #ifdef DEBUG_GETREALFN_MAIN
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
139 /* debugging main function to debug this code stand-alone */
140 int main(int argc, char ** argv)
141 {
32856094 »
2004-04-02 Some getrealfn.c improvements for symlinks and /./././. crap.
142 int i;
143 for (i=1; i<argc; i++)
b0c02f4f »
2004-03-17 Initial import from original csync2 CVS.
144 printf("%s -> %s\n", argv[i], getrealfn(argv[i]));
145 return 0;
146 }
147 #endif
148
Something went wrong with that request. Please try again.