1 /* Copyright (C) 1992,1993,1994,1995,1996,1997,1999,2001,2004,2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 #include <hurd/lookup.h>
25 /* Translate the error from dir_lookup into the error the user sees. */
27 lookup_error (error_t error)
33 /* These indicate that the server does not understand dir_lookup
34 at all. If it were a directory, it would, by definition. */
42 __hurd_file_name_lookup (error_t (*use_init_port)
43 (int which, error_t (*operate) (file_t)),
44 file_t (*get_dtable_port) (int fd),
46 (file_t dir, char *name, int flags, mode_t mode,
47 retry_type *do_retry, string_t retry_name,
49 const char *file_name, int flags, mode_t mode,
53 enum retry_type doretry;
54 char retryname[1024]; /* XXX string_t LOSES! */
57 error_t lookup_op (mach_port_t startdir)
59 return lookup_error ((*lookup) (startdir, file_name, flags, mode,
60 &doretry, retryname, result));
64 lookup = __dir_lookup;
66 if (file_name[0] == '\0')
69 startport = (file_name[0] == '/') ? INIT_PORT_CRDIR : INIT_PORT_CWDIR;
70 while (file_name[0] == '/')
73 if (flags & O_NOFOLLOW) /* See lookup-retry.c about O_NOFOLLOW. */
76 if (flags & O_DIRECTORY)
78 /* The caller wants to require that the file we look up is a directory.
79 We can do this without an extra RPC by appending a trailing slash
80 to the file name we look up. */
81 size_t len = strlen (file_name);
84 else if (file_name[len - 1] != '/')
86 char *n = alloca (len + 2);
87 memcpy (n, file_name, len);
94 err = (*use_init_port) (startport, &lookup_op);
96 err = __hurd_file_name_lookup_retry (use_init_port, get_dtable_port,
97 lookup, doretry, retryname,
102 weak_alias (__hurd_file_name_lookup, hurd_file_name_lookup)
105 __hurd_file_name_split (error_t (*use_init_port)
106 (int which, error_t (*operate) (file_t)),
107 file_t (*get_dtable_port) (int fd),
109 (file_t dir, char *name, int flags, mode_t mode,
110 retry_type *do_retry, string_t retry_name,
111 mach_port_t *result),
112 const char *file_name,
113 file_t *dir, char **name)
115 error_t addref (file_t crdir)
118 return __mach_port_mod_refs (__mach_task_self (),
119 crdir, MACH_PORT_RIGHT_SEND, +1);
122 const char *lastslash = strrchr (file_name, '/');
124 if (lastslash != NULL)
126 if (lastslash == file_name)
128 /* "/foobar" => crdir + "foobar". */
129 *name = (char *) file_name + 1;
130 return (*use_init_port) (INIT_PORT_CRDIR, &addref);
134 /* "/dir1/dir2/.../file". */
135 char dirname[lastslash - file_name + 1];
136 memcpy (dirname, file_name, lastslash - file_name);
137 dirname[lastslash - file_name] = '\0';
138 *name = (char *) lastslash + 1;
140 __hurd_file_name_lookup (use_init_port, get_dtable_port, lookup,
144 else if (file_name[0] == '\0')
148 /* "foobar" => cwdir + "foobar". */
149 *name = (char *) file_name;
150 return (*use_init_port) (INIT_PORT_CWDIR, &addref);
153 weak_alias (__hurd_file_name_split, hurd_file_name_split)
155 /* This is the same as hurd_file_name_split, except that it ignores
156 trailing slashes (so *NAME is never ""). */
158 __hurd_directory_name_split (error_t (*use_init_port)
159 (int which, error_t (*operate) (file_t)),
160 file_t (*get_dtable_port) (int fd),
162 (file_t dir, char *name, int flags, mode_t mode,
163 retry_type *do_retry, string_t retry_name,
164 mach_port_t *result),
165 const char *file_name,
166 file_t *dir, char **name)
168 error_t addref (file_t crdir)
171 return __mach_port_mod_refs (__mach_task_self (),
172 crdir, MACH_PORT_RIGHT_SEND, +1);
175 const char *lastslash = strrchr (file_name, '/');
177 if (lastslash != NULL && lastslash[1] == '\0')
179 /* Trailing slash doesn't count. Look back further. */
181 /* Back up over all trailing slashes. */
182 while (lastslash > file_name && *lastslash == '/')
185 /* Find the last one earlier in the string, before the trailing ones. */
186 lastslash = __memrchr (file_name, '/', lastslash - file_name);
189 if (lastslash != NULL)
191 if (lastslash == file_name)
193 /* "/foobar" => crdir + "foobar". */
194 *name = (char *) file_name + 1;
195 return (*use_init_port) (INIT_PORT_CRDIR, &addref);
199 /* "/dir1/dir2/.../file". */
200 char dirname[lastslash - file_name + 1];
201 memcpy (dirname, file_name, lastslash - file_name);
202 dirname[lastslash - file_name] = '\0';
203 *name = (char *) lastslash + 1;
205 __hurd_file_name_lookup (use_init_port, get_dtable_port, lookup,
209 else if (file_name[0] == '\0')
213 /* "foobar" => cwdir + "foobar". */
214 *name = (char *) file_name;
215 return (*use_init_port) (INIT_PORT_CWDIR, &addref);
218 weak_alias (__hurd_directory_name_split, hurd_directory_name_split)
222 __file_name_lookup (const char *file_name, int flags, mode_t mode)
227 err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0,
228 file_name, flags, mode & ~_hurd_umask,
231 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
233 weak_alias (__file_name_lookup, file_name_lookup)
237 __file_name_split (const char *file_name, char **name)
242 err = __hurd_file_name_split (&_hurd_ports_use, &__getdport, 0,
243 file_name, &result, name);
245 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
247 weak_alias (__file_name_split, file_name_split)
250 __directory_name_split (const char *directory_name, char **name)
255 err = __hurd_directory_name_split (&_hurd_ports_use, &__getdport, 0,
256 directory_name, &result, name);
258 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
260 weak_alias (__directory_name_split, directory_name_split)
264 __file_name_lookup_under (file_t startdir,
265 const char *file_name, int flags, mode_t mode)
270 error_t use_init_port (int which, error_t (*operate) (mach_port_t))
272 return (which == INIT_PORT_CWDIR ? (*operate) (startdir) :
273 _hurd_ports_use (which, operate));
276 err = __hurd_file_name_lookup (&use_init_port, &__getdport, 0,
277 file_name, flags, mode & ~_hurd_umask,
280 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
282 weak_alias (__file_name_lookup_under, file_name_lookup_under)