[augeas-devel] [PATCH] added close method to the ruby bindings. This will close the augeas handle, and therefor the object is unusable after calling close. Added a new structure to hold augeas and the state of the handle (open, closed). Nothing is done to mark the object for garbage collection, only when gc is run

David Lutterkort dlutter at redhat.com
Mon Aug 11 22:28:42 UTC 2008


It's not necessary to have the explicit augeas_wrapper structure, we can
keep track of whether the connection has been closed already by setting
the DATA_PTR to NULL. I reworked the patch a little - let me know if it
causes trouble for you.

3 files changed, 58 insertions(+), 5 deletions(-)
README.rdoc          |   21 ++++++++++++++++++++-
ext/augeas/_augeas.c |   28 ++++++++++++++++++++++++----
tests/tc_augeas.rb   |   14 ++++++++++++++


# HG changeset patch
# User David Lutterkort <dlutter at redhat.com>
# Date 1218493411 25200
# Node ID c2f62fa08387457770f7636c95b0e2aee4a25f1b
# Parent  ee2908e2a8668a3055b6332d6ad2194a29d9c6cf
Add a close method to the ruby bindings.

This makes it possible to explicitly relinquish all the memory
associated
with an Augeas instance, instead of waiting for the garbage collector
until
Augeas is told to free its resources.

After a Augeas connection has been closed, it can not be used for any
other
operations.

diff -r ee2908e2a866 -r c2f62fa08387 README.rdoc
--- a/README.rdoc	Mon Aug 11 14:54:14 2008 -0700
+++ b/README.rdoc	Mon Aug 11 15:23:31 2008 -0700
@@ -1,3 +1,22 @@
 = Ruby bindings for augeas
 
-The class Augeas provides bindings to augeas [http://augeas.net]
library
+The class Augeas provides bindings to augeas [http://augeas.net]
library.
+
+== Usage: Setting Data
+    aug = Augeas.open("/", "", 0)
+    aug.set("/files/etc/sysconfig/firstboot/RUN_FIRSTBOOT", "YES")
+    aug.save()
+    aug.close()
+
+== Usage: Accessing Data
+    aug = Augeas.open("/", "", 0)
+    aug.get("/files/etc/sysconfig/firstboot/RUN_FIRSTBOOT")
+    aug.close
+
+== Usage: Removing Data
+    aug = Augeas.open("/", "", 0)
+    aug.rm("/files/etc/sysconfig/firstboot/RUN_FIRSTBOOT")
+    aug.save()
+    aug.close
+
+
diff -r ee2908e2a866 -r c2f62fa08387 ext/augeas/_augeas.c
--- a/ext/augeas/_augeas.c	Mon Aug 11 14:54:14 2008 -0700
+++ b/ext/augeas/_augeas.c	Mon Aug 11 15:23:31 2008 -0700
@@ -34,8 +34,9 @@
     return aug;
 }
 
-static void augeas_close(void *aug) {
-    aug_close(aug);
+static void augeas_free(augeas *aug) {
+    if (aug != NULL)
+        aug_close(aug);
 }
 
 /*
@@ -176,7 +177,16 @@
  * call-seq:
  *       open(ROOT, LOADPATH, FLAGS) -> Augeas
  *
- * Create a new instance and return it
+ * Create a new Augeas instance and return it.
+ *
+ * Use ROOT as the filesystem root. If ROOT is NULL, use the value of
the
+ * environment variable AUGEAS_ROOT. If that doesn't exist eitehr, use
"/".
+ *
+ * LOADPATH is a colon-spearated list of directories that modules
should be
+ * searched in. This is in addition to the standard load path and the
+ * directories in AUGEAS_LENS_LIB
+ *
+ * FLAGS is a bitmask made up of values from AUG_FLAGS.
  */
 VALUE augeas_init(VALUE m, VALUE r, VALUE l, VALUE f) {
     unsigned int flags = NUM2UINT(f);
@@ -188,7 +198,16 @@
     if (aug == NULL) {
         rb_raise(rb_eSystemCallError, "Failed to initialize Augeas");
     }
-    return Data_Wrap_Struct(c_augeas, NULL, augeas_close, aug);
+    return Data_Wrap_Struct(c_augeas, NULL, augeas_free, aug);
+}
+
+VALUE augeas_close (VALUE s) {
+    augeas *aug = aug_handle(s);
+
+    aug_close(aug);
+    DATA_PTR(s) = NULL;
+
+    return Qnil;
 }
 
 void Init__augeas() {
@@ -214,6 +233,7 @@
     rb_define_method(c_augeas, "match", augeas_match, 1);
     rb_define_method(c_augeas, "save", augeas_save, 0);
     rb_define_method(c_augeas, "set", augeas_set, 2);
+    rb_define_method(c_augeas, "close", augeas_close, 0);
 }
 
 /*
diff -r ee2908e2a866 -r c2f62fa08387 tests/tc_augeas.rb
--- a/tests/tc_augeas.rb	Mon Aug 11 14:54:14 2008 -0700
+++ b/tests/tc_augeas.rb	Mon Aug 11 15:23:31 2008 -0700
@@ -28,4 +28,18 @@
             Augeas.new
         end
     end
+
+    def test_close
+        aug = Augeas::open("/tmp", nil, Augeas::SAVE_NEWFILE)
+        assert_equal("newfile", aug.get("/augeas/save"))
+        aug.close
+
+        assert_raise(SystemCallError) {
+            aug.get("/augeas/save")
+        }
+
+        assert_raise(SystemCallError) {
+            aug.close
+        }
+    end
 end







More information about the augeas-devel mailing list