 Makefile                     |    4 +-
 bindings/python/setup.py     |   29 +++++++++++++++++++-----
 libjio/Makefile              |    5 +++-
 libjio/compat.c              |   51 ++++++++++++++++++++++++++++++++++++++++-
 libjio/compat.h              |   24 +++++++++++++++++++
 libjio/doxygen/Doxyfile.base |    2 +-
 libjio/journal.c             |    6 +---
 libjio/libjio.skel.pc        |    2 +-
 8 files changed, 106 insertions(+), 17 deletions(-)

diff --git a/Makefile b/Makefile
index a810bc8..c5c046d 100644
--- a/Makefile
+++ b/Makefile
@@ -11,13 +11,13 @@ install:
 	$(MAKE) -C libjio/ install
 
 
-python2:
+python2: libjio
 	cd bindings/python && python setup.py build
 
 python2_install: python2
 	cd bindings/python && python setup.py install
 
-python3:
+python3: libjio
 	cd bindings/python && python3 setup.py build
 
 python3_install: python3
diff --git a/bindings/python/setup.py b/bindings/python/setup.py
index 3b6de68..a7a077b 100644
--- a/bindings/python/setup.py
+++ b/bindings/python/setup.py
@@ -10,14 +10,31 @@ elif sys.version_info[0] == 3:
 libjio = Extension("libjio",
 		libraries = ['jio'],
 		sources = ['libjio.c'],
-		define_macros = [ver_define] )
+		define_macros = [ver_define],
+
+		# these two allow us to build without having libjio installed,
+		# assuming we're in the libjio source tree
+		include_dirs = ['../../libjio/'],
+		library_dirs=['../../libjio/']
+	)
 
 setup(
 	name = 'libjio',
-	description = "A library for journaled I/O",
-	author="Alberto Bertogli",
-	author_email="albertito@blitiri.com.ar",
-	url="http://blitiri.com.ar/p/libjio",
-	ext_modules = [libjio]
+	version = '0.51',
+	description = "A library for journaled, transactional I/O",
+	author = "Alberto Bertogli",
+	author_email = "albertito@blitiri.com.ar",
+	url = "http://blitiri.com.ar/p/libjio",
+	ext_modules = [libjio],
+	classifiers = [
+		"License :: Public Domain",
+		"Operating System :: POSIX",
+		"Programming Language :: C",
+		"Programming Language :: Python",
+		"Programming Language :: Python :: 2",
+		"Programming Language :: Python :: 3",
+		"Topic :: Software Development",
+		"Topic :: Software Development :: Libraries",
+	],
 )
 
diff --git a/libjio/Makefile b/libjio/Makefile
index fecbfda..8ce1ca1 100644
--- a/libjio/Makefile
+++ b/libjio/Makefile
@@ -10,7 +10,10 @@ MANDATORY_LDFLAGS := $(shell getconf LFS_LIBS 2>/dev/null)
 ALL_CFLAGS += $(CFLAGS) $(MANDATORY_CFLAGS) -fPIC
 ALL_LDFLAGS += $(LDFLAGS) $(MANDATORY_LDFLAGS) -fPIC
 
-LIBS = -lpthread -lrt
+# some platforms do not have librt, we only use it if available
+NEED_LIBRT := $(shell ld -o rtcheck.so -shared -lrt 2>/dev/null && echo -lrt; \
+	rm -f rtcheck.so)
+LIBS = -lpthread $(NEED_LIBRT)
 
 ifdef DEBUG
 ALL_CFLAGS += -g
diff --git a/libjio/compat.c b/libjio/compat.c
index 8c7645b..3ad4463 100644
--- a/libjio/compat.c
+++ b/libjio/compat.c
@@ -10,7 +10,8 @@
 #define _REMOVE_GNU_SOURCE
 #endif
 
-#include <fcntl.h>		/* sync_range_submit(), if possible */
+/* Must be down here because otherwise we might try to #include things twice:
+ * once with _GNU_SOURCE and one without it */
 #include "compat.h"
 
 
@@ -18,6 +19,9 @@
  * sync_file_range() support through an internal similar API
  */
 
+#include <fcntl.h>		/* sync_range_submit(), if possible */
+#include <sys/types.h>		/* off_t, size_t */
+
 #ifdef SYNC_FILE_RANGE_WRITE
 const int have_sync_range = 1;
 
@@ -38,7 +42,7 @@ int sync_range_wait(int fd, off_t offset, size_t nbytes)
 
 #else
 
-#warning "No sync_file_range()"
+#warning "Using fdatasync() instead of sync_file_range()"
 const int have_sync_range = 0;
 
 int sync_range_submit(int fd, off_t offset, size_t nbytes)
@@ -53,7 +57,50 @@ int sync_range_wait(int fd, off_t offset, size_t nbytes)
 
 #endif /* defined SYNC_FILE_RANGE_WRITE */
 
+/* It is no longer needed */
 #ifdef _REMOVE_GNU_SOURCE
 #undef _GNU_SOURCE
 #endif
 
+
+/* When posix_fadvise() is not available, we just show a message since there
+ * is no alternative implementation */
+#ifdef LACK_POSIX_FADVISE
+#warning "Not using posix_fadvise()"
+#endif
+
+
+/*
+ * Support for platforms where clock_gettime() is not available.
+ */
+
+#ifdef LACK_CLOCK_GETTIME
+#warning "Using gettimeofday() instead of clock_gettime()"
+
+#include <sys/time.h>		/* gettimeofday() */
+
+int clock_gettime(int clk_id, struct timespec *tp)
+{
+	struct timeval tv;
+
+	gettimeofday(&tv, NULL);
+
+	tp->tv_sec = tv.tv_sec;
+	tp->tv_nsec = tv.tv_usec / 1000.0;
+
+	return 0;
+}
+
+#endif /* defined LACK_CLOCK_GETTIME */
+
+#ifdef LACK_FDATASYNC
+#warning "Using fsync() instead of fdatasync()"
+
+#include <unistd.h>		/* fsync() */
+
+int fdatasync(int fd)
+{
+	return fsync(fd);
+}
+#endif /* defined LACK_FDATASYNC */
+
diff --git a/libjio/compat.h b/libjio/compat.h
index cf90798..cceeb29 100644
--- a/libjio/compat.h
+++ b/libjio/compat.h
@@ -4,6 +4,9 @@
 #ifndef _COMPAT_H
 #define _COMPAT_H
 
+#include <sys/types.h>		/* off_t, size_t */
+
+
 /* sync_file_range() is linux-specific, so we provide an internal similar API,
  * with a constant to be able to check for its presence; the implementation is
  * in compat.c */
@@ -20,8 +23,29 @@ int sync_range_wait(int fd, off_t offset, size_t nbytes);
  * nice, but it's simple, it works and should be reliable. */
 #include <fcntl.h>
 #ifndef POSIX_FADV_WILLNEED
+#define LACK_POSIX_FADVISE 1
 #define posix_fadvise(fd, offset, len, advise)
 #endif
 
+
+/* fdatasync() is super standard, but some BSDs (FreeBSD, DragonflyBSD at
+ * least) do not have it. Since there is no reliable way to test for it, we
+ * have to resort to OS detection. */
+#if ! ( (defined __linux__) || (defined (__SVR4) && defined (__sun)) )
+#define LACK_FDATASYNC 1
+int fdatasync(int fd);
+#endif
+
+
+/* Some platforms do not have clock_gettime() so we define an alternative for
+ * them, in compat.c. We should check for _POSIX_TIMERS, but some platforms do
+ * not have it yet they do have clock_gettime() (DragonflyBSD), so we just
+ * check for CLOCK_REALTIME. */
+#include <time.h>
+#ifndef CLOCK_REALTIME
+#define LACK_CLOCK_GETTIME 1
+#define CLOCK_REALTIME 0
+#endif
+
 #endif
 
diff --git a/libjio/doxygen/Doxyfile.base b/libjio/doxygen/Doxyfile.base
index 9db9a87..aae43eb 100644
--- a/libjio/doxygen/Doxyfile.base
+++ b/libjio/doxygen/Doxyfile.base
@@ -1,6 +1,6 @@
 DOXYFILE_ENCODING      = UTF-8
 PROJECT_NAME           = libjio
-PROJECT_NUMBER         = 0.50
+PROJECT_NUMBER         = 0.51
 OUTPUT_DIRECTORY       = 
 CREATE_SUBDIRS         = NO
 OUTPUT_LANGUAGE        = English
diff --git a/libjio/journal.c b/libjio/journal.c
index be9e04b..b059eee 100644
--- a/libjio/journal.c
+++ b/libjio/journal.c
@@ -196,10 +196,8 @@ unlink_error:
 	close(fd);
 
 error:
-	if (name)
-		free(name);
-	if (jop)
-		free(jop);
+	free(name);
+	free(jop);
 
 	return NULL;
 }
diff --git a/libjio/libjio.skel.pc b/libjio/libjio.skel.pc
index 3a7b0f1..79f0b99 100644
--- a/libjio/libjio.skel.pc
+++ b/libjio/libjio.skel.pc
@@ -6,7 +6,7 @@ includedir=${prefix}/include
 Name: libjio
 Description: A library for Journaled I/O
 URL: http://blitiri.com.ar/p/libjio/
-Version: 0.23
+Version: 0.51
 Libs: -L${libdir} -ljio
 Cflags: -I${includedir} ++CFLAGS++
 
