git_indexwriter: lock then write the index Introduce `git_indexwriter`, to allow us to lock the index while performing additional operations, then complete the write (or abort, unlocking the index).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
diff --git a/src/index.c b/src/index.c
index cbace36..58575fe 100644
--- a/src/index.c
+++ b/src/index.c
@@ -656,39 +656,15 @@ int git_index__changed_relative_to(
int git_index_write(git_index *index)
{
- git_filebuf file = GIT_FILEBUF_INIT;
+ git_indexwriter writer = GIT_INDEXWRITER_INIT;
int error;
- if (!index->index_file_path)
- return create_index_error(-1,
- "Failed to read index: The index is in-memory only");
-
- if (index_sort_if_needed(index, true) < 0)
- return -1;
- git_vector_sort(&index->reuc);
-
- if ((error = git_filebuf_open(
- &file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) {
- if (error == GIT_ELOCKED)
- giterr_set(GITERR_INDEX, "The index is locked. This might be due to a concurrent or crashed process");
-
- return error;
- }
+ if ((error = git_indexwriter_init(&writer, index)) == 0)
+ error = git_indexwriter_commit(&writer);
- if ((error = write_index(index, &file)) < 0) {
- git_filebuf_cleanup(&file);
- return error;
- }
+ git_indexwriter_cleanup(&writer);
- if ((error = git_filebuf_commit(&file)) < 0)
- return error;
-
- if (git_futils_filestamp_check(&index->stamp, index->index_file_path) < 0)
- /* index could not be read from disk! */;
- else
- index->on_disk = 1;
-
- return 0;
+ return error;
}
const char * git_index_path(const git_index *index)
@@ -2686,3 +2662,59 @@ int git_index_snapshot_find(
{
return index_find_in_entries(out, entries, entry_srch, path, path_len, stage);
}
+
+int git_indexwriter_init(
+ git_indexwriter *writer,
+ git_index *index)
+{
+ int error;
+
+ writer->index = index;
+
+ if (!index->index_file_path)
+ return create_index_error(-1,
+ "Failed to write index: The index is in-memory only");
+
+ if ((error = git_filebuf_open(
+ &writer->file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) {
+ if (error == GIT_ELOCKED)
+ giterr_set(GITERR_INDEX, "The index is locked. This might be due to a concurrent or crashed process");
+
+ return error;
+ }
+
+ return 0;
+}
+
+int git_indexwriter_commit(git_indexwriter *writer)
+{
+ int error;
+
+ if (index_sort_if_needed(writer->index, true) < 0)
+ return -1;
+
+ git_vector_sort(&writer->index->reuc);
+
+ if ((error = write_index(writer->index, &writer->file)) < 0) {
+ git_indexwriter_cleanup(writer);
+ return error;
+ }
+
+ if ((error = git_filebuf_commit(&writer->file)) < 0)
+ return error;
+
+ if ((error = git_futils_filestamp_check(
+ &writer->index->stamp, writer->index->index_file_path)) < 0) {
+ giterr_set(GITERR_OS, "Could not read index timestamp");
+ return -1;
+ }
+
+ writer->index->on_disk = 1;
+
+ return 0;
+}
+
+void git_indexwriter_cleanup(git_indexwriter *writer)
+{
+ git_filebuf_cleanup(&writer->file);
+}
diff --git a/src/index.h b/src/index.h
index 2eb93fb..d151f66 100644
--- a/src/index.h
+++ b/src/index.h
@@ -94,4 +94,22 @@ extern int git_index_snapshot_find(
const char *path, size_t path_len, int stage);
+typedef struct {
+ git_index *index;
+ git_filebuf file;
+} git_indexwriter;
+
+#define GIT_INDEXWRITER_INIT { NULL, GIT_FILEBUF_INIT }
+
+/* Lock the index for eventual writing. */
+extern int git_indexwriter_init(git_indexwriter *writer, git_index *index);
+
+/* Write the index and unlock it. */
+extern int git_indexwriter_commit(git_indexwriter *writer);
+
+/* Cleanup an index writing session, unlocking the file (if it is still
+ * locked and freeing any data structures.
+ */
+extern void git_indexwriter_cleanup(git_indexwriter *writer);
+
#endif
diff --git a/tests/index/tests.c b/tests/index/tests.c
index a63183e..4cf7051 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -677,3 +677,24 @@ void test_index_tests__reload_while_ignoring_case(void)
git_index_free(index);
}
+
+void test_index_tests__can_lock_index(void)
+{
+ git_index *index;
+ git_indexwriter one = GIT_INDEXWRITER_INIT,
+ two = GIT_INDEXWRITER_INIT;
+
+ cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
+ cl_git_pass(git_indexwriter_init(&one, index));
+
+ cl_git_fail_with(GIT_ELOCKED, git_indexwriter_init(&two, index));
+ cl_git_fail_with(GIT_ELOCKED, git_index_write(index));
+
+ cl_git_pass(git_indexwriter_commit(&one));
+
+ cl_git_pass(git_index_write(index));
+
+ git_indexwriter_cleanup(&one);
+ git_indexwriter_cleanup(&two);
+ git_index_free(index);
+}