diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-05-24 15:40:36 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-06-01 19:25:49 +0100 |
commit | f697f9ea97ee3e3aa73b3d4d79643b892afb0603 (patch) | |
tree | 9db2199431f0e54be152172516833a7cd144393e | |
parent | cdbb3008ccfd21003df635c3d522051c02f19e15 (diff) | |
download | libguestfs-f697f9ea97ee3e3aa73b3d4d79643b892afb0603.tar.gz libguestfs-f697f9ea97ee3e3aa73b3d4d79643b892afb0603.tar.xz libguestfs-f697f9ea97ee3e3aa73b3d4d79643b892afb0603.zip |
inspection: Don't fail if /etc/HOSTNAME or /etc/hostname are empty files (RHBZ#823821).
Change guestfs___first_line_of_file so that if the file is empty this
returns an empty string instead of an error. This is consistent with
the behaviour of this function in the case where the file starts with
a \n character, where it would previously have returned an empty
string.
Change all callers so that they handle this case.
Then change the hostname parsing code so that it doesn't give up when
/etc/HOSTNAME is empty, but falls through to the next alternative, and
similarly for /etc/hostname.
Thanks Todd Mummert for finding and diagnosing this bug.
(cherry picked from commit f00066d22b11bf40d0272f68565a2a27fea15627)
-rw-r--r-- | src/inspect_fs.c | 8 | ||||
-rw-r--r-- | src/inspect_fs_unix.c | 20 |
2 files changed, 24 insertions, 4 deletions
diff --git a/src/inspect_fs.c b/src/inspect_fs.c index baae5428..0859a2b4 100644 --- a/src/inspect_fs.c +++ b/src/inspect_fs.c @@ -507,6 +507,10 @@ check_package_management (guestfs_h *g, struct inspect_fs *fs) /* Get the first line of a small file, without any trailing newline * character. + * + * NOTE: If the file is completely empty or begins with a '\n' + * character, this returns an empty string (not NULL). The caller + * will usually need to check for this case. */ char * guestfs___first_line_of_file (guestfs_h *g, const char *filename) @@ -532,9 +536,9 @@ guestfs___first_line_of_file (guestfs_h *g, const char *filename) if (lines == NULL) return NULL; if (lines[0] == NULL) { - error (g, _("%s: file is empty"), filename); guestfs___free_string_list (lines); - return NULL; + /* Empty file: Return an empty string as explained above. */ + return safe_strdup (g, ""); } /* lines[1] should be NULL because of '1' argument above ... */ diff --git a/src/inspect_fs_unix.c b/src/inspect_fs_unix.c index 2caf4002..06936c23 100644 --- a/src/inspect_fs_unix.c +++ b/src/inspect_fs_unix.c @@ -181,6 +181,12 @@ parse_release_file (guestfs_h *g, struct inspect_fs *fs, fs->product_name = guestfs___first_line_of_file (g, release_filename); if (fs->product_name == NULL) return -1; + if (STREQ (fs->product_name, "")) { + free (fs->product_name); + fs->product_name = NULL; + error (g, _("release file %s is empty or malformed"), release_filename); + return -1; + } return 0; } @@ -659,13 +665,23 @@ check_hostname_unix (guestfs_h *g, struct inspect_fs *fs) fs->hostname = guestfs___first_line_of_file (g, "/etc/HOSTNAME"); if (fs->hostname == NULL) return -1; + if (STREQ (fs->hostname, "")) { + free (fs->hostname); + fs->hostname = NULL; + } } - else if (guestfs_is_file (g, "/etc/hostname")) { + + if (!fs->hostname && guestfs_is_file (g, "/etc/hostname")) { fs->hostname = guestfs___first_line_of_file (g, "/etc/hostname"); if (fs->hostname == NULL) return -1; + if (STREQ (fs->hostname, "")) { + free (fs->hostname); + fs->hostname = NULL; + } } - else if (guestfs_is_file (g, "/etc/sysconfig/network")) { + + if (!fs->hostname && guestfs_is_file (g, "/etc/sysconfig/network")) { const char *configfiles[] = { "/etc/sysconfig/network", NULL }; if (inspect_with_augeas (g, fs, configfiles, check_hostname_redhat) == -1) |