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