1 /* Make a link between file names relative to open directories. Hurd version.
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 <errno.h>
20 #include <fcntl.h>
21 #include <stddef.h>
22 #include <unistd.h>
23 #include <hurd.h>
24 #include <hurd/fd.h>
25
26
27 /* Make a link to FROM relative to FROMFD called TO relative to TOFD. */
28 int
linkat(int fromfd,const char * from,int tofd,const char * to,int flags)29 linkat (int fromfd, const char *from, int tofd, const char *to, int flags)
30 {
31 error_t err;
32 file_t oldfile, linknode, todir;
33 char *toname;
34
35 /* POSIX says linkat doesn't follow symlinks by default, so pass
36 O_NOLINK. That can be overridden by AT_SYMLINK_FOLLOW in FLAGS. */
37 oldfile = __file_name_lookup_at (fromfd, flags, from, O_NOLINK, 0);
38 if (oldfile == MACH_PORT_NULL)
39 return -1;
40
41 /* The file_getlinknode RPC returns the port that should be passed to
42 the receiving filesystem (the one containing TODIR) in dir_link. */
43
44 err = __file_getlinknode (oldfile, &linknode);
45 __mach_port_deallocate (__mach_task_self (), oldfile);
46 if (err)
47 return __hurd_fail (err);
48
49 todir = __file_name_split_at (tofd, to, &toname);
50 if (todir != MACH_PORT_NULL)
51 {
52 err = __dir_link (todir, linknode, toname, 1);
53 __mach_port_deallocate (__mach_task_self (), todir);
54 }
55 __mach_port_deallocate (__mach_task_self (), linknode);
56 if (todir == MACH_PORT_NULL)
57 return -1;
58
59 if (err)
60 return __hurd_fail (err);
61 return 0;
62 }
63