2016-09-13 18:54:26 -07:00
|
|
|
#include "common.h"
|
|
|
|
|
2016-09-18 18:06:42 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2016-12-02 18:51:50 -08:00
|
|
|
#include "io.h"
|
2017-04-07 12:32:12 -07:00
|
|
|
#include <functional>
|
2016-12-02 18:51:50 -08:00
|
|
|
|
2016-11-06 17:31:14 -08:00
|
|
|
/* This is used to define the array of object IDs. */
|
2017-02-26 00:32:43 -08:00
|
|
|
const UT_icd object_id_icd = {sizeof(ObjectID), NULL, NULL, NULL};
|
2016-11-06 17:31:14 -08:00
|
|
|
|
2017-02-26 00:32:43 -08:00
|
|
|
const UniqueID NIL_ID = {{255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255}};
|
2016-09-29 21:12:06 -07:00
|
|
|
|
2016-12-08 20:57:08 -08:00
|
|
|
const unsigned char NIL_DIGEST[DIGEST_SIZE] = {0};
|
|
|
|
|
2017-02-26 00:32:43 -08:00
|
|
|
UniqueID globally_unique_id(void) {
|
2016-09-18 18:06:42 -07:00
|
|
|
/* Use /dev/urandom for "real" randomness. */
|
|
|
|
int fd;
|
2016-11-22 17:04:24 -08:00
|
|
|
int const flags = 0 /* for Windows compatibility */;
|
|
|
|
if ((fd = open("/dev/urandom", O_RDONLY, flags)) == -1) {
|
2016-11-15 20:33:29 -08:00
|
|
|
LOG_ERROR("Could not generate random number");
|
2016-09-18 18:06:42 -07:00
|
|
|
}
|
2017-02-26 00:32:43 -08:00
|
|
|
UniqueID result;
|
2016-12-02 18:51:50 -08:00
|
|
|
CHECK(read_bytes(fd, &result.id[0], UNIQUE_ID_SIZE) >= 0);
|
2016-09-18 18:06:42 -07:00
|
|
|
close(fd);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-07 12:32:12 -07:00
|
|
|
/* ObjectID equality function. */
|
|
|
|
bool operator==(const ObjectID &x, const ObjectID &y) {
|
|
|
|
return UNIQUE_ID_EQ(x, y);
|
|
|
|
}
|
|
|
|
|
2017-02-26 00:32:43 -08:00
|
|
|
bool ObjectID_equal(ObjectID first_id, ObjectID second_id) {
|
2016-11-18 19:57:51 -08:00
|
|
|
return UNIQUE_ID_EQ(first_id, second_id);
|
2016-11-10 18:13:26 -08:00
|
|
|
}
|
|
|
|
|
2017-02-26 00:32:43 -08:00
|
|
|
bool ObjectID_is_nil(ObjectID id) {
|
|
|
|
return ObjectID_equal(id, NIL_OBJECT_ID);
|
2016-11-08 14:46:34 -08:00
|
|
|
}
|
|
|
|
|
2017-02-26 00:32:43 -08:00
|
|
|
bool DBClientID_equal(DBClientID first_id, DBClientID second_id) {
|
2016-11-18 19:57:51 -08:00
|
|
|
return UNIQUE_ID_EQ(first_id, second_id);
|
|
|
|
}
|
|
|
|
|
2017-04-24 18:10:21 -07:00
|
|
|
bool WorkerID_equal(WorkerID first_id, WorkerID second_id) {
|
|
|
|
return UNIQUE_ID_EQ(first_id, second_id);
|
|
|
|
}
|
|
|
|
|
2017-02-26 00:32:43 -08:00
|
|
|
char *ObjectID_to_string(ObjectID obj_id, char *id_string, int id_length) {
|
2016-12-22 03:11:46 -08:00
|
|
|
CHECK(id_length >= ID_STRING_SIZE);
|
2016-09-13 18:54:26 -07:00
|
|
|
static const char hex[] = "0123456789abcdef";
|
2016-12-22 03:11:46 -08:00
|
|
|
char *buf = id_string;
|
2016-09-13 18:54:26 -07:00
|
|
|
|
|
|
|
for (int i = 0; i < UNIQUE_ID_SIZE; i++) {
|
2016-12-22 03:11:46 -08:00
|
|
|
unsigned int val = obj_id.id[i];
|
2016-09-13 18:54:26 -07:00
|
|
|
*buf++ = hex[val >> 4];
|
|
|
|
*buf++ = hex[val & 0xf];
|
|
|
|
}
|
|
|
|
*buf = '\0';
|
|
|
|
|
2016-12-22 03:11:46 -08:00
|
|
|
return id_string;
|
2016-09-13 18:54:26 -07:00
|
|
|
}
|