From: Richard Jones Date: Fri, 8 May 2009 13:27:47 +0000 (+0100) Subject: Implement 'strings' and 'hexdump' commands. X-Git-Tag: 1.0.22~14 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=fa7c8bb79b45aecdf65ed93635a42f3fdf301134;p=libguestfs.git Implement 'strings' and 'hexdump' commands. --- diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 063f1ab..8460d44 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -37,12 +37,14 @@ guestfsd_SOURCES = \ fsck.c \ grub.c \ guestfsd.c \ + hexdump.c \ ls.c \ lvm.c \ mount.c \ pingdaemon.c \ proto.c \ stat.c \ + strings.c \ stubs.c \ sync.c \ tar.c \ diff --git a/daemon/hexdump.c b/daemon/hexdump.c new file mode 100644 index 0000000..9de602a --- /dev/null +++ b/daemon/hexdump.c @@ -0,0 +1,60 @@ +/* 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 "daemon.h" +#include "actions.h" + +char * +do_hexdump (const char *path) +{ + int len; + char *buf; + int r; + char *out, *err; + + NEED_ROOT (NULL); + ABS_PATH (path, NULL); + + len = strlen (path) + 9; + buf = malloc (len); + if (!buf) { + reply_with_perror ("malloc"); + return NULL; + } + + snprintf (buf, len, "/sysroot%s", path); + + r = command (&out, &err, "hexdump", "-C", buf, NULL); + free (buf); + if (r == -1) { + reply_with_error ("hexdump: %s: %s", path, err); + free (err); + free (out); + return NULL; + } + + free (err); + + return out; /* caller frees */ +} diff --git a/daemon/strings.c b/daemon/strings.c new file mode 100644 index 0000000..5e9c3a8 --- /dev/null +++ b/daemon/strings.c @@ -0,0 +1,91 @@ +/* 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 "daemon.h" +#include "actions.h" + +char ** +do_strings_e (const char *encoding, const char *path) +{ + int len; + char *buf; + int r; + char *out, *err; + char **lines = NULL; + int size = 0, alloc = 0; + char *p, *pend; + + NEED_ROOT (NULL); + ABS_PATH (path, NULL); + + len = strlen (path) + 9; + buf = malloc (len); + if (!buf) { + reply_with_perror ("malloc"); + return NULL; + } + + snprintf (buf, len, "/sysroot%s", path); + + r = command (&out, &err, "strings", "-e", encoding, buf, NULL); + free (buf); + if (r == -1) { + reply_with_error ("strings: %s: %s", path, err); + free (err); + free (out); + return NULL; + } + + free (err); + + /* Now convert the output to a list of lines. */ + p = out; + while (p && *p) { + pend = strchr (p, '\n'); + if (pend) { + *pend = '\0'; + pend++; + } + + if (add_string (&lines, &size, &alloc, p) == -1) { + free (out); + return NULL; + } + + p = pend; + } + + free (out); + + if (add_string (&lines, &size, &alloc, NULL) == -1) + return NULL; + + return lines; /* Caller frees. */ +} + +char ** +do_strings (const char *path) +{ + return do_strings_e ("s", path); +} diff --git a/src/generator.ml b/src/generator.ml index 4679add..b7b1907 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -1807,6 +1807,43 @@ true if their content is exactly equal, or false otherwise. The external L program is used for the comparison."); + ("strings", (RStringList "stringsout", [String "path"]), 94, [ProtocolLimitWarning], + [InitBasicFS, TestOutputList ( + [["write_file"; "/new"; "hello\nworld\n"; "0"]; + ["strings"; "/new"]], ["hello"; "world"])], + "print the printable strings in a file", + "\ +This runs the L command on a file and returns +the list of printable strings found."); + + ("strings_e", (RStringList "stringsout", [String "encoding"; String "path"]), 95, [ProtocolLimitWarning], + [InitBasicFS, TestOutputList ( + [["write_file"; "/new"; "hello\nworld\n"; "0"]; + ["strings_e"; "b"; "/new"]], []); + (*InitBasicFS, TestOutputList ( + [["write_file"; "/new"; "\000h\000e\000l\000l\000o\000\n\000w\000o\000r\000l\000d\000\n"; "24"]; + ["strings_e"; "b"; "/new"]], ["hello"; "world"])*)], + "print the printable strings in a file", + "\ +This is like the C command, but allows you to +specify the encoding. + +See the L manpage for the full list of encodings. + +Commonly useful encodings are C (lower case L) which will +show strings inside Windows/x86 files. + +The returned strings are transcoded to UTF-8."); + + ("hexdump", (RString "dump", [String "path"]), 96, [ProtocolLimitWarning], + [InitBasicFS, TestOutput ( + [["write_file"; "/new"; "hello\nworld\n"; "12"]; + ["hexdump"; "/new"]], "00000000 68 65 6c 6c 6f 0a 77 6f 72 6c 64 0a |hello.world.|\n0000000c\n")], + "dump a file in hexadecimal", + "\ +This runs C on the given C. The result is +the human-readable, canonical hex dump of the file."); + ] let all_functions = non_daemon_functions @ daemon_functions