librazor/error.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Oct 09 17:27:41 2014 +0100 (2014-10-09)
changeset 455 df914f383f5c
parent 447 0a5e583393e1
child 499 c89e5edb8eae
permissions -rw-r--r--
Support downloading from local repository even without libcurl

Using the --url option of the razor executable, it is possible
to specify a yum repository on the local machine (eg., on installation
media) and import from there, eg.,:

C> razor --url file:///d:/ import-yum

This will be handled by libcurl if available but if not, an internal
copy routine will be used.

Note that if Microsoft's KTM implementation of atomic transactions is
used, then the current directory must support atomic transactions
(also improve error messages for this, and other, cases).
     1 /*
     2  * Copyright (C) 2011-2012, 2014  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License along
    15  * with this program; if not, write to the Free Software Foundation, Inc.,
    16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    17  */
    18 
    19 #include "config.h"
    20 
    21 #ifdef MSWIN_API
    22 #include <windows.h>
    23 #ifndef ERROR_TRANSACTIONAL_OPEN_NOT_ALLOWED
    24 #define ERROR_TRANSACTIONAL_OPEN_NOT_ALLOWED	6832L
    25 #endif
    26 #endif
    27 #include <stdlib.h>
    28 #include <string.h>
    29 
    30 #include "razor.h"
    31 #include "razor-internal.h"
    32 
    33 RAZOR_EXPORT int
    34 razor_error_get_domain(struct razor_error *error)
    35 {
    36 	return error->domain;
    37 }
    38 
    39 RAZOR_EXPORT int
    40 razor_error_get_code(struct razor_error *error)
    41 {
    42 	return error->code;
    43 }
    44 
    45 RAZOR_EXPORT const char *
    46 razor_error_get_object(struct razor_error *error)
    47 {
    48 	return error->object;
    49 }
    50 
    51 static const char *razor_error_get_obj_str(struct razor_error *error)
    52 {
    53 	if (error->obj_str)
    54 		return error->obj_str;
    55 
    56 	if (error->object) {
    57 		error->obj_str = razor_concat(error->object, ": ",
    58 					      error->str, NULL);
    59 		return error->obj_str;
    60 	}
    61 
    62 	return error->str;
    63 }
    64 
    65 /**
    66  * razor_error_get_primary_text:
    67  *
    68  * Retrieves the basic information about an error. If a summary has been set
    69  * then this will be returned. Otherwise the error string possibly prefixed
    70  * by the object will be returned instead.
    71  *
    72  * Returns: Primary text of error.
    73  **/
    74 RAZOR_EXPORT const char *
    75 razor_error_get_primary_text(struct razor_error *error)
    76 {
    77 	if (error->summary)
    78 		return error->summary;
    79 
    80 	return razor_error_get_obj_str(error);
    81 }
    82 
    83 /**
    84  * razor_error_get_secondary_text:
    85  *
    86  * Retrieves more detailed information about an error, if any. If a summary
    87  * has been set the error string possibly prefixed by the object will be
    88  * returned. Otherwise NULL will be returned.
    89  *
    90  * Returns: Secondary text of error or NULL.
    91  **/
    92 RAZOR_EXPORT const char *
    93 razor_error_get_secondary_text(struct razor_error *error)
    94 {
    95 	if (!error->summary)
    96 		return NULL;
    97 
    98 	return razor_error_get_obj_str(error);
    99 }
   100 
   101 RAZOR_EXPORT const char *
   102 razor_error_get_msg(struct razor_error *error)
   103 {
   104 	const char *primary, *secondary;
   105 
   106 	if (error->msg)
   107 		return error->msg;
   108 
   109 	primary = razor_error_get_primary_text(error);
   110 
   111 	secondary = razor_error_get_secondary_text(error);
   112 
   113 	if (secondary) {
   114 		error->msg = razor_concat(primary, "\n", secondary, NULL);
   115 		return error->msg;
   116 	} else
   117 		return primary;
   118 }
   119 
   120 #ifdef MSWIN_API
   121 struct razor_error *
   122 razor_error_new_mswin(const wchar_t *object, DWORD err)
   123 {
   124 	struct razor_error *error;
   125 	wchar_t *buf;
   126 
   127 	error = zalloc(sizeof *error);
   128 
   129 	error->domain = RAZOR_MSWIN_ERROR;
   130 	error->code = err;
   131 
   132 	if (object)
   133 		error->object = razor_utf16_to_utf8(object, -1);
   134 
   135 	switch(err) {
   136 	case ERROR_TRANSACTIONAL_OPEN_NOT_ALLOWED:
   137 		/*
   138 		 * Attempting to include files in a transaction on a filesystem
   139 		 * that doesn't support them (only NTFS?) produces this error
   140 		 * for which the default text isn't very informative. Try and
   141 		 * give more useful information.
   142 		 */
   143 		error->str = strdup("Not allowed (perhaps the filesystem "
   144 				    "doesn't support transactions)");
   145 		break;
   146 	default:
   147 		FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
   148 			       FORMAT_MESSAGE_FROM_SYSTEM|
   149 			       FORMAT_MESSAGE_IGNORE_INSERTS,
   150 			       NULL, err,
   151 			       MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
   152 			       (LPWSTR)&buf, 0, NULL);
   153 		error->str = razor_utf16_to_utf8(buf, -1);
   154 		LocalFree(buf);
   155 		break;
   156 	}
   157 
   158 	return error;
   159 }
   160 
   161 struct razor_error *
   162 razor_error_new_str2(int domain, int code, const wchar_t *object,
   163 		     const char *str)
   164 {
   165 	struct razor_error *error;
   166 
   167 	error = zalloc(sizeof *error);
   168 
   169 	error->domain = domain;
   170 	error->code = code;
   171 
   172 	if (object)
   173 		error->object = razor_utf16_to_utf8(object, -1);
   174 
   175 	error->str = strdup(str);
   176 
   177 	return error;
   178 }
   179 #endif	/* MSWIN_API */
   180 
   181 RAZOR_EXPORT struct razor_error *
   182 razor_error_new_str(int domain, int code, const char *object, const char *str)
   183 {
   184 	struct razor_error *error;
   185 
   186 	error = zalloc(sizeof *error);
   187 
   188 	error->domain = domain;
   189 	error->code = code;
   190 
   191 	if (object)
   192 		error->object = strdup(object);
   193 
   194 	error->str = strdup(str);
   195 
   196 	return error;
   197 }
   198 
   199 RAZOR_EXPORT struct razor_error *
   200 razor_error_dup(struct razor_error *src, const char *summary)
   201 {
   202 	struct razor_error *error;
   203 
   204 	error = zalloc(sizeof *error);
   205 
   206 	error->domain = src->domain;
   207 	error->code = src->code;
   208 
   209 	if (summary)
   210 		error->summary = strdup(summary);
   211 	else if (src->summary)
   212 		error->summary = strdup(src->summary);
   213 
   214 	if (src->object)
   215 		error->object = strdup(src->object);
   216 
   217 	if (src->str)
   218 		error->str = strdup(src->str);
   219 
   220 	return error;
   221 }
   222 
   223 RAZOR_EXPORT void
   224 razor_error_free(struct razor_error *error)
   225 {
   226 	free(error->object);
   227 	free(error->str);
   228 	free(error->obj_str);
   229 	free(error->summary);
   230 	free(error->msg);
   231 	free(error);
   232 }