mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-22 07:03:38 +00:00
Added file(1) command.
This commit is contained in:
@@ -314,3 +314,52 @@ do_write_file (const char *path, const char *content, int size)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* This runs the 'file' command. */
|
||||
char *
|
||||
do_file (const char *path)
|
||||
{
|
||||
char *out, *err;
|
||||
int r, len;
|
||||
char *buf;
|
||||
|
||||
NEED_ROOT (NULL);
|
||||
ABS_PATH (path, NULL);
|
||||
|
||||
len = strlen (path) + 9;
|
||||
buf = malloc (len);
|
||||
if (!buf) {
|
||||
reply_with_perror ("malloc");
|
||||
return NULL;
|
||||
}
|
||||
snprintf (buf, len, "/sysroot%s", path);
|
||||
|
||||
/* file(1) manpage claims "file returns 0 on success, and non-zero on
|
||||
* error", but this is evidently not true. It always returns 0, in
|
||||
* every scenario I can think up. So check the target is readable
|
||||
* first.
|
||||
*/
|
||||
if (access (buf, R_OK) == -1) {
|
||||
free (buf);
|
||||
reply_with_perror ("access: %s", path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
r = command (&out, &err, "file", "-bsL", buf, NULL);
|
||||
free (buf);
|
||||
|
||||
if (r == -1) {
|
||||
free (out);
|
||||
reply_with_error ("file: %s: %s", path, err);
|
||||
free (err);
|
||||
return NULL;
|
||||
}
|
||||
free (err);
|
||||
|
||||
/* We need to remove the trailing \n from output of file(1). */
|
||||
len = strlen (out);
|
||||
if (out[len-1] == '\n')
|
||||
out[len-1] = '\0';
|
||||
|
||||
return out; /* caller frees */
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ set -e
|
||||
# larger.
|
||||
debug=yes
|
||||
|
||||
modules="-i kernel -i bash -i coreutils -i lvm2 -i ntfs-3g -i util-linux-ng -i MAKEDEV -i net-tools -i augeas-libs"
|
||||
modules="-i kernel -i bash -i coreutils -i lvm2 -i ntfs-3g -i util-linux-ng -i MAKEDEV -i net-tools -i augeas-libs -i file"
|
||||
|
||||
if [ "x$debug" = "xyes" ]; then
|
||||
modules="$modules -i module-init-tools -i procps -i strace -i iputils"
|
||||
|
||||
@@ -950,6 +950,25 @@ Some internal mounts are not unmounted by this call.");
|
||||
This command removes all LVM logical volumes, volume groups
|
||||
and physical volumes.");
|
||||
|
||||
("file", (RString "description", [String "path"]), 49, [],
|
||||
[InitBasicFS, TestOutput (
|
||||
[["touch"; "/new"];
|
||||
["file"; "/new"]], "empty");
|
||||
InitBasicFS, TestOutput (
|
||||
[["write_file"; "/new"; "some content\n"; "0"];
|
||||
["file"; "/new"]], "ASCII text");
|
||||
InitBasicFS, TestLastFail (
|
||||
[["file"; "/nofile"]])],
|
||||
"determine file type",
|
||||
"\
|
||||
This call uses the standard L<file(1)> command to determine
|
||||
the type or contents of the file. This also works on devices,
|
||||
for example to find out whether a partition contains a filesystem.
|
||||
|
||||
The exact command which runs is C<file -bsL path>. Note in
|
||||
particular that the filename is not prepended to the output
|
||||
(the C<-b> option).");
|
||||
|
||||
]
|
||||
|
||||
let all_functions = non_daemon_functions @ daemon_functions
|
||||
|
||||
Reference in New Issue
Block a user