+
+/* Download to a guest file to a local temporary file. Refuse to
+ * download the guest file if it is larger than max_size. The caller
+ * is responsible for deleting the temporary file after use.
+ */
+static int
+download_to_tmp (guestfs_h *g, const char *filename,
+ char *localtmp, int64_t max_size)
+{
+ int fd;
+ char buf[32];
+ int64_t size;
+
+ size = guestfs_filesize (g, filename);
+ if (size == -1)
+ /* guestfs_filesize failed and has already set error in handle */
+ return -1;
+ if (size > max_size) {
+ error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
+ filename, size);
+ return -1;
+ }
+
+ fd = mkstemp (localtmp);
+ if (fd == -1) {
+ perrorf (g, "mkstemp");
+ return -1;
+ }
+
+ snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
+
+ if (guestfs_download (g, filename, buf) == -1) {
+ close (fd);
+ unlink (localtmp);
+ return -1;
+ }
+
+ if (close (fd) == -1) {
+ perrorf (g, "close: %s", localtmp);
+ unlink (localtmp);
+ return -1;
+ }
+
+ return 0;
+}
+
+/* Call 'f' with Augeas opened and having parsed 'filename' (this file
+ * must exist). As a security measure, this bails if the file is too
+ * large for a reasonable configuration file. After the call to 'f'
+ * Augeas is closed.
+ */
+static int
+inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename,
+ int (*f) (guestfs_h *, struct inspect_fs *))
+{
+ /* Security: Refuse to do this if filename is too large. */
+ int64_t size = guestfs_filesize (g, filename);
+ if (size == -1)
+ /* guestfs_filesize failed and has already set error in handle */
+ return -1;
+ if (size > MAX_AUGEAS_FILE_SIZE) {
+ error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
+ filename, size);
+ return -1;
+ }
+
+ /* If !feature_available (g, "augeas") then the next call will fail.
+ * Arguably we might want to fall back to a non-Augeas method in
+ * this case.
+ */
+ if (guestfs_aug_init (g, "/", 16|32) == -1)
+ return -1;
+
+ int r = -1;
+
+ /* Tell Augeas to only load one file (thanks Raphaƫl Pinson). */
+ char buf[strlen (filename) + 64];
+ snprintf (buf, strlen (filename) + 64, "/augeas/load//incl[. != \"%s\"]",
+ filename);
+ if (guestfs_aug_rm (g, buf) == -1)
+ goto out;
+
+ if (guestfs_aug_load (g) == -1)
+ goto out;
+
+ r = f (g, fs);
+
+ out:
+ guestfs_aug_close (g);
+
+ return r;
+}
+
+/* Get the first line of a small file, without any trailing newline
+ * character.
+ */
+static char *
+first_line_of_file (guestfs_h *g, const char *filename)
+{
+ char **lines;
+ int64_t size;
+ char *ret;
+
+ /* Don't trust guestfs_head_n not to break with very large files.
+ * Check the file size is something reasonable first.
+ */
+ size = guestfs_filesize (g, filename);
+ if (size == -1)
+ /* guestfs_filesize failed and has already set error in handle */
+ return NULL;
+ if (size > MAX_SMALL_FILE_SIZE) {
+ error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
+ filename, size);
+ return NULL;
+ }
+
+ lines = guestfs_head_n (g, 1, filename);
+ if (lines == NULL)
+ return NULL;
+ if (lines[0] == NULL) {
+ error (g, _("%s: file is empty"), filename);
+ guestfs___free_string_list (lines);
+ return NULL;
+ }
+ /* lines[1] should be NULL because of '1' argument above ... */
+
+ ret = lines[0]; /* caller frees */
+ free (lines); /* free the array */
+
+ return ret;
+}
+
+#else /* no PCRE or 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 PCRE or hivex libraries")); \
+ 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_windows_systemroot (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_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);
+}
+
+struct guestfs_application_list *
+guestfs__inspect_list_applications (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 PCRE or 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].arch);
+ free (g->fses[i].hostname);
+ free (g->fses[i].windows_systemroot);
+ 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);
+ }
+ 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;
+}
+
+#ifdef HAVE_PCRE
+
+/* Match a regular expression which contains no captures. Returns
+ * true if it matches or false if it doesn't.
+ */
+int
+guestfs___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;
+ }
+
+ return 1;
+}
+
+/* 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.
+ */
+char *
+guestfs___match1 (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 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 NULL;
+ }
+
+ return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
+}
+
+/* Match a regular expression which contains exactly two captures. */
+int
+guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
+ char **ret1, char **ret2)
+{
+ size_t len = strlen (str);
+ int vec[30], r;
+
+ r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
+ if (r == PCRE_ERROR_NOMATCH)
+ return 0;
+ if (r != 3) {
+ /* 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;
+ }
+
+ *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
+ *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
+
+ return 1;
+}
+
+/* Match a regular expression which contains exactly three captures. */
+int
+guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
+ char **ret1, char **ret2, char **ret3)
+{
+ size_t len = strlen (str);
+ int vec[30], r;
+
+ r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
+ if (r == PCRE_ERROR_NOMATCH)
+ return 0;
+ if (r != 4) {
+ /* 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;
+ }
+
+ *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
+ *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
+ *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
+
+ return 1;
+}
+
+#endif /* HAVE_PCRE */