From da7cc6eed793940b1cda9e978e6a00dc56c9abae Mon Sep 17 00:00:00 2001 From: Pietro Cerutti Date: Fri, 13 Sep 2013 08:03:50 +0200 Subject: [PATCH] Fix uuid on FreeBSD In FreeBSD, just as in DARWIN, uuid functions are in libc and no external libuuid is required. The API is quite different from Linux's though, so a different implementation of uuid () (util.cpp) is needed. --- CMakeLists.txt | 8 ++++---- src/util.cpp | 23 +++++++++++++++++++++++ src/util.h | 4 ++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 056f1ce06..cd74e204f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,10 +90,10 @@ check_struct_has_member ("struct tm" tm_gmtoff time.h HAVE_TM_GMTOFF) check_struct_has_member ("struct stat" st_birthtime "sys/types.h;sys/stat.h" HAVE_ST_BIRTHTIME) message ("-- Looking for libuuid") -if (DARWIN) - # Apple includes the uuid functions in their libc, rather than libuuid +if (DARWIN OR FREEBSD) + # Apple and FreeBSD include the uuid functions in their libc, rather than libuuid check_function_exists (uuid_unparse_lower HAVE_UUID_UNPARSE_LOWER) -else (DARWIN) +else (DARWIN OR FREEBSD) find_path (UUID_INCLUDE_DIR uuid/uuid.h) find_library (UUID_LIBRARY NAMES uuid) if (UUID_INCLUDE_DIR AND UUID_LIBRARY) @@ -106,7 +106,7 @@ else (DARWIN) else (UUID_INCLUDE_DIR AND UUID_LIBRARY) message (FATAL_ERROR "-- libuuid not found.") endif (UUID_INCLUDE_DIR AND UUID_LIBRARY) -endif (DARWIN) +endif (DARWIN OR FREEBSD) if (HAVE_UUID_UNPARSE_LOWER) message ("-- Found libuuid") diff --git a/src/util.cpp b/src/util.cpp index a0b148c20..21480df0d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -229,6 +229,28 @@ int autoComplete ( return matches.size (); } +// Handle the generation of UUIDs on FreeBSD in a separate implementation +// of the uuid () function, since the API is quite different from Linux's. +// Also, uuid_unparse_lower is not needed on FreeBSD, because the string +// representation is always lowercase anyway. +// For the implementation details, refer to +// http://svnweb.freebsd.org/base/head/sys/kern/kern_uuid.c +#ifdef __FreeBSD__ +const std::string uuid () +{ + uuid_t id; + uint32_t status; + char *buffer (0); + uuid_create (&id, &status); + uuid_to_string (&id, &buffer, &status); + + std::string res (buffer); + free (buffer); + + return res; +} +#else + //////////////////////////////////////////////////////////////////////////////// #ifndef HAVE_UUID_UNPARSE_LOWER // Older versions of libuuid don't have uuid_unparse_lower(), only uuid_unparse() @@ -253,6 +275,7 @@ const std::string uuid () return std::string (buffer); } +#endif //////////////////////////////////////////////////////////////////////////////// // On Solaris no flock function exists. diff --git a/src/util.h b/src/util.h index 0206517c4..28de3cc65 100644 --- a/src/util.h +++ b/src/util.h @@ -32,7 +32,11 @@ #include #include #include +#ifdef __FreeBSD__ +#include +#else #include +#endif #include // util.cpp