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