[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

Bryan Kearney bkearney at redhat.com
Thu Aug 7 11:47:22 UTC 2008


# HG changeset patch
# User Bryan Kearney <bkearney at redhat.com>
# Date 1218109620 14400
# Node ID 0bd266ecddcf8fd93cbbb44f3638d7d9c07943e3
# Parent  762841fb5355baff008291ba569882f126aa3cb7
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.

diff -r 762841fb5355 -r 0bd266ecddcf README.rdoc
--- a/README.rdoc	Thu May 01 14:23:42 2008 -0700
+++ b/README.rdoc	Thu Aug 07 07:47:00 2008 -0400
@@ -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 762841fb5355 -r 0bd266ecddcf ext/augeas/_augeas.c
--- a/ext/augeas/_augeas.c	Thu May 01 14:23:42 2008 -0700
+++ b/ext/augeas/_augeas.c	Thu Aug 07 07:47:00 2008 -0400
@@ -24,18 +24,33 @@
 
 static VALUE c_augeas;
 
+typedef struct augeas_wrapper {
+    augeas *aug;
+    int open ;
+} augeas_wrapper ;
+
 static augeas *aug_handle(VALUE s) {
-    augeas *aug;
+    augeas *aug = NULL;
+    augeas_wrapper *wrapper ;
 
-    Data_Get_Struct(s, struct augeas, aug);
-    if (aug == NULL) {
+    Data_Get_Struct(s, struct augeas_wrapper, wrapper);
+    if (wrapper == NULL) {
         rb_raise(rb_eSystemCallError, "Failed to retrieve connection");
+    } else if (wrapper->open == 1){
+        aug = wrapper->aug ;
+    } else {
+        rb_raise(rb_eSystemCallError, "Close has been called");      
     }
+        
     return aug;
 }
 
-static void augeas_close(void *aug) {
-    aug_close(aug);
+static void augeas_free(augeas_wrapper *wrapper) {
+    if (wrapper->open == 1){
+        aug_close(wrapper->aug);
+        wrapper->open = 0 ;    
+        /*free(wrapper);*/
+    }
 }
 
 /*
@@ -176,19 +191,39 @@
  * call-seq:
  *       open(ROOT, LOADPATH, FLAGS) -> Augeas
  *
- * Create a new instance and return it
+ * Create a new instance of and return it. The ROOT is the location for
+ * from which files are referenced. LOADPATH is used to locate lenses.
+ * FLAGS controls how augeas treats the changes.
  */
 VALUE augeas_init(VALUE m, VALUE r, VALUE l, VALUE f) {
     unsigned int flags = NUM2UINT(f);
     const char *root = (r == Qnil) ? NULL : StringValueCStr(r);
     const char *loadpath = (l == Qnil) ? NULL : StringValueCStr(l);
     augeas *aug = NULL;
+    augeas_wrapper *wrapper = NULL;
+    VALUE obj ;
 
     aug = aug_init(root, loadpath, flags);
     if (aug == NULL) {
         rb_raise(rb_eSystemCallError, "Failed to initialize Augeas");
     }
-    return Data_Wrap_Struct(c_augeas, NULL, augeas_close, aug);
+    obj = Data_Make_Struct(c_augeas, struct augeas_wrapper,
+            NULL, augeas_free, wrapper) ;
+
+    wrapper->aug = aug ;
+    wrapper->open = 1 ;
+    return obj ;
+}
+
+VALUE augeas_close (VALUE m) {
+    augeas_wrapper *wrapper ; 
+    Data_Get_Struct(m, struct augeas_wrapper, wrapper);
+    
+    // Check to see if we have been closed already    
+    if (wrapper->open == 1) {
+        augeas_free(wrapper) ;
+    }
+    return m ;
 }
 
 void Init__augeas() {
@@ -214,6 +249,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);    
 }
 
 /*




More information about the augeas-devel mailing list