Added file(1) command.
authorRichard Jones <rjones@redhat.com>
Mon, 13 Apr 2009 22:58:02 +0000 (23:58 +0100)
committerRichard Jones <rjones@redhat.com>
Mon, 13 Apr 2009 22:58:02 +0000 (23:58 +0100)
daemon/file.c
make-initramfs.sh.in
src/generator.ml

index 0b8b463..d8759f0 100644 (file)
@@ -314,3 +314,52 @@ do_write_file (const char *path, const char *content, int size)
 
   return 0;
 }
 
   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 */
+}
index 3dbac63..612db90 100755 (executable)
@@ -25,7 +25,7 @@ set -e
 # larger.
 debug=yes
 
 # 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"
 
 if [ "x$debug" = "xyes" ]; then
     modules="$modules -i module-init-tools -i procps -i strace -i iputils"
index 84ee90f..c9da57e 100755 (executable)
@@ -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.");
 
 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
 ]
 
 let all_functions = non_daemon_functions @ daemon_functions