1 /* Lookup helper function for Hurd implementation of *at functions.
2    Copyright (C) 2006-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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    <https://www.gnu.org/licenses/>.  */
18 
19 #include <hurd.h>
20 #include <hurd/lookup.h>
21 #include <hurd/fd.h>
22 #include <string.h>
23 #include <fcntl.h>
24 
25 file_t
__file_name_lookup_at(int fd,int at_flags,const char * file_name,int flags,mode_t mode)26 __file_name_lookup_at (int fd, int at_flags,
27 		       const char *file_name, int flags, mode_t mode)
28 {
29   error_t err;
30   file_t result;
31   int empty = at_flags & AT_EMPTY_PATH;
32 
33   at_flags &= ~AT_EMPTY_PATH;
34 
35   err = __hurd_at_flags (&at_flags, &flags);
36   if (err)
37     return (__hurd_fail (err), MACH_PORT_NULL);
38 
39   if (fd == AT_FDCWD || file_name[0] == '/')
40     return __file_name_lookup (file_name, flags, mode);
41 
42   if (empty != 0 && file_name[0] == '\0')
43     {
44       enum retry_type doretry;
45       char retryname[1024];	/* XXX string_t LOSES! */
46 
47       err = HURD_DPORT_USE (fd, __dir_lookup (port, "", flags, mode,
48 					      &doretry, retryname,
49 					      &result));
50 
51       if (! err)
52 	err = __hurd_file_name_lookup_retry (&_hurd_ports_use, &__getdport,
53 					     NULL, doretry, retryname,
54 					     flags, mode, &result);
55 
56       return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result;
57     }
58 
59   file_t startdir;
60   error_t use_init_port (int which, error_t (*operate) (mach_port_t))
61     {
62       return (which == INIT_PORT_CWDIR ? (*operate) (startdir)
63 	      : _hurd_ports_use (which, operate));
64     }
65 
66   err = HURD_DPORT_USE (fd, (startdir = port,
67 			     __hurd_file_name_lookup (&use_init_port,
68 						      &__getdport, NULL,
69 						      file_name,
70 						      flags,
71 						      mode & ~_hurd_umask,
72 						      &result)));
73 
74   return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result;
75 }
76 
77 file_t
__file_name_split_at(int fd,const char * file_name,char ** name)78 __file_name_split_at (int fd, const char *file_name, char **name)
79 {
80   error_t err;
81   file_t result;
82 
83   if (fd == AT_FDCWD || file_name[0] == '/')
84     return __file_name_split (file_name, name);
85 
86   err = __hurd_file_name_split (&_hurd_ports_use, &__getdport, 0,
87 				file_name, &result, name);
88 
89   file_t startdir;
90   error_t use_init_port (int which, error_t (*operate) (mach_port_t))
91   {
92     return (which == INIT_PORT_CWDIR ? (*operate) (startdir)
93 	    : _hurd_ports_use (which, operate));
94   }
95 
96   err = HURD_DPORT_USE (fd, (startdir = port,
97 			     __hurd_file_name_split (&use_init_port,
98 						     &__getdport, 0,
99 						     file_name,
100 						     &result, name)));
101 
102   return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result;
103 }
104 
105 file_t
__directory_name_split_at(int fd,const char * directory_name,char ** name)106 __directory_name_split_at (int fd, const char *directory_name, char **name)
107 {
108   error_t err;
109   file_t result;
110 
111   if (fd == AT_FDCWD || directory_name[0] == '/')
112     return __directory_name_split (directory_name, name);
113 
114   file_t startdir;
115   error_t use_init_port (int which, error_t (*operate) (mach_port_t))
116     {
117       return (which == INIT_PORT_CWDIR ? (*operate) (startdir)
118 	      : _hurd_ports_use (which, operate));
119     }
120 
121   err = HURD_DPORT_USE (fd, (startdir = port,
122 			     __hurd_directory_name_split (&use_init_port,
123 							  &__getdport, 0,
124 							  directory_name,
125 							  &result, name)));
126 
127   return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result;
128 }
129