Commit bf6a5b6143a733dc171878140f46c419160e1660

Philip Kelley 2014-05-17T11:38:03

Merge remote-tracking branch 'upstream/cmn/indexer-mmap' into development

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
diff --git a/src/indexer.c b/src/indexer.c
index 68496ce..ebfdffb 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -34,7 +34,6 @@ struct git_indexer {
 		have_delta :1;
 	struct git_pack_header hdr;
 	struct git_pack_file *pack;
-	git_filebuf pack_file;
 	unsigned int mode;
 	git_off_t off;
 	git_off_t entry_start;
@@ -67,33 +66,18 @@ const git_oid *git_indexer_hash(const git_indexer *idx)
 	return &idx->hash;
 }
 
-static int open_pack(struct git_pack_file **out, const char *filename)
-{
-	struct git_pack_file *pack;
-
-	if (git_packfile_alloc(&pack, filename) < 0)
-		return -1;
-
-	if ((pack->mwf.fd = p_open(pack->pack_name, O_RDONLY)) < 0) {
-		giterr_set(GITERR_OS, "Failed to open packfile.");
-		git_packfile_free(pack);
-		return -1;
-	}
-
-	*out = pack;
-	return 0;
-}
-
 static int parse_header(struct git_pack_header *hdr, struct git_pack_file *pack)
 {
 	int error;
+	git_map map;
 
-	/* Verify we recognize this pack file format. */
-	if ((error = p_read(pack->mwf.fd, hdr, sizeof(*hdr))) < 0) {
-		giterr_set(GITERR_OS, "Failed to read in pack header");
+	if ((error = p_mmap(&map, sizeof(*hdr), GIT_PROT_READ, GIT_MAP_SHARED, pack->mwf.fd, 0)) < 0)
 		return error;
-	}
 
+	memcpy(hdr, map.data, sizeof(*hdr));
+	p_munmap(&map);
+
+	/* Verify we recognize this pack file format. */
 	if (hdr->hdr_signature != ntohl(PACK_SIGNATURE)) {
 		giterr_set(GITERR_INDEXER, "Wrong pack signature");
 		return -1;
@@ -124,9 +108,9 @@ int git_indexer_new(
 		void *progress_payload)
 {
 	git_indexer *idx;
-	git_buf path = GIT_BUF_INIT;
+	git_buf path = GIT_BUF_INIT, tmp_path = GIT_BUF_INIT;
 	static const char suff[] = "/pack";
-	int error;
+	int error, fd;
 
 	idx = git__calloc(1, sizeof(git_indexer));
 	GITERR_CHECK_ALLOC(idx);
@@ -140,19 +124,30 @@ int git_indexer_new(
 	if (error < 0)
 		goto cleanup;
 
-	error = git_filebuf_open(&idx->pack_file, path.ptr,
-		GIT_FILEBUF_TEMPORARY | GIT_FILEBUF_DO_NOT_BUFFER,
-		idx->mode);
+	fd = git_futils_mktmp(&tmp_path, git_buf_cstr(&path), idx->mode);
 	git_buf_free(&path);
+	if (fd < 0)
+		goto cleanup;
+
+	error = git_packfile_alloc(&idx->pack, git_buf_cstr(&tmp_path));
+	git_buf_free(&tmp_path);
+
 	if (error < 0)
 		goto cleanup;
 
+	idx->pack->mwf.fd = fd;
+	if ((error = git_mwindow_file_register(&idx->pack->mwf)) < 0)
+		goto cleanup;
+
 	*out = idx;
 	return 0;
 
 cleanup:
+	if (fd != -1)
+		p_close(fd);
+
 	git_buf_free(&path);
-	git_filebuf_cleanup(&idx->pack_file);
+	git_buf_free(&tmp_path);
 	git__free(idx);
 	return -1;
 }
@@ -429,6 +424,42 @@ static void hash_partially(git_indexer *idx, const uint8_t *data, size_t size)
 	idx->inbuf_len += size - to_expell;
 }
 
+static int write_at(git_indexer *idx, const void *data, git_off_t offset, size_t size)
+{
+	git_file fd = idx->pack->mwf.fd;
+	long page_size = git__page_size();
+	git_off_t page_start, page_offset;
+	unsigned char *map_data;
+	git_map map;
+	int error;
+
+	/* the offset needs to be at the beginning of the a page boundary */
+	page_start = (offset / page_size) * page_size;
+	page_offset = offset - page_start;
+
+	if ((error = p_mmap(&map, page_offset + size, GIT_PROT_WRITE, GIT_MAP_SHARED, fd, page_start)) < 0)
+		return error;
+
+	map_data = (unsigned char *)map.data;
+	memcpy(map_data + page_offset, data, size);
+	p_munmap(&map);
+
+	return 0;
+}
+
+static int append_to_pack(git_indexer *idx, const void *data, size_t size)
+{
+	git_off_t current_size = idx->pack->mwf.size;
+
+	/* add the extra space we need at the end */
+	if (p_ftruncate(idx->pack->mwf.fd, current_size + size) < 0) {
+		giterr_system_set(errno);
+		return -1;
+	}
+
+	return write_at(idx, data, idx->pack->mwf.size, size);
+}
+
 int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_transfer_progress *stats)
 {
 	int error = -1;
@@ -440,22 +471,13 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran
 
 	processed = stats->indexed_objects;
 
-	if ((error = git_filebuf_write(&idx->pack_file, data, size)) < 0)
+	if ((error = append_to_pack(idx, data, size)) < 0)
 		return error;
 
 	hash_partially(idx, data, (int)size);
 
 	/* Make sure we set the new size of the pack */
-	if (idx->opened_pack) {
-		idx->pack->mwf.size += size;
-	} else {
-		if ((error = open_pack(&idx->pack, idx->pack_file.path_lock)) < 0)
-			return error;
-		idx->opened_pack = 1;
-		mwf = &idx->pack->mwf;
-		if ((error = git_mwindow_file_register(&idx->pack->mwf)) < 0)
-			return error;
-	}
+	idx->pack->mwf.size += size;
 
 	if (!idx->parsed_header) {
 		unsigned int total_objects;
@@ -616,17 +638,10 @@ static int index_path(git_buf *path, git_indexer *idx, const char *suffix)
  * Rewind the packfile by the trailer, as we might need to fix the
  * packfile by injecting objects at the tail and must overwrite it.
  */
-static git_off_t seek_back_trailer(git_indexer *idx)
+static void seek_back_trailer(git_indexer *idx)
 {
-	git_off_t off;
-
-	if ((off = p_lseek(idx->pack_file.fd, -GIT_OID_RAWSZ, SEEK_CUR)) < 0)
-		return -1;
-
 	idx->pack->mwf.size -= GIT_OID_RAWSZ;
 	git_mwindow_free_all(&idx->pack->mwf);
-
-	return off;
 }
 
 static int inject_object(git_indexer *idx, git_oid *id)
@@ -642,7 +657,8 @@ static int inject_object(git_indexer *idx, git_oid *id)
 	size_t len, hdr_len;
 	int error;
 
-	entry_start = seek_back_trailer(idx);
+	seek_back_trailer(idx);
+	entry_start = idx->pack->mwf.size;
 
 	if (git_odb_read(&obj, idx->odb, id) < 0)
 		return -1;
@@ -657,7 +673,9 @@ static int inject_object(git_indexer *idx, git_oid *id)
 
 	/* Write out the object header */
 	hdr_len = git_packfile__object_header(hdr, len, git_odb_object_type(obj));
-	git_filebuf_write(&idx->pack_file, hdr, hdr_len);
+	if ((error = append_to_pack(idx, hdr, hdr_len)) < 0)
+		goto cleanup;
+
 	idx->pack->mwf.size += hdr_len;
 	entry->crc = crc32(entry->crc, hdr, (uInt)hdr_len);
 
@@ -665,13 +683,16 @@ static int inject_object(git_indexer *idx, git_oid *id)
 		goto cleanup;
 
 	/* And then the compressed object */
-	git_filebuf_write(&idx->pack_file, buf.ptr, buf.size);
+	if ((error = append_to_pack(idx, buf.ptr, buf.size)) < 0)
+		goto cleanup;
+
 	idx->pack->mwf.size += buf.size;
 	entry->crc = htonl(crc32(entry->crc, (unsigned char *)buf.ptr, (uInt)buf.size));
 	git_buf_free(&buf);
 
 	/* Write a fake trailer so the pack functions play ball */
-	if ((error = git_filebuf_write(&idx->pack_file, &foo, GIT_OID_RAWSZ)) < 0)
+
+	if ((error = append_to_pack(idx, &foo, GIT_OID_RAWSZ)) < 0)
 		goto cleanup;
 
 	idx->pack->mwf.size += GIT_OID_RAWSZ;
@@ -817,19 +838,12 @@ static int update_header_and_rehash(git_indexer *idx, git_transfer_progress *sta
 	ctx = &idx->trailer;
 
 	git_hash_ctx_init(ctx);
-	git_mwindow_free_all(mwf);
+
 
 	/* Update the header to include the numer of local objects we injected */
 	idx->hdr.hdr_entries = htonl(stats->total_objects + stats->local_objects);
-	if (p_lseek(idx->pack_file.fd, 0, SEEK_SET) < 0) {
-		giterr_set(GITERR_OS, "failed to seek to the beginning of the pack");
+	if (write_at(idx, &idx->hdr, 0, sizeof(struct git_pack_header)) < 0)
 		return -1;
-	}
-
-	if (p_write(idx->pack_file.fd, &idx->hdr, sizeof(struct git_pack_header)) < 0) {
-		giterr_set(GITERR_OS, "failed to update the pack header");
-		return -1;
-	}
 
 	/*
 	 * We now use the same technique as before to determine the
@@ -837,6 +851,7 @@ static int update_header_and_rehash(git_indexer *idx, git_transfer_progress *sta
 	 * hash_partially() keep the existing trailer out of the
 	 * calculation.
 	 */
+	git_mwindow_free_all(mwf);
 	idx->inbuf_len = 0;
 	while (hashed < mwf->size) {
 		ptr = git_mwindow_open(mwf, &w, hashed, chunk, &left);
@@ -906,13 +921,7 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
 			return -1;
 
 		git_hash_final(&trailer_hash, &idx->trailer);
-		if (p_lseek(idx->pack_file.fd, -GIT_OID_RAWSZ, SEEK_END) < 0)
-			return -1;
-
-		if (p_write(idx->pack_file.fd, &trailer_hash, GIT_OID_RAWSZ) < 0) {
-			giterr_set(GITERR_OS, "failed to update pack trailer");
-			return -1;
-		}
+		write_at(idx, &trailer_hash, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ);
 	}
 
 	git_vector_sort(&idx->objects);
@@ -995,14 +1004,18 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
 
 	git_mwindow_free_all(&idx->pack->mwf);
 	/* We need to close the descriptor here so Windows doesn't choke on commit_at */
-	p_close(idx->pack->mwf.fd);
+	if (p_close(idx->pack->mwf.fd) < 0) {
+		giterr_set(GITERR_OS, "failed to close packfile");
+		goto on_error;
+	}
+
 	idx->pack->mwf.fd = -1;
 
 	if (index_path(&filename, idx, ".pack") < 0)
 		goto on_error;
+
 	/* And don't forget to rename the packfile to its new place. */
-	if (git_filebuf_commit_at(&idx->pack_file, filename.ptr) < 0)
-		return -1;
+	p_rename(idx->pack->pack_name, git_buf_cstr(&filename));
 
 	git_buf_free(&filename);
 	return 0;
@@ -1022,7 +1035,7 @@ void git_indexer_free(git_indexer *idx)
 
 	git_vector_free_deep(&idx->objects);
 
-	if (idx->pack) {
+	if (idx->pack && idx->pack->idx_cache) {
 		struct git_pack_entry *pentry;
 		kh_foreach_value(
 			idx->pack->idx_cache, pentry, { git__free(pentry); });
@@ -1032,6 +1045,5 @@ void git_indexer_free(git_indexer *idx)
 
 	git_vector_free_deep(&idx->deltas);
 	git_packfile_free(idx->pack);
-	git_filebuf_cleanup(&idx->pack_file);
 	git__free(idx);
 }
diff --git a/src/map.h b/src/map.h
index da3d1e1..722eb7a 100644
--- a/src/map.h
+++ b/src/map.h
@@ -42,5 +42,6 @@ typedef struct { /* memory mapped buffer	*/
 
 extern int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset);
 extern int p_munmap(git_map *map);
+extern long git__page_size(void);
 
 #endif /* INCLUDE_map_h__ */
diff --git a/src/posix.c b/src/posix.c
index 7484ac0..7aeb0e6 100644
--- a/src/posix.c
+++ b/src/posix.c
@@ -207,6 +207,13 @@ int p_write(git_file fd, const void *buf, size_t cnt)
 
 #include "map.h"
 
+long git__page_size(void)
+{
+	/* dummy; here we don't need any alignment anyway */
+	return 4096;
+}
+
+
 int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
 {
 	GIT_MMAP_VALIDATE(out, len, prot, flags);
diff --git a/src/posix.h b/src/posix.h
index f85b1ae..965cd98 100644
--- a/src/posix.h
+++ b/src/posix.h
@@ -73,6 +73,7 @@ extern int p_rename(const char *from, const char *to);
 #define p_rmdir(p) rmdir(p)
 #define p_chmod(p,m) chmod(p, m)
 #define p_access(p,m) access(p,m)
+#define p_ftruncate(fd, sz) ftruncate(fd, sz)
 #define p_recv(s,b,l,f) recv(s,b,l,f)
 #define p_send(s,b,l,f) send(s,b,l,f)
 typedef int GIT_SOCKET;
diff --git a/src/unix/map.c b/src/unix/map.c
index e62ab3e..3d0cbba 100644
--- a/src/unix/map.c
+++ b/src/unix/map.c
@@ -10,8 +10,14 @@
 
 #include "map.h"
 #include <sys/mman.h>
+#include <unistd.h>
 #include <errno.h>
 
+long git__page_size(void)
+{
+	return sysconf(_SC_PAGE_SIZE);
+}
+
 int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
 {
 	int mprot = 0;
diff --git a/src/win32/map.c b/src/win32/map.c
index 902ea39..ef83f88 100644
--- a/src/win32/map.c
+++ b/src/win32/map.c
@@ -23,6 +23,11 @@ static DWORD get_page_size(void)
 	return page_size;
 }
 
+long git__page_size(void)
+{
+	return (long)get_page_size();
+}
+
 int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
 {
 	HANDLE fh = (HANDLE)_get_osfhandle(fd);
diff --git a/src/win32/posix.h b/src/win32/posix.h
index 7f9d57c..2cbea18 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -19,6 +19,12 @@
 # define EAFNOSUPPORT (INT_MAX-1)
 #endif
 
+#ifdef _MSC_VER
+# define p_ftruncate(fd, sz) _chsize_s(fd, sz)
+#else  /* MinGW */
+# define p_ftruncate(fd, sz) _chsize(fd, sz)
+#endif
+
 GIT_INLINE(int) p_link(const char *old, const char *new)
 {
 	GIT_UNUSED(old);
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 73bf925..3493843 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -578,7 +578,7 @@ int p_mkstemp(char *tmp_path)
 		return -1;
 #endif
 
-	return p_creat(tmp_path, 0744); //-V536
+	return p_open(tmp_path, O_RDWR | O_CREAT | O_EXCL, 0744); //-V536
 }
 
 int p_access(const char* path, mode_t mode)