static int read_osinfo_db_directory (guestfs_h *g, const char *directory) { DIR *dir; int r; dir = opendir (directory); if (!dir) { debug (g, "osinfo: %s: %s", directory, strerror (errno)); return 0; /* This is not an error: RHBZ#948324. */ } for (;;) { struct dirent *d; errno = 0; d = readdir (dir); if (!d) break; if (STRSUFFIX (d->d_name, ".xml")) { CLEANUP_FREE char *pathname = NULL; pathname = safe_asprintf (g, "%s/%s", directory, d->d_name); r = read_osinfo_db_xml (g, pathname); if (r == -1) goto error; } } /* Check for failure in readdir. */ if (errno != 0) { perrorf (g, "readdir: %s", directory); goto error; } /* Close the directory handle. */ r = closedir (dir); dir = NULL; if (r == -1) { perrorf (g, "closedir: %s", directory); goto error; } return 1; error: if (dir) closedir (dir); return -1; }
static int read_osinfo_db (guestfs_h *g) { DIR *dir = NULL; struct dirent *d; int r; size_t i; assert (osinfo_db_size == 0); dir = opendir (LIBOSINFO_DB_OS_PATH); if (!dir) { debug (g, "osinfo: %s: %s", LIBOSINFO_DB_OS_PATH, strerror (errno)); return 0; /* This is not an error: RHBZ#948324. */ } debug (g, "osinfo: loading database from %s", LIBOSINFO_DB_OS_PATH); for (;;) { errno = 0; d = readdir (dir); if (!d) break; if (STRSUFFIX (d->d_name, ".xml")) { r = read_osinfo_db_xml (g, d->d_name); if (r == -1) goto error; } } /* Check for failure in readdir. */ if (errno != 0) { perrorf (g, "readdir: %s", LIBOSINFO_DB_OS_PATH); goto error; } /* Close the directory handle. */ r = closedir (dir); dir = NULL; if (r == -1) { perrorf (g, "closedir: %s", LIBOSINFO_DB_OS_PATH); goto error; } return 0; error: if (dir) closedir (dir); /* Fatal error: free any database entries which have been read, and * mark the database as having a permanent error. */ if (osinfo_db_size > 0) { for (i = 0; i < (size_t) osinfo_db_size; ++i) free_osinfo_db_entry (&osinfo_db[i]); } free (osinfo_db); osinfo_db = NULL; osinfo_db_size = -1; return -1; }