1 /* Copyright (C) 1992,93,94,95,96,97,99,2001,2004
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <hurd/lookup.h>
26 /* Translate the error from dir_lookup into the error the user sees. */
28 lookup_error (error_t error
)
34 /* These indicate that the server does not understand dir_lookup
35 at all. If it were a directory, it would, by definition. */
43 __hurd_file_name_lookup (error_t (*use_init_port
)
44 (int which
, error_t (*operate
) (file_t
)),
45 file_t (*get_dtable_port
) (int fd
),
47 (file_t dir
, char *name
, int flags
, mode_t mode
,
48 retry_type
*do_retry
, string_t retry_name
,
50 const char *file_name
, int flags
, mode_t mode
,
54 enum retry_type doretry
;
55 char retryname
[1024]; /* XXX string_t LOSES! */
58 error_t
lookup_op (mach_port_t startdir
)
60 return lookup_error ((*lookup
) (startdir
, file_name
, flags
, mode
,
61 &doretry
, retryname
, result
));
65 lookup
= __dir_lookup
;
67 if (file_name
[0] == '\0')
70 startport
= (file_name
[0] == '/') ? INIT_PORT_CRDIR
: INIT_PORT_CWDIR
;
71 while (file_name
[0] == '/')
74 if (flags
& O_NOFOLLOW
) /* See lookup-retry.c about O_NOFOLLOW. */
77 if (flags
& O_DIRECTORY
)
79 /* The caller wants to require that the file we look up is a directory.
80 We can do this without an extra RPC by appending a trailing slash
81 to the file name we look up. */
82 size_t len
= strlen (file_name
);
85 else if (file_name
[len
- 1] != '/')
87 char *n
= alloca (len
+ 2);
88 memcpy (n
, file_name
, len
);
95 err
= (*use_init_port
) (startport
, &lookup_op
);
97 err
= __hurd_file_name_lookup_retry (use_init_port
, get_dtable_port
,
98 lookup
, doretry
, retryname
,
103 weak_alias (__hurd_file_name_lookup
, hurd_file_name_lookup
)
106 __hurd_file_name_split (error_t (*use_init_port
)
107 (int which
, error_t (*operate
) (file_t
)),
108 file_t (*get_dtable_port
) (int fd
),
110 (file_t dir
, char *name
, int flags
, mode_t mode
,
111 retry_type
*do_retry
, string_t retry_name
,
112 mach_port_t
*result
),
113 const char *file_name
,
114 file_t
*dir
, char **name
)
116 error_t
addref (file_t crdir
)
119 return __mach_port_mod_refs (__mach_task_self (),
120 crdir
, MACH_PORT_RIGHT_SEND
, +1);
123 const char *lastslash
= strrchr (file_name
, '/');
125 if (lastslash
!= NULL
)
127 if (lastslash
== file_name
)
129 /* "/foobar" => crdir + "foobar". */
130 *name
= (char *) file_name
+ 1;
131 return (*use_init_port
) (INIT_PORT_CRDIR
, &addref
);
135 /* "/dir1/dir2/.../file". */
136 char dirname
[lastslash
- file_name
+ 1];
137 memcpy (dirname
, file_name
, lastslash
- file_name
);
138 dirname
[lastslash
- file_name
] = '\0';
139 *name
= (char *) lastslash
+ 1;
141 __hurd_file_name_lookup (use_init_port
, get_dtable_port
, lookup
,
145 else if (file_name
[0] == '\0')
149 /* "foobar" => cwdir + "foobar". */
150 *name
= (char *) file_name
;
151 return (*use_init_port
) (INIT_PORT_CWDIR
, &addref
);
154 weak_alias (__hurd_file_name_split
, hurd_file_name_split
)
156 /* This is the same as hurd_file_name_split, except that it ignores
157 trailing slashes (so *NAME is never ""). */
159 __hurd_directory_name_split (error_t (*use_init_port
)
160 (int which
, error_t (*operate
) (file_t
)),
161 file_t (*get_dtable_port
) (int fd
),
163 (file_t dir
, char *name
, int flags
, mode_t mode
,
164 retry_type
*do_retry
, string_t retry_name
,
165 mach_port_t
*result
),
166 const char *file_name
,
167 file_t
*dir
, char **name
)
169 error_t
addref (file_t crdir
)
172 return __mach_port_mod_refs (__mach_task_self (),
173 crdir
, MACH_PORT_RIGHT_SEND
, +1);
176 const char *lastslash
= strrchr (file_name
, '/');
178 if (lastslash
!= NULL
&& lastslash
[1] == '\0')
180 /* Trailing slash doesn't count. Look back further. */
182 /* Back up over all trailing slashes. */
183 while (lastslash
> file_name
&& *lastslash
== '/')
186 /* Find the last one earlier in the string, before the trailing ones. */
187 #if __GLIBC__ > 2 || __GLIBC_MINOR__ >= 2
188 lastslash
= __memrchr (file_name
, '/', lastslash
- file_name
);
190 /* Keep backing up, looking for a slash. */
192 if (lastslash
== file_name
)
194 /* Hit the start with no slash. */
198 while (*lastslash
-- != '/');
202 if (lastslash
!= NULL
)
204 if (lastslash
== file_name
)
206 /* "/foobar" => crdir + "foobar". */
207 *name
= (char *) file_name
+ 1;
208 return (*use_init_port
) (INIT_PORT_CRDIR
, &addref
);
212 /* "/dir1/dir2/.../file". */
213 char dirname
[lastslash
- file_name
+ 1];
214 memcpy (dirname
, file_name
, lastslash
- file_name
);
215 dirname
[lastslash
- file_name
] = '\0';
216 *name
= (char *) lastslash
+ 1;
218 __hurd_file_name_lookup (use_init_port
, get_dtable_port
, lookup
,
222 else if (file_name
[0] == '\0')
226 /* "foobar" => cwdir + "foobar". */
227 *name
= (char *) file_name
;
228 return (*use_init_port
) (INIT_PORT_CWDIR
, &addref
);
231 weak_alias (__hurd_directory_name_split
, hurd_directory_name_split
)
235 __file_name_lookup (const char *file_name
, int flags
, mode_t mode
)
240 err
= __hurd_file_name_lookup (&_hurd_ports_use
, &__getdport
, 0,
241 file_name
, flags
, mode
& ~_hurd_umask
,
244 return err
? (__hurd_fail (err
), MACH_PORT_NULL
) : result
;
246 weak_alias (__file_name_lookup
, file_name_lookup
)
250 __file_name_split (const char *file_name
, char **name
)
255 err
= __hurd_file_name_split (&_hurd_ports_use
, &__getdport
, 0,
256 file_name
, &result
, name
);
258 return err
? (__hurd_fail (err
), MACH_PORT_NULL
) : result
;
260 weak_alias (__file_name_split
, file_name_split
)
263 __directory_name_split (const char *directory_name
, char **name
)
268 err
= __hurd_directory_name_split (&_hurd_ports_use
, &__getdport
, 0,
269 directory_name
, &result
, name
);
271 return err
? (__hurd_fail (err
), MACH_PORT_NULL
) : result
;
273 weak_alias (__directory_name_split
, directory_name_split
)
277 __file_name_lookup_under (file_t startdir
,
278 const char *file_name
, int flags
, mode_t mode
)
283 error_t
use_init_port (int which
, error_t (*operate
) (mach_port_t
))
285 return (which
== INIT_PORT_CWDIR
? (*operate
) (startdir
) :
286 _hurd_ports_use (which
, operate
));
289 err
= __hurd_file_name_lookup (&use_init_port
, &__getdport
, 0,
290 file_name
, flags
, mode
& ~_hurd_umask
,
293 return err
? (__hurd_fail (err
), MACH_PORT_NULL
) : result
;
295 weak_alias (__file_name_lookup_under
, file_name_lookup_under
)