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
|
446
|
38 /* First char after start of absolute filename. */
|
|
39 #define ABS_START(name) (name + ABS_LENGTH (name))
|
|
40
|
|
41 #if defined (WIN32_NATIVE)
|
|
42 /* Length of start of absolute filename. */
|
771
|
43 # define ABS_LENGTH(name) (mswindows_abs_start (name))
|
|
44 static int mswindows_abs_start (const Intbyte *name);
|
|
45 # define readlink_and_correct_case mswindows_readlink_and_correct_case
|
446
|
46 #else
|
|
47 # ifdef CYGWIN
|
|
48 # define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? \
|
|
49 (IS_DIRECTORY_SEP (name[1]) ? 2 : 1) : 0)
|
771
|
50 # define readlink_and_correct_case cygwin_readlink_and_correct_case
|
446
|
51 # else
|
|
52 # define ABS_LENGTH(name) (IS_DIRECTORY_SEP (*name) ? 1 : 0)
|
771
|
53 # define readlink_and_correct_case qxe_readlink
|
446
|
54 # endif /* CYGWIN */
|
|
55 #endif /* WIN32_NATIVE */
|
|
56
|
|
57 #if defined (WIN32_NATIVE) || defined (CYGWIN)
|
|
58 #include "syswindows.h"
|
771
|
59 /* "Emulate" readlink on mswindows - finds real name (i.e. correct
|
|
60 case) of a file. (#### "readlink" is used extremely misleadingly
|
|
61 here. This is much more like "truename"!) UNC servers and shares
|
|
62 are lower-cased. Directories must be given without trailing
|
|
63 '/'. One day, this could read Win2K's reparse points. */
|
446
|
64 static int
|
771
|
65 mswindows_readlink_and_correct_case (const Intbyte *name, Intbyte *buf,
|
|
66 int size)
|
446
|
67 {
|
|
68 int len = 0;
|
|
69 int err = 0;
|
771
|
70 const Intbyte *lastname;
|
446
|
71 int count = 0;
|
771
|
72 const Intbyte *tmp;
|
|
73 DECLARE_EISTRING (result);
|
446
|
74
|
|
75 assert (*name);
|
|
76
|
|
77 /* Sort of check we have a valid filename. */
|
771
|
78 if (qxestrpbrk (name, "*?|<>\"") || qxestrlen (name) >= PATH_MAX)
|
446
|
79 {
|
|
80 errno = EIO;
|
|
81 return -1;
|
|
82 }
|
|
83
|
|
84 /* Find start of filename */
|
771
|
85 lastname = name + qxestrlen (name);
|
446
|
86 while (lastname > name && !IS_DIRECTORY_SEP (lastname[-1]))
|
|
87 --lastname;
|
|
88
|
|
89 /* Count slashes in unc path */
|
|
90 if (ABS_LENGTH (name) == 2)
|
|
91 for (tmp = name; *tmp; tmp++)
|
|
92 if (IS_DIRECTORY_SEP (*tmp))
|
|
93 count++;
|
|
94
|
|
95 if (count >= 2 && count < 4)
|
|
96 {
|
771
|
97 eicpy_rawz (result, lastname);
|
|
98 eilwr (result);
|
446
|
99 }
|
|
100 else
|
771
|
101 {
|
|
102 WIN32_FIND_DATAW find_data;
|
|
103 Extbyte *nameext;
|
|
104 HANDLE dir_handle;
|
446
|
105
|
771
|
106 C_STRING_TO_TSTR (name, nameext);
|
|
107 dir_handle = qxeFindFirstFile (nameext, &find_data);
|
|
108 if (dir_handle == INVALID_HANDLE_VALUE)
|
446
|
109 {
|
771
|
110 errno = ENOENT;
|
|
111 return -1;
|
446
|
112 }
|
771
|
113 eicpy_ext (result, (Extbyte *) find_data.cFileName, Qmswindows_tstr);
|
|
114 FindClose (dir_handle);
|
|
115 }
|
|
116
|
|
117 if ((len = eilen (result)) < size)
|
|
118 {
|
|
119 DECLARE_EISTRING (eilastname);
|
|
120
|
|
121 eicpy_rawz (eilastname, lastname);
|
|
122 if (eicmp_ei (eilastname, result) == 0)
|
|
123 /* Signal that the name is already OK. */
|
|
124 err = EINVAL;
|
446
|
125 else
|
771
|
126 memcpy (buf, eidata (result), len + 1);
|
446
|
127 }
|
|
128 else
|
771
|
129 err = ENAMETOOLONG;
|
446
|
130
|
|
131 errno = err;
|
|
132 return err ? -1 : len;
|
|
133 }
|
|
134 #endif /* WIN32_NATIVE || CYGWIN */
|
|
135
|
|
136 #ifdef CYGWIN
|
|
137 /* Call readlink and try to find out the correct case for the file. */
|
|
138 static int
|
771
|
139 cygwin_readlink_and_correct_case (const Intbyte *name, Intbyte *buf,
|
|
140 int size)
|
446
|
141 {
|
771
|
142 int n = qxe_readlink (name, buf, size);
|
462
|
143 if (n < 0 && errno == EINVAL)
|
446
|
144 {
|
|
145 /* The file may exist, but isn't a symlink. Try to find the
|
|
146 right name. */
|
771
|
147 Intbyte *tmp =
|
|
148 (Intbyte *) alloca (cygwin_posix_to_win32_path_list_buf_size
|
|
149 ((char *) name));
|
|
150 cygwin_posix_to_win32_path_list ((char *) name, (char *) tmp);
|
|
151 n = mswindows_readlink_and_correct_case (tmp, buf, size);
|
446
|
152 }
|
|
153 return n;
|
|
154 }
|
|
155 #endif /* CYGWIN */
|
|
156
|
|
157 #ifdef WIN32_NATIVE
|
|
158 #ifndef ELOOP
|
|
159 #define ELOOP 10062 /* = WSAELOOP in winsock.h */
|
|
160 #endif
|
|
161 /* Length of start of absolute filename. */
|
|
162 static int
|
771
|
163 mswindows_abs_start (const Intbyte *name)
|
446
|
164 {
|
|
165 if (isalpha (*name) && IS_DEVICE_SEP (name[1])
|
|
166 && IS_DIRECTORY_SEP (name[2]))
|
|
167 return 3;
|
|
168 else if (IS_DIRECTORY_SEP (*name))
|
|
169 return IS_DIRECTORY_SEP (name[1]) ? 2 : 1;
|
|
170 else
|
|
171 return 0;
|
|
172 }
|
|
173 #endif /* WIN32_NATIVE */
|
|
174
|
771
|
175 /* Mule Note: This function works with and returns
|
|
176 internally-formatted strings. */
|
440
|
177
|
771
|
178 Intbyte *
|
|
179 qxe_realpath (const Intbyte *path, Intbyte *resolved_path)
|
428
|
180 {
|
771
|
181 Intbyte copy_path[PATH_MAX];
|
|
182 Intbyte *new_path = resolved_path;
|
|
183 Intbyte *max_path;
|
|
184 #if defined (HAVE_READLINK) || defined (WIN32_NATIVE)
|
428
|
185 int readlinks = 0;
|
771
|
186 Intbyte link_path[PATH_MAX];
|
428
|
187 int n;
|
446
|
188 int abslen = ABS_LENGTH (path);
|
428
|
189 #endif
|
|
190
|
|
191 /* Make a copy of the source path since we may need to modify it. */
|
771
|
192 qxestrcpy (copy_path, path);
|
428
|
193 path = copy_path;
|
|
194 max_path = copy_path + PATH_MAX - 2;
|
446
|
195
|
442
|
196 #ifdef WIN32_NATIVE
|
446
|
197 /* Check for c:/... or //server/... */
|
|
198 if (abslen == 2 || abslen == 3)
|
428
|
199 {
|
771
|
200 qxestrncpy (new_path, path, abslen);
|
462
|
201 /* Make sure drive letter is lowercased. */
|
|
202 if (abslen == 3)
|
|
203 *new_path = tolower (*new_path);
|
446
|
204 new_path += abslen;
|
|
205 path += abslen;
|
428
|
206 }
|
446
|
207 /* No drive letter, but a beginning slash? Prepend drive letter. */
|
|
208 else if (abslen == 1)
|
428
|
209 {
|
771
|
210 get_initial_directory (new_path, PATH_MAX - 1);
|
428
|
211 new_path += 3;
|
|
212 path++;
|
|
213 }
|
446
|
214 /* Just a path name, prepend the current directory */
|
428
|
215 else
|
|
216 {
|
771
|
217 get_initial_directory (new_path, PATH_MAX - 1);
|
|
218 new_path += qxestrlen (new_path);
|
446
|
219 if (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
220 *new_path++ = DIRECTORY_SEP;
|
428
|
221 }
|
|
222 #else
|
771
|
223 /* If it's a relative pathname use get_initial_directory for starters. */
|
446
|
224 if (abslen == 0)
|
428
|
225 {
|
771
|
226 get_initial_directory (new_path, PATH_MAX - 1);
|
|
227 new_path += qxestrlen (new_path);
|
446
|
228 if (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
229 *new_path++ = DIRECTORY_SEP;
|
428
|
230 }
|
|
231 else
|
|
232 {
|
446
|
233 /* Copy first directory sep. May have two on cygwin. */
|
771
|
234 qxestrncpy (new_path, path, abslen);
|
446
|
235 new_path += abslen;
|
|
236 path += abslen;
|
428
|
237 }
|
|
238 #endif
|
|
239 /* Expand each slash-separated pathname component. */
|
|
240 while (*path != '\0')
|
|
241 {
|
|
242 /* Ignore stray "/". */
|
446
|
243 if (IS_DIRECTORY_SEP (*path))
|
428
|
244 {
|
|
245 path++;
|
|
246 continue;
|
|
247 }
|
|
248
|
|
249 if (*path == '.')
|
|
250 {
|
|
251 /* Ignore ".". */
|
446
|
252 if (path[1] == '\0' || IS_DIRECTORY_SEP (path[1]))
|
428
|
253 {
|
|
254 path++;
|
|
255 continue;
|
|
256 }
|
|
257
|
442
|
258 /* Handle ".." */
|
|
259 if (path[1] == '.' &&
|
446
|
260 (path[2] == '\0' || IS_DIRECTORY_SEP (path[2])))
|
428
|
261 {
|
442
|
262 path += 2;
|
428
|
263
|
442
|
264 /* Ignore ".." at root. */
|
446
|
265 if (new_path == ABS_START (resolved_path))
|
442
|
266 continue;
|
428
|
267
|
442
|
268 /* Handle ".." by backing up. */
|
446
|
269 --new_path;
|
|
270 while (!IS_DIRECTORY_SEP (new_path[-1]))
|
|
271 --new_path;
|
442
|
272 continue;
|
428
|
273 }
|
|
274 }
|
|
275
|
|
276 /* Safely copy the next pathname component. */
|
446
|
277 while (*path != '\0' && !IS_DIRECTORY_SEP (*path))
|
428
|
278 {
|
|
279 if (path > max_path)
|
|
280 {
|
|
281 errno = ENAMETOOLONG;
|
|
282 return NULL;
|
|
283 }
|
|
284 *new_path++ = *path++;
|
|
285 }
|
|
286
|
771
|
287 #if defined (HAVE_READLINK) || defined (WIN32_NATIVE)
|
|
288 /* See if latest pathname component is a symlink or needs case
|
|
289 correction. */
|
428
|
290 *new_path = '\0';
|
771
|
291 n = readlink_and_correct_case (resolved_path, link_path, PATH_MAX - 1);
|
428
|
292
|
|
293 if (n < 0)
|
|
294 {
|
771
|
295 /* EINVAL means the file exists but isn't a symlink or doesn't
|
|
296 need case correction. */
|
462
|
297 #ifdef CYGWIN
|
|
298 if (errno != EINVAL && errno != ENOENT)
|
|
299 #else
|
|
300 if (errno != EINVAL)
|
|
301 #endif
|
428
|
302 return NULL;
|
|
303 }
|
|
304 else
|
|
305 {
|
|
306 /* Protect against infinite loops. */
|
|
307 if (readlinks++ > MAX_READLINKS)
|
|
308 {
|
|
309 errno = ELOOP;
|
|
310 return NULL;
|
|
311 }
|
|
312
|
|
313 /* Note: readlink doesn't add the null byte. */
|
|
314 link_path[n] = '\0';
|
446
|
315
|
|
316 if (ABS_LENGTH (link_path) > 0)
|
428
|
317 /* Start over for an absolute symlink. */
|
446
|
318 new_path = resolved_path + ABS_LENGTH (link_path) - 1;
|
428
|
319 else
|
|
320 /* Otherwise back up over this component. */
|
446
|
321 for (--new_path; !IS_DIRECTORY_SEP (*new_path); --new_path)
|
|
322 assert (new_path > resolved_path);
|
428
|
323
|
|
324 /* Safe sex check. */
|
771
|
325 if (qxestrlen (path) + n >= PATH_MAX)
|
428
|
326 {
|
|
327 errno = ENAMETOOLONG;
|
|
328 return NULL;
|
|
329 }
|
|
330
|
|
331 /* Insert symlink contents into path. */
|
771
|
332 qxestrcat (link_path, path);
|
|
333 qxestrcpy (copy_path, link_path);
|
428
|
334 path = copy_path;
|
|
335 }
|
771
|
336 #endif /* HAVE_READLINK || WIN32_NATIVE */
|
446
|
337 *new_path++ = DIRECTORY_SEP;
|
428
|
338 }
|
|
339
|
|
340 /* Delete trailing slash but don't whomp a lone slash. */
|
771
|
341 if (new_path != ABS_START (resolved_path) &&
|
|
342 IS_DIRECTORY_SEP (new_path[-1]))
|
428
|
343 new_path--;
|
|
344
|
|
345 /* Make sure it's null terminated. */
|
|
346 *new_path = '\0';
|
446
|
347
|
428
|
348 return resolved_path;
|
|
349 }
|