diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2009-07-01 20:56:58 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2009-07-02 10:11:55 +0100 |
commit | 5186251f8f681f2ebb028423bb49a748861fd11e (patch) | |
tree | b95ea92a8ed1b9443dc04aaf7cdacc8191291bc0 /perl | |
parent | f20854ec61eef1aea313920f0cf193a78c1a9219 (diff) | |
download | libguestfs-5186251f8f681f2ebb028423bb49a748861fd11e.tar.gz libguestfs-5186251f8f681f2ebb028423bb49a748861fd11e.tar.xz libguestfs-5186251f8f681f2ebb028423bb49a748861fd11e.zip |
Add 'readdir' call.
This adds a readdir call (mostly intended for programs). The
return value is a list of guestfs_dirent structures.
This adds the new types 'struct guestfs_dirent' and
'struct guestfs_dirent_list', along with all the code to
return these in the different language bindings.
Also includes additional tests for OCaml and Perl bindings
to test this.
Diffstat (limited to 'perl')
-rw-r--r-- | perl/Guestfs.xs | 40 | ||||
-rw-r--r-- | perl/lib/Sys/Guestfs.pm | 12 | ||||
-rw-r--r-- | perl/t/060-readdir.t | 63 |
3 files changed, 106 insertions, 9 deletions
diff --git a/perl/Guestfs.xs b/perl/Guestfs.xs index 8f4ab5f3..e1cd84a3 100644 --- a/perl/Guestfs.xs +++ b/perl/Guestfs.xs @@ -356,7 +356,7 @@ PREINIT: (void) hv_store (hv, "pe_start", 8, my_newSVull (valout->val[i].pe_start), 0); (void) hv_store (hv, "pv_mda_count", 12, my_newSVll (valout->val[i].pv_mda_count), 0); (void) hv_store (hv, "pv_mda_free", 11, my_newSVull (valout->val[i].pv_mda_free), 0); - PUSHs (sv_2mortal ((SV *) hv)); + PUSHs (sv_2mortal (newRV ((SV *) hv))); } guestfs_free_lvm_pv_list (valout); @@ -388,7 +388,7 @@ PREINIT: (void) hv_store (hv, "pe_start", 8, my_newSVull (valout->val[i].pe_start), 0); (void) hv_store (hv, "pv_mda_count", 12, my_newSVll (valout->val[i].pv_mda_count), 0); (void) hv_store (hv, "pv_mda_free", 11, my_newSVull (valout->val[i].pv_mda_free), 0); - PUSHs (sv_2mortal ((SV *) hv)); + PUSHs (sv_2mortal (newRV ((SV *) hv))); } guestfs_free_lvm_pv_list (valout); @@ -426,7 +426,7 @@ PREINIT: (void) hv_store (hv, "vg_tags", 7, newSVpv (valout->val[i].vg_tags, 0), 0); (void) hv_store (hv, "vg_mda_count", 12, my_newSVll (valout->val[i].vg_mda_count), 0); (void) hv_store (hv, "vg_mda_free", 11, my_newSVull (valout->val[i].vg_mda_free), 0); - PUSHs (sv_2mortal ((SV *) hv)); + PUSHs (sv_2mortal (newRV ((SV *) hv))); } guestfs_free_lvm_vg_list (valout); @@ -463,7 +463,7 @@ PREINIT: (void) hv_store (hv, "vg_tags", 7, newSVpv (valout->val[i].vg_tags, 0), 0); (void) hv_store (hv, "vg_mda_count", 12, my_newSVll (valout->val[i].vg_mda_count), 0); (void) hv_store (hv, "vg_mda_free", 11, my_newSVull (valout->val[i].vg_mda_free), 0); - PUSHs (sv_2mortal ((SV *) hv)); + PUSHs (sv_2mortal (newRV ((SV *) hv))); } guestfs_free_lvm_vg_list (valout); @@ -498,7 +498,7 @@ PREINIT: (void) hv_store (hv, "lv_tags", 7, newSVpv (valout->val[i].lv_tags, 0), 0); (void) hv_store (hv, "mirror_log", 10, newSVpv (valout->val[i].mirror_log, 0), 0); (void) hv_store (hv, "modules", 7, newSVpv (valout->val[i].modules, 0), 0); - PUSHs (sv_2mortal ((SV *) hv)); + PUSHs (sv_2mortal (newRV ((SV *) hv))); } guestfs_free_lvm_lv_list (valout); @@ -532,7 +532,7 @@ PREINIT: (void) hv_store (hv, "lv_tags", 7, newSVpv (valout->val[i].lv_tags, 0), 0); (void) hv_store (hv, "mirror_log", 10, newSVpv (valout->val[i].mirror_log, 0), 0); (void) hv_store (hv, "modules", 7, newSVpv (valout->val[i].modules, 0), 0); - PUSHs (sv_2mortal ((SV *) hv)); + PUSHs (sv_2mortal (newRV ((SV *) hv))); } guestfs_free_lvm_lv_list (valout); @@ -1185,7 +1185,7 @@ PREINIT: (void) hv_store (hv, "pe_start", 8, my_newSVull (physvols->val[i].pe_start), 0); (void) hv_store (hv, "pv_mda_count", 12, my_newSVll (physvols->val[i].pv_mda_count), 0); (void) hv_store (hv, "pv_mda_free", 11, my_newSVull (physvols->val[i].pv_mda_free), 0); - PUSHs (sv_2mortal ((SV *) hv)); + PUSHs (sv_2mortal (newRV ((SV *) hv))); } guestfs_free_lvm_pv_list (physvols); @@ -1222,7 +1222,7 @@ PREINIT: (void) hv_store (hv, "vg_tags", 7, newSVpv (volgroups->val[i].vg_tags, 0), 0); (void) hv_store (hv, "vg_mda_count", 12, my_newSVll (volgroups->val[i].vg_mda_count), 0); (void) hv_store (hv, "vg_mda_free", 11, my_newSVull (volgroups->val[i].vg_mda_free), 0); - PUSHs (sv_2mortal ((SV *) hv)); + PUSHs (sv_2mortal (newRV ((SV *) hv))); } guestfs_free_lvm_vg_list (volgroups); @@ -1256,7 +1256,7 @@ PREINIT: (void) hv_store (hv, "lv_tags", 7, newSVpv (logvols->val[i].lv_tags, 0), 0); (void) hv_store (hv, "mirror_log", 10, newSVpv (logvols->val[i].mirror_log, 0), 0); (void) hv_store (hv, "modules", 7, newSVpv (logvols->val[i].modules, 0), 0); - PUSHs (sv_2mortal ((SV *) hv)); + PUSHs (sv_2mortal (newRV ((SV *) hv))); } guestfs_free_lvm_lv_list (logvols); @@ -2970,3 +2970,25 @@ PREINIT: OUTPUT: RETVAL +void +readdir (g, dir) + guestfs_h *g; + char *dir; +PREINIT: + struct guestfs_dirent_list *entries; + int i; + HV *hv; + PPCODE: + entries = guestfs_readdir (g, dir); + if (entries == NULL) + croak ("readdir: %s", guestfs_last_error (g)); + EXTEND (SP, entries->len); + for (i = 0; i < entries->len; ++i) { + hv = newHV (); + (void) hv_store (hv, "ino", 3, my_newSVull (entries->val[i].ino), 0); + (void) hv_store (hv, "ftyp", 4, newSVpv (&entries->val[i].ftyp, 1), 0); + (void) hv_store (hv, "name", 4, newSVpv (entries->val[i].name, 0), 0); + PUSHs (newRV (sv_2mortal ((SV *) hv))); + } + guestfs_free_dirent_list (entries); + diff --git a/perl/lib/Sys/Guestfs.pm b/perl/lib/Sys/Guestfs.pm index cd26bedc..e624d552 100644 --- a/perl/lib/Sys/Guestfs.pm +++ b/perl/lib/Sys/Guestfs.pm @@ -1118,6 +1118,18 @@ Note that this function cannot correctly handle binary files as end of line). For those you need to use the C<$h-E<gt>read_file> function which has a more complex interface. +=item @entries = $h->readdir ($dir); + +This returns the list of directory entries in directory C<dir>. + +All entries in the directory are returned, including C<.> and +C<..>. The entries are I<not> sorted, but returned in the same +order as the underlying filesystem. + +This function is primarily intended for use by programs. To +get a simple list of names, use C<$h-E<gt>ls>. To get a printable +directory for human consumption, use C<$h-E<gt>ll>. + =item $h->resize2fs ($device); This resizes an ext2 or ext3 filesystem to match the size of diff --git a/perl/t/060-readdir.t b/perl/t/060-readdir.t new file mode 100644 index 00000000..17dfd24b --- /dev/null +++ b/perl/t/060-readdir.t @@ -0,0 +1,63 @@ +# libguestfs Perl bindings -*- perl -*- +# Copyright (C) 2009 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +use strict; +use warnings; +use Test::More tests => 13; + +use Sys::Guestfs; + +my $h = Sys::Guestfs->new (); +ok ($h); +open FILE, ">test.img"; +truncate FILE, 10*1024*1024; +close FILE; +ok (1); + +$h->add_drive ("test.img"); +ok (1); + +$h->launch (); +ok (1); +$h->wait_ready (); +ok (1); + +$h->sfdisk ("/dev/sda", 0, 0, 0, [","]); +ok (1); +$h->mkfs ("ext2", "/dev/sda1"); +ok (1); +$h->mount ("/dev/sda1", "/"); +ok (1); +$h->mkdir ("/p"); +ok (1); +$h->touch ("/q"); +ok (1); + +my @dirs = $h->readdir ("/"); +@dirs = sort { $a->{name} cmp $b->{name} } @dirs; +foreach (@dirs) { + print "$_->{name} $_->{ino} $_->{ftyp}\n"; +} +ok (1); + +$h->sync (); +ok (1); + +undef $h; +ok (1); + +unlink ("test.img"); |