summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--daemon/file.c49
-rwxr-xr-xmake-initramfs.sh.in2
-rwxr-xr-xsrc/generator.ml19
3 files changed, 69 insertions, 1 deletions
diff --git a/daemon/file.c b/daemon/file.c
index 0b8b463c..d8759f0d 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 3dbac635..612db905 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 84ee90ff..c9da57e9 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<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