2 * Copyright (C) 2010-2011 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
39 #include "ignore-value.h"
42 #include "guestfs-internal.h"
44 #if defined(HAVE_HIVEX) && defined(DB_DUMP)
46 static unsigned char *convert_hex_to_binary (guestfs_h *g, const char *hex, size_t hexlen, size_t *binlen_rtn);
48 /* This helper function is specialized to just reading the hash-format
49 * output from db_dump/db4_dump. It's just enough to support the RPM
50 * database format. Note that the filename must not contain any shell
51 * characters (this is guaranteed by the caller).
54 guestfs___read_db_dump (guestfs_h *g,
55 const char *dumpfile, void *opaque,
56 guestfs___db_dump_callback callback)
58 #define cmd_len (strlen (dumpfile) + 64)
64 unsigned char *key = NULL, *value = NULL;
65 size_t keylen, valuelen;
68 snprintf (cmd, cmd_len, DB_DUMP " -k '%s'", dumpfile);
70 debug (g, "read_db_dump command: %s", cmd);
72 pp = popen (cmd, "r");
74 perrorf (g, "popen: %s", cmd);
78 /* Ignore everything to end-of-header marker. */
79 while ((linelen = getline (&line, &len, pp)) != -1) {
80 if (STRPREFIX (line, "HEADER=END"))
85 error (g, _("unexpected end of output from db_dump command before end of header"));
89 /* Now read the key, value pairs. They are prefixed with a space and
90 * printed as hex strings, so convert those strings to binary. Pass
91 * the strings up to the callback function.
93 while ((linelen = getline (&line, &len, pp)) != -1) {
94 if (STRPREFIX (line, "DATA=END"))
97 if (linelen < 1 || line[0] != ' ') {
98 error (g, _("unexpected line from db_dump command, no space prefix"));
102 if ((key = convert_hex_to_binary (g, &line[1], linelen-1,
106 if ((linelen = getline (&line, &len, pp)) == -1)
109 if (linelen < 1 || line[0] != ' ') {
110 error (g, _("unexpected line from db_dump command, no space prefix"));
114 if ((value = convert_hex_to_binary (g, &line[1], linelen-1,
118 if (callback (g, key, keylen, value, valuelen, opaque) == -1)
127 error (g, _("unexpected end of output from db_dump command before end of data"));
131 /* Catch errors from the db_dump command. */
132 if (pclose (pp) != 0) {
133 perrorf (g, "pclose: %s", cmd);
153 convert_hex_octet (const char *h)
159 r = (h[0] - 'a' + 10) << 4;
162 r = (h[0] - 'A' + 10) << 4;
165 r = (h[0] - '0') << 4;
173 r |= h[1] - 'a' + 10;
176 r |= h[1] - 'A' + 10;
188 static unsigned char *
189 convert_hex_to_binary (guestfs_h *g, const char *hex, size_t hexlen,
197 if (hexlen > 0 && hex[hexlen-1] == '\n')
201 bin = safe_malloc (g, binlen);
203 for (i = o = 0; i+1 < hexlen && o < binlen; i += 2, ++o) {
204 b = convert_hex_octet (&hex[i]);
208 error (g, _("unexpected non-hex digits in output of db_dump command"));
214 *binlen_rtn = binlen;
218 #endif /* defined(HAVE_HIVEX) && defined(DB_DUMP) */