Add error domains and codes
authorJ. Ali Harlow <ali@juiblex.co.uk>
Tue, 9 Sep 2014 14:27:12 +0000 (15:27 +0100)
committerJ. Ali Harlow <ali@juiblex.co.uk>
Tue, 9 Sep 2014 14:27:12 +0000 (15:27 +0100)
12 files changed:
librazor/atomic-actions.c
librazor/atomic-emulate.c
librazor/atomic-ktm.c
librazor/atomic.c
librazor/error.c
librazor/razor-internal.h
librazor/razor.c
librazor/razor.h
librazor/root.c
librazor/rpm.c
librazor/util.c
src/main.c

index df06432..5c586a5 100644 (file)
@@ -260,8 +260,7 @@ atomic_action_create_dir(struct razor_atomic *atomic,
 
        if (errno != EEXIST || chmod(action->args.path, mode) < 0) {
                if (!atomic->error)
-                       atomic->error = razor_error_new_str(action->args.path,
-                                                           strerror(errno));
+                       atomic->error = razor_error_new_posix(action->args.path);
                atomic_action_free(action);
                return NULL;
        }
@@ -279,8 +278,7 @@ static struct atomic_action *atomic_action_rmdir(struct razor_atomic *atomic,
 
        if (rmdir(action->args.path) < 0) {
                if (!atomic->error)
-                       atomic->error = razor_error_new_str(action->args.path,
-                                                           strerror(errno));
+                       atomic->error = razor_error_new_posix(action->args.path);
                atomic_action_free(action);
                return NULL;
        } else
@@ -302,8 +300,7 @@ atomic_action_create_symlink(struct razor_atomic *atomic,
        r = symlink(action->args.u.create_symlink.target, action->args.path);
        if (r < 0) {
                if (!atomic->error)
-                       atomic->error = razor_error_new_str(action->args.path,
-                                                           strerror(errno));
+                       atomic->error = razor_error_new_posix(action->args.path);
                atomic_action_free(action);
                return NULL;
        }
@@ -322,8 +319,7 @@ atomic_action_remove_symlink(struct razor_atomic *atomic,
 
        if (unlink(action->args.path) < 0) {
                if (!atomic->error)
-                       atomic->error = razor_error_new_str(action->args.path,
-                                                           strerror(errno));
+                       atomic->error = razor_error_new_posix(action->args.path);
                atomic_action_free(action);
                return NULL;
        }
@@ -362,8 +358,7 @@ move_file(struct razor_atomic *atomic, const char *path, const char *dest)
 #else
        if (rename(path, dest)) {
                if (!atomic->error)
-                       atomic->error = razor_error_new_str(dest,
-                                                           strerror(errno));
+                       atomic->error = razor_error_new_posix(dest);
                return -1;
        }
 #endif
index ab020d1..dae72ff 100644 (file)
@@ -244,14 +244,12 @@ razor_atomic_set_toplevel_from_path(struct razor_atomic *atomic,
 
                abspath = absolute_path(path);
                if (!abspath) {
-                       atomic->error = razor_error_new_str(path,
-                                                           strerror(errno));
+                       atomic->error = razor_error_new_posix(path);
                        return -1;
                }
 
                if (stat(abspath, &buf) < 0) {
-                       atomic->error = razor_error_new_str(abspath,
-                                                           strerror(errno));
+                       atomic->error = razor_error_new_posix(abspath);
                        free(abspath);
                        return -1;
                }
@@ -269,8 +267,7 @@ razor_atomic_set_toplevel_from_path(struct razor_atomic *atomic,
                        }
 
                        if (stat(abspath, &buf) < 0) {
-                               atomic->error =
-                                 razor_error_new_str(abspath, strerror(errno));
+                               atomic->error = razor_error_new_posix(abspath);
                                free(abspath);
                                return -1;
                        }
@@ -304,8 +301,7 @@ razor_atomic_set_toplevel_from_path(struct razor_atomic *atomic,
 
 #ifndef MSWIN_API
                        if (stat(".", &buf) < 0) {
-                               atomic->error =
-                                 razor_error_new_str(".", strerror(errno));
+                               atomic->error = razor_error_new_posix(".");
                                free(s);
                                free(atomic->toplevel);
                                atomic->toplevel = NULL;
@@ -329,7 +325,8 @@ razor_atomic_set_toplevel_from_path(struct razor_atomic *atomic,
                }
 #endif
 
-               atomic->error = razor_error_new_str(atomic->toplevel,
+               atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, err,
+                                                   atomic->toplevel,
                                                    strerror(err));
 
                free(atomic->toplevel);
@@ -434,7 +431,7 @@ razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
 
        return 0;
 #else
-       atomic->error = razor_error_new_str(NULL,
+       atomic->error = razor_error_new_str(RAZOR_POSIX_ERROR, ENOSYS, NULL,
                                            "Symbolic links not supported "
                                            "on this platform");
 
@@ -460,7 +457,7 @@ razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
                  mode & (S_IRWXU | S_IRWXG | S_IRWXO));
 
        if (fd == -1)
-               atomic->error = razor_error_new_str(filename, strerror(errno));
+               atomic->error = razor_error_new_posix(filename);
        else {
                a = atomic_action_new(ACTION_MOVE);
                a->args.path = tmpnam;
index 475dcb3..e176743 100644 (file)
@@ -214,7 +214,9 @@ razor_atomic_make_dirs(struct razor_atomic *atomic, const char *root,
                                }
                        } else if (!(fa.dwFileAttributes&
                                     FILE_ATTRIBUTE_DIRECTORY)) {
-                               razor_set_error2(&atomic->error, buffer->str,
+                               razor_set_error2(&atomic->error,
+                                                RAZOR_MSWIN_ERROR,
+                                                ERROR_DIRECTORY, buffer->str,
                                                 "Not a directory");
                                razor_wstr_destroy(buffer);
                                return -1;
@@ -378,8 +380,8 @@ razor_atomic_create_symlink(struct razor_atomic *atomic, const char *target,
         * and we don't always know that at the time when the
         * link is created, so it's a convienent lie for now.
         */
-       razor_set_error(&atomic->error, NULL,
-                       "Symbolic links not supported on this platform");
+       razor_set_error(&atomic->error, RAZOR_MSWIN_ERROR, ERROR_NOT_SUPPORTED,
+                       NULL, "Symbolic links not supported on this platform");
 
        return -1;
 }
@@ -398,7 +400,8 @@ razor_atomic_create_file(struct razor_atomic *atomic, const char *filename,
        files = realloc(atomic->files,
                        (atomic->n_files+1) * sizeof(struct razor_atomic_file));
        if (!files) {
-               razor_set_error(&atomic->error, NULL, "Not enough memory");
+               razor_set_error(&atomic->error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
+                               "Not enough memory");
                return -1;
        }
        atomic->n_files++;
index 88a5d2e..9fec205 100644 (file)
@@ -69,10 +69,20 @@ razor_atomic_get_error_msg(struct razor_atomic *atomic)
 }
 
 RAZOR_EXPORT void
-razor_atomic_abort(struct razor_atomic *atomic, const char *error_msg)
+razor_atomic_abort(struct razor_atomic *atomic, int domain, int code,
+                  const char *error_msg)
 {
        if (!atomic->error)
-               atomic->error = razor_error_new_str(NULL, error_msg);
+               atomic->error = razor_error_new_str(domain, code, NULL,
+                                                   error_msg);
+}
+
+RAZOR_EXPORT void
+razor_atomic_propagate_error(struct razor_atomic *atomic,
+                            struct razor_error *error, const char *summary)
+{
+       if (!atomic->error)
+               atomic->error = razor_error_dup(error, summary);
 }
 
 RAZOR_EXPORT int
@@ -121,8 +131,7 @@ razor_atomic_write(struct razor_atomic *atomic, int fd, const void *data,
        while(size) {
                written = write(fd, data, size);
                if (written < 0) {
-                       atomic->error = razor_error_new_str(NULL,
-                                                           strerror(errno));
+                       atomic->error = razor_error_new_posix(NULL);
 
                        (void)close(fd);
 
@@ -143,7 +152,7 @@ razor_atomic_sync(struct razor_atomic *atomic, int handle)
                return -1;
 
        if (fsync(handle) < 0) {
-               atomic->error = razor_error_new_str(NULL, strerror(errno));
+               atomic->error = razor_error_new_posix(NULL);
                return -1;
        }
 
@@ -157,7 +166,7 @@ razor_atomic_close(struct razor_atomic *atomic, int fd)
                return -1;
 
        if (close(fd) < 0) {
-               atomic->error = razor_error_new_str(NULL, strerror(errno));
+               atomic->error = razor_error_new_posix(NULL);
                return -1;
        }
 
index 2ad1207..24a00c5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011-2012  J. Ali Harlow <ali@juiblex.co.uk>
+ * Copyright (C) 2011-2012, 2014  J. Ali Harlow <ali@juiblex.co.uk>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "razor.h"
 #include "razor-internal.h"
 
-static const char *razor_error_get_path_str(struct razor_error *error)
+RAZOR_EXPORT int
+razor_error_get_domain(struct razor_error *error)
 {
-       if (error->path_str)
-               return error->path_str;
+       return error->domain;
+}
+
+RAZOR_EXPORT int
+razor_error_get_code(struct razor_error *error)
+{
+       return error->code;
+}
+
+RAZOR_EXPORT const char *
+razor_error_get_object(struct razor_error *error)
+{
+       return error->object;
+}
+
+static const char *razor_error_get_obj_str(struct razor_error *error)
+{
+       if (error->obj_str)
+               return error->obj_str;
 
-       if (error->path) {
-               error->path_str = razor_concat(error->path, ": ",
-                                              error->str, NULL);
-               return error->path_str;
+       if (error->object) {
+               error->obj_str = razor_concat(error->object, ": ",
+                                             error->str, NULL);
+               return error->obj_str;
        }
 
        return error->str;
@@ -46,7 +64,7 @@ static const char *razor_error_get_path_str(struct razor_error *error)
  *
  * Retrieves the basic information about an error. If a summary has been set
  * then this will be returned. Otherwise the error string possibly prefixed
- * by the path will be returned instead.
+ * by the object will be returned instead.
  *
  * Returns: Primary text of error.
  **/
@@ -56,14 +74,14 @@ razor_error_get_primary_text(struct razor_error *error)
        if (error->summary)
                return error->summary;
 
-       return razor_error_get_path_str(error);
+       return razor_error_get_obj_str(error);
 }
 
 /**
  * razor_error_get_secondary_text:
  *
  * Retrieves more detailed information about an error, if any. If a summary
- * has been set the error string possibly prefixed by the path will be
+ * has been set the error string possibly prefixed by the object will be
  * returned. Otherwise NULL will be returned.
  *
  * Returns: Secondary text of error or NULL.
@@ -74,7 +92,7 @@ razor_error_get_secondary_text(struct razor_error *error)
        if (!error->summary)
                return NULL;
 
-       return razor_error_get_path_str(error);
+       return razor_error_get_obj_str(error);
 }
 
 RAZOR_EXPORT const char *
@@ -98,15 +116,18 @@ razor_error_get_msg(struct razor_error *error)
 
 #ifdef MSWIN_API
 struct razor_error *
-razor_error_new_mswin(const wchar_t *path, DWORD err)
+razor_error_new_mswin(const wchar_t *object, DWORD err)
 {
        struct razor_error *error;
        wchar_t *buf;
 
        error = zalloc(sizeof *error);
 
-       if (path)
-               error->path = razor_utf16_to_utf8(path, -1);
+       error->domain = RAZOR_MSWIN_ERROR;
+       error->code = err;
+
+       if (object)
+               error->object = razor_utf16_to_utf8(object, -1);
 
        FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
                       FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
@@ -119,14 +140,18 @@ razor_error_new_mswin(const wchar_t *path, DWORD err)
 }
 
 struct razor_error *
-razor_error_new_str2(const wchar_t *path, const char *str)
+razor_error_new_str2(int domain, int code, const wchar_t *object,
+                    const char *str)
 {
        struct razor_error *error;
 
        error = zalloc(sizeof *error);
 
-       if (path)
-               error->path = razor_utf16_to_utf8(path, -1);
+       error->domain = domain;
+       error->code = code;
+
+       if (object)
+               error->object = razor_utf16_to_utf8(object, -1);
 
        error->str = strdup(str);
 
@@ -135,14 +160,17 @@ razor_error_new_str2(const wchar_t *path, const char *str)
 #endif /* MSWIN_API */
 
 RAZOR_EXPORT struct razor_error *
-razor_error_new_str(const char *path, const char *str)
+razor_error_new_str(int domain, int code, const char *object, const char *str)
 {
        struct razor_error *error;
 
        error = zalloc(sizeof *error);
 
-       if (path)
-               error->path = strdup(path);
+       error->domain = domain;
+       error->code = code;
+
+       if (object)
+               error->object = strdup(object);
 
        error->str = strdup(str);
 
@@ -156,13 +184,16 @@ razor_error_dup(struct razor_error *src, const char *summary)
 
        error = zalloc(sizeof *error);
 
+       error->domain = src->domain;
+       error->code = src->code;
+
        if (summary)
                error->summary = strdup(summary);
        else if (src->summary)
                error->summary = strdup(src->summary);
 
-       if (src->path)
-               error->path = strdup(src->path);
+       if (src->object)
+               error->object = strdup(src->object);
 
        if (src->str)
                error->str = strdup(src->str);
@@ -173,9 +204,9 @@ razor_error_dup(struct razor_error *src, const char *summary)
 RAZOR_EXPORT void
 razor_error_free(struct razor_error *error)
 {
-       free(error->path);
+       free(error->object);
        free(error->str);
-       free(error->path_str);
+       free(error->obj_str);
        free(error->summary);
        free(error->msg);
        free(error);
index 55ad9cb..029c26a 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
  * Copyright (C) 2008  Red Hat, Inc
- * Copyright (C) 2009, 2011-2012  J. Ali Harlow <ali@juiblex.co.uk>
+ * Copyright (C) 2009, 2011-2012, 2014  J. Ali Harlow <ali@juiblex.co.uk>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -247,24 +247,35 @@ wchar_t *razor_utf8_to_utf16(const char *utf8, int len);
 
 /* Error functions */
 struct razor_error {
-       char *path;
+       int domain;
+       int code;
+       char *object;
        char *str;
-       char *path_str;
+       char *obj_str;
        char *summary;
        char *msg;
 };
 
+#define razor_error_new_posix(object) \
+       razor_error_new_str(RAZOR_POSIX_ERROR, errno, object, strerror(errno))
+#define razor_set_error_posix(error, object) \
+       if (error) \
+               *(error) = razor_error_new_posix(object); \
+       else
+
 #ifdef MSWIN_API
-struct razor_error *razor_error_new_mswin(const wchar_t *path, DWORD error);
-struct razor_error *razor_error_new_str2(const wchar_t *path, const char *str);
+struct razor_error *razor_error_new_mswin(const wchar_t *object, DWORD error);
+struct razor_error *razor_error_new_str2(int domain, int code,
+                                        const wchar_t *object,
+                                        const char *str);
 
-#define razor_set_error_mswin(error, path, err) \
+#define razor_set_error_mswin(error, object, err) \
        if (error) \
-               *(error) = razor_error_new_mswin(path, err); \
+               *(error) = razor_error_new_mswin(object, err); \
        else
-#define razor_set_error2(error, path, str) \
+#define razor_set_error2(error, domain, code, object, str) \
        if (error) \
-               *(error) = razor_error_new_str2(path, str); \
+               *(error) = razor_error_new_str2(domain, code, object, str); \
        else
 #endif /* MSWIN_API */
 
index 9917649..ab3ab41 100644 (file)
@@ -147,11 +147,12 @@ razor_set_bind_sections(struct razor_set *set, const char *filename,
        struct razor_mapped_file *file;
        const char *pool, *reason;
        struct array *array;
-       int i, j;
+       int i, j, code;
 
        file = zalloc(sizeof *file);
        if (file == NULL) {
-               razor_set_error(error, NULL, "Not enough memory");
+               razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
+                               "Not enough memory");
                return -1;
        }
 
@@ -163,18 +164,22 @@ razor_set_bind_sections(struct razor_set *set, const char *filename,
                return -1;
        }
 
-       if (file->size < sizeof *file->header)
+       if (file->size < sizeof *file->header) {
+               code = RAZOR_GENERAL_ERROR_DATABASE_CORRUPTED;
                reason = "Premature EOF";
-       else if (file->header->magic != RAZOR_MAGIC)
+       } else if (file->header->magic != RAZOR_MAGIC) {
+               code = RAZOR_GENERAL_ERROR_DATABASE_CORRUPTED;
                reason = "Bad magic number";
-       else if (file->header->version < RAZOR_HEADER_VERSION_MIN ||
-                file->header->version > RAZOR_HEADER_VERSION)
+       } else if (file->header->version < RAZOR_HEADER_VERSION_MIN ||
+                  file->header->version > RAZOR_HEADER_VERSION) {
+               code = RAZOR_GENERAL_ERROR_DATABASE_INCOMPATIBLE;
                reason = "Incompatible file version";
-       else
+       } else
                reason = NULL;
 
        if (reason) {
-               razor_set_error(error, filename, reason);
+               razor_set_error(error, RAZOR_GENERAL_ERROR, code, filename,
+                               reason);
                razor_file_free_contents(file->header, file->size);
                free(file);
                return -1;
@@ -222,7 +227,8 @@ razor_set_open(const char *filename, enum razor_set_flags flags,
 
        set = zalloc(sizeof *set);
        if (!set) {
-               razor_set_error(error, NULL, "Not enough memory");
+               razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
+                               "Not enough memory");
                return NULL;
        }
 
index cc13905..276d186 100644 (file)
@@ -114,13 +114,14 @@ enum razor_set_flags {
  */
 struct razor_error;
 
-struct razor_error *razor_error_new_str(const char *path, const char *str);
+struct razor_error *razor_error_new_str(int domain, int code,
+                                       const char *object, const char *str);
 struct razor_error *razor_error_dup(struct razor_error *src,
                                    const char *summary);
 
-#define razor_set_error(error, path, str) \
+#define razor_set_error(error, domain, code, object, str) \
        if (error) \
-               *(error) = razor_error_new_str(path, str); \
+               *(error) = razor_error_new_str(domain, code, object, str); \
        else
 
 #define razor_propagate_error(dest, src, summary) \
@@ -128,6 +129,26 @@ struct razor_error *razor_error_dup(struct razor_error *src,
                *(dest) = razor_error_dup(src, summary); \
        else
 
+#define RAZOR_ERROR_DOMAIN(i1,i2,i3,c) \
+       (((i1)&0xff)<<24|((i2)&0xff)<<16|((i3)&0xff)<<8|(c)&0xff)
+
+#define RAZOR_GENERAL_ERROR    RAZOR_ERROR_DOMAIN('R','z','r',0)
+#define RAZOR_POSIX_ERROR      RAZOR_ERROR_DOMAIN('R','z','r',1)
+#define RAZOR_MSWIN_ERROR      RAZOR_ERROR_DOMAIN('R','z','r',2)
+#define RAZOR_ZLIB_ERROR       RAZOR_ERROR_DOMAIN('R','z','r',3)
+
+enum razor_general_error {
+       RAZOR_GENERAL_ERROR_FAILED,
+       RAZOR_GENERAL_ERROR_DATABASE_CORRUPTED,
+       RAZOR_GENERAL_ERROR_DATABASE_INCOMPATIBLE,
+       RAZOR_GENERAL_ERROR_DATABASE_EXISTS,
+       RAZOR_GENERAL_ERROR_DATABASE_LOCKED,
+       RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
+};
+
+int razor_error_get_domain(struct razor_error *error);
+int razor_error_get_code(struct razor_error *error);
+const char *razor_error_get_object(struct razor_error *error);
 const char *razor_error_get_primary_text(struct razor_error *error);
 const char *razor_error_get_secondary_text(struct razor_error *error);
 const char *razor_error_get_msg(struct razor_error *error);
@@ -215,7 +236,10 @@ int razor_atomic_write(struct razor_atomic *atomic, int handle,
   const void *data, size_t size);
 int razor_atomic_close(struct razor_atomic *atomic, int handle);
 int razor_atomic_sync(struct razor_atomic *atomic, int handle);
-void razor_atomic_abort(struct razor_atomic *atomic, const char *error_msg);
+void razor_atomic_abort(struct razor_atomic *atomic, int domain, int code,
+  const char *error_msg);
+void razor_atomic_propagate_error(struct razor_atomic *atomic,
+  struct razor_error *error, const char *summary);
 int razor_atomic_in_error_state(struct razor_atomic *atomic);
 
 /**
index 9061bcc..d6a37f0 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
+#include <errno.h>
 #include <sys/stat.h>
 #include <dirent.h>
 #include <unistd.h>
@@ -125,12 +126,13 @@ razor_root_create(const char *root, struct razor_error **error)
                /* root is file system root */
        } else if (stat(root, &buf) < 0) {
                if (mkdir(root, 0777) < 0) {
-                       razor_set_error(error, root,
+                       razor_set_error(error, RAZOR_POSIX_ERROR, errno, root,
                                        "Could not create install root");
                        return -1;
                }
        } else if (!S_ISDIR(buf.st_mode)) {
-               razor_set_error(error, root, "Not a directory");
+               razor_set_error(error, RAZOR_POSIX_ERROR, ENOTDIR, root,
+                               "Not a directory");
                return -1;
        }
 
@@ -139,7 +141,8 @@ razor_root_create(const char *root, struct razor_error **error)
        path = razor_path_add_root(file, root);
        retval = !stat(path, &buf);
        if (retval) {
-               razor_set_error(error, NULL,
+               razor_set_error(error, RAZOR_GENERAL_ERROR,
+                               RAZOR_GENERAL_ERROR_DATABASE_EXISTS, NULL,
                                "A razor install root is already initialized");
                free(path);
                free(file);
@@ -175,14 +178,16 @@ razor_root_open(const char *root, struct razor_error **error)
        razor_root_init();
        image = malloc(sizeof *image);
        if (image == NULL) {
-               razor_set_error(error, NULL, "Not enough memory");
+               razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
+                               "Not enough memory");
                return NULL;
        }
 
        image->system = razor_set_create_without_root();
        if (image->system == NULL) {
                free(image);
-               razor_set_error(error, NULL, "Not enough memory");
+               razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
+                               "Not enough memory");
                return NULL;
        }
 
@@ -195,7 +200,8 @@ razor_root_open(const char *root, struct razor_error **error)
        free(lock_path);
 
        if (r < 0) {
-               razor_set_error(error, NULL,
+               razor_set_error(error, RAZOR_GENERAL_ERROR,
+                               RAZOR_GENERAL_ERROR_DATABASE_LOCKED, NULL,
                                "Failed to aquire exclusive system lock");
                razor_set_unref(image->system);
                free(image);
@@ -228,7 +234,8 @@ razor_root_open_read_only(const char *root, struct razor_error **error)
        razor_root_init();
        set = razor_set_create_without_root();
        if (set == NULL) {
-               razor_set_error(error, NULL, "Not enough memory");
+               razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
+                               "Not enough memory");
                return NULL;
        }
 
@@ -237,7 +244,8 @@ razor_root_open_read_only(const char *root, struct razor_error **error)
        free(s);
 
        if (razor_set_aquire_lock(set, path, 0) < 0) {
-               razor_set_error(error, NULL,
+               razor_set_error(error, RAZOR_GENERAL_ERROR,
+                               RAZOR_GENERAL_ERROR_DATABASE_LOCKED, NULL,
                                "Failed to aquire non-exclusive system lock");
                free(path);
                razor_set_unref(set);
index ff58f4a..c5385c7 100644 (file)
@@ -613,7 +613,8 @@ razor_rpm_open(const char *filename, struct razor_error **error)
 
        rpm = zalloc(sizeof *rpm);
        if (rpm == NULL) {
-               razor_set_error(error, NULL, "Not enough memory");
+               razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
+                               "Not enough memory");
                return NULL;
        }
        memset(rpm, 0, sizeof *rpm);
@@ -650,7 +651,9 @@ razor_rpm_open(const char *filename, struct razor_error **error)
                                              &count);
                if (name) {
                        razor_rpm_close(rpm);
-                       razor_set_error(error, filename,
+                       razor_set_error(error, RAZOR_GENERAL_ERROR,
+                                       RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
+                                       filename,
                                        "Old filenames not supported");
                        return NULL;
                }
@@ -670,7 +673,9 @@ razor_rpm_open(const char *filename, struct razor_error **error)
                                                &count);
                if (prefix) {
                        razor_rpm_close(rpm);
-                       razor_set_error(error, filename,
+                       razor_set_error(error, RAZOR_GENERAL_ERROR,
+                                       RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
+                                       filename,
                                        "Default prefix not supported");
                        return NULL;
                }
@@ -737,7 +742,8 @@ installer_inflate(struct installer *installer)
        installer->stream.avail_out = length;
        err = inflate(&installer->stream, Z_SYNC_FLUSH);
        if (err != Z_OK && err != Z_STREAM_END) {
-               razor_atomic_abort(installer->atomic, "Failed to inflate");
+               razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err,
+                                  "Failed to inflate");
                return -1;
        }
 
@@ -762,7 +768,8 @@ installer_align(struct installer *installer, size_t size)
 
        err = inflate(&installer->stream, Z_SYNC_FLUSH);
        if (err != Z_OK && err != Z_STREAM_END) {
-               razor_atomic_abort(installer->atomic, "Failed to inflate");
+               razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err,
+                                  "Failed to inflate");
                return -1;
        }
 
@@ -806,6 +813,8 @@ create_path(struct installer *installer, const char *path, unsigned int mode)
                        return -1;
                if (installer->length >= sizeof installer->buffer) {
                        razor_atomic_abort(installer->atomic,
+                         RAZOR_GENERAL_ERROR,
+                         RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
                          "Link target too long");
                        return -1;
                }
@@ -824,7 +833,8 @@ unsupported:
                free(buffer);
                buffer = razor_concat(s, " are not supported on this platform",
                  NULL);
-               razor_atomic_abort(installer->atomic, buffer);
+               razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
+                 RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED, buffer);
                free(buffer);
                return -1;
        case CDEV:
@@ -838,7 +848,8 @@ unsupported:
                goto unsupported;
        default:
                free(buffer);
-               razor_atomic_abort(installer->atomic, "Unknown file type");
+               razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
+                 RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED, "Unknown file type");
                return -1;
        }
 }
@@ -1048,7 +1059,8 @@ installer_init(struct installer *installer)
 
        gz_header = installer->rpm->payload;
        if (gz_header[0] != 0x1f || gz_header[1] != 0x8b) {
-               razor_atomic_abort(installer->atomic, 
+               razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
+                                  RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
                                   "Payload section doesn't have gz header");
                return -1;
        }
@@ -1057,7 +1069,8 @@ installer_init(struct installer *installer)
        flags = gz_header[3];
 
        if (method != Z_DEFLATED || flags != 0) {
-               razor_atomic_abort(installer->atomic, 
+               razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
+                                  RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
                                   "Unknown payload compression method or "
                                   "flags set");
                return -1;
@@ -1078,7 +1091,7 @@ installer_init(struct installer *installer)
        if (err != Z_OK) {
                sprintf(buffer, "%d", err);
                s = razor_concat("inflateEnd error: ", buffer, NULL);
-               razor_atomic_abort(installer->atomic, s);
+               razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err, s);
                free(s);
                return -1;
        }
@@ -1097,7 +1110,7 @@ installer_finish(struct installer *installer)
        if (err != Z_OK) {
                sprintf(buffer, "%d", err);
                s = razor_concat("inflateEnd error: ", buffer, NULL);
-               razor_atomic_abort(installer->atomic, s);
+               razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err, s);
                free(s);
        }
 
@@ -1132,7 +1145,7 @@ razor_rpm_install(struct razor_rpm *rpm, struct razor_atomic *atomic,
        const char *path;
        size_t filesize;
        char *s;
-       int retval = 0;
+       int retval = 0, code;
 
        assert (rpm != NULL);
        assert (root != NULL);
@@ -1142,9 +1155,10 @@ razor_rpm_install(struct razor_rpm *rpm, struct razor_atomic *atomic,
        installer.atomic = atomic;
 
        /* FIXME: Only do this before a transaction, not per rpm. */
-       if (*root && (stat(root, &buf) < 0 || !S_ISDIR(buf.st_mode))) {
+       if (*root && ((retval = stat(root, &buf)) || !S_ISDIR(buf.st_mode))) {
+               code = retval ? errno : ENOTDIR;
                s = razor_concat(root, ": Directory does not exist", NULL);
-               razor_atomic_abort(atomic, s);
+               razor_atomic_abort(atomic, RAZOR_POSIX_ERROR, code, s);
                free(s);
                return -1;
        }
index a09dcd1..b9994ae 100644 (file)
@@ -74,12 +74,12 @@ razor_file_get_contents(const char *filename, size_t *length, int private,
 
        fd = open(filename, O_RDONLY | O_BINARY);
        if (fd < 0) {
-               razor_set_error(error, filename, strerror(errno));
+               razor_set_error_posix(error, filename);
                return NULL;
        }
 
        if (fstat(fd, &st) < 0) {
-               razor_set_error(error, filename, strerror(errno));
+               razor_set_error_posix(error, filename);
                close(fd);
                return NULL;
        }
@@ -99,8 +99,7 @@ razor_file_get_contents(const char *filename, size_t *length, int private,
                        while(nb < st.st_size) {
                                res = read(fd, addr + nb, st.st_size - nb);
                                if (res <= 0) {
-                                       razor_set_error(error, filename,
-                                                       strerror(errno));
+                                       razor_set_error_posix(error, filename);
                                        free(addr);
                                        addr = NULL;
                                        break;
@@ -108,7 +107,8 @@ razor_file_get_contents(const char *filename, size_t *length, int private,
                                nb += res;
                        }
                } else
-                       razor_set_error(error, NULL, "Not enough memory");
+                       razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
+                                       "Not enough memory");
        }
        close(fd);
 
index d7e7f69..aa116ba 100644 (file)
@@ -1110,7 +1110,7 @@ relocate_packages(struct razor_set *set, struct razor_atomic *atomic,
                rpm = razor_rpm_open(file, &error);
                free(file);
                if (rpm == NULL) {
-                       razor_atomic_abort(atomic, razor_error_get_msg(error));
+                       razor_atomic_propagate_error(atomic, error, NULL);
                        razor_error_free(error);
                        razor_package_iterator_destroy(pkg_iter);
                        razor_importer_destroy(importer);
@@ -1187,7 +1187,7 @@ install_package(struct razor_transaction *trans, struct razor_set *set,
        rpm = razor_rpm_open(file, &error);
        free(file);
        if (rpm == NULL) {
-               razor_atomic_abort(atomic, razor_error_get_msg(error));
+               razor_atomic_propagate_error(atomic, error, NULL);
                fprintf(stderr, "%s\n", razor_error_get_msg(error));
                razor_error_free(error);
                return -1;