diff options
author | Richard Jones <rjones@trick.home.annexia.org> | 2009-07-13 18:00:07 +0100 |
---|---|---|
committer | Richard Jones <rjones@trick.home.annexia.org> | 2009-07-13 18:00:07 +0100 |
commit | 7d41d75c1d4e6dbe61f6cd353bc104663340483f (patch) | |
tree | 2b853abebb6d4fa6a5cf818f2c97b18cfd143c64 /daemon/file.c | |
parent | f9ecb943be086087feeeaeb49bd8865fd5e54545 (diff) | |
download | libguestfs-7d41d75c1d4e6dbe61f6cd353bc104663340483f.tar.gz libguestfs-7d41d75c1d4e6dbe61f6cd353bc104663340483f.tar.xz libguestfs-7d41d75c1d4e6dbe61f6cd353bc104663340483f.zip |
Implement new 'zfile' command, to show file type inside compressed files.
Diffstat (limited to 'daemon/file.c')
-rw-r--r-- | daemon/file.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/daemon/file.c b/daemon/file.c index 3ef7441d..98c356df 100644 --- a/daemon/file.c +++ b/daemon/file.c @@ -376,3 +376,63 @@ do_file (char *path) return out; /* caller frees */ } + +/* zcat | file */ +char * +do_zfile (char *method, char *path) +{ + int len; + char *cmd; + FILE *fp; + char line[256]; + + NEED_ROOT (NULL); + ABS_PATH (path, NULL); + + len = 2 * strlen (path) + 64; + cmd = malloc (len); + if (!cmd) { + reply_with_perror ("malloc"); + return NULL; + } + + if (strcmp (method, "gzip") == 0 || strcmp (method, "compress") == 0) + strcpy (cmd, "zcat"); + else if (strcmp (method, "bzip2") == 0) + strcpy (cmd, "bzcat"); + else { + free (cmd); + reply_with_error ("zfile: unknown method"); + return NULL; + } + + strcat (cmd, " /sysroot"); + shell_quote (cmd + strlen (cmd), len - strlen (cmd), path); + strcat (cmd, " | file -bsL -"); + + fp = popen (cmd, "r"); + if (fp == NULL) { + reply_with_perror ("%s", cmd); + free (cmd); + return NULL; + } + + free (cmd); + + if (fgets (line, sizeof line, fp) == NULL) { + reply_with_perror ("zfile: fgets"); + fclose (fp); + return NULL; + } + + if (fclose (fp) == -1) { + reply_with_perror ("zfile: fclose"); + return NULL; + } + + len = strlen (line); + if (len > 0 && line[len-1] == '\n') + line[len-1] = '\0'; + + return strdup (line); +} |