1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/* libguestfs - the guestfsd daemon
* Copyright (C) 2009 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include "daemon.h"
#include "actions.h"
char *
do_readlink (const char *path)
{
ssize_t r;
char *ret;
char link[PATH_MAX];
CHROOT_IN;
r = readlink (path, link, sizeof link);
CHROOT_OUT;
if (r == -1) {
reply_with_perror ("readlink");
return NULL;
}
ret = strndup (link, r);
if (ret == NULL) {
reply_with_perror ("strndup");
return NULL;
}
return ret; /* caller frees */
}
char **
do_readlinklist (const char *path, char *const *names)
{
int fd_cwd;
size_t i;
ssize_t r;
char link[PATH_MAX];
const char *str;
DECLARE_STRINGSBUF (ret);
CHROOT_IN;
fd_cwd = open (path, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
CHROOT_OUT;
if (fd_cwd == -1) {
reply_with_perror ("open: %s", path);
return NULL;
}
for (i = 0; names[i] != NULL; ++i) {
r = readlinkat (fd_cwd, names[i], link, sizeof link);
if (r >= PATH_MAX) {
reply_with_perror ("readlinkat: returned link is too long");
close (fd_cwd);
return NULL;
}
/* Because of the way this function is intended to be used,
* we actually expect to see errors here, and they are not fatal.
*/
if (r >= 0) {
link[r] = '\0';
str = link;
} else
str = "";
if (add_string (&ret, str) == -1) {
close (fd_cwd);
return NULL;
}
}
close (fd_cwd);
if (end_stringsbuf (&ret) == -1)
return NULL;
return ret.argv;
}
static int
_link (const char *flag, int symbolic, const char *target, const char *linkname)
{
int r;
char *err;
char *buf_linkname;
char *buf_target;
/* Prefix linkname with sysroot. */
buf_linkname = sysroot_path (linkname);
if (!buf_linkname) {
reply_with_perror ("malloc");
return -1;
}
/* Only prefix target if it's _not_ a symbolic link, and if
* the target is absolute. Note that the resulting link will
* always be "broken" from the p.o.v. of the appliance, ie:
* /a -> /b but the path as seen here is /sysroot/b
*/
buf_target = NULL;
if (!symbolic && target[0] == '/') {
buf_target = sysroot_path (target);
if (!buf_target) {
reply_with_perror ("malloc");
free (buf_linkname);
return -1;
}
}
if (flag)
r = command (NULL, &err,
"ln", flag, "--", /* target could begin with '-' */
buf_target ? : target, buf_linkname, NULL);
else
r = command (NULL, &err,
"ln", "--",
buf_target ? : target, buf_linkname, NULL);
free (buf_linkname);
free (buf_target);
if (r == -1) {
reply_with_error ("ln%s%s: %s: %s: %s",
flag ? " " : "",
flag ? : "",
target, linkname, err);
free (err);
return -1;
}
free (err);
return 0;
}
int
do_ln (const char *target, const char *linkname)
{
return _link (NULL, 0, target, linkname);
}
int
do_ln_f (const char *target, const char *linkname)
{
return _link ("-f", 0, target, linkname);
}
int
do_ln_s (const char *target, const char *linkname)
{
return _link ("-s", 1, target, linkname);
}
int
do_ln_sf (const char *target, const char *linkname)
{
return _link ("-sf", 1, target, linkname);
}
|