From 42283403886da648bb239177369aa65c0a659255 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Mon, 13 Apr 2009 23:58:02 +0100 Subject: [PATCH] Added file(1) command. --- daemon/file.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ make-initramfs.sh.in | 2 +- src/generator.ml | 19 +++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/daemon/file.c b/daemon/file.c index 0b8b463..d8759f0 100644 --- a/daemon/file.c +++ b/daemon/file.c @@ -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 */ +} diff --git a/make-initramfs.sh.in b/make-initramfs.sh.in index 3dbac63..612db90 100755 --- a/make-initramfs.sh.in +++ b/make-initramfs.sh.in @@ -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" diff --git a/src/generator.ml b/src/generator.ml index 84ee90f..c9da57e 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -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 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. Note in +particular that the filename is not prepended to the output +(the C<-b> option)."); + ] let all_functions = non_daemon_functions @ daemon_functions -- 1.8.3.1