428
|
1 /*
|
|
2 * realpath.c -- canonicalize pathname by removing symlinks
|
|
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
|
2526
|
4 * Copyright (C) 2001, 2002, 2004 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
|
2526
|
34 #include "profile.h"
|
|
35
|
558
|
36 #include "sysfile.h"
|
1116
|
37 #include "sysdir.h"
|
428
|
38
|
771
|
39 #define MAX_READLINKS 32
|
|
40
|
1116
|
41 #ifdef WIN32_ANY
|
446
|
42 #include "syswindows.h"
|
|
43 #ifndef ELOOP
|
|
44 #define ELOOP 10062 /* = WSAELOOP in winsock.h */
|
|
45 #endif
|
1116
|
46 #endif
|
|
47
|
2526
|
48 Lisp_Object QSin_qxe_realpath;
|
|
49
|
446
|
50 /* Length of start of absolute filename. */
|
|
51 static int
|
1116
|
52 abs_start (const Ibyte *name)
|
446
|
53 {
|
1116
|
54 #ifdef WIN32_ANY
|
446
|
55 if (isalpha (*name) && IS_DEVICE_SEP (name[1])
|
|
56 && IS_DIRECTORY_SEP (name[2]))
|
|
57 return 3;
|
|
58 else if (IS_DIRECTORY_SEP (*name))
|
|
59 return IS_DIRECTORY_SEP (name[1]) ? 2 : 1;
|
|
60 else
|
|
61 return 0;
|
1116
|
62 #else /* not WIN32_ANY */
|
|
63 return IS_DIRECTORY_SEP (*name) ? 1 : 0;
|
|
64 #endif
|
446
|
65 }
|
1116
|
66
|
2526
|
67 /* Find real name of a file by resolving symbolic links and/or shortcuts
|
|
68 under Windows (.LNK links), if such support is enabled.
|
|
69
|
|
70 If no link found, and LINKS_ONLY is false, look up the correct case in
|
|
71 the file system of the last component.
|
1116
|
72
|
|
73 Under Windows, UNC servers and shares are lower-cased. Directories must
|
|
74 be given without trailing '/'. One day, this could read Win2K's reparse
|
2526
|
75 points.
|
|
76
|
|
77 Returns length of characters copied info BUF.
|
|
78 DOES NOT ZERO TERMINATE!!!!!
|
|
79 */
|
1116
|
80
|
|
81 static int
|
2526
|
82 readlink_or_correct_case (const Ibyte *name, Ibyte *buf, Bytecount size,
|
3042
|
83 #ifndef WIN32_ANY
|
3044
|
84 Boolint UNUSED (links_only)
|
3042
|
85 #else
|
3044
|
86 Boolint links_only
|
3042
|
87 #endif
|
3044
|
88 )
|
1116
|
89 {
|
|
90 #ifndef WIN32_ANY
|
2526
|
91 return qxe_readlink (name, buf, (size_t) size);
|
1116
|
92 #else
|
|
93 # ifdef CYGWIN
|
|
94 Ibyte *tmp;
|
2526
|
95 int n = qxe_readlink (name, buf, (size_t) size);
|
1116
|
96 if (n >= 0 || errno != EINVAL)
|
|
97 return n;
|
|
98
|
|
99 /* The file may exist, but isn't a symlink. Try to find the
|
|
100 right name. */
|
2367
|
101 tmp =
|
|
102 alloca_ibytes (cygwin_posix_to_win32_path_list_buf_size ((char *) name));
|
1116
|
103 cygwin_posix_to_win32_path_list ((char *) name, (char *) tmp);
|
|
104 name = tmp;
|
2526
|
105 # else
|
|
106 if (mswindows_shortcuts_are_symlinks)
|
|
107 {
|
|
108 Ibyte *tmp = mswindows_read_link (name);
|
|
109
|
|
110 if (tmp != NULL)
|
|
111 {
|
|
112 /* Fucking fixed buffers. */
|
|
113 Bytecount len = qxestrlen (tmp);
|
|
114 if (len > size)
|
|
115 {
|
|
116 errno = ENAMETOOLONG;
|
|
117 return -1;
|
|
118 }
|
|
119 memcpy (buf, tmp, len);
|
|
120 xfree (tmp, Ibyte *);
|
|
121 return len;
|
|
122 }
|
|
123 }
|
1116
|
124 # endif
|
|
125
|
2526
|
126 if (links_only)
|
|
127 {
|
|
128 errno = EINVAL;
|
|
129 return -1;
|
|
130 }
|
|
131
|
1116
|
132 {
|
|
133 int len = 0;
|
|
134 int err = 0;
|
|
135 const Ibyte *lastname;
|
|
136 int count = 0;
|
1204
|
137 const Ibyte *nn;
|
1116
|
138 DECLARE_EISTRING (result);
|
|
139
|
|
140 assert (*name);
|
|
141
|
|
142 /* Sort of check we have a valid filename. */
|
|
143 if (qxestrpbrk (name, "*?|<>\""))
|
|
144 {
|
|
145 errno = ENOENT;
|
|
146 return -1;
|
|
147 }
|
2421
|
148 else if (qxestrlen (name) >= PATH_MAX_INTERNAL)
|
1116
|
149 {
|
|
150 errno = ENAMETOOLONG;
|
|
151 return -1;
|
|
152 }
|
|
153
|
|
154 /* Find start of filename */
|
|
155 lastname = name + qxestrlen (name);
|
|
156 while (lastname > name && !IS_DIRECTORY_SEP (lastname[-1]))
|
|
157 --lastname;
|
|
158
|
|
159 /* Count slashes in unc path */
|
|
160 if (abs_start (name) == 2)
|
1204
|
161 for (nn = name; *nn; nn++)
|
|
162 if (IS_DIRECTORY_SEP (*nn))
|
1116
|
163 count++;
|
|
164
|
|
165 if (count >= 2 && count < 4)
|
|
166 {
|
|
167 eicpy_rawz (result, lastname);
|
|
168 eilwr (result);
|
|
169 }
|
|
170 else
|
|
171 {
|
|
172 WIN32_FIND_DATAW find_data;
|
|
173 Extbyte *nameext;
|
|
174 HANDLE dir_handle;
|
|
175
|
|
176 C_STRING_TO_TSTR (name, nameext);
|
|
177 dir_handle = qxeFindFirstFile (nameext, &find_data);
|
|
178 if (dir_handle == INVALID_HANDLE_VALUE)
|
|
179 {
|
|
180 errno = ENOENT;
|
|
181 return -1;
|
|
182 }
|
|
183 eicpy_ext (result, (Extbyte *) find_data.cFileName, Qmswindows_tstr);
|
|
184 FindClose (dir_handle);
|
|
185 }
|
|
186
|
2526
|
187 if ((len = eilen (result)) <= size)
|
1116
|
188 {
|
|
189 DECLARE_EISTRING (eilastname);
|
|
190
|
|
191 eicpy_rawz (eilastname, lastname);
|
|
192 if (eicmp_ei (eilastname, result) == 0)
|
2526
|
193 /* Signal that the name is already OK. */
|
|
194 err = EINVAL;
|
1116
|
195 else
|
2526
|
196 memcpy (buf, eidata (result), len);
|
1116
|
197 }
|
|
198 else
|
|
199 err = ENAMETOOLONG;
|
|
200
|
|
201 errno = err;
|
|
202 return err ? -1 : len;
|
|
203 }
|
|
204 #endif /* WIN32_ANY */
|
|
205 }
|
446
|
206
|
771
|
207 /* Mule Note: This function works with and returns
|
2526
|
208 internally-formatted strings.
|
|
209
|
|
210 if LINKS_ONLY is true, don't do case canonicalization under
|
|
211 Windows. */
|
440
|
212
|
867
|
213 Ibyte *
|
2526
|
214 qxe_realpath (const Ibyte *path, Ibyte *resolved_path, Boolint links_only)
|
428
|
215 {
|
2421
|
216 Ibyte copy_path[PATH_MAX_INTERNAL];
|
867
|
217 Ibyte *new_path = resolved_path;
|
|
218 Ibyte *max_path;
|
2526
|
219 Ibyte *retval = NULL;
|
1116
|
220 #if defined (HAVE_READLINK) || defined (WIN32_ANY)
|
428
|
221 int readlinks = 0;
|
2421
|
222 Ibyte link_path[PATH_MAX_INTERNAL];
|
428
|
223 int n;
|
1116
|
224 int abslen = abs_start (path);
|
428
|
225 #endif
|
|
226
|
2526
|
227 PROFILE_DECLARE ();
|
|
228
|
|
229 PROFILE_RECORD_ENTERING_SECTION (QSin_qxe_realpath);
|
|
230
|
1760
|
231 restart:
|
|
232
|
428
|
233 /* Make a copy of the source path since we may need to modify it. */
|
771
|
234 qxestrcpy (copy_path, path);
|
428
|
235 path = copy_path;
|
2421
|
236 max_path = copy_path + PATH_MAX_INTERNAL - 2;
|
446
|
237
|
819
|
238 if (0)
|
|
239 ;
|
1116
|
240 #ifdef WIN32_ANY
|
446
|
241 /* Check for c:/... or //server/... */
|
988
|
242 else if (abslen == 3 || abslen == 2)
|
428
|
243 {
|
462
|
244 /* Make sure drive letter is lowercased. */
|
1116
|
245 if (abslen == 3)
|
|
246 {
|
|
247 *new_path = tolower (*path);
|
|
248 new_path++;
|
|
249 path++;
|
|
250 abslen--;
|
|
251 }
|
988
|
252 /* Coerce directory chars. */
|
1116
|
253 while (abslen-- > 0)
|
|
254 {
|
|
255 if (IS_DIRECTORY_SEP (*path))
|
|
256 *new_path++ = DIRECTORY_SEP;
|
|
257 else
|
|
258 *new_path++ = *path;
|
|
259 path++;
|
|
260 }
|
428
|
261 }
|
819
|
262 #endif
|
|
263 #ifdef WIN32_NATIVE
|
446
|
264 /* No drive letter, but a beginning slash? Prepend drive letter. */
|
|
265 else if (abslen == 1)
|
428
|
266 {
|
2421
|
267 get_initial_directory (new_path, PATH_MAX_INTERNAL - 1);
|
428
|
268 new_path += 3;
|
|
269 path++;
|
|
270 }
|
446
|
271 /* Just a path name, prepend the current directory */
|
1116
|
272 else
|
428
|
273 {
|
2421
|
274 get_initial_directory (new_path, PATH_MAX_INTERNAL - 1);
|
771
|
275 new_path += qxestrlen (new_path);
|
446
|
276 if (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
277 *new_path++ = DIRECTORY_SEP;
|
428
|
278 }
|
|
279 #else
|
771
|
280 /* If it's a relative pathname use get_initial_directory for starters. */
|
819
|
281 else if (abslen == 0)
|
428
|
282 {
|
2421
|
283 get_initial_directory (new_path, PATH_MAX_INTERNAL - 1);
|
771
|
284 new_path += qxestrlen (new_path);
|
446
|
285 if (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
286 *new_path++ = DIRECTORY_SEP;
|
428
|
287 }
|
|
288 else
|
|
289 {
|
446
|
290 /* Copy first directory sep. May have two on cygwin. */
|
771
|
291 qxestrncpy (new_path, path, abslen);
|
446
|
292 new_path += abslen;
|
|
293 path += abslen;
|
428
|
294 }
|
|
295 #endif
|
|
296 /* Expand each slash-separated pathname component. */
|
|
297 while (*path != '\0')
|
|
298 {
|
|
299 /* Ignore stray "/". */
|
446
|
300 if (IS_DIRECTORY_SEP (*path))
|
428
|
301 {
|
|
302 path++;
|
|
303 continue;
|
|
304 }
|
|
305
|
|
306 if (*path == '.')
|
|
307 {
|
|
308 /* Ignore ".". */
|
446
|
309 if (path[1] == '\0' || IS_DIRECTORY_SEP (path[1]))
|
428
|
310 {
|
|
311 path++;
|
|
312 continue;
|
|
313 }
|
|
314
|
442
|
315 /* Handle ".." */
|
|
316 if (path[1] == '.' &&
|
446
|
317 (path[2] == '\0' || IS_DIRECTORY_SEP (path[2])))
|
428
|
318 {
|
442
|
319 path += 2;
|
428
|
320
|
442
|
321 /* Ignore ".." at root. */
|
1116
|
322 if (new_path == resolved_path + abs_start (resolved_path))
|
442
|
323 continue;
|
428
|
324
|
442
|
325 /* Handle ".." by backing up. */
|
446
|
326 --new_path;
|
|
327 while (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
328 --new_path;
|
442
|
329 continue;
|
428
|
330 }
|
|
331 }
|
|
332
|
|
333 /* Safely copy the next pathname component. */
|
446
|
334 while (*path != '\0' && !IS_DIRECTORY_SEP (*path))
|
428
|
335 {
|
|
336 if (path > max_path)
|
|
337 {
|
|
338 errno = ENAMETOOLONG;
|
2526
|
339 goto done;
|
428
|
340 }
|
|
341 *new_path++ = *path++;
|
|
342 }
|
|
343
|
1116
|
344 #if defined (HAVE_READLINK) || defined (WIN32_ANY)
|
771
|
345 /* See if latest pathname component is a symlink or needs case
|
|
346 correction. */
|
428
|
347 *new_path = '\0';
|
2526
|
348 n = readlink_or_correct_case (resolved_path, link_path,
|
|
349 PATH_MAX_INTERNAL - 1, links_only);
|
428
|
350
|
|
351 if (n < 0)
|
|
352 {
|
771
|
353 /* EINVAL means the file exists but isn't a symlink or doesn't
|
|
354 need case correction. */
|
1116
|
355 #ifdef WIN32_ANY
|
462
|
356 if (errno != EINVAL && errno != ENOENT)
|
|
357 #else
|
|
358 if (errno != EINVAL)
|
|
359 #endif
|
2526
|
360 goto done;
|
428
|
361 }
|
|
362 else
|
|
363 {
|
|
364 /* Protect against infinite loops. */
|
|
365 if (readlinks++ > MAX_READLINKS)
|
|
366 {
|
|
367 errno = ELOOP;
|
2526
|
368 goto done;
|
428
|
369 }
|
|
370
|
|
371 /* Note: readlink doesn't add the null byte. */
|
|
372 link_path[n] = '\0';
|
446
|
373
|
1760
|
374 abslen = abs_start (link_path);
|
|
375 if (abslen > 0)
|
|
376 {
|
|
377 /* Start over for an absolute symlink. */
|
|
378 new_path = resolved_path;
|
|
379 qxestrcat (link_path, path);
|
|
380 path = link_path;
|
|
381 goto restart;
|
|
382 }
|
|
383
|
|
384 /* Otherwise back up over this component. */
|
|
385 for (--new_path; !IS_DIRECTORY_SEP (*new_path); --new_path)
|
|
386 assert (new_path > resolved_path);
|
428
|
387
|
|
388 /* Safe sex check. */
|
2421
|
389 if (qxestrlen (path) + n >= PATH_MAX_INTERNAL)
|
428
|
390 {
|
|
391 errno = ENAMETOOLONG;
|
2526
|
392 goto done;
|
428
|
393 }
|
|
394
|
|
395 /* Insert symlink contents into path. */
|
771
|
396 qxestrcat (link_path, path);
|
|
397 qxestrcpy (copy_path, link_path);
|
428
|
398 path = copy_path;
|
|
399 }
|
1116
|
400 #endif /* HAVE_READLINK || WIN32_ANY */
|
446
|
401 *new_path++ = DIRECTORY_SEP;
|
428
|
402 }
|
|
403
|
|
404 /* Delete trailing slash but don't whomp a lone slash. */
|
1116
|
405 if (new_path != resolved_path + abs_start (resolved_path) &&
|
771
|
406 IS_DIRECTORY_SEP (new_path[-1]))
|
428
|
407 new_path--;
|
|
408
|
|
409 /* Make sure it's null terminated. */
|
|
410 *new_path = '\0';
|
446
|
411
|
2526
|
412 retval = resolved_path;
|
|
413 done:
|
|
414 PROFILE_RECORD_EXITING_SECTION (QSin_qxe_realpath);
|
|
415 return retval;
|
428
|
416 }
|
2526
|
417
|
|
418 void
|
|
419 vars_of_realpath (void)
|
|
420 {
|
|
421 QSin_qxe_realpath =
|
|
422 build_msg_string ("(in qxe_realpath)");
|
|
423 staticpro (&QSin_qxe_realpath);
|
|
424 }
|