Commit 7e656b930dec54dd96e304ea8e3427a5531abaf9

Stefan Sperling 2018-03-17T01:37:48

add struct got_pack; some preparation for mmap, which isn't used yet

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
416
417
418
diff --git a/lib/got_pack_lib.h b/lib/got_pack_lib.h
index f84a8a1..7810cfb 100644
--- a/lib/got_pack_lib.h
+++ b/lib/got_pack_lib.h
@@ -14,6 +14,30 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+/* A pack file segment mapped with mmap(2). */
+struct got_pack_mapping {
+	TAILQ_ENTRY(got_pack_mapping) entry;
+	int fd;
+	uint8_t *addr;
+	off_t offset;
+	size_t len;
+};
+
+#define GOT_PACK_MAX_OPEN_MAPPINGS	512
+#define GOT_PACK_MAPPING_MIN_SIZE	8192
+#define GOT_PACK_MAPPING_MAX_SIZE	(2048 * GOT_PACK_MAPPING_MIN_SIZE)
+
+/* An open pack file. */
+struct got_pack {
+	char *path_packfile;
+	FILE *packfile;
+	size_t filesize;
+	int nmappings;
+	TAILQ_HEAD(, got_pack_mapping) mappings;
+};
+
+const struct got_error *got_pack_close(struct got_pack *);
+
 /* See Documentation/technical/pack-format.txt in Git. */
 
 struct got_packidx_trailer {
diff --git a/lib/got_repository_lib.h b/lib/got_repository_lib.h
index a8e4ced..410d1e0 100644
--- a/lib/got_repository_lib.h
+++ b/lib/got_repository_lib.h
@@ -28,6 +28,7 @@ struct got_delta_cache {
 };
 
 #define GOT_PACKIDX_CACHE_SIZE	64
+#define GOT_PACK_CACHE_SIZE	GOT_PACKIDX_CACHE_SIZE
 
 struct got_repository {
 	char *path;
@@ -36,7 +37,9 @@ struct got_repository {
 	/* The pack index cache speeds up search for packed objects. */
 	struct got_packidx_v2_hdr *packidx_cache[GOT_PACKIDX_CACHE_SIZE];
 
-	/* The delta cache speeds up reconstruction of packed objects. */
-	struct got_delta_cache delta_cache[GOT_PACKIDX_CACHE_SIZE];
-};
+	/* Open file handles, memory maps, and cached deltas for pack files. */
+	struct got_pack packs[GOT_PACK_CACHE_SIZE];
 
+	/* XXX TODO move into packs[] */
+	struct got_delta_cache delta_cache[GOT_DELTA_CACHE_SIZE];
+};
diff --git a/lib/pack.c b/lib/pack.c
index b9e653b..2554b19 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -17,8 +17,10 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/queue.h>
+#include <sys/mman.h>
 
 #include <dirent.h>
+#include <fcntl.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdint.h>
@@ -500,34 +502,173 @@ read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
 	return err;
 }
 
+#ifdef notyet
 static const struct got_error *
-open_packfile(FILE **packfile, char **path_packfile,
-    struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
+map_packfile_segment(struct got_pack_mapping **map, const char *path_packfile,
+    off_t offset, size_t len)
 {
-	const struct got_error *err;
+	const struct got_error *err = NULL;
 
-	*packfile = NULL;
+	if (len < GOT_PACK_MAPPING_MIN_SIZE)
+		len = GOT_PACK_MAPPING_MIN_SIZE;
 
-	err = get_packfile_path(path_packfile, repo, packidx);
-	if (err)
+	if (len > GOT_PACK_MAPPING_MAX_SIZE)
+		return got_error(GOT_ERR_NO_SPACE);
+
+	*map = calloc(1, sizeof(**map));
+	if (*map == NULL)
+		return got_error(GOT_ERR_NO_MEM);
+
+	(*map)->fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
+	if ((*map)->fd == -1) {
+		err = got_error_from_errno();
+		free((*map));
+		*map = NULL;
+		return err;
+	}
+
+	(*map)->addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, (*map)->fd, offset);
+	if ((*map)->addr == NULL) {
+		err = got_error_from_errno();
+		close((*map)->fd);
+		free((*map));
+		*map = NULL;
 		return err;
+	}
+
+	(*map)->offset = offset;
+	(*map)->len = len;
+
+	return NULL;
+}
+#endif
+
+static const struct got_error *
+unmap_packfile_segment(struct got_pack_mapping *map)
+{
+	if (munmap(map->addr, map->len) == -1 || close(map->fd) == -1)
+		return got_error_from_errno();
+	free(map);
+	return NULL;
+}
 
-	*packfile = fopen(*path_packfile, "rb");
+static const struct got_error *
+open_packfile(FILE **packfile, const char *path_packfile,
+    struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
+{
+	const struct got_error *err = NULL;
+
+	*packfile = NULL;
+
+	*packfile = fopen(path_packfile, "rb");
 	if (*packfile == NULL) {
 		err = got_error_from_errno();
-		free(*path_packfile);
 		return err;
 	}
 
-	err = read_packfile_hdr(*packfile, packidx);
-	if (err) {
-		fclose(*packfile);
-		*packfile = NULL;
+	if (packidx) {
+		err = read_packfile_hdr(*packfile, packidx);
+		if (err) {
+			fclose(*packfile);
+			*packfile = NULL;
+		}
+	}
+	return err;
+}
+
+const struct got_error *
+got_pack_close(struct got_pack *pack)
+{
+	const struct got_error *err = NULL;
+
+	while (!TAILQ_EMPTY(&pack->mappings)) {
+		struct got_pack_mapping *map = TAILQ_FIRST(&pack->mappings);
+		err = unmap_packfile_segment(map);
+		if (err)
+			break;
+		TAILQ_REMOVE(&pack->mappings, map, entry);
+		pack->nmappings--;
+	}
+
+	if (err == NULL) {
+		fclose(pack->packfile);
+		pack->packfile = NULL;
+		free(pack->path_packfile);
+		pack->path_packfile = NULL;
+		pack->filesize = 0;
 	}
 	return err;
 }
 
 static const struct got_error *
+cache_pack(struct got_pack **packp, const char *path_packfile,
+    struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_pack *pack = NULL;
+	int i;
+
+	if (packp)
+		*packp = NULL;
+
+	for (i = 0; i < nitems(repo->packs); i++) {
+		pack = &repo->packs[i];
+		if (pack->path_packfile == NULL)
+			break;
+		if (strcmp(pack->path_packfile, path_packfile) == 0)
+			return NULL;
+	}
+
+	if (i == nitems(repo->packs) - 1) {
+		got_pack_close(&repo->packs[i - 1]);
+		memmove(&repo->packs[1], &repo->packs[0],
+		    sizeof(repo->packs) - sizeof(repo->packs[0]));
+		i = 0;
+	}
+
+	pack = &repo->packs[i];
+
+	TAILQ_INIT(&pack->mappings);
+
+	pack->path_packfile = strdup(path_packfile);
+	if (pack->path_packfile == NULL) {
+		err = got_error(GOT_ERR_NO_MEM);
+		goto done;
+	}
+
+	err = open_packfile(&pack->packfile, path_packfile, repo, packidx);
+	if (err)
+		goto done;
+
+	err = get_packfile_size(&pack->filesize, path_packfile);
+done:
+	if (err) {
+		if (pack)
+			free(pack->path_packfile);
+		free(pack);
+	} else if (packp)
+		*packp = pack;
+	return err;
+}
+
+struct got_pack *
+get_cached_pack(const char *path_packfile, struct got_repository *repo)
+{
+	struct got_pack *pack = NULL;
+	int i;
+
+	for (i = 0; i < nitems(repo->packs); i++) {
+		pack = &repo->packs[i];
+		if (pack->path_packfile == NULL)
+			break;
+		if (strcmp(pack->path_packfile, path_packfile) == 0)
+			return pack;
+	}
+
+	return NULL;
+}
+
+static const struct got_error *
 parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
     FILE *packfile)
 {
@@ -740,7 +881,11 @@ resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
 		return got_error(GOT_ERR_BAD_PACKIDX);
 	}
 
-	err = open_packfile(&base_packfile, &path_base_packfile, repo, packidx);
+	err = get_packfile_path(&path_base_packfile, repo, packidx);
+	if (err)
+		return err;
+
+	err = open_packfile(&base_packfile, path_base_packfile, repo, packidx);
 	got_packidx_close(packidx);
 	if (err)
 		return err;
@@ -863,7 +1008,11 @@ open_packed_object(struct got_object **obj, struct got_repository *repo,
 	if (offset == (uint64_t)-1)
 		return got_error(GOT_ERR_BAD_PACKIDX);
 
-	err = open_packfile(&packfile, &path_packfile, repo, packidx);
+	err = get_packfile_path(&path_packfile, repo, packidx);
+	if (err)
+		return err;
+
+	err = open_packfile(&packfile, path_packfile, repo, packidx);
 	if (err)
 		return err;
 
@@ -915,6 +1064,10 @@ got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
 		return err;
 
 	err = open_packed_object(obj, repo, packidx, idx, id);
+	if (err)
+		return err;
+
+	err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
 	got_packidx_close(packidx);
 	return err;
 }
@@ -1143,7 +1296,7 @@ dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
 				goto done;
 			}
 			if (base_file)
-				err = got_inflate_to_file(&delta_len,
+				err = got_inflate_to_file(&base_len,
 				    packfile, base_file);
 			else {
 				err = got_inflate_to_mem(&base_buf, &base_len,
@@ -1353,7 +1506,7 @@ got_packfile_extract_object(FILE **f, struct got_object *obj,
     struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
-	FILE *packfile = NULL;
+	struct got_pack *pack;
 
 	if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
 		return got_error(GOT_ERR_OBJ_NOT_PACKED);
@@ -1364,25 +1517,24 @@ got_packfile_extract_object(FILE **f, struct got_object *obj,
 		goto done;
 	}
 
-	packfile = fopen(obj->path_packfile, "rb");
-	if (packfile == NULL) {
-		err = got_error_from_errno();
-		goto done;
+	pack = get_cached_pack(obj->path_packfile, repo);
+	if (pack == NULL) {
+		err = cache_pack(&pack, obj->path_packfile, NULL, repo);
+		if (err)
+			goto done;
 	}
 
 	if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
-		if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
+		if (fseeko(pack->packfile, obj->pack_offset, SEEK_SET) != 0) {
 			err = got_error_from_errno();
 			goto done;
 		}
 
-		err = got_inflate_to_file(&obj->size, packfile, *f);
+		err = got_inflate_to_file(&obj->size, pack->packfile, *f);
 	} else
 		err = dump_delta_chain_to_file(&obj->size, &obj->deltas, *f,
-		    packfile, obj->path_packfile, repo);
+		    pack->packfile, obj->path_packfile, repo);
 done:
-	if (packfile)
-		fclose(packfile);
 	if (err && *f)
 		fclose(*f);
 	return err;
@@ -1394,26 +1546,28 @@ got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
 {
 	const struct got_error *err = NULL;
 	FILE *packfile = NULL;
+	struct got_pack *pack;
 
 	if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
 		return got_error(GOT_ERR_OBJ_NOT_PACKED);
 
-	packfile = fopen(obj->path_packfile, "rb");
-	if (packfile == NULL) {
-		err = got_error_from_errno();
-		goto done;
+	pack = get_cached_pack(obj->path_packfile, repo);
+	if (pack == NULL) {
+		err = cache_pack(&pack, obj->path_packfile, NULL, repo);
+		if (err)
+			goto done;
 	}
 
 	if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
-		if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
+		if (fseeko(pack->packfile, obj->pack_offset, SEEK_SET) != 0) {
 			err = got_error_from_errno();
 			goto done;
 		}
 
-		err = got_inflate_to_mem(buf, len, packfile);
+		err = got_inflate_to_mem(buf, len, pack->packfile);
 	} else
-		err = dump_delta_chain_to_mem(buf, len, &obj->deltas, packfile,
-		    obj->path_packfile, repo);
+		err = dump_delta_chain_to_mem(buf, len, &obj->deltas,
+		    pack->packfile, obj->path_packfile, repo);
 done:
 	if (packfile)
 		fclose(packfile);
diff --git a/lib/repository.c b/lib/repository.c
index c19c013..c8d7850 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -29,11 +29,11 @@
 #include "got_repository.h"
 
 #include "got_path_lib.h"
-#include "got_repository_lib.h"
-#include "got_zbuf_lib.h"
 #include "got_delta_lib.h"
+#include "got_zbuf_lib.h"
 #include "got_object_lib.h"
 #include "got_pack_lib.h"
+#include "got_repository_lib.h"
 
 #ifndef nitems
 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
@@ -208,6 +208,12 @@ got_repo_close(struct got_repository *repo)
 		got_packidx_close(repo->packidx_cache[i]);
 	}
 
+	for (i = 0; i < nitems(repo->packs); i++) {
+		if (repo->packs[i].path_packfile == NULL)
+			break;
+		got_pack_close(&repo->packs[i]);
+	}
+
 	for (i = 0; i < nitems(repo->delta_cache); i++) {
 		struct got_delta_cache *cache = &repo->delta_cache[i];
 		int j;