1 /* Copyright (C) 1992-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
19 #include <hurd/lookup.h>
24 /* Translate the error from dir_lookup into the error the user sees. */
26 lookup_error (error_t error
)
32 /* These indicate that the server does not understand dir_lookup
33 at all. If it were a directory, it would, by definition. */
41 __hurd_file_name_lookup (error_t (*use_init_port
)
42 (int which
, error_t (*operate
) (file_t
)),
43 file_t (*get_dtable_port
) (int fd
),
45 (file_t dir
, char *name
, int flags
, mode_t mode
,
46 retry_type
*do_retry
, string_t retry_name
,
48 const char *file_name
, int flags
, mode_t mode
,
52 enum retry_type doretry
;
53 char retryname
[1024]; /* XXX string_t LOSES! */
56 error_t
lookup_op (mach_port_t startdir
)
58 return lookup_error ((*lookup
) (startdir
, file_name
, flags
, mode
,
59 &doretry
, retryname
, result
));
63 lookup
= __dir_lookup
;
65 if (file_name
[0] == '\0')
68 startport
= (file_name
[0] == '/') ? INIT_PORT_CRDIR
: INIT_PORT_CWDIR
;
69 while (file_name
[0] == '/')
72 if (flags
& O_NOFOLLOW
) /* See lookup-retry.c about O_NOFOLLOW. */
75 if (flags
& O_DIRECTORY
)
77 /* The caller wants to require that the file we look up is a directory.
78 We can do this without an extra RPC by appending a trailing slash
79 to the file name we look up. */
80 size_t len
= strlen (file_name
);
83 else if (file_name
[len
- 1] != '/')
85 char *n
= alloca (len
+ 2);
86 memcpy (n
, file_name
, len
);
93 err
= (*use_init_port
) (startport
, &lookup_op
);
95 err
= __hurd_file_name_lookup_retry (use_init_port
, get_dtable_port
,
96 lookup
, doretry
, retryname
,
101 weak_alias (__hurd_file_name_lookup
, hurd_file_name_lookup
)
104 __hurd_file_name_split (error_t (*use_init_port
)
105 (int which
, error_t (*operate
) (file_t
)),
106 file_t (*get_dtable_port
) (int fd
),
108 (file_t dir
, char *name
, int flags
, mode_t mode
,
109 retry_type
*do_retry
, string_t retry_name
,
110 mach_port_t
*result
),
111 const char *file_name
,
112 file_t
*dir
, char **name
)
114 error_t
addref (file_t crdir
)
117 return __mach_port_mod_refs (__mach_task_self (),
118 crdir
, MACH_PORT_RIGHT_SEND
, +1);
121 const char *lastslash
= strrchr (file_name
, '/');
123 if (lastslash
!= NULL
)
125 if (lastslash
== file_name
)
127 /* "/foobar" => crdir + "foobar". */
128 *name
= (char *) file_name
+ 1;
129 return (*use_init_port
) (INIT_PORT_CRDIR
, &addref
);
133 /* "/dir1/dir2/.../file". */
134 char dirname
[lastslash
- file_name
+ 1];
135 memcpy (dirname
, file_name
, lastslash
- file_name
);
136 dirname
[lastslash
- file_name
] = '\0';
137 *name
= (char *) lastslash
+ 1;
139 __hurd_file_name_lookup (use_init_port
, get_dtable_port
, lookup
,
143 else if (file_name
[0] == '\0')
147 /* "foobar" => cwdir + "foobar". */
148 *name
= (char *) file_name
;
149 return (*use_init_port
) (INIT_PORT_CWDIR
, &addref
);
152 weak_alias (__hurd_file_name_split
, hurd_file_name_split
)
154 /* This is the same as hurd_file_name_split, except that it ignores
155 trailing slashes (so *NAME is never ""). */
157 __hurd_directory_name_split (error_t (*use_init_port
)
158 (int which
, error_t (*operate
) (file_t
)),
159 file_t (*get_dtable_port
) (int fd
),
161 (file_t dir
, char *name
, int flags
, mode_t mode
,
162 retry_type
*do_retry
, string_t retry_name
,
163 mach_port_t
*result
),
164 const char *file_name
,
165 file_t
*dir
, char **name
)
167 error_t
addref (file_t crdir
)
170 return __mach_port_mod_refs (__mach_task_self (),
171 crdir
, MACH_PORT_RIGHT_SEND
, +1);
174 const char *lastslash
= strrchr (file_name
, '/');
176 if (lastslash
!= NULL
&& lastslash
[1] == '\0')
178 /* Trailing slash doesn't count. Look back further. */
180 /* Back up over all trailing slashes. */
181 while (lastslash
> file_name
&& *lastslash
== '/')
184 /* Find the last one earlier in the string, before the trailing ones. */
185 lastslash
= __memrchr (file_name
, '/', lastslash
- file_name
);
188 if (lastslash
!= NULL
)
190 if (lastslash
== file_name
)
192 /* "/foobar" => crdir + "foobar". */
193 *name
= (char *) file_name
+ 1;
194 return (*use_init_port
) (INIT_PORT_CRDIR
, &addref
);
198 /* "/dir1/dir2/.../file". */
199 char dirname
[lastslash
- file_name
+ 1];
200 memcpy (dirname
, file_name
, lastslash
- file_name
);
201 dirname
[lastslash
- file_name
] = '\0';
202 *name
= (char *) lastslash
+ 1;
204 __hurd_file_name_lookup (use_init_port
, get_dtable_port
, lookup
,
208 else if (file_name
[0] == '\0')
212 /* "foobar" => cwdir + "foobar". */
213 *name
= (char *) file_name
;
214 return (*use_init_port
) (INIT_PORT_CWDIR
, &addref
);
217 weak_alias (__hurd_directory_name_split
, hurd_directory_name_split
)
221 __file_name_lookup (const char *file_name
, int flags
, mode_t mode
)
226 err
= __hurd_file_name_lookup (&_hurd_ports_use
, &__getdport
, 0,
227 file_name
, flags
, mode
& ~_hurd_umask
,
230 return err
? (__hurd_fail (err
), MACH_PORT_NULL
) : result
;
232 weak_alias (__file_name_lookup
, file_name_lookup
)
236 __file_name_split (const char *file_name
, char **name
)
241 err
= __hurd_file_name_split (&_hurd_ports_use
, &__getdport
, 0,
242 file_name
, &result
, name
);
244 return err
? (__hurd_fail (err
), MACH_PORT_NULL
) : result
;
246 weak_alias (__file_name_split
, file_name_split
)
249 __directory_name_split (const char *directory_name
, char **name
)
254 err
= __hurd_directory_name_split (&_hurd_ports_use
, &__getdport
, 0,
255 directory_name
, &result
, name
);
257 return err
? (__hurd_fail (err
), MACH_PORT_NULL
) : result
;
259 weak_alias (__directory_name_split
, directory_name_split
)
263 __file_name_lookup_under (file_t startdir
,
264 const char *file_name
, int flags
, mode_t mode
)
269 error_t
use_init_port (int which
, error_t (*operate
) (mach_port_t
))
271 return (which
== INIT_PORT_CWDIR
? (*operate
) (startdir
) :
272 _hurd_ports_use (which
, operate
));
275 err
= __hurd_file_name_lookup (&use_init_port
, &__getdport
, 0,
276 file_name
, flags
, mode
& ~_hurd_umask
,
279 return err
? (__hurd_fail (err
), MACH_PORT_NULL
) : result
;
281 weak_alias (__file_name_lookup_under
, file_name_lookup_under
)