From: Richard W.M. Jones Date: Fri, 31 Jul 2009 07:39:43 +0000 (+0100) Subject: New commands: 'ln', 'ln-f', 'ln-s', 'ln-sf' and 'readlink'. X-Git-Tag: 1.0.66~50 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=afaff775c12f32b7912f194e2fcc8e76b8c82572;p=libguestfs.git New commands: 'ln', 'ln-f', 'ln-s', 'ln-sf' and 'readlink'. These commands can be used to make hard and symbolic links. The readlink command is used to read existing symbolic links. --- diff --git a/TODO b/TODO index d4a93b8..2b9aae1 100644 --- a/TODO +++ b/TODO @@ -145,8 +145,6 @@ Ideas for extra commands General glibc / core programs: chgrp dd (?) - ln / ln -s - readlink utime / utimes / futimes / futimens / l.. more mk*temp calls some sort of alloc/fallocate/posix_fallocate call to create empty space diff --git a/daemon/Makefile.am b/daemon/Makefile.am index c0e8bc9..52482d0 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -45,6 +45,7 @@ guestfsd_SOURCES = \ headtail.c \ hexdump.c \ initrd.c \ + link.c \ ls.c \ lvm.c \ mknod.c \ diff --git a/daemon/link.c b/daemon/link.c new file mode 100644 index 0000000..57e3538 --- /dev/null +++ b/daemon/link.c @@ -0,0 +1,137 @@ +/* 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include + +#include +#include +#include +#include +#include + +#include "daemon.h" +#include "actions.h" + +char * +do_readlink (char *path) +{ + ssize_t r; + char *ret; + char link[PATH_MAX]; + + NEED_ROOT (NULL); + ABS_PATH (path, NULL); + + 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 */ +} + +static int +_link (const char *flag, int symbolic, const char *target, const char *linkname) +{ + int r; + char *err; + char *buf_linkname; + char *buf_target; + + NEED_ROOT (-1); + ABS_PATH (linkname, -1); + /* but target does not need to be absolute */ + + /* 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 (char *target, char *linkname) +{ + return _link (NULL, 0, target, linkname); +} + +int +do_ln_f (char *target, char *linkname) +{ + return _link ("-f", 0, target, linkname); +} + +int +do_ln_s (char *target, char *linkname) +{ + return _link ("-s", 1, target, linkname); +} + +int +do_ln_sf (char *target, char *linkname) +{ + return _link ("-sf", 1, target, linkname); +} diff --git a/po/POTFILES.in b/po/POTFILES.in index 1b2f03e..fc6c9f0 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -23,6 +23,7 @@ daemon/guestfsd.c daemon/headtail.c daemon/hexdump.c daemon/initrd.c +daemon/link.c daemon/ls.c daemon/lvm.c daemon/mknod.c @@ -32,6 +33,7 @@ daemon/ntfs.c daemon/pingdaemon.c daemon/proto.c daemon/readdir.c +daemon/realpath.c daemon/scrub.c daemon/sfdisk.c daemon/sleep.c diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index 9cc2bc3..de8febe 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -163 +168 diff --git a/src/generator.ml b/src/generator.ml index 3a35fa2..3472193 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -3141,6 +3141,52 @@ matching lines."); Return the canonicalized absolute pathname of C. The returned path has no C<.>, C<..> or symbolic link path elements."); + ("ln", (RErr, [String "target"; String "linkname"]), 164, [], + [InitBasicFS, Always, TestOutputStruct ( + [["touch"; "/a"]; + ["ln"; "/a"; "/b"]; + ["stat"; "/b"]], [CompareWithInt ("nlink", 2)])], + "create a hard link", + "\ +This command creates a hard link using the C command."); + + ("ln_f", (RErr, [String "target"; String "linkname"]), 165, [], + [InitBasicFS, Always, TestOutputStruct ( + [["touch"; "/a"]; + ["touch"; "/b"]; + ["ln_f"; "/a"; "/b"]; + ["stat"; "/b"]], [CompareWithInt ("nlink", 2)])], + "create a hard link", + "\ +This command creates a hard link using the C command. +The C<-f> option removes the link (C) if it exists already."); + + ("ln_s", (RErr, [String "target"; String "linkname"]), 166, [], + [InitBasicFS, Always, TestOutputStruct ( + [["touch"; "/a"]; + ["ln_s"; "a"; "/b"]; + ["lstat"; "/b"]], [CompareWithInt ("mode", 0o120777)])], + "create a symbolic link", + "\ +This command creates a symbolic link using the C command."); + + ("ln_sf", (RErr, [String "target"; String "linkname"]), 167, [], + [InitBasicFS, Always, TestOutput ( + [["mkdir_p"; "/a/b"]; + ["touch"; "/a/b/c"]; + ["ln_sf"; "../d"; "/a/b/c"]; + ["readlink"; "/a/b/c"]], "../d")], + "create a symbolic link", + "\ +This command creates a symbolic link using the C command, +The C<-f> option removes the link (C) if it exists already."); + + ("readlink", (RString "link", [String "path"]), 168, [], + [] (* XXX tested above *), + "read the target of a symbolic link", + "\ +This command reads the target of a symbolic link."); + ] let all_functions = non_daemon_functions @ daemon_functions