Back to index

glibc  2.9
symlinkat.c
Go to the documentation of this file.
00001 /* Create a symbolic link named relative to an open directory.  Hurd version.
00002    Copyright (C) 1991,1992,1993,1994,1995,1996,1997,2006
00003        Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <fcntl.h>
00023 #include <stddef.h>
00024 #include <unistd.h>
00025 #include <hurd.h>
00026 #include <hurd/paths.h>
00027 #include <hurd/fd.h>
00028 #include <string.h>
00029 
00030 
00031 /* Make a link to FROM called TO relative to FD.  */
00032 int
00033 symlinkat (from, fd, to)
00034      const char *from;
00035      int fd;
00036      const char *to;
00037 {
00038   error_t err;
00039   file_t dir, node;
00040   char *name;
00041   const size_t len = strlen (from) + 1;
00042   char buf[sizeof (_HURD_SYMLINK) + len];
00043 
00044   /* A symlink is a file whose translator is "/hurd/symlink\0target\0".  */
00045 
00046   memcpy (buf, _HURD_SYMLINK, sizeof (_HURD_SYMLINK));
00047   memcpy (&buf[sizeof (_HURD_SYMLINK)], from, len);
00048 
00049   dir = __file_name_split_at (fd, to, &name);
00050   if (dir == MACH_PORT_NULL)
00051     return -1;
00052 
00053   /* Create a new, unlinked node in the target directory.  */
00054   err = __dir_mkfile (dir, O_WRITE, 0777 & ~_hurd_umask, &node);
00055 
00056   if (! err)
00057     /* Set the node's translator to make it a symlink.  */
00058     err = __file_set_translator (node,
00059                              FS_TRANS_EXCL|FS_TRANS_SET,
00060                              FS_TRANS_EXCL|FS_TRANS_SET, 0,
00061                              buf, sizeof (_HURD_SYMLINK) + len,
00062                              MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
00063 
00064   if (! err)
00065     /* Link the node, now a valid symlink, into the target directory.  */
00066     err = __dir_link (dir, node, name, 1);
00067 
00068   __mach_port_deallocate (__mach_task_self (), dir);
00069   __mach_port_deallocate (__mach_task_self (), node);
00070 
00071   if (err)
00072     return __hurd_fail (err);
00073   return 0;
00074 }