/* libguestfs
- * Copyright (C) 2010 Red Hat Inc.
+ * Copyright (C) 2010-2011 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
+#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
+#include <errno.h>
+#include <endian.h>
#include <pcre.h>
-#include <magic.h>
+#ifdef HAVE_HIVEX
+#include <hivex.h>
+#endif
+
+#include "c-ctype.h"
#include "ignore-value.h"
+#include "xstrtol.h"
#include "guestfs.h"
#include "guestfs-internal.h"
#include "guestfs-internal-actions.h"
#include "guestfs_protocol.h"
-/* Compile all the regular expressions once when the shared library is
- * loaded. PCRE is thread safe so we're supposedly OK here if
- * multiple threads call into the libguestfs API functions below
- * simultaneously.
- */
-static pcre *re_file_elf;
-static pcre *re_file_win64;
-static pcre *re_elf_ppc64;
+#if defined(HAVE_HIVEX)
-static void compile_regexps (void) __attribute__((constructor));
-static void
-compile_regexps (void)
+/* The main inspection code. */
+char **
+guestfs__inspect_os (guestfs_h *g)
{
- const char *err;
- int offset;
+ /* Remove any information previously stored in the handle. */
+ guestfs___free_inspect_info (g);
+
+ if (guestfs_umount_all (g) == -1)
+ return NULL;
+
+ /* Iterate over all possible devices. Try to mount each
+ * (read-only). Examine ones which contain filesystems and add that
+ * information to the handle.
+ */
+ /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
+ char **devices;
+ devices = guestfs_list_devices (g);
+ if (devices == NULL)
+ return NULL;
+
+ size_t i;
+ for (i = 0; devices[i] != NULL; ++i) {
+ if (guestfs___check_for_filesystem_on (g, devices[i], 1, 0) == -1) {
+ guestfs___free_string_list (devices);
+ guestfs___free_inspect_info (g);
+ return NULL;
+ }
+ }
+ guestfs___free_string_list (devices);
-#define COMPILE(re,pattern,options) \
- do { \
- re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
- if (re == NULL) { \
- ignore_value (write (2, err, strlen (err))); \
- abort (); \
- } \
- } while (0)
+ /* Look at all partitions. */
+ char **partitions;
+ partitions = guestfs_list_partitions (g);
+ if (partitions == NULL) {
+ guestfs___free_inspect_info (g);
+ return NULL;
+ }
- COMPILE (re_file_elf,
- "ELF.*(?:executable|shared object|relocatable), (.+?),", 0);
- COMPILE (re_elf_ppc64, "64.*PowerPC", 0);
+ for (i = 0; partitions[i] != NULL; ++i) {
+ if (guestfs___check_for_filesystem_on (g, partitions[i], 0, i+1) == -1) {
+ guestfs___free_string_list (partitions);
+ guestfs___free_inspect_info (g);
+ return NULL;
+ }
+ }
+ guestfs___free_string_list (partitions);
+
+ /* Look at all LVs. */
+ if (guestfs___feature_available (g, "lvm2")) {
+ char **lvs;
+ lvs = guestfs_lvs (g);
+ if (lvs == NULL) {
+ guestfs___free_inspect_info (g);
+ return NULL;
+ }
+
+ for (i = 0; lvs[i] != NULL; ++i) {
+ if (guestfs___check_for_filesystem_on (g, lvs[i], 0, 0) == -1) {
+ guestfs___free_string_list (lvs);
+ guestfs___free_inspect_info (g);
+ return NULL;
+ }
+ }
+ guestfs___free_string_list (lvs);
+ }
+
+ /* At this point we have, in the handle, a list of all filesystems
+ * found and data about each one. Now we assemble the list of
+ * filesystems which are root devices and return that to the user.
+ * Fall through to guestfs__inspect_get_roots to do that.
+ */
+ char **ret = guestfs__inspect_get_roots (g);
+ if (ret == NULL)
+ guestfs___free_inspect_info (g);
+ return ret;
}
-/* Match a regular expression which contains no captures. Returns
- * true if it matches or false if it doesn't.
- */
-static int
-match (guestfs_h *g, const char *str, const pcre *re)
-{
- size_t len = strlen (str);
- int vec[30], r;
-
- r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
- if (r == PCRE_ERROR_NOMATCH)
- return 0;
- if (r != 1) {
- /* Internal error -- should not happen. */
- fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
- __FILE__, __func__, r, str);
- return 0;
+char **
+guestfs__inspect_get_roots (guestfs_h *g)
+{
+ /* NB. Doesn't matter if g->nr_fses == 0. We just return an empty
+ * list in this case.
+ */
+
+ size_t i;
+ size_t count = 0;
+ for (i = 0; i < g->nr_fses; ++i)
+ if (g->fses[i].is_root)
+ count++;
+
+ char **ret = calloc (count+1, sizeof (char *));
+ if (ret == NULL) {
+ perrorf (g, "calloc");
+ return NULL;
+ }
+
+ count = 0;
+ for (i = 0; i < g->nr_fses; ++i) {
+ if (g->fses[i].is_root) {
+ ret[count] = safe_strdup (g, g->fses[i].device);
+ count++;
+ }
}
+ ret[count] = NULL;
- return 1;
+ return ret;
}
-/* Match a regular expression which contains exactly one capture. If
- * the string matches, return the capture, otherwise return NULL. The
- * caller must free the result.
- */
-static char *
-match1 (guestfs_h *g, const char *str, const pcre *re)
+char *
+guestfs__inspect_get_type (guestfs_h *g, const char *root)
{
- size_t len = strlen (str);
- int vec[30], r;
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return NULL;
+
+ char *ret;
+ switch (fs->type) {
+ case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
+ case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
+ case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
+ case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
+ }
- r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
- if (r == PCRE_ERROR_NOMATCH)
+ return ret;
+}
+
+char *
+guestfs__inspect_get_arch (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
return NULL;
- if (r != 2) {
- /* Internal error -- should not happen. */
- fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
- __FILE__, __func__, r, str);
+
+ return safe_strdup (g, fs->arch ? : "unknown");
+}
+
+char *
+guestfs__inspect_get_distro (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
return NULL;
+
+ char *ret;
+ switch (fs->distro) {
+ case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
+ case OS_DISTRO_CENTOS: ret = safe_strdup (g, "centos"); break;
+ case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
+ case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
+ case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
+ case OS_DISTRO_LINUX_MINT: ret = safe_strdup (g, "linuxmint"); break;
+ case OS_DISTRO_MANDRIVA: ret = safe_strdup (g, "mandriva"); break;
+ case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
+ case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
+ case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
+ case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
+ case OS_DISTRO_SCIENTIFIC_LINUX: ret = safe_strdup (g, "scientificlinux"); break;
+ case OS_DISTRO_SLACKWARE: ret = safe_strdup (g, "slackware"); break;
+ case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
+ case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
+ case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
}
- return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
+ return ret;
}
-/* Convert output from 'file' command on ELF files to the canonical
- * architecture string. Caller must free the result.
- */
-static char *
-canonical_elf_arch (guestfs_h *g, const char *elf_arch)
-{
- const char *r;
-
- if (strstr (elf_arch, "Intel 80386"))
- r = "i386";
- else if (strstr (elf_arch, "Intel 80486"))
- r = "i486";
- else if (strstr (elf_arch, "x86-64"))
- r = "x86_64";
- else if (strstr (elf_arch, "AMD x86-64"))
- r = "x86_64";
- else if (strstr (elf_arch, "SPARC32"))
- r = "sparc";
- else if (strstr (elf_arch, "SPARC V9"))
- r = "sparc64";
- else if (strstr (elf_arch, "IA-64"))
- r = "ia64";
- else if (match (g, elf_arch, re_elf_ppc64))
- r = "ppc64";
- else if (strstr (elf_arch, "PowerPC"))
- r = "ppc";
- else
- r = elf_arch;
-
- char *ret = safe_strdup (g, r);
+int
+guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return -1;
+
+ return fs->major_version;
+}
+
+int
+guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return -1;
+
+ return fs->minor_version;
+}
+
+char *
+guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return NULL;
+
+ return safe_strdup (g, fs->product_name ? : "unknown");
+}
+
+char *
+guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return NULL;
+
+ return safe_strdup (g, fs->product_variant ? : "unknown");
+}
+
+char *
+guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return NULL;
+
+ if (!fs->windows_systemroot) {
+ error (g, _("not a Windows guest, or systemroot could not be determined"));
+ return NULL;
+ }
+
+ return safe_strdup (g, fs->windows_systemroot);
+}
+
+char *
+guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
+ const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return NULL;
+
+ if (!fs->windows_current_control_set) {
+ error (g, _("not a Windows guest, or CurrentControlSet could not be determined"));
+ return NULL;
+ }
+
+ return safe_strdup (g, fs->windows_current_control_set);
+}
+
+char *
+guestfs__inspect_get_format (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return NULL;
+
+ char *ret;
+ switch (fs->format) {
+ case OS_FORMAT_INSTALLED: ret = safe_strdup (g, "installed"); break;
+ case OS_FORMAT_INSTALLER: ret = safe_strdup (g, "installer"); break;
+ case OS_FORMAT_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
+ }
+
return ret;
}
-static int
-is_regular_file (const char *filename)
+int
+guestfs__inspect_is_live (guestfs_h *g, const char *root)
{
- struct stat statbuf;
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return -1;
- return lstat (filename, &statbuf) == 0 && S_ISREG (statbuf.st_mode);
+ return fs->is_live_disk;
}
-/* Download and uncompress the cpio file to find binaries within.
- * Notes:
- * (1) Two lists must be identical.
- * (2) Implicit limit of 31 bytes for length of each element (see code
- * below).
- */
-#define INITRD_BINARIES1 "bin/ls bin/rm bin/modprobe sbin/modprobe bin/sh bin/bash bin/dash bin/nash"
-#define INITRD_BINARIES2 {"bin/ls", "bin/rm", "bin/modprobe", "sbin/modprobe", "bin/sh", "bin/bash", "bin/dash", "bin/nash"}
-
-static char *
-cpio_arch (guestfs_h *g, const char *file, const char *path)
-{
- char *ret = NULL;
-
- const char *method;
- if (strstr (file, "gzip"))
- method = "zcat";
- else if (strstr (file, "bzip2"))
- method = "bzcat";
- else
- method = "cat";
-
- char dir[] = "/tmp/initrd.XXXXXX";
-#define dir_len (sizeof dir)
- if (mkdtemp (dir) == NULL) {
- perrorf (g, "mkdtemp");
- goto out;
+int
+guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return -1;
+
+ return fs->is_netinst_disk;
+}
+
+int
+guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return -1;
+
+ return fs->is_multipart_disk;
+}
+
+char **
+guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return NULL;
+
+ char **ret;
+
+ /* If no fstab information (Windows) return just the root. */
+ if (fs->nr_fstab == 0) {
+ ret = calloc (3, sizeof (char *));
+ ret[0] = safe_strdup (g, "/");
+ ret[1] = safe_strdup (g, root);
+ ret[2] = NULL;
+ return ret;
+ }
+
+#define CRITERION fs->fstab[i].mountpoint[0] == '/'
+ size_t i, count = 0;
+ for (i = 0; i < fs->nr_fstab; ++i)
+ if (CRITERION)
+ count++;
+
+ /* Hashtables have 2N+1 entries. */
+ ret = calloc (2*count+1, sizeof (char *));
+ if (ret == NULL) {
+ perrorf (g, "calloc");
+ return NULL;
+ }
+
+ count = 0;
+ for (i = 0; i < fs->nr_fstab; ++i)
+ if (CRITERION) {
+ ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
+ ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
+ count++;
+ }
+#undef CRITERION
+
+ return ret;
+}
+
+char **
+guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return NULL;
+
+ char **ret;
+
+ /* If no fstab information (Windows) return just the root. */
+ if (fs->nr_fstab == 0) {
+ ret = calloc (2, sizeof (char *));
+ ret[0] = safe_strdup (g, root);
+ ret[1] = NULL;
+ return ret;
}
- char dir_initrd[dir_len + 16];
- snprintf (dir_initrd, dir_len + 16, "%s/initrd", dir);
- if (guestfs_download (g, path, dir_initrd) == -1)
- goto out;
-
- char cmd[dir_len + 256];
- snprintf (cmd, dir_len + 256,
- "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
- dir, method);
- int r = system (cmd);
- if (r == -1 || WEXITSTATUS (r) != 0) {
- perrorf (g, "cpio command failed");
- goto out;
+ ret = calloc (fs->nr_fstab + 1, sizeof (char *));
+ if (ret == NULL) {
+ perrorf (g, "calloc");
+ return NULL;
}
- char bin[dir_len + 32];
- const char *bins[] = INITRD_BINARIES2;
size_t i;
- for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) {
- snprintf (bin, dir_len + 32, "%s/%s", dir, bins[i]);
+ for (i = 0; i < fs->nr_fstab; ++i)
+ ret[i] = safe_strdup (g, fs->fstab[i].device);
- if (is_regular_file (bin)) {
- int flags = g->verbose ? MAGIC_DEBUG : 0;
- flags |= MAGIC_ERROR | MAGIC_RAW;
+ return ret;
+}
- magic_t m = magic_open (flags);
- if (m == NULL) {
- perrorf (g, "magic_open");
- goto out;
- }
+char **
+guestfs__inspect_get_drive_mappings (guestfs_h *g, const char *root)
+{
+ char **ret;
+ size_t i, count;
+ struct inspect_fs *fs;
- if (magic_load (m, NULL) == -1) {
- perrorf (g, "magic_load: default magic database file");
- magic_close (m);
- goto out;
- }
+ fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return NULL;
- const char *line = magic_file (m, bin);
- if (line == NULL) {
- perrorf (g, "magic_file: %s", bin);
- magic_close (m);
- goto out;
- }
+ /* If no drive mappings, return an empty hashtable. */
+ if (!fs->drive_mappings)
+ count = 0;
+ else {
+ for (count = 0; fs->drive_mappings[count] != NULL; count++)
+ ;
+ }
- char *elf_arch;
- if ((elf_arch = match1 (g, line, re_file_elf)) != NULL) {
- ret = canonical_elf_arch (g, elf_arch);
- free (elf_arch);
- magic_close (m);
- goto out;
- }
- magic_close (m);
- }
+ ret = calloc (count+1, sizeof (char *));
+ if (ret == NULL) {
+ perrorf (g, "calloc");
+ return NULL;
}
- error (g, "file_architecture: could not determine architecture of cpio archive");
- out:
- /* Free up the temporary directory. Note the directory name cannot
- * contain shell meta-characters because of the way it was
- * constructed above.
+ /* We need to make a deep copy of the hashtable since the caller
+ * will free it.
*/
- snprintf (cmd, dir_len + 256, "rm -rf %s", dir);
- ignore_value (system (cmd));
+ for (i = 0; i < count; ++i)
+ ret[i] = safe_strdup (g, fs->drive_mappings[i]);
+
+ ret[count] = NULL;
return ret;
-#undef dir_len
}
char *
-guestfs__file_architecture (guestfs_h *g, const char *path)
+guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
{
- char *file = NULL;
- char *elf_arch = NULL;
- char *ret = NULL;
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return NULL;
- /* Get the output of the "file" command. Note that because this
- * runs in the daemon, LANG=C so it's in English.
- */
- file = guestfs_file (g, path);
- if (file == NULL)
+ char *ret;
+ switch (fs->package_format) {
+ case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
+ case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
+ case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
+ case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
+ case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
+ case OS_PACKAGE_FORMAT_UNKNOWN:
+ default:
+ ret = safe_strdup (g, "unknown");
+ break;
+ }
+
+ return ret;
+}
+
+char *
+guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
+ return NULL;
+
+ char *ret;
+ switch (fs->package_management) {
+ case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
+ case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
+ case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
+ case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
+ case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
+ case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
+ case OS_PACKAGE_MANAGEMENT_URPMI: ret = safe_strdup (g, "urpmi"); break;
+ case OS_PACKAGE_MANAGEMENT_UNKNOWN:
+ default:
+ ret = safe_strdup (g, "unknown");
+ break;
+ }
+
+ return ret;
+}
+
+char *
+guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
+{
+ struct inspect_fs *fs = guestfs___search_for_root (g, root);
+ if (!fs)
return NULL;
- if ((elf_arch = match1 (g, file, re_file_elf)) != NULL)
- ret = canonical_elf_arch (g, elf_arch);
- else if (strstr (file, "PE32 executable"))
- ret = safe_strdup (g, "i386");
- else if (strstr (file, "PE32+ executable"))
- ret = safe_strdup (g, "x86_64");
- else if (strstr (file, "cpio archive"))
- ret = cpio_arch (g, file, path);
- else
- error (g, "file_architecture: unknown architecture: %s", path);
-
- free (file);
- free (elf_arch);
- return ret; /* caller frees */
+ return safe_strdup (g, fs->hostname ? : "unknown");
+}
+
+/* Download a guest file to a local temporary file. The file is
+ * cached in the temporary directory, and is not downloaded again.
+ *
+ * The name of the temporary (downloaded) file is returned. The
+ * caller must free the pointer, but does *not* need to delete the
+ * temporary file. It will be deleted when the handle is closed.
+ *
+ * Refuse to download the guest file if it is larger than max_size.
+ * On this and other errors, NULL is returned.
+ *
+ * There is actually one cache per 'struct inspect_fs *' in order
+ * to handle the case of multiple roots.
+ */
+char *
+guestfs___download_to_tmp (guestfs_h *g, struct inspect_fs *fs,
+ const char *filename,
+ const char *basename, int64_t max_size)
+{
+ char *r;
+ int fd;
+ char devfd[32];
+ int64_t size;
+
+ /* Make the basename unique by prefixing it with the fs number. */
+ if (asprintf (&r, "%s/%ld-%s", g->tmpdir, fs - g->fses, basename) == -1) {
+ perrorf (g, "asprintf");
+ return NULL;
+ }
+
+ /* If the file has already been downloaded, return. */
+ if (access (r, R_OK) == 0)
+ return r;
+
+ /* Check size of remote file. */
+ size = guestfs_filesize (g, filename);
+ if (size == -1)
+ /* guestfs_filesize failed and has already set error in handle */
+ goto error;
+ if (size > max_size) {
+ error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
+ filename, size);
+ goto error;
+ }
+
+ fd = open (r, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0600);
+ if (fd == -1) {
+ perrorf (g, "open: %s", r);
+ goto error;
+ }
+
+ snprintf (devfd, sizeof devfd, "/dev/fd/%d", fd);
+
+ if (guestfs_download (g, filename, devfd) == -1) {
+ unlink (r);
+ close (fd);
+ goto error;
+ }
+
+ if (close (fd) == -1) {
+ perrorf (g, "close: %s", r);
+ unlink (r);
+ goto error;
+ }
+
+ return r;
+
+ error:
+ free (r);
+ return NULL;
+}
+
+struct inspect_fs *
+guestfs___search_for_root (guestfs_h *g, const char *root)
+{
+ if (g->nr_fses == 0) {
+ error (g, _("no inspection data: call guestfs_inspect_os first"));
+ return NULL;
+ }
+
+ size_t i;
+ struct inspect_fs *fs;
+ for (i = 0; i < g->nr_fses; ++i) {
+ fs = &g->fses[i];
+ if (fs->is_root && STREQ (root, fs->device))
+ return fs;
+ }
+
+ error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
+ root);
+ return NULL;
+}
+
+#else /* no hivex at compile time */
+
+/* XXX These functions should be in an optgroup. */
+
+#define NOT_IMPL(r) \
+ error (g, _("inspection API not available since this version of libguestfs was compiled without the hivex library")); \
+ return r
+
+char **
+guestfs__inspect_os (guestfs_h *g)
+{
+ NOT_IMPL(NULL);
+}
+
+char **
+guestfs__inspect_get_roots (guestfs_h *g)
+{
+ NOT_IMPL(NULL);
+}
+
+char *
+guestfs__inspect_get_type (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char *
+guestfs__inspect_get_arch (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char *
+guestfs__inspect_get_distro (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+int
+guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(-1);
+}
+
+int
+guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(-1);
+}
+
+char *
+guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char *
+guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char *
+guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char *
+guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
+ const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char **
+guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char **
+guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char **
+guestfs__inspect_get_drive_mappings (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char *
+guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char *
+guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char *
+guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+char *
+guestfs__inspect_get_format (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(NULL);
+}
+
+int
+guestfs__inspect_is_live (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(-1);
+}
+
+int
+guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(-1);
+}
+
+int
+guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
+{
+ NOT_IMPL(-1);
+}
+
+#endif /* no hivex at compile time */
+
+void
+guestfs___free_inspect_info (guestfs_h *g)
+{
+ size_t i;
+ for (i = 0; i < g->nr_fses; ++i) {
+ free (g->fses[i].device);
+ free (g->fses[i].product_name);
+ free (g->fses[i].product_variant);
+ free (g->fses[i].arch);
+ free (g->fses[i].hostname);
+ free (g->fses[i].windows_systemroot);
+ free (g->fses[i].windows_current_control_set);
+ size_t j;
+ for (j = 0; j < g->fses[i].nr_fstab; ++j) {
+ free (g->fses[i].fstab[j].device);
+ free (g->fses[i].fstab[j].mountpoint);
+ }
+ free (g->fses[i].fstab);
+ if (g->fses[i].drive_mappings)
+ guestfs___free_string_list (g->fses[i].drive_mappings);
+ }
+ free (g->fses);
+ g->nr_fses = 0;
+ g->fses = NULL;
+}
+
+/* In the Perl code this is a public function. */
+int
+guestfs___feature_available (guestfs_h *g, const char *feature)
+{
+ /* If there's an error we should ignore it, so to do that we have to
+ * temporarily replace the error handler with a null one.
+ */
+ guestfs_error_handler_cb old_error_cb = g->error_cb;
+ g->error_cb = NULL;
+
+ const char *groups[] = { feature, NULL };
+ int r = guestfs_available (g, (char * const *) groups);
+
+ g->error_cb = old_error_cb;
+
+ return r == 0 ? 1 : 0;
}