summaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2009-07-01 20:56:58 +0100
committerRichard W.M. Jones <rjones@redhat.com>2009-07-02 10:11:55 +0100
commit5186251f8f681f2ebb028423bb49a748861fd11e (patch)
treeb95ea92a8ed1b9443dc04aaf7cdacc8191291bc0 /python
parentf20854ec61eef1aea313920f0cf193a78c1a9219 (diff)
downloadlibguestfs-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 'python')
-rw-r--r--python/guestfs-py.c53
-rw-r--r--python/guestfs.py17
2 files changed, 70 insertions, 0 deletions
diff --git a/python/guestfs-py.c b/python/guestfs-py.c
index 98a14a03..44b446aa 100644
--- a/python/guestfs-py.c
+++ b/python/guestfs-py.c
@@ -389,6 +389,33 @@ put_statvfs (struct guestfs_statvfs *statvfs)
};
static PyObject *
+put_dirent (struct guestfs_dirent *dirent)
+{
+ PyObject *dict;
+
+ dict = PyDict_New ();
+ PyDict_SetItemString (dict, "ino",
+ PyLong_FromLongLong (dirent->ino));
+ PyDict_SetItemString (dict, "ftyp",
+ PyString_FromStringAndSize (&dirent->ftyp, 1));
+ PyDict_SetItemString (dict, "name",
+ PyString_FromString (dirent->name));
+ return dict;
+};
+
+static PyObject *
+put_dirent_list (struct guestfs_dirent_list *dirents)
+{
+ PyObject *list;
+ int i;
+
+ list = PyList_New (dirents->len);
+ for (i = 0; i < dirents->len; ++i)
+ PyList_SetItem (list, i, put_dirent (&dirents->val[i]));
+ return list;
+};
+
+static PyObject *
py_guestfs_test0 (PyObject *self, PyObject *args)
{
PyObject *py_g;
@@ -5187,6 +5214,31 @@ py_guestfs_umask (PyObject *self, PyObject *args)
return py_r;
}
+static PyObject *
+py_guestfs_readdir (PyObject *self, PyObject *args)
+{
+ PyObject *py_g;
+ guestfs_h *g;
+ PyObject *py_r;
+ struct guestfs_dirent_list *r;
+ const char *dir;
+
+ if (!PyArg_ParseTuple (args, (char *) "Os:guestfs_readdir",
+ &py_g, &dir))
+ return NULL;
+ g = get_handle (py_g);
+
+ r = guestfs_readdir (g, dir);
+ if (r == NULL) {
+ PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));
+ return NULL;
+ }
+
+ py_r = put_dirent_list (r);
+ guestfs_free_dirent_list (r);
+ return py_r;
+}
+
static PyMethodDef methods[] = {
{ (char *) "create", py_guestfs_create, METH_VARARGS, NULL },
{ (char *) "close", py_guestfs_close, METH_VARARGS, NULL },
@@ -5381,6 +5433,7 @@ static PyMethodDef methods[] = {
{ (char *) "mknod_b", py_guestfs_mknod_b, METH_VARARGS, NULL },
{ (char *) "mknod_c", py_guestfs_mknod_c, METH_VARARGS, NULL },
{ (char *) "umask", py_guestfs_umask, METH_VARARGS, NULL },
+ { (char *) "readdir", py_guestfs_readdir, METH_VARARGS, NULL },
{ NULL, NULL, 0, NULL }
};
diff --git a/python/guestfs.py b/python/guestfs.py
index ad44df7f..784c5671 100644
--- a/python/guestfs.py
+++ b/python/guestfs.py
@@ -1958,3 +1958,20 @@ class GuestFS:
"""
return libguestfsmod.umask (self._o, mask)
+ def readdir (self, dir):
+ u"""This returns the list of directory entries in directory
+ "dir".
+
+ All entries in the directory are returned, including "."
+ and "..". The entries are *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 "g.ls". To get a
+ printable directory for human consumption, use "g.ll".
+
+ This function returns a list of directory entries. Each
+ directory entry is represented as a dictionary.
+ """
+ return libguestfsmod.readdir (self._o, dir)
+