[Libguestfs] [PATCH] Add 'filesize' call.

Richard W.M. Jones rjones at redhat.com
Mon Jan 25 11:52:35 UTC 2010


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://et.redhat.com/~rjones/libguestfs/
See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html
-------------- next part --------------
>From 306077ac965cde0a5605782921d33a72bb77c382 Mon Sep 17 00:00:00 2001
From: Richard Jones <rjones at redhat.com>
Date: Mon, 25 Jan 2010 11:34:56 +0000
Subject: [PATCH] Add 'filesize' call.

Returns the size of a file.  You can already do this with 'stat',
but this call is good for scripting.
---
 daemon/file.c    |   18 ++++++++++++++++++
 src/MAX_PROC_NR  |    2 +-
 src/generator.ml |   12 ++++++++++++
 3 files changed, 31 insertions(+), 1 deletions(-)

diff --git a/daemon/file.c b/daemon/file.c
index 0b50eeb..839713f 100644
--- a/daemon/file.c
+++ b/daemon/file.c
@@ -520,3 +520,21 @@ do_zfile (const char *method, const char *path)
 
   return strdup (line);
 }
+
+int64_t
+do_filesize (const char *path)
+{
+  int r;
+  struct stat buf;
+
+  CHROOT_IN;
+  r = stat (path, &buf);        /* follow symlinks */
+  CHROOT_OUT;
+
+  if (r == -1) {
+    reply_with_perror ("filesize: %s", path);
+    return -1;
+  }
+
+  return buf.st_size;
+}
diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR
index 0ddd619..dc6f4a8 100644
--- a/src/MAX_PROC_NR
+++ b/src/MAX_PROC_NR
@@ -1 +1 @@
-217
+218
diff --git a/src/generator.ml b/src/generator.ml
index 4bf4b0f..e55b2b5 100755
--- a/src/generator.ml
+++ b/src/generator.ml
@@ -4188,6 +4188,18 @@ If the destination is a device, it must be as large or larger
 than the source file or device, otherwise the copy will fail.
 This command cannot do partial copies.");
 
+  ("filesize", (RInt64 "size", [Pathname "file"]), 218, [],
+   [InitBasicFS, Always, TestOutputInt (
+      [["write_file"; "/file"; "hello, world"; "0"];
+       ["filesize"; "/file"]], 12)],
+   "return the size of the file in bytes",
+   "\
+This command returns the size of C<file> in bytes.
+
+To get other stats about a file, use C<guestfs_stat>, C<guestfs_lstat>,
+C<guestfs_is_dir>, C<guestfs_is_file> etc.
+To get the size of block devices, use C<guestfs_blockdev_getsize64>.");
+
 ]
 
 let all_functions = non_daemon_functions @ daemon_functions
-- 
1.6.5.2



More information about the Libguestfs mailing list