[xmlsec] close file handle asap to release resources



commit 17a07fdea425923fd6f5f0257ca354fdfdae7a5d
Author: Aleksey Sanin <aleksey aleksey com>
Date:   Mon Mar 2 15:52:51 2015 -0800

    close file handle asap to release resources

 include/xmlsec/io.h |    1 +
 src/io.c            |   47 +++++++++++++++++++++++++++++++++++++++++++----
 src/transforms.c    |   12 ++++++++++++
 3 files changed, 56 insertions(+), 4 deletions(-)
---
diff --git a/include/xmlsec/io.h b/include/xmlsec/io.h
index d5a41c1..e65617f 100644
--- a/include/xmlsec/io.h
+++ b/include/xmlsec/io.h
@@ -45,6 +45,7 @@ XMLSEC_EXPORT int       xmlSecIORegisterCallbacks               (xmlInputMatchCa
 XMLSEC_EXPORT xmlSecTransformId xmlSecTransformInputURIGetKlass (void);
 XMLSEC_EXPORT int       xmlSecTransformInputURIOpen             (xmlSecTransformPtr transform,
                                                                  const xmlChar* uri);
+XMLSEC_EXPORT int       xmlSecTransformInputURIClose            (xmlSecTransformPtr transform);
 
 #ifdef __cplusplus
 }
diff --git a/src/io.c b/src/io.c
index 3f3b9ef..6e4cccf 100644
--- a/src/io.c
+++ b/src/io.c
@@ -433,6 +433,35 @@ xmlSecTransformInputURIOpen(xmlSecTransformPtr transform, const xmlChar *uri) {
     return(0);
 }
 
+
+/**
+ * xmlSecTransformInputURIClose:
+ * @transform:          the pointer to IO transform.
+ *
+ * Closes the given @transform and frees up resourses.
+ *
+ * Returns: 0 on success or a negative value otherwise.
+ */
+int
+xmlSecTransformInputURIClose(xmlSecTransformPtr transform) {
+    xmlSecInputURICtxPtr ctx;
+
+    xmlSecAssert2(xmlSecTransformCheckId(transform, xmlSecTransformInputURIId), -1);
+
+    ctx = xmlSecTransformInputUriGetCtx(transform);
+    xmlSecAssert2(ctx != NULL, -1);
+
+    /* close if still open and mark as closed */
+    if((ctx->clbksCtx != NULL) && (ctx->clbks != NULL) && (ctx->clbks->closecallback != NULL)) {
+       (ctx->clbks->closecallback)(ctx->clbksCtx);
+       ctx->clbksCtx = NULL;
+       ctx->clbks = NULL;
+    }
+
+    /* done */
+    return(0);
+}
+
 static int
 xmlSecTransformInputURIInitialize(xmlSecTransformPtr transform) {
     xmlSecInputURICtxPtr ctx;
@@ -448,17 +477,27 @@ xmlSecTransformInputURIInitialize(xmlSecTransformPtr transform) {
 
 static void
 xmlSecTransformInputURIFinalize(xmlSecTransformPtr transform) {
-    xmlSecInputURICtxPtr ctx;
+       xmlSecInputURICtxPtr ctx;
+       int ret;
 
     xmlSecAssert(xmlSecTransformCheckId(transform, xmlSecTransformInputURIId));
 
     ctx = xmlSecTransformInputUriGetCtx(transform);
     xmlSecAssert(ctx != NULL);
 
-    if((ctx->clbksCtx != NULL) && (ctx->clbks != NULL) && (ctx->clbks->closecallback != NULL)) {
-        (ctx->clbks->closecallback)(ctx->clbksCtx);
-    }
+    ret = xmlSecTransformInputURIClose(transform);
+    if(ret < 0) {
+               xmlSecError(XMLSEC_ERRORS_HERE,
+                                       xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
+                                       "xmlSecTransformInputURIClose",
+                                       XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                                       "ret=%d", ret);
+               /* ignore the error */
+               /* return; */
+       }
+
     memset(ctx, 0, sizeof(xmlSecInputURICtx));
+    return;
 }
 
 static int
diff --git a/src/transforms.c b/src/transforms.c
index d384a0e..8193461 100644
--- a/src/transforms.c
+++ b/src/transforms.c
@@ -1201,6 +1201,18 @@ xmlSecTransformCtxUriExecute(xmlSecTransformCtxPtr ctx, const xmlChar* uri) {
         return(-1);
     }
 
+    /* Close to free up file handle */
+    ret = xmlSecTransformInputURIClose(uriTransform);
+    if(ret < 0) {
+               xmlSecError(XMLSEC_ERRORS_HERE,
+                                       NULL,
+                                       "xmlSecTransformInputURIClose",
+                                       XMLSEC_ERRORS_R_XMLSEC_FAILED,
+                                       "ret=%d", ret);
+               return(-1);
+       }
+
+    /* Done */
     ctx->status = xmlSecTransformStatusFinished;
     return(0);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]