The following commit has been merged in the master branch:
commit b03f212d3a29bef2be2f269f0656e482bb9e56ad
Author: Guillem Jover <[email protected]>
Date:   Tue May 10 19:50:15 2011 +0200

    libdpkg: Add new error module
    
    This new module provides error reporting infrastructure, which will
    be used to report back error information from inner functions that
    are not supposed to ohshit() directly, the caller should be
    responsible for the outcome, including just forwarding the error
    to its own caller.

diff --git a/lib/dpkg/Makefile.am b/lib/dpkg/Makefile.am
index 98d084d..25c3159 100644
--- a/lib/dpkg/Makefile.am
+++ b/lib/dpkg/Makefile.am
@@ -35,6 +35,7 @@ libdpkg_a_SOURCES = \
        dir.c \
        dump.c \
        ehandle.c \
+       error.c \
        fdio.c \
        file.c \
        fields.c \
@@ -81,6 +82,7 @@ pkginclude_HEADERS = \
        dpkg.h \
        dpkg-db.h \
        ehandle.h \
+       error.h \
        fdio.h \
        file.h \
        glob.h \
diff --git a/lib/dpkg/error.c b/lib/dpkg/error.c
new file mode 100644
index 0000000..cb736ef
--- /dev/null
+++ b/lib/dpkg/error.c
@@ -0,0 +1,93 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * error.c - error message reporting
+ *
+ * Copyright © 2011 Guillem Jover <[email protected]>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <errno.h>
+#include <stdlib.h>
+
+#include <dpkg/dpkg.h>
+#include <dpkg/varbuf.h>
+#include <dpkg/error.h>
+
+static void DPKG_ATTR_VPRINTF(3)
+dpkg_error_set(struct dpkg_error *err, int type, const char *fmt, va_list args)
+{
+       struct varbuf str = VARBUF_INIT;
+
+       if (err == NULL)
+               return;
+
+       err->type = type;
+
+       varbuf_vprintf(&str, fmt, args);
+       err->str = str.buf;
+}
+
+int
+dpkg_put_warn(struct dpkg_error *err, const char *fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       dpkg_error_set(err, DPKG_MSG_WARN, fmt, args);
+       va_end(args);
+
+       return -1;
+}
+
+int
+dpkg_put_error(struct dpkg_error *err, const char *fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       dpkg_error_set(err, DPKG_MSG_ERROR, fmt, args);
+       va_end(args);
+
+       return -1;
+}
+
+int
+dpkg_put_errno(struct dpkg_error *err, const char *fmt, ...)
+{
+       va_list args;
+       char *new_fmt;
+       int errno_saved = errno;
+
+       va_start(args, fmt);
+       m_asprintf(&new_fmt, "%s (%s)", fmt, strerror(errno_saved));
+       va_end(args);
+
+       dpkg_error_set(err, DPKG_MSG_ERROR, new_fmt, args);
+
+       free(new_fmt);
+
+       return -1;
+}
+
+void
+dpkg_error_destroy(struct dpkg_error *err)
+{
+       err->type = DPKG_MSG_NONE;
+       free(err->str);
+       err->str = NULL;
+}
diff --git a/lib/dpkg/progname.h b/lib/dpkg/error.h
similarity index 59%
copy from lib/dpkg/progname.h
copy to lib/dpkg/error.h
index 14034b9..2f285df 100644
--- a/lib/dpkg/progname.h
+++ b/lib/dpkg/error.h
@@ -1,6 +1,6 @@
 /*
  * libdpkg - Debian packaging suite library routines
- * progname.h - program name handling functions
+ * error.h - error message reporting
  *
  * Copyright © 2011 Guillem Jover <[email protected]>
  *
@@ -18,16 +18,32 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef LIBDPKG_PROGNAME_H
-#define LIBDPKG_PROGNAME_H
+#ifndef LIBDPKG_ERROR_H
+#define LIBDPKG_ERROR_H
 
 #include <dpkg/macros.h>
 
 DPKG_BEGIN_DECLS
 
-void dpkg_set_progname(const char *name);
-const char *dpkg_get_progname(void);
+struct dpkg_error {
+       enum dpkg_msg_type {
+               DPKG_MSG_NONE,
+               DPKG_MSG_WARN,
+               DPKG_MSG_ERROR,
+       } type;
+
+       char *str;
+};
+
+int dpkg_put_warn(struct dpkg_error *err, const char *fmt, ...)
+       DPKG_ATTR_PRINTF(2);
+int dpkg_put_error(struct dpkg_error *err, const char *fmt, ...)
+       DPKG_ATTR_PRINTF(2);
+int dpkg_put_errno(struct dpkg_error *err, const char *fmt, ...)
+       DPKG_ATTR_PRINTF(2);
+
+void dpkg_error_destroy(struct dpkg_error *err);
 
 DPKG_END_DECLS
 
-#endif
+#endif /* LIBDPKG_ERROR_H */

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to