Added file(1) command.

This commit is contained in:
Richard Jones
2009-04-13 23:58:02 +01:00
parent a29a2e99ce
commit 4228340388
3 changed files with 69 additions and 1 deletions

View File

@@ -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 */
}

View File

@@ -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"

View File

@@ -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