rpm 5.3.12
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     int ec = 1;         /* assume error */
00026 #ifdef WITH_UUID
00027     uuid_rc_t rc;
00028     uuid_t *uuid = NULL;
00029     uuid_t *uuid_ns = NULL;
00030     char *result_ptr;
00031     size_t result_len;
00032 
00033     /* sanity check version */
00034     if (!(version == 1 || (version >= 3 && version <= 5))) {
00035         rpmlog(RPMLOG_ERR, _("invalid UUID version number"));
00036         goto exit;
00037     }
00038     if ((version == 3 || version == 5) && (ns == NULL || data == NULL)) {
00039         rpmlog(RPMLOG_ERR, _("namespace or data required for requested UUID version\n"));
00040         goto exit;
00041     }
00042     if (buf_str == NULL && buf_bin == NULL) {
00043         rpmlog(RPMLOG_ERR, _("either string or binary result buffer required\n"));
00044         goto exit;
00045     }
00046 
00047     /* create UUID object */
00048     if ((rc = uuid_create(&uuid)) != UUID_RC_OK) {
00049         rpmlog(RPMLOG_ERR, _("failed to create UUID object: %s\n"), uuid_error(rc));
00050         goto exit;
00051     }
00052 
00053     /* create optional UUID namespace object */
00054     if (version == 3 || version == 5) {
00055         if ((rc = uuid_create(&uuid_ns)) != UUID_RC_OK) {
00056             rpmlog(RPMLOG_ERR, _("failed to create UUID namespace object: %s\n"), uuid_error(rc));
00057             goto exit;
00058         }
00059         if ((rc = uuid_load(uuid_ns, ns)) != UUID_RC_OK) {
00060             if ((rc = uuid_import(uuid_ns, UUID_FMT_STR, ns, strlen(ns))) != UUID_RC_OK) {
00061                 rpmlog(RPMLOG_ERR, _("failed to import UUID namespace object: %s\n"), uuid_error(rc));
00062                 goto exit;
00063             }
00064         }
00065     }
00066 
00067     /*  generate UUID  */
00068     if (version == 1)
00069         rc = uuid_make(uuid, UUID_MAKE_V1);
00070     else if (version == 3)
00071         rc = uuid_make(uuid, UUID_MAKE_V3, uuid_ns, data);
00072     else if (version == 4)
00073         rc = uuid_make(uuid, UUID_MAKE_V4);
00074     else if (version == 5)
00075         rc = uuid_make(uuid, UUID_MAKE_V5, uuid_ns, data);
00076     if (rc != UUID_RC_OK) {
00077         rpmlog(RPMLOG_ERR, _("failed to make UUID object: %s\n"), uuid_error(rc));
00078         goto exit;
00079     }
00080 
00081     /*  export UUID */
00082     if (buf_str != NULL) {
00083         result_ptr = buf_str;
00084         result_len = UUID_LEN_STR+1;
00085         if ((rc = uuid_export(uuid, UUID_FMT_STR, &result_ptr, &result_len)) != UUID_RC_OK) {
00086             rpmlog(RPMLOG_ERR, _("failed to export UUID object as string representation: %s\n"), uuid_error(rc));
00087             goto exit;
00088         }
00089     }
00090     if (buf_bin != NULL) {
00091         result_ptr = (char *)buf_bin;
00092         result_len = UUID_LEN_BIN;
00093         if ((rc = uuid_export(uuid, UUID_FMT_BIN, &result_ptr, &result_len)) != UUID_RC_OK) {
00094             rpmlog(RPMLOG_ERR, _("failed to export UUID object as binary representation: %s\n"), uuid_error(rc));
00095             goto exit;
00096         }
00097     }
00098 
00099 exit:
00100     /*  destroy UUID object(s)  */
00101     if (uuid != NULL)
00102            (void) uuid_destroy(uuid);
00103     if (uuid_ns != NULL)
00104         (void) uuid_destroy(uuid_ns);
00105 
00106     ec = 0;     /* indicate success */
00107 #else
00108     rpmlog(RPMLOG_ERR, _("UUID generator not available!\n"));
00109 #endif
00110 
00111     return ec;
00112 }
00113