1 /* Copyright (C) 1991-2023 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 <https://www.gnu.org/licenses/>. */
19 #include <sys/types.h>
22 #include <hurd/port.h>
31 /* Get the canonical absolute name of the given directory port, and put it
32 in SIZE bytes of BUF. Returns NULL if the directory couldn't be
33 determined or SIZE was too small. If successful, returns BUF. In GNU,
34 if BUF is NULL, an array is allocated with `malloc'; the array is SIZE
35 bytes long, unless SIZE <= 0, in which case it is as big as necessary.
36 If our root directory cannot be reached, the result will not begin with
37 a slash to indicate that it is relative to some unknown root directory. */
40 __hurd_canonicalize_directory_name_internal (file_t thisdir
,
45 mach_port_t rootid
, thisid
, rootdevid
, thisdevid
;
46 ino64_t rootino
, thisino
;
51 unsigned int dirbufsize
= 0;
52 const size_t orig_size
= size
;
54 inline void cleanup (void)
56 if (parent
!= thisdir
)
57 __mach_port_deallocate (__mach_task_self (), parent
);
59 __mach_port_deallocate (__mach_task_self (), thisid
);
60 __mach_port_deallocate (__mach_task_self (), thisdevid
);
61 __mach_port_deallocate (__mach_task_self (), rootid
);
64 __vm_deallocate (__mach_task_self (),
65 (vm_address_t
) dirbuf
, dirbufsize
);
72 return __hurd_fail (EINVAL
), NULL
;
74 size
= FILENAME_MAX
* 4 + 1; /* Good starting guess. */
81 file_name
= malloc (size
);
82 if (file_name
== NULL
)
86 file_namep
= file_name
+ size
;
89 /* Get a port to our root directory and get its identity. */
91 if (err
= __USEPORT (CRDIR
, __io_identity (port
,
92 &rootid
, &rootdevid
, &rootino
)))
93 return __hurd_fail (err
), NULL
;
94 __mach_port_deallocate (__mach_task_self (), rootdevid
);
96 /* Stat the port to the directory of interest. */
98 if (err
= __io_identity (thisdir
, &thisid
, &thisdevid
, &thisino
))
100 __mach_port_deallocate (__mach_task_self (), rootid
);
101 return __hurd_fail (err
), NULL
;
105 while (thisid
!= rootid
)
107 /* PARENT is a port to the directory we are currently on;
108 THISID, THISDEV, and THISINO are its identity.
109 Look in its parent (..) for a file with the same file number. */
112 mach_port_t dotid
, dotdevid
;
117 mach_msg_type_number_t dirdatasize
;
118 int direntry
, nentries
;
121 /* Look at the parent directory. */
122 newp
= __file_name_lookup_under (parent
, "..", O_READ
, 0);
123 if (newp
== MACH_PORT_NULL
)
125 if (parent
!= thisdir
)
126 __mach_port_deallocate (__mach_task_self (), parent
);
129 /* Get this directory's identity and figure out if it's a mount
131 if (err
= __io_identity (parent
, &dotid
, &dotdevid
, &dotino
))
133 mount_point
= dotdevid
!= thisdevid
;
137 /* `..' == `.' but it is not our root directory. */
138 __mach_port_deallocate (__mach_task_self (), dotid
);
139 __mach_port_deallocate (__mach_task_self (), dotdevid
);
143 /* Search for the last directory. */
146 dirdatasize
= dirbufsize
;
147 while (!(err
= __dir_readdir (parent
, &dirdata
, &dirdatasize
,
148 direntry
, -1, 0, &nentries
))
151 /* We have a block of directory entries. */
155 direntry
+= nentries
;
157 if (dirdata
!= dirbuf
)
159 /* The data was passed out of line, so our old buffer is no
160 longer useful. Deallocate the old buffer and reset our
161 information for the new buffer. */
162 __vm_deallocate (__mach_task_self (),
163 (vm_address_t
) dirbuf
, dirbufsize
);
165 dirbufsize
= round_page (dirdatasize
);
168 /* Iterate over the returned directory entries, looking for one
169 whose file number is THISINO. */
172 while (offset
< dirdatasize
)
174 d
= (struct dirent64
*) &dirdata
[offset
];
175 offset
+= d
->d_reclen
;
177 /* Ignore `.' and `..'. */
178 if (d
->d_name
[0] == '.'
180 || (d
->d_namlen
== 2 && d
->d_name
[1] == '.')))
183 if (mount_point
|| d
->d_ino
== thisino
)
185 file_t
try = __file_name_lookup_under (parent
, d
->d_name
,
189 if (try == MACH_PORT_NULL
)
191 err
= __io_identity (try, &id
, &devid
, &fileno
);
192 __mach_port_deallocate (__mach_task_self (), try);
195 __mach_port_deallocate (__mach_task_self (), id
);
196 __mach_port_deallocate (__mach_task_self (), devid
);
205 inner_errlose
: /* Goto ERRLOSE: after cleaning up. */
206 __mach_port_deallocate (__mach_task_self (), dotid
);
207 __mach_port_deallocate (__mach_task_self (), dotdevid
);
210 else if (nentries
== 0)
212 /* We got to the end of the directory without finding anything!
213 We are in a directory that has been unlinked, or something is
221 /* Prepend the directory name just discovered. */
222 size_t offset
= file_namep
- file_name
;
224 if (offset
< d
->d_namlen
+ 1)
227 return __hurd_fail (ERANGE
), NULL
;
231 buf
= realloc (file_name
, size
);
237 file_namep
= &buf
[offset
+ size
/ 2];
239 /* Move current contents up to the end of the buffer.
240 This is guaranteed to be non-overlapping. */
241 memcpy (file_namep
, file_namep
- size
/ 2,
242 file_name
+ size
- file_namep
);
245 file_namep
-= d
->d_namlen
;
246 (void) memcpy (file_namep
, d
->d_name
, d
->d_namlen
);
250 /* The next iteration will find the name of the directory we
251 just searched through. */
252 __mach_port_deallocate (__mach_task_self (), thisid
);
253 __mach_port_deallocate (__mach_task_self (), thisdevid
);
255 thisdevid
= dotdevid
;
259 if (file_namep
== &file_name
[size
- 1])
260 /* We found nothing and got all the way to the root.
261 So the root is our current directory. */
264 memmove (file_name
, file_namep
, file_name
+ size
- file_namep
);
270 (void) __hurd_fail (err
);
277 strong_alias (__hurd_canonicalize_directory_name_internal
, _hurd_canonicalize_directory_name_internal
)
280 __canonicalize_directory_name_internal (const char *thisdir
, char *buf
,
284 file_t port
= __file_name_lookup (thisdir
, 0, 0);
285 if (port
== MACH_PORT_NULL
)
287 result
= __hurd_canonicalize_directory_name_internal (port
, buf
, size
);
288 __mach_port_deallocate (__mach_task_self (), port
);
292 /* Get the pathname of the current working directory, and put it in SIZE
293 bytes of BUF. Returns NULL if the directory couldn't be determined or
294 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
295 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
296 unless SIZE <= 0, in which case it is as big as necessary. */
298 __getcwd (char *buf
, size_t size
)
302 __hurd_canonicalize_directory_name_internal (port
,
306 libc_hidden_def (__getcwd
)
307 weak_alias (__getcwd
, getcwd
)