Commit 6d3ad7e09ee4b101e8e68f38783e3e4139bc2691

Edward Thomson 2016-12-13T10:58:43

Add `ENABLE_SYNCHRONIZED_OBJECT_CREATION` option Allow users to enable `SYNCHRONIZED_OBJECT_CREATION` with a setting.

diff --git a/include/git2/common.h b/include/git2/common.h
index f13dfd5..f5d37ed 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -179,6 +179,7 @@ typedef enum {
 	GIT_OPT_SET_SSL_CIPHERS,
 	GIT_OPT_GET_USER_AGENT,
 	GIT_OPT_ENABLE_OFS_DELTA,
+	GIT_OPT_ENABLE_SYNCHRONIZED_OBJECT_CREATION,
 } git_libgit2_opt_t;
 
 /**
@@ -316,6 +317,13 @@ typedef enum {
  *		> Packfiles containing offset deltas can still be read.
  *		> This defaults to enabled.
  *
+ *	* opts(GIT_OPT_ENABLE_SYNCHRONIZED_OBJECT_CREATION, int enabled)
+ *
+ *		> Enable synchronized writes of new objects using `fsync`
+ *		> (or the platform equivalent) to ensure that new object data
+ *		> is written to permanent storage, not simply cached.  This
+ *		> defaults to disabled.
+ *
  * @param option Option key
  * @param ... value to set the option
  * @return 0 on success, <0 on failure
diff --git a/src/object.c b/src/object.c
index 2da36a2..c92adbb 100644
--- a/src/object.c
+++ b/src/object.c
@@ -16,6 +16,7 @@
 #include "tag.h"
 
 bool git_object__strict_input_validation = true;
+bool git_object__synchronized_writing = false;
 
 typedef struct {
 	const char	*str;	/* type name string */
diff --git a/src/object.h b/src/object.h
index dd227d1..7d94d8b 100644
--- a/src/object.h
+++ b/src/object.h
@@ -10,6 +10,7 @@
 #include "repository.h"
 
 extern bool git_object__strict_input_validation;
+extern bool git_object__synchronized_writing;
 
 /** Base git object for inheritance */
 struct git_object {
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 8796f4b..5a80b7a 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -14,6 +14,7 @@
 #include "odb.h"
 #include "delta.h"
 #include "filebuf.h"
+#include "object.h"
 
 #include "git2/odb_backend.h"
 #include "git2/types.h"
@@ -843,7 +844,7 @@ static int filebuf_flags(loose_backend *backend)
 	int flags = GIT_FILEBUF_TEMPORARY |
 		(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT);
 
-	if (backend->fsync_object_files)
+	if (backend->fsync_object_files || git_object__synchronized_writing)
 		flags |= GIT_FILEBUF_FSYNC;
 
 	return flags;
diff --git a/src/settings.c b/src/settings.c
index 2158567..f3120ff 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -227,6 +227,10 @@ int git_libgit2_opts(int key, ...)
 		git_smart__ofs_delta_enabled = (va_arg(ap, int) != 0);
 		break;
 
+	case GIT_OPT_ENABLE_SYNCHRONIZED_OBJECT_CREATION:
+		git_object__synchronized_writing = (va_arg(ap, int) != 0);
+		break;
+
 	default:
 		giterr_set(GITERR_INVALID, "invalid option key");
 		error = -1;