+/* 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 > 100000) {
+ 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 > 1000000) {
+ 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;
+}
+