예제 #1
0
char *
guestfs__file_architecture (guestfs_h *g, const char *path)
{
  CLEANUP_FREE char *file = NULL;
  CLEANUP_FREE char *elf_arch = NULL;
  char *ret = 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)
    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);

  return ret;                   /* caller frees */
}
예제 #2
0
static char *
icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
{
  char *ret = NOT_FOUND;
  char *type = NULL;
  char *local = NULL;
  char *pngfile = NULL;
  struct command *cmd;
  int r;

  r = guestfs_exists (g, CIRROS_LOGO);
  if (r == -1) {
    ret = NULL; /* a real error */
    goto out;
  }
  if (r == 0) goto out;

  /* Check the file type and geometry. */
  type = guestfs_file (g, CIRROS_LOGO);
  if (!type) goto out;

  if (!STRPREFIX (type, "ASCII text")) goto out;

  local = guestfs___download_to_tmp (g, fs, CIRROS_LOGO, "icon", 1024);
  if (!local) goto out;

  /* Use pbmtext to render it. */
  pngfile = safe_asprintf (g, "%s/cirros.png", g->tmpdir);

  cmd = guestfs___new_command (g);
  guestfs___cmd_add_string_unquoted (cmd, PBMTEXT " < ");
  guestfs___cmd_add_string_quoted   (cmd, local);
  guestfs___cmd_add_string_unquoted (cmd, " | " PNMTOPNG " > ");
  guestfs___cmd_add_string_quoted   (cmd, pngfile);
  r = guestfs___cmd_run (cmd);
  guestfs___cmd_close (cmd);
  if (r == -1)
    goto out;
  if (!WIFEXITED (r) || WEXITSTATUS (r) != 0)
    goto out;

  /* Read it into memory. */
  if (read_whole_file (g, pngfile, &ret, size_r) == -1) {
    ret = NULL;
    goto out;
  }

 out:
  free (pngfile);
  free (local);
  free (type);

  return ret;
}
/* Check that the named file 'filename' is a PNG file and is reasonable.
 * If it is, download and return it.
 */
static char *
get_png (guestfs_h *g, struct inspect_fs *fs, const char *filename,
         size_t *size_r, size_t max_size)
{
  char *ret;
  CLEANUP_FREE char *real = NULL;
  CLEANUP_FREE char *type = NULL;
  CLEANUP_FREE char *local = NULL;
  int r, w, h;

  r = guestfs_is_file_opts (g, filename,
                            GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1);
  if (r == -1)
    return NULL; /* a real error */
  if (r == 0)
    return NOT_FOUND;

  /* Resolve the path, in case it's a symbolic link (as in RHEL 7). */
  guestfs_push_error_handler (g, NULL, NULL);
  real = guestfs_realpath (g, filename);
  guestfs_pop_error_handler (g);
  if (real == NULL)
    return NOT_FOUND; /* could just be a broken link */

  /* Check the file type and geometry. */
  type = guestfs_file (g, real);
  if (!type)
    return NOT_FOUND;

  if (!STRPREFIX (type, "PNG image data, "))
    return NOT_FOUND;
  if (sscanf (&type[16], "%d x %d", &w, &h) != 2)
    return NOT_FOUND;
  if (w < 16 || h < 16 || w > 1024 || h > 1024)
    return NOT_FOUND;

  /* Define a maximum reasonable size based on the geometry.  This
   * also limits the maximum we allocate below to around 4 MB.
   */
  if (max_size == 0)
    max_size = 4 * w * h;

  local = guestfs___download_to_tmp (g, fs, real, "icon", max_size);
  if (!local)
    return NOT_FOUND;

  /* Successfully passed checks and downloaded.  Read it into memory. */
  if (read_whole_file (g, local, &ret, size_r) == -1)
    return NULL;

  return ret;
}
예제 #4
0
static char *
icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
{
  char *ret;
  CLEANUP_FREE char *type = NULL;
  CLEANUP_FREE char *local = NULL;
  CLEANUP_FREE char *pngfile = NULL;
  CLEANUP_CMD_CLOSE struct command *cmd = guestfs___new_command (g);
  int r;

  r = guestfs_is_file_opts (g, CIRROS_LOGO,
                            GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1);
  if (r == -1)
    return NULL; /* a real error */
  if (r == 0)
    return NOT_FOUND;

  /* Check the file type and geometry. */
  type = guestfs_file (g, CIRROS_LOGO);
  if (!type)
    return NOT_FOUND;

  if (!STRPREFIX (type, "ASCII text"))
    return NOT_FOUND;

  local = guestfs___download_to_tmp (g, fs, CIRROS_LOGO, "icon", 1024);
  if (!local)
    return NOT_FOUND;

  /* Use pbmtext to render it. */
  pngfile = safe_asprintf (g, "%s/cirros.png", g->tmpdir);

  guestfs___cmd_add_string_unquoted (cmd, PBMTEXT " < ");
  guestfs___cmd_add_string_quoted   (cmd, local);
  guestfs___cmd_add_string_unquoted (cmd, " | " PNMTOPNG " > ");
  guestfs___cmd_add_string_quoted   (cmd, pngfile);
  r = guestfs___cmd_run (cmd);
  if (r == -1)
    return NOT_FOUND;
  if (!WIFEXITED (r) || WEXITSTATUS (r) != 0)
    return NOT_FOUND;

  /* Read it into memory. */
  if (read_whole_file (g, pngfile, &ret, size_r) == -1)
    return NULL;

  return ret;
}
예제 #5
0
/* Check that the named file 'filename' is a PNG file and is reasonable.
 * If it is, download and return it.
 */
static char *
get_png (guestfs_h *g, struct inspect_fs *fs, const char *filename,
         size_t *size_r, size_t max_size)
{
  char *ret = NOT_FOUND;
  char *type = NULL;
  char *local = NULL;
  int r, w, h;

  r = guestfs_exists (g, filename);
  if (r == -1) {
    ret = NULL; /* a real error */
    goto out;
  }
  if (r == 0) goto out;

  /* Check the file type and geometry. */
  type = guestfs_file (g, filename);
  if (!type) goto out;

  if (!STRPREFIX (type, "PNG image data, ")) goto out;
  if (sscanf (&type[16], "%d x %d", &w, &h) != 2) goto out;
  if (w < 16 || h < 16 || w > 1024 || h > 1024) goto out;

  /* Define a maximum reasonable size based on the geometry.  This
   * also limits the maximum we allocate below to around 4 MB.
   */
  if (max_size == 0)
    max_size = 4 * w * h;

  local = guestfs___download_to_tmp (g, fs, filename, "icon", max_size);
  if (!local) goto out;

  /* Successfully passed checks and downloaded.  Read it into memory. */
  if (read_whole_file (g, local, &ret, size_r) == -1) {
    ret = NULL;
    goto out;
  }

 out:
  free (local);
  free (type);

  return ret;
}