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