428
|
1 /*
|
|
2 * realpath.c -- canonicalize pathname by removing symlinks
|
|
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
|
|
4 *
|
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: Not in FSF. */
|
|
24
|
572
|
25 #define DONT_ENCAPSULATE
|
428
|
26 #include <config.h>
|
446
|
27 #include "lisp.h"
|
442
|
28
|
558
|
29 #include "sysfile.h"
|
428
|
30
|
446
|
31 /* First char after start of absolute filename. */
|
|
32 #define ABS_START(name) (name + ABS_LENGTH (name))
|
|
33
|
|
34 #if defined (WIN32_NATIVE)
|
|
35 /* Length of start of absolute filename. */
|
|
36 # define ABS_LENGTH(name) (win32_abs_start (name))
|
|
37 static int win32_abs_start (const char * name);
|
|
38 /* System dependent version of readlink. */
|
|
39 # define system_readlink win32_readlink
|
|
40 #else
|
|
41 # ifdef CYGWIN
|
|
42 # define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? \
|
|
43 (IS_DIRECTORY_SEP (name[1]) ? 2 : 1) : 0)
|
|
44 # define system_readlink cygwin_readlink
|
|
45 # else
|
|
46 # define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? 1 : 0)
|
|
47 # define system_readlink readlink
|
|
48 # endif /* CYGWIN */
|
|
49 #endif /* WIN32_NATIVE */
|
|
50
|
|
51 #if defined (WIN32_NATIVE) || defined (CYGWIN)
|
|
52 #include "syswindows.h"
|
|
53 /* Emulate readlink on win32 - finds real name (i.e. correct case) of
|
|
54 a file. UNC servers and shares are lower-cased. Directories must be
|
|
55 given without trailing '/'. One day, this could read Win2K's
|
|
56 reparse points. */
|
|
57 static int
|
|
58 win32_readlink (const char * name, char * buf, int size)
|
|
59 {
|
|
60 WIN32_FIND_DATA find_data;
|
|
61 HANDLE dir_handle = NULL;
|
|
62 int len = 0;
|
|
63 int err = 0;
|
|
64 const char* lastname;
|
|
65 int count = 0;
|
|
66 const char* tmp;
|
|
67 char* res = NULL;
|
|
68
|
|
69 assert (*name);
|
|
70
|
|
71 /* Sort of check we have a valid filename. */
|
558
|
72 if (strpbrk (name, "*?|<>\"") || strlen (name) >= PATH_MAX)
|
446
|
73 {
|
|
74 errno = EIO;
|
|
75 return -1;
|
|
76 }
|
|
77
|
|
78 /* Find start of filename */
|
|
79 lastname = name + strlen (name);
|
|
80 while (lastname > name && !IS_DIRECTORY_SEP (lastname[-1]))
|
|
81 --lastname;
|
|
82
|
|
83 /* Count slashes in unc path */
|
|
84 if (ABS_LENGTH (name) == 2)
|
|
85 for (tmp = name; *tmp; tmp++)
|
|
86 if (IS_DIRECTORY_SEP (*tmp))
|
|
87 count++;
|
|
88
|
|
89 if (count >= 2 && count < 4)
|
|
90 {
|
|
91 /* UNC server or share name: just copy lowercased name. */
|
|
92 res = find_data.cFileName;
|
|
93 for (tmp = lastname; *tmp; tmp++)
|
|
94 *res++ = tolower (*tmp);
|
|
95 *res = '\0';
|
|
96 }
|
|
97 else
|
|
98 dir_handle = FindFirstFile (name, &find_data);
|
|
99
|
|
100 if (res || dir_handle != INVALID_HANDLE_VALUE)
|
|
101 {
|
|
102 if ((len = strlen (find_data.cFileName)) < size)
|
|
103 {
|
|
104 if (strcmp (lastname, find_data.cFileName) == 0)
|
|
105 /* Signal that the name is already OK. */
|
|
106 err = EINVAL;
|
|
107 else
|
|
108 memcpy (buf, find_data.cFileName, len + 1);
|
|
109 }
|
|
110 else
|
|
111 err = ENAMETOOLONG;
|
|
112 if (!res) FindClose (dir_handle);
|
|
113 }
|
|
114 else
|
|
115 err = ENOENT;
|
|
116
|
|
117 errno = err;
|
|
118 return err ? -1 : len;
|
|
119 }
|
|
120 #endif /* WIN32_NATIVE || CYGWIN */
|
|
121
|
|
122 #ifdef CYGWIN
|
|
123 /* Call readlink and try to find out the correct case for the file. */
|
|
124 static int
|
|
125 cygwin_readlink (const char * name, char * buf, int size)
|
|
126 {
|
|
127 int n = readlink (name, buf, size);
|
462
|
128 if (n < 0 && errno == EINVAL)
|
446
|
129 {
|
|
130 /* The file may exist, but isn't a symlink. Try to find the
|
|
131 right name. */
|
|
132 char* tmp = alloca (cygwin_posix_to_win32_path_list_buf_size (name));
|
|
133 cygwin_posix_to_win32_path_list (name, tmp);
|
|
134 n = win32_readlink (tmp, buf, size);
|
|
135 }
|
|
136 return n;
|
|
137 }
|
|
138 #endif /* CYGWIN */
|
|
139
|
|
140 #ifdef WIN32_NATIVE
|
|
141 #ifndef ELOOP
|
|
142 #define ELOOP 10062 /* = WSAELOOP in winsock.h */
|
|
143 #endif
|
|
144 /* Length of start of absolute filename. */
|
|
145 static int
|
|
146 win32_abs_start (const char * name)
|
|
147 {
|
|
148 if (isalpha (*name) && IS_DEVICE_SEP (name[1])
|
|
149 && IS_DIRECTORY_SEP (name[2]))
|
|
150 return 3;
|
|
151 else if (IS_DIRECTORY_SEP (*name))
|
|
152 return IS_DIRECTORY_SEP (name[1]) ? 2 : 1;
|
|
153 else
|
|
154 return 0;
|
|
155 }
|
|
156 #endif /* WIN32_NATIVE */
|
|
157
|
440
|
158 #if !defined (HAVE_GETCWD) && defined (HAVE_GETWD)
|
|
159 #undef getcwd
|
|
160 #define getcwd(buffer, len) getwd (buffer)
|
|
161 #endif
|
|
162
|
428
|
163 #ifndef PATH_MAX
|
440
|
164 # if defined (_POSIX_PATH_MAX)
|
|
165 # define PATH_MAX _POSIX_PATH_MAX
|
|
166 # elif defined (MAXPATHLEN)
|
|
167 # define PATH_MAX MAXPATHLEN
|
|
168 # else
|
|
169 # define PATH_MAX 1024
|
|
170 # endif
|
428
|
171 #endif
|
|
172
|
|
173 #define MAX_READLINKS 32
|
|
174
|
440
|
175 char * xrealpath (const char *path, char resolved_path []);
|
428
|
176 char *
|
|
177 xrealpath (const char *path, char resolved_path [])
|
|
178 {
|
|
179 char copy_path[PATH_MAX];
|
|
180 char *new_path = resolved_path;
|
|
181 char *max_path;
|
446
|
182 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
|
428
|
183 int readlinks = 0;
|
|
184 char link_path[PATH_MAX];
|
|
185 int n;
|
446
|
186 int abslen = ABS_LENGTH (path);
|
428
|
187 #endif
|
|
188
|
|
189 /* Make a copy of the source path since we may need to modify it. */
|
442
|
190 strcpy (copy_path, path);
|
428
|
191 path = copy_path;
|
|
192 max_path = copy_path + PATH_MAX - 2;
|
446
|
193
|
442
|
194 #ifdef WIN32_NATIVE
|
446
|
195 /* Check for c:/... or //server/... */
|
|
196 if (abslen == 2 || abslen == 3)
|
428
|
197 {
|
446
|
198 strncpy (new_path, path, abslen);
|
462
|
199 /* Make sure drive letter is lowercased. */
|
|
200 if (abslen == 3)
|
|
201 *new_path = tolower (*new_path);
|
446
|
202 new_path += abslen;
|
|
203 path += abslen;
|
428
|
204 }
|
446
|
205 /* No drive letter, but a beginning slash? Prepend drive letter. */
|
|
206 else if (abslen == 1)
|
428
|
207 {
|
440
|
208 getcwd (new_path, PATH_MAX - 1);
|
428
|
209 new_path += 3;
|
|
210 path++;
|
|
211 }
|
446
|
212 /* Just a path name, prepend the current directory */
|
428
|
213 else
|
|
214 {
|
440
|
215 getcwd (new_path, PATH_MAX - 1);
|
446
|
216 new_path += strlen (new_path);
|
|
217 if (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
218 *new_path++ = DIRECTORY_SEP;
|
428
|
219 }
|
|
220 #else
|
440
|
221 /* If it's a relative pathname use getcwd for starters. */
|
446
|
222 if (abslen == 0)
|
428
|
223 {
|
440
|
224 getcwd (new_path, PATH_MAX - 1);
|
446
|
225 new_path += strlen (new_path);
|
|
226 if (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
227 *new_path++ = DIRECTORY_SEP;
|
428
|
228 }
|
|
229 else
|
|
230 {
|
446
|
231 /* Copy first directory sep. May have two on cygwin. */
|
|
232 strncpy (new_path, path, abslen);
|
|
233 new_path += abslen;
|
|
234 path += abslen;
|
428
|
235 }
|
|
236 #endif
|
|
237 /* Expand each slash-separated pathname component. */
|
|
238 while (*path != '\0')
|
|
239 {
|
|
240 /* Ignore stray "/". */
|
446
|
241 if (IS_DIRECTORY_SEP (*path))
|
428
|
242 {
|
|
243 path++;
|
|
244 continue;
|
|
245 }
|
|
246
|
|
247 if (*path == '.')
|
|
248 {
|
|
249 /* Ignore ".". */
|
446
|
250 if (path[1] == '\0' || IS_DIRECTORY_SEP (path[1]))
|
428
|
251 {
|
|
252 path++;
|
|
253 continue;
|
|
254 }
|
|
255
|
442
|
256 /* Handle ".." */
|
|
257 if (path[1] == '.' &&
|
446
|
258 (path[2] == '\0' || IS_DIRECTORY_SEP (path[2])))
|
428
|
259 {
|
442
|
260 path += 2;
|
428
|
261
|
442
|
262 /* Ignore ".." at root. */
|
446
|
263 if (new_path == ABS_START (resolved_path))
|
442
|
264 continue;
|
428
|
265
|
442
|
266 /* Handle ".." by backing up. */
|
446
|
267 --new_path;
|
|
268 while (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
269 --new_path;
|
442
|
270 continue;
|
428
|
271 }
|
|
272 }
|
|
273
|
|
274 /* Safely copy the next pathname component. */
|
446
|
275 while (*path != '\0' && !IS_DIRECTORY_SEP (*path))
|
428
|
276 {
|
|
277 if (path > max_path)
|
|
278 {
|
|
279 errno = ENAMETOOLONG;
|
|
280 return NULL;
|
|
281 }
|
|
282 *new_path++ = *path++;
|
|
283 }
|
|
284
|
446
|
285 #if defined (S_IFLNK) || defined (WIN32_NATIVE)
|
428
|
286 /* See if latest pathname component is a symlink. */
|
|
287 *new_path = '\0';
|
446
|
288 n = system_readlink (resolved_path, link_path, PATH_MAX - 1);
|
428
|
289
|
|
290 if (n < 0)
|
|
291 {
|
|
292 /* EINVAL means the file exists but isn't a symlink. */
|
462
|
293 #ifdef CYGWIN
|
|
294 if (errno != EINVAL && errno != ENOENT)
|
|
295 #else
|
|
296 if (errno != EINVAL)
|
|
297 #endif
|
428
|
298 return NULL;
|
|
299 }
|
|
300 else
|
|
301 {
|
|
302 /* Protect against infinite loops. */
|
|
303 if (readlinks++ > MAX_READLINKS)
|
|
304 {
|
|
305 errno = ELOOP;
|
|
306 return NULL;
|
|
307 }
|
|
308
|
|
309 /* Note: readlink doesn't add the null byte. */
|
|
310 link_path[n] = '\0';
|
446
|
311
|
|
312 if (ABS_LENGTH (link_path) > 0)
|
428
|
313 /* Start over for an absolute symlink. */
|
446
|
314 new_path = resolved_path + ABS_LENGTH (link_path) - 1;
|
428
|
315 else
|
|
316 /* Otherwise back up over this component. */
|
446
|
317 for (--new_path; !IS_DIRECTORY_SEP (*new_path); --new_path)
|
|
318 assert (new_path > resolved_path);
|
428
|
319
|
|
320 /* Safe sex check. */
|
|
321 if (strlen(path) + n >= PATH_MAX)
|
|
322 {
|
|
323 errno = ENAMETOOLONG;
|
|
324 return NULL;
|
|
325 }
|
|
326
|
|
327 /* Insert symlink contents into path. */
|
|
328 strcat(link_path, path);
|
|
329 strcpy(copy_path, link_path);
|
|
330 path = copy_path;
|
|
331 }
|
446
|
332 #endif /* S_IFLNK || WIN32_NATIVE */
|
|
333 *new_path++ = DIRECTORY_SEP;
|
428
|
334 }
|
|
335
|
|
336 /* Delete trailing slash but don't whomp a lone slash. */
|
446
|
337 if (new_path != ABS_START (resolved_path) && IS_DIRECTORY_SEP (new_path[-1]))
|
428
|
338 new_path--;
|
|
339
|
|
340 /* Make sure it's null terminated. */
|
|
341 *new_path = '\0';
|
446
|
342
|
428
|
343 return resolved_path;
|
|
344 }
|