• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

rpmio/rpmuuid.c

Go to the documentation of this file.
00001 
00005 #if defined(__APPLE__)
00006 /* workaround for "uuid_t" type conflict, between <unistd.h> and "uuid.h" */
00007 #define _UUID_T
00008 #define uuid_t __darwin_uuid_t
00009 #include <unistd.h>
00010 #undef uuid_t
00011 #undef _UUID_T
00012 #endif
00013 
00014 #include "system.h"
00015 #include <string.h>
00016 #include "rpmlog.h"
00017 #include "rpmuuid.h"
00018 #ifdef WITH_UUID
00019 #include "uuid.h"
00020 #endif
00021 #include "debug.h"
00022 
00023 int rpmuuidMake(int version, const char *ns, const char *data, char *buf_str, unsigned char *buf_bin)
00024 {
00025 #ifdef WITH_UUID
00026     uuid_rc_t rc;
00027     uuid_t *uuid = NULL;
00028     uuid_t *uuid_ns = NULL;
00029     char *result_ptr;
00030     size_t result_len;
00031 
00032     /* sanity check version */
00033     if (!(version == 1 || (version >= 3 && version <= 5))) {
00034         rpmlog(RPMLOG_ERR, _("invalid UUID version number"));
00035         return 1;
00036     }
00037     if ((version == 3 || version == 5) && (ns == NULL || data == NULL)) {
00038         rpmlog(RPMLOG_ERR, _("namespace or data required for requested UUID version\n"));
00039         return 1;
00040     }
00041     if (buf_str == NULL && buf_bin == NULL) {
00042         rpmlog(RPMLOG_ERR, _("either string or binary result buffer required\n"));
00043         return 1;
00044     }
00045 
00046     /* create UUID object */
00047     if ((rc = uuid_create(&uuid)) != UUID_RC_OK) {
00048         rpmlog(RPMLOG_ERR, _("failed to create UUID object: %s\n"), uuid_error(rc));
00049         return 1;
00050     }
00051 
00052     /* create optional UUID namespace object */
00053     if (version == 3 || version == 5) {
00054         if ((rc = uuid_create(&uuid_ns)) != UUID_RC_OK) {
00055             rpmlog(RPMLOG_ERR, _("failed to create UUID namespace object: %s\n"), uuid_error(rc));
00056             return 1;
00057         }
00058         if ((rc = uuid_load(uuid_ns, ns)) != UUID_RC_OK) {
00059             if ((rc = uuid_import(uuid_ns, UUID_FMT_STR, ns, strlen(ns))) != UUID_RC_OK) {
00060                 rpmlog(RPMLOG_ERR, _("failed to import UUID namespace object: %s\n"), uuid_error(rc));
00061                 return 1;
00062             }
00063         }
00064     }
00065 
00066     /*  generate UUID  */
00067     if (version == 1)
00068         rc = uuid_make(uuid, UUID_MAKE_V1);
00069     else if (version == 3)
00070         rc = uuid_make(uuid, UUID_MAKE_V3, uuid_ns, data);
00071     else if (version == 4)
00072         rc = uuid_make(uuid, UUID_MAKE_V4);
00073     else if (version == 5)
00074         rc = uuid_make(uuid, UUID_MAKE_V5, uuid_ns, data);
00075     if (rc != UUID_RC_OK) {
00076         (void) uuid_destroy(uuid);
00077         if (uuid_ns != NULL)
00078             (void) uuid_destroy(uuid_ns);
00079         rpmlog(RPMLOG_ERR, _("failed to make UUID object: %s\n"), uuid_error(rc));
00080         return 1;
00081     }
00082 
00083     /*  export UUID */
00084     if (buf_str != NULL) {
00085         result_ptr = buf_str;
00086         result_len = UUID_LEN_STR+1;
00087         if ((rc = uuid_export(uuid, UUID_FMT_STR, &result_ptr, &result_len)) != UUID_RC_OK) {
00088             (void) uuid_destroy(uuid);
00089             if (uuid_ns != NULL)
00090                 (void) uuid_destroy(uuid_ns);
00091             rpmlog(RPMLOG_ERR, _("failed to export UUID object as string representation: %s\n"), uuid_error(rc));
00092             return 1;
00093         }
00094     }
00095     if (buf_bin != NULL) {
00096         result_ptr = (char *)buf_bin;
00097         result_len = UUID_LEN_BIN;
00098         if ((rc = uuid_export(uuid, UUID_FMT_BIN, &result_ptr, &result_len)) != UUID_RC_OK) {
00099             (void) uuid_destroy(uuid);
00100             if (uuid_ns != NULL)
00101                 (void) uuid_destroy(uuid_ns);
00102             rpmlog(RPMLOG_ERR, _("failed to export UUID object as binary representation: %s\n"), uuid_error(rc));
00103             return 1;
00104         }
00105     }
00106 
00107     /*  destroy UUID object(s)  */
00108     (void) uuid_destroy(uuid);
00109     if (uuid_ns != NULL)
00110         (void) uuid_destroy(uuid_ns);
00111 
00112     /* indicate success */
00113     return 0;
00114 #else
00115     rpmlog(RPMLOG_ERR, _("UUID generator not available!\n"));
00116 
00117     /* indicate error */
00118     return 1;
00119 #endif
00120 }
00121 

Generated on Fri Dec 3 2010 20:54:41 for rpm by  doxygen 1.7.2