[augeas-devel] [PATCH] Add a close method to the ruby bindings

David Lutterkort dlutter at redhat.com
Tue Aug 12 19:55:28 UTC 2008


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 1218570873 25200
# Node ID 7b301b2f072e6335b58c7d064bdb359132684e58
# 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 7b301b2f072e README.rdoc
--- a/README.rdoc	Mon Aug 11 14:54:14 2008 -0700
+++ b/README.rdoc	Tue Aug 12 12:54:33 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 7b301b2f072e ext/augeas/_augeas.c
--- a/ext/augeas/_augeas.c	Mon Aug 11 14:54:14 2008 -0700
+++ b/ext/augeas/_augeas.c	Tue Aug 12 12:54:33 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 7b301b2f072e tests/tc_augeas.rb
--- a/tests/tc_augeas.rb	Mon Aug 11 14:54:14 2008 -0700
+++ b/tests/tc_augeas.rb	Tue Aug 12 12:54:33 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