]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: error: Add helpers for saving and restoring of last error
authorPeter Krempa <pkrempa@redhat.com>
Fri, 1 Sep 2017 14:19:56 +0000 (16:19 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 15 Sep 2017 03:28:21 +0000 (05:28 +0200)
Some cleanup paths overwrite a usefull error message with a less useful
one and we then try to preserve the original message. The handlers added
in this patch will simplify the operations since they are designed right
for the purpose.

src/libvirt_private.syms
src/util/virerror.c
src/util/virerror.h

index 247d1175ba0e359c1602e631e6ef3186aa5c03ca..e4fa771d9d2a63dec5c33d2c2d69957651125cbf 100644 (file)
@@ -1605,6 +1605,8 @@ ebtablesRemoveForwardAllowIn;
 virDispatchError;
 virErrorCopyNew;
 virErrorInitialize;
+virErrorPreserveLast;
+virErrorRestore;
 virErrorSetErrnoFromLastError;
 virLastErrorIsSystemErrno;
 virRaiseErrorFull;
index a5a2d6ed10f3b56edfa3ee06bcb9c2e9ca2087b9..1f15c5dbbea8114feea2113177d104d221f6b0c8 100644 (file)
@@ -371,6 +371,51 @@ virSaveLastError(void)
     return to;
 }
 
+
+/**
+ * virErrorPreserveLast:
+ * @saveerr: pointer to virErrorPtr for storing last error object
+ *
+ * Preserves the currently set last error (for the thread) into @saveerr so that
+ * it can be restored via virErrorRestore(). @saveerr must be passed to
+ * virErrorRestore()
+ */
+void
+virErrorPreserveLast(virErrorPtr *saveerr)
+{
+    int saved_errno = errno;
+    virErrorPtr lasterr = virGetLastError();
+
+    *saveerr = NULL;
+
+    if (lasterr)
+        *saveerr = virErrorCopyNew(lasterr);
+
+    errno = saved_errno;
+}
+
+
+/**
+ * virErrorRestore:
+ * @savederr: error object holding saved error
+ *
+ * Restores the error passed via @savederr and clears associated memory.
+ */
+void
+virErrorRestore(virErrorPtr *savederr)
+{
+    int saved_errno = errno;
+
+    if (!*savederr)
+        return;
+
+    virSetError(*savederr);
+    virFreeError(*savederr);
+    *savederr = NULL;
+    errno = saved_errno;
+}
+
+
 /**
  * virResetError:
  * @err: pointer to the virError to clean up
index 234864812a95c8c757d0acdb6266640e5858b32e..54530d081175eb25ba6628b8dfd55f1dc0a28aba 100644 (file)
@@ -196,4 +196,7 @@ void virErrorSetErrnoFromLastError(void);
 
 bool virLastErrorIsSystemErrno(int errnum);
 
+void virErrorPreserveLast(virErrorPtr *saveerr);
+void virErrorRestore(virErrorPtr *savederr);
+
 #endif