|
Packit |
6c4009 |
/* Create a symbolic link named relative to an open directory. Hurd version.
|
|
Packit |
6c4009 |
Copyright (C) 1991-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <fcntl.h>
|
|
Packit |
6c4009 |
#include <stddef.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <hurd.h>
|
|
Packit |
6c4009 |
#include <hurd/paths.h>
|
|
Packit |
6c4009 |
#include <hurd/fd.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Make a link to FROM called TO relative to FD. */
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
symlinkat (const char *from, int fd, const char *to)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
error_t err;
|
|
Packit |
6c4009 |
file_t dir, node;
|
|
Packit |
6c4009 |
char *name;
|
|
Packit |
6c4009 |
const size_t len = strlen (from) + 1;
|
|
Packit |
6c4009 |
char buf[sizeof (_HURD_SYMLINK) + len];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* A symlink is a file whose translator is "/hurd/symlink\0target\0". */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
memcpy (buf, _HURD_SYMLINK, sizeof (_HURD_SYMLINK));
|
|
Packit |
6c4009 |
memcpy (&buf[sizeof (_HURD_SYMLINK)], from, len);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
dir = __file_name_split_at (fd, to, &name);
|
|
Packit |
6c4009 |
if (dir == MACH_PORT_NULL)
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Create a new, unlinked node in the target directory. */
|
|
Packit |
6c4009 |
err = __dir_mkfile (dir, O_WRITE, 0777 & ~_hurd_umask, &node);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (! err)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Set the node's translator to make it a symlink. */
|
|
Packit |
6c4009 |
err = __file_set_translator (node,
|
|
Packit |
6c4009 |
FS_TRANS_EXCL|FS_TRANS_SET,
|
|
Packit |
6c4009 |
FS_TRANS_EXCL|FS_TRANS_SET, 0,
|
|
Packit |
6c4009 |
buf, sizeof (_HURD_SYMLINK) + len,
|
|
Packit |
6c4009 |
MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (! err)
|
|
Packit |
6c4009 |
/* Link the node, now a valid symlink, into the target directory. */
|
|
Packit |
6c4009 |
err = __dir_link (dir, node, name, 1);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
__mach_port_deallocate (__mach_task_self (), node);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
__mach_port_deallocate (__mach_task_self (), dir);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (err)
|
|
Packit |
6c4009 |
return __hurd_fail (err);
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|