Skip to content
Permalink
Browse files

switch to own tinyuuid implementation

  • Loading branch information...
jacobgorm committed Feb 28, 2019
1 parent 1dd223a commit c97d549a01710878adccddca700d0fed53cb03b2
Showing with 92 additions and 10 deletions.
  1. +6 −4 CMakeLists.txt
  2. +6 −5 block-swap.c
  3. +2 −1 nbd.c
  4. +69 −0 tinyuuid.c
  5. +9 −0 tinyuuid.h
@@ -23,6 +23,7 @@ add_library(swap
block-swap.c
aio.c
ioh.c
tinyuuid.c
block-swap/crypto.c
block-swap/dubtree.c
block-swap/hashtable.c
@@ -34,16 +35,16 @@ install(TARGETS swap
ARCHIVE DESTINATION lib)

add_executable(oneroot nbd.c)
target_link_libraries(oneroot swap -lpthread -llz4 -luuid ${OPENSSL_LIBRARIES} ${CURL_LIBRARIES})
target_link_libraries(oneroot swap -lpthread -llz4 ${OPENSSL_LIBRARIES} ${CURL_LIBRARIES})

add_executable(img-test img-test.c mt19937-64.c)
target_link_libraries(img-test swap -lpthread -llz4 -luuid ${OPENSSL_LIBRARIES} ${CURL_LIBRARIES})
target_link_libraries(img-test swap -lpthread -llz4 ${OPENSSL_LIBRARIES} ${CURL_LIBRARIES})

add_executable(img-fsck img-fsck.c)
target_link_libraries(img-fsck swap -lpthread -llz4 -luuid ${OPENSSL_LIBRARIES} ${CURL_LIBRARIES})
target_link_libraries(img-fsck swap -lpthread -llz4 ${OPENSSL_LIBRARIES} ${CURL_LIBRARIES})

add_executable(img-heat img-heat.c mt19937-64.c)
target_link_libraries(img-heat swap -lpthread -llz4 -luuid ${OPENSSL_LIBRARIES} ${CURL_LIBRARIES})
target_link_libraries(img-heat swap -lpthread -llz4 ${OPENSSL_LIBRARIES} ${CURL_LIBRARIES})

install(TARGETS oneroot img-fsck img-test DESTINATION bin)

@@ -52,6 +53,7 @@ if (BUILD_PYKV)
kv.c
aio.c
ioh.c
tinyuuid.c
block-swap/crypto.c
block-swap/dubtree.c
block-swap/hashtable.c
@@ -55,6 +55,7 @@
#include "thread-event.h"
//#include "timer.h"
#include "queue.h"
#include "tinyuuid.h"

#ifndef _WIN32
#include <sys/mman.h>
@@ -765,7 +766,7 @@ static int swap_read_header(BDRVSwapState *s)
if (!strncmp(line, "size=", 5)) {
s->size = strtoll(line + 5, NULL, 0);
} else if (!strncmp(line, "uuid=", 5)) {
uuid_parse(line + 5 + (line[5]=='{'), s->uuid);
tiny_uuid_parse(line + 5 + (line[5]=='{'), s->uuid);
} else if (!strncmp(line, "swapdata=", 9)) {
s->swapdata = strdup(line + 9);
if (!s->swapdata) {
@@ -803,7 +804,7 @@ static int swap_write_header(BDRVSwapState *s)
}

char uuid_str[37];
uuid_unparse_lower(s->uuid, uuid_str);
tiny_uuid_unparse(s->uuid, uuid_str);
fprintf(f, "uuid=%s\n", uuid_str);
fprintf(f, "size=%lu\n", s->size);

@@ -918,7 +919,7 @@ int swap_open(BlockDriverState *bs, const char *filename, int flags)
* of "swapdata" component. */
char *default_swapdata;
char uuid_str[37];
uuid_unparse_lower(s->uuid, uuid_str);
tiny_uuid_unparse(s->uuid, uuid_str);
asprintf(&default_swapdata, "swapdata-%s", uuid_str);
asprintf(&swapdata, "%s/%s",
real_path, s->swapdata ? s->swapdata : default_swapdata);
@@ -1742,8 +1743,8 @@ int swap_create(const char *filename, int64_t size, int flags)
}

//uuid_generate_truly_random(uuid);
uuid_generate_random(uuid);
uuid_unparse_lower(uuid, uuid_str);
tiny_uuid_generate_random(uuid);
tiny_uuid_unparse(uuid, uuid_str);

ret = fprintf(file, "uuid=%s\n", uuid_str);
if (ret < 0) {
3 nbd.c
@@ -23,6 +23,7 @@
#include "aio.h"
#include "block-swap.h"
#include "ioh.h"
#include "tinyuuid.h"

extern void dump_swapstat(void);

@@ -387,7 +388,7 @@ int main(int argc, char **argv)
uuid_t uuid;
swap_ioctl(&bs, 0, uuid);
char uuid_str[37];
uuid_unparse_lower(uuid, uuid_str);
tiny_uuid_unparse(uuid, uuid_str);

char pid_str[16];
sprintf(pid_str, "%d", getpid());
@@ -0,0 +1,69 @@
#include <stdio.h>
#include <err.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#include "tinyuuid.h"

void tiny_uuid_generate_random(uuid_t out) {
int r;
int f = open("/dev/urandom", O_RDONLY);
if (f >= 0) {
do {
r = read(f, out, sizeof(uuid_t));
} while (r == EINTR);
close(f);
} else {
err(1, "unable to open /dev/urandom");
}

}

void tiny_uuid_unparse(uuid_t src, char *dst) {

for (int i = 0; i < 16; ++i) {
switch (i) {
case 4:
case 6:
case 8:
case 10:
*dst++ = '-';
default:
break;
}
sprintf(dst, "%02x", src[i]);
dst +=2 ;
}
*dst = '\0';
}

static inline unsigned int hex_to_int(char c) {
char d = (c >= '0' && c <= '9') ? '0' :
(c >= 'a' && c <= 'f') ? ('a' - 0xa) :
(c >= 'A' && c <= 'F') ? ('A' - 0xa) : 0;
return c - d;
}

int tiny_uuid_parse(char *src, uuid_t dst) {

for (int i = 0; i < 16; ++i) {
switch (i) {
case 4:
case 6:
case 8:
case 10:
if (*src++ != '-') {
return -1;
}
default:
break;
}
*dst++ = (hex_to_int(src[0]) << 4) | hex_to_int(src[1]);
src += 2;
}
return 0;
}
@@ -0,0 +1,9 @@
#ifndef __UUID_H__
#define __UUID_H__

typedef unsigned char uuid_t[16];
void tiny_uuid_generate_random(uuid_t out);
void tiny_uuid_unparse(uuid_t src, char *dst);
int tiny_uuid_parse(char *src, uuid_t dst);

#endif /* __UUID_H__ */

0 comments on commit c97d549

Please sign in to comment.
You can’t perform that action at this time.