Implement 'strings' and 'hexdump' commands.

This commit is contained in:
Richard Jones
2009-05-08 14:27:47 +01:00
parent bd7c8ee043
commit fa7c8bb79b
4 changed files with 190 additions and 0 deletions

View File

@@ -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 \

60
daemon/hexdump.c Normal file
View File

@@ -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 <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 */
}

91
daemon/strings.c Normal file
View File

@@ -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 <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}

View File

@@ -1807,6 +1807,43 @@ true if their content is exactly equal, or false otherwise.
The external L<cmp(1)> 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<strings(1)> 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<guestfs_strings> command, but allows you to
specify the encoding.
See the L<strings(1)> manpage for the full list of encodings.
Commonly useful encodings are C<l> (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<hexdump -C> on the given C<path>. The result is
the human-readable, canonical hex dump of the file.");
]
let all_functions = non_daemon_functions @ daemon_functions