From 6ec9ef1881e636540cfe28e6f32c93e1781ad173 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Fri, 22 Aug 2008 12:15:56 -0400 Subject: [PATCH] libuuid: Windows portability fixes Make the uuid library (more) portable for Windows. Addresses-Sourceforge-Feature-Request: #1937287 Signed-off-by: "Theodore Ts'o" --- configure | 3 ++- configure.in | 2 +- lib/uuid/gen_uuid.c | 46 +++++++++++++++++++++++++++++++++++++++++++--- lib/uuid/tst_uuid.c | 6 ++++++ lib/uuid/uuid.h | 2 ++ lib/uuid/uuid_time.c | 20 +++++++++++++++----- 6 files changed, 69 insertions(+), 10 deletions(-) diff --git a/configure b/configure index 74ec761..db058bd 100755 --- a/configure +++ b/configure @@ -11883,7 +11883,8 @@ fi -for ac_header in dirent.h errno.h getopt.h malloc.h mntent.h paths.h setjmp.h signal.h stdarg.h stdint.h stdlib.h termios.h termio.h unistd.h utime.h linux/fd.h linux/major.h net/if_dl.h netinet/in.h sys/disklabel.h sys/ioctl.h sys/mkdev.h sys/mman.h sys/prctl.h sys/queue.h sys/resource.h sys/select.h sys/socket.h sys/sockio.h sys/stat.h sys/syscall.h sys/sysmacros.h sys/time.h sys/types.h sys/un.h sys/wait.h + +for ac_header in dirent.h errno.h getopt.h malloc.h mntent.h paths.h setjmp.h signal.h stdarg.h stdint.h stdlib.h termios.h termio.h unistd.h utime.h linux/fd.h linux/major.h net/if_dl.h netinet/in.h sys/disklabel.h sys/file.h sys/ioctl.h sys/mkdev.h sys/mman.h sys/prctl.h sys/queue.h sys/resource.h sys/select.h sys/socket.h sys/sockio.h sys/stat.h sys/syscall.h sys/sysmacros.h sys/time.h sys/types.h sys/un.h sys/wait.h do as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh` if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then diff --git a/configure.in b/configure.in index 3b9c53f..2e0f935 100644 --- a/configure.in +++ b/configure.in @@ -623,7 +623,7 @@ if test $cross_compiling = no; then else AC_CHECK_PROGS(BUILD_CC, gcc cc) fi -AC_CHECK_HEADERS(dirent.h errno.h getopt.h malloc.h mntent.h paths.h setjmp.h signal.h stdarg.h stdint.h stdlib.h termios.h termio.h unistd.h utime.h linux/fd.h linux/major.h net/if_dl.h netinet/in.h sys/disklabel.h sys/ioctl.h sys/mkdev.h sys/mman.h sys/prctl.h sys/queue.h sys/resource.h sys/select.h sys/socket.h sys/sockio.h sys/stat.h sys/syscall.h sys/sysmacros.h sys/time.h sys/types.h sys/un.h sys/wait.h) +AC_CHECK_HEADERS(dirent.h errno.h getopt.h malloc.h mntent.h paths.h setjmp.h signal.h stdarg.h stdint.h stdlib.h termios.h termio.h unistd.h utime.h linux/fd.h linux/major.h net/if_dl.h netinet/in.h sys/disklabel.h sys/file.h sys/ioctl.h sys/mkdev.h sys/mman.h sys/prctl.h sys/queue.h sys/resource.h sys/select.h sys/socket.h sys/sockio.h sys/stat.h sys/syscall.h sys/sysmacros.h sys/time.h sys/types.h sys/un.h sys/wait.h) AC_CHECK_HEADERS(sys/disk.h sys/mount.h,,, [[ #if HAVE_SYS_QUEUE_H diff --git a/lib/uuid/gen_uuid.c b/lib/uuid/gen_uuid.c index fd9314d..152a6c0 100644 --- a/lib/uuid/gen_uuid.c +++ b/lib/uuid/gen_uuid.c @@ -38,6 +38,11 @@ */ #define _SVID_SOURCE +#ifdef _WIN32 +#define _WIN32_WINNT 0x0500 +#include +#define UUID MYUUID +#endif #include #ifdef HAVE_UNISTD_H #include @@ -49,10 +54,14 @@ #include #include #include +#ifdef HAVE_SYS_TIME_H #include +#endif #include #include +#ifdef HAVE_SYS_FILE_H #include +#endif #ifdef HAVE_SYS_IOCTL_H #include #endif @@ -97,6 +106,30 @@ THREAD_LOCAL unsigned short jrand_seed[3]; #endif +#ifdef _WIN32 +static void gettimeofday (struct timeval *tv, void *dummy) +{ + FILETIME ftime; + uint64_t n; + + GetSystemTimeAsFileTime (&ftime); + n = (((uint64_t) ftime.dwHighDateTime << 32) + + (uint64_t) ftime.dwLowDateTime); + if (n) { + n /= 10; + n -= ((369 * 365 + 89) * (uint64_t) 86400) * 1000000; + } + + tv->tv_sec = n / 1000000; + tv->tv_usec = n % 1000000; +} + +static int getuid (void) +{ + return 1; +} +#endif + static int get_random_fd(void) { struct timeval tv; @@ -105,6 +138,7 @@ static int get_random_fd(void) if (fd == -2) { gettimeofday(&tv, 0); +#ifndef _WIN32 fd = open("/dev/urandom", O_RDONLY); if (fd == -1) fd = open("/dev/random", O_RDONLY | O_NONBLOCK); @@ -113,6 +147,7 @@ static int get_random_fd(void) if (i >= 0) fcntl(fd, F_SETFD, i | FD_CLOEXEC); } +#endif srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec); #ifdef DO_JRAND_MIX jrand_seed[0] = getpid() ^ (tv.tv_sec & 0xFFFF); @@ -173,6 +208,11 @@ static void get_random_bytes(void *buf, int nbytes) /* * Get the ethernet hardware address, if we can find it... + * + * XXX for a windows version, probably should use GetAdaptersInfo: + * http://www.codeguru.com/cpp/i-n/network/networkinformation/article.php/c5451 + * commenting out get_node_id just to get gen_uuid to compile under windows + * is not the right way to go! */ static int get_node_id(unsigned char *node_id) { @@ -269,7 +309,7 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low, THREAD_LOCAL uint16_t clock_seq; struct timeval tv; struct flock fl; - unsigned long long clock_reg; + uint64_t clock_reg; mode_t save_umask; if (state_fd == -2) { @@ -339,8 +379,8 @@ try_again: } clock_reg = tv.tv_usec*10 + adjustment; - clock_reg += ((unsigned long long) tv.tv_sec)*10000000; - clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000; + clock_reg += ((uint64_t) tv.tv_sec)*10000000; + clock_reg += (((uint64_t) 0x01B21DD2) << 32) + 0x13814000; if (num && (*num > 1)) { adjustment += *num - 1; diff --git a/lib/uuid/tst_uuid.c b/lib/uuid/tst_uuid.c index 086d35c..01606d7 100644 --- a/lib/uuid/tst_uuid.c +++ b/lib/uuid/tst_uuid.c @@ -32,6 +32,12 @@ * %End-Header% */ +#ifdef _WIN32 +#define _WIN32_WINNT 0x0500 +#include +#define UUID MYUUID +#endif + #include #include diff --git a/lib/uuid/uuid.h b/lib/uuid/uuid.h index e9cf889..1c241e3 100644 --- a/lib/uuid/uuid.h +++ b/lib/uuid/uuid.h @@ -36,7 +36,9 @@ #define _UUID_UUID_H #include +#ifndef _WIN32 #include +#endif #include typedef unsigned char uuid_t[16]; diff --git a/lib/uuid/uuid_time.c b/lib/uuid/uuid_time.c index d5f992b..fc1d266 100644 --- a/lib/uuid/uuid_time.c +++ b/lib/uuid/uuid_time.c @@ -34,28 +34,38 @@ * %End-Header% */ +#ifdef _WIN32 +#define _WIN32_WINNT 0x0500 +#include +#define UUID MYUUID +#endif + #include +#ifdef HAVE_UNISTD_H #include +#endif #include #include +#ifdef HAVE_SYS_TIME_H #include +#endif #include #include "uuidP.h" time_t uuid_time(const uuid_t uu, struct timeval *ret_tv) { - struct uuid uuid; - uint32_t high; struct timeval tv; - unsigned long long clock_reg; + struct uuid uuid; + uint32_t high; + uint64_t clock_reg; uuid_unpack(uu, &uuid); high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16); - clock_reg = uuid.time_low | ((unsigned long long) high << 32); + clock_reg = uuid.time_low | ((uint64_t) high << 32); - clock_reg -= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000; + clock_reg -= (((uint64_t) 0x01B21DD2) << 32) + 0x13814000; tv.tv_sec = clock_reg / 10000000; tv.tv_usec = (clock_reg % 10000000) / 10; -- 1.8.3.1