Commit 3022d2728c5aaec4a574a76d30136d850fa5d154

Stefan Sperling 2019-11-14T17:12:32

reduce the amount of memcpy() and strdup() while parsing tree entries

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
419
420
421
422
423
diff --git a/lib/got_lib_object_parse.h b/lib/got_lib_object_parse.h
index cc4f183..cea9915 100644
--- a/lib/got_lib_object_parse.h
+++ b/lib/got_lib_object_parse.h
@@ -14,14 +14,27 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+struct got_pathlist_head;
+
 const struct got_error *got_object_qid_alloc_partial(struct got_object_qid **);
 struct got_commit_object *got_object_commit_alloc_partial(void);
 struct got_tree_entry *got_alloc_tree_entry_partial(void);
 
 const struct got_error *got_object_parse_commit(struct got_commit_object **,
     char *, size_t);
-const struct got_error *got_object_parse_tree(struct got_tree_object **,
+
+/*
+ * In a pathlist returned by got_object_parse_tree(), a pointer to the entry's
+ * name is stored in the pathlist element's path, while the pathlist element's
+ * data pointer points to a struct got_parsed_tree_entry.
+ */
+struct got_parsed_tree_entry {
+	mode_t mode; /* Mode parsed from tree buffer. */
+	uint8_t *id; /* Points to ID in parsed tree buffer. */
+};
+const struct got_error *got_object_parse_tree(struct got_pathlist_head *, int *,
     uint8_t *, size_t);
+
 const struct got_error *got_object_parse_tag(struct got_tag_object **,
     uint8_t *, size_t);
 const struct got_error *got_read_file_to_mem(uint8_t **, size_t *, FILE *);
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index f1e7f2a..a37a450 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -231,6 +231,7 @@ struct got_imsg_packed_object {
 
 struct got_pack;
 struct got_packidx;
+struct got_pathlist_head;
 
 const struct got_error *got_privsep_wait_for_child(pid_t);
 const struct got_error *got_privsep_send_stop(int);
@@ -261,7 +262,7 @@ const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
     struct imsgbuf *);
 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
-    struct got_tree_object *);
+    struct got_pathlist_head *, int);
 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t, size_t,
     const uint8_t *);
 const struct got_error *got_privsep_recv_blob(uint8_t **, size_t *, size_t *,
diff --git a/lib/object_parse.c b/lib/object_parse.c
index 9a37c6f..9124070 100644
--- a/lib/object_parse.c
+++ b/lib/object_parse.c
@@ -45,6 +45,7 @@
 #include "got_lib_delta.h"
 #include "got_lib_inflate.h"
 #include "got_lib_object.h"
+#include "got_lib_object_parse.h"
 #include "got_lib_object_cache.h"
 #include "got_lib_pack.h"
 #include "got_lib_privsep.h"
@@ -663,88 +664,83 @@ got_alloc_tree_entry_partial(void)
 }
 
 static const struct got_error *
-parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
+parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
+    size_t *elen, char *buf,
     size_t maxlen)
 {
 	char *p, *space;
 	const struct got_error *err = NULL;
 
+	*name = NULL;
 	*elen = 0;
 
-	*te = got_alloc_tree_entry_partial();
-	if (*te == NULL)
-		return got_error_from_errno("got_alloc_tree_entry_partial");
+	*pte = malloc(sizeof(**pte));
+	if (*pte == NULL)
+		return got_error_from_errno("malloc");
 
 	*elen = strnlen(buf, maxlen) + 1;
 	if (*elen > maxlen) {
-		free(*te);
-		*te = NULL;
+		free(*pte);
+		*pte = NULL;
 		return got_error(GOT_ERR_BAD_OBJ_DATA);
 	}
 
 	space = memchr(buf, ' ', *elen);
 	if (space == NULL || space <= buf) {
 		err = got_error(GOT_ERR_BAD_OBJ_DATA);
-		free(*te);
-		*te = NULL;
+		free(*pte);
+		*pte = NULL;
 		return err;
 	}
-	(*te)->mode = 0;
+	(*pte)->mode = 0;
 	p = buf;
 	while (p < space) {
 		if (*p < '0' && *p > '7') {
 			err = got_error(GOT_ERR_BAD_OBJ_DATA);
 			goto done;
 		}
-		(*te)->mode <<= 3;
-		(*te)->mode |= *p - '0';
+		(*pte)->mode <<= 3;
+		(*pte)->mode |= *p - '0';
 		p++;
 	}
 
-	(*te)->name = strdup(space + 1);
 	if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
 		err = got_error(GOT_ERR_BAD_OBJ_DATA);
 		goto done;
 	}
+	*name = space + 1;
 	buf += *elen;
-	memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
+	(*pte)->id = buf;
 	*elen += SHA1_DIGEST_LENGTH;
 done:
 	if (err) {
-		got_object_tree_entry_close(*te);
-		*te = NULL;
+		free(*pte);
+		*pte = NULL;
 	}
 	return err;
 }
 
 const struct got_error *
-got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
+got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
+    uint8_t *buf, size_t len)
 {
-	const struct got_error *err;
+	const struct got_error *err = NULL;
 	size_t remain = len;
-	struct got_pathlist_head pathlist;
-	struct got_pathlist_entry *pe;
-
-	TAILQ_INIT(&pathlist);
-
-	*tree = calloc(1, sizeof(**tree));
-	if (*tree == NULL)
-		return got_error_from_errno("calloc");
-
-	SIMPLEQ_INIT(&(*tree)->entries.head);
 
+	*nentries = 0;
 	if (remain == 0)
 		return NULL; /* tree is empty */
 
 	while (remain > 0) {
-		struct got_tree_entry *te;
+		struct got_parsed_tree_entry *pte;
 		struct got_pathlist_entry *new = NULL;
+		const char *name;
 		size_t elen;
 
-		err = parse_tree_entry(&te, &elen, buf, remain);
+		err = parse_tree_entry(&pte, &name, &elen, buf, remain);
 		if (err)
 			goto done;
-		err = got_pathlist_insert(&new, &pathlist, te->name, te);
+		err = got_pathlist_insert(&new, entries, name, pte);
 		if (err)
 			goto done;
 		if (new == NULL) {
@@ -753,22 +749,18 @@ got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
 		}
 		buf += elen;
 		remain -= elen;
+		(*nentries)++;
 	}
 
 	if (remain != 0) {
-		got_object_tree_close(*tree);
-		*tree = NULL;
 		err = got_error(GOT_ERR_BAD_OBJ_DATA);
 		goto done;
 	}
-
-	TAILQ_FOREACH(pe, &pathlist, entry) {
-		struct got_tree_entry *te = pe->data;
-		(*tree)->entries.nentries++;
-		SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
-	}
 done:
-	got_pathlist_free(&pathlist);
+	if (err) {
+		got_pathlist_free(entries);
+		*nentries = 0;
+	}
 	return err;
 }
 
diff --git a/lib/privsep.c b/lib/privsep.c
index 62a79cf..0de0608 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -33,6 +33,7 @@
 
 #include "got_object.h"
 #include "got_error.h"
+#include "got_path.h"
 
 #include "got_lib_sha1.h"
 #include "got_lib_delta.h"
@@ -690,25 +691,28 @@ got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
 }
 
 const struct got_error *
-got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
+got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
+    int nentries)
 {
 	const struct got_error *err = NULL;
 	struct got_imsg_tree_object itree;
-	struct got_tree_entry *te;
+	struct got_pathlist_entry *pe;
 	size_t totlen;
 	int nimsg; /* number of imsg queued in ibuf */
 
-	itree.nentries = tree->entries.nentries;
+	itree.nentries = nentries;
 	if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
 	    == -1)
 		return got_error_from_errno("imsg_compose TREE");
 
 	totlen = sizeof(itree);
 	nimsg = 1;
-	SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
-		struct got_imsg_tree_entry *ite;
-		uint8_t *buf = NULL;
-		size_t len = sizeof(*ite) + strlen(te->name);
+	TAILQ_FOREACH(pe, entries, entry) {
+		const char *name = pe->path;
+		struct got_parsed_tree_entry *pte = pe->data;
+		struct ibuf *wbuf;
+		size_t namelen = strlen(name);
+		size_t len = sizeof(struct got_imsg_tree_object) + namelen;
 
 		if (len > MAX_IMSGSIZE)
 			return got_error(GOT_ERR_NO_SPACE);
@@ -721,21 +725,28 @@ got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
 			nimsg = 0;
 		}
 
-		buf = malloc(len);
-		if (buf == NULL)
-			return got_error_from_errno("malloc");
+		wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
+		if (wbuf == NULL)
+			return got_error_from_errno("imsg_create TREE_ENTRY");
 
-		ite = (struct got_imsg_tree_entry *)buf;
-		memcpy(ite->id, te->id->sha1, sizeof(ite->id));
-		ite->mode = te->mode;
-		memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
+		/* Keep in sync with struct got_imsg_tree_object definition! */
+		if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
+			err = got_error_from_errno("imsg_add TREE_ENTRY");
+		if (err)
+			return err;
+		if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
+			err = got_error_from_errno("imsg_add TREE_ENTRY");
+		if (err)
+			return err;
 
-		if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
-		    buf, len) == -1)
-			err = got_error_from_errno("imsg_compose TREE_ENTRY");
-		free(buf);
+		if (imsg_add(wbuf, name, namelen) == -1)
+			err = got_error_from_errno("imsg_add TREE_ENTRY");
 		if (err)
 			return err;
+
+		wbuf->fd = -1;
+		imsg_close(ibuf, wbuf);
+
 		totlen += len;
 	}
 
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index d8c2e0f..f130a21 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -33,6 +33,7 @@
 
 #include "got_error.h"
 #include "got_object.h"
+#include "got_path.h"
 
 #include "got_lib_delta.h"
 #include "got_lib_delta_cache.h"
@@ -166,12 +167,15 @@ tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
 	const struct got_error *err = NULL;
 	struct got_imsg_packed_object iobj;
 	struct got_object *obj = NULL;
-	struct got_tree_object *tree = NULL;
+	struct got_pathlist_head entries;
+	int nentries = 0;
 	uint8_t *buf = NULL;
 	size_t len;
 	struct got_object_id id;
 	size_t datalen;
 
+	TAILQ_INIT(&entries);
+
 	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
 	if (datalen != sizeof(iobj))
 		return got_error(GOT_ERR_PRIVSEP_LEN);
@@ -193,16 +197,15 @@ tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
 		goto done;
 
 	obj->size = len;
-	err = got_object_parse_tree(&tree, buf, len);
+	err = got_object_parse_tree(&entries, &nentries, buf, len);
 	if (err)
 		goto done;
 
-	err = got_privsep_send_tree(ibuf, tree);
+	err = got_privsep_send_tree(ibuf, &entries, nentries);
 done:
+	got_pathlist_free(&entries);
 	free(buf);
 	got_object_close(obj);
-	if (tree)
-		got_object_tree_close(tree);
 	if (err) {
 		if (err->code == GOT_ERR_PRIVSEP_PIPE)
 			err = NULL;
diff --git a/libexec/got-read-tree/got-read-tree.c b/libexec/got-read-tree/got-read-tree.c
index 78ffcce..3e24db2 100644
--- a/libexec/got-read-tree/got-read-tree.c
+++ b/libexec/got-read-tree/got-read-tree.c
@@ -32,6 +32,7 @@
 
 #include "got_error.h"
 #include "got_object.h"
+#include "got_path.h"
 
 #include "got_lib_delta.h"
 #include "got_lib_inflate.h"
@@ -46,19 +47,20 @@ catch_sigint(int signo)
 {
 	sigint_received = 1;
 }
+
 static const struct got_error *
-read_tree_object(struct got_tree_object **tree, FILE *f)
+read_tree_object(struct got_pathlist_head *entries, int *nentries,
+    uint8_t **p, FILE *f)
 {
 	const struct got_error *err = NULL;
 	struct got_object *obj;
 	size_t len;
-	uint8_t *p;
 
-	err = got_inflate_to_mem(&p, &len, f);
+	err = got_inflate_to_mem(p, &len, f);
 	if (err)
 		return err;
 
-	err = got_object_parse_header(&obj, p, len);
+	err = got_object_parse_header(&obj, *p, len);
 	if (err)
 		return err;
 
@@ -69,9 +71,8 @@ read_tree_object(struct got_tree_object **tree, FILE *f)
 
 	/* Skip object header. */
 	len -= obj->hdrlen;
-	err = got_object_parse_tree(tree, p + obj->hdrlen, len);
+	err = got_object_parse_tree(entries, nentries, *p + obj->hdrlen, len);
 done:
-	free(p);
 	got_object_close(obj);
 	return err;
 }
@@ -98,7 +99,11 @@ main(int argc, char *argv[])
 	for (;;) {
 		struct imsg imsg;
 		FILE *f = NULL;
-		struct got_tree_object *tree = NULL;
+		struct got_pathlist_head entries;
+		int nentries = 0;
+		uint8_t *buf = NULL;
+
+		TAILQ_INIT(&entries);
 
 		if (sigint_received) {
 			err = got_error(GOT_ERR_CANCELLED);
@@ -132,12 +137,14 @@ main(int argc, char *argv[])
 			goto done;
 		}
 
-		err = read_tree_object(&tree, f);
+		err = read_tree_object(&entries, &nentries, &buf, f);
 		if (err)
 			goto done;
 
-		err = got_privsep_send_tree(&ibuf, tree);
+		err = got_privsep_send_tree(&ibuf, &entries, nentries);
 done:
+		got_pathlist_free(&entries);
+		free(buf);
 		if (f) {
 			if (fclose(f) != 0 && err == NULL)
 				err = got_error_from_errno("fclose");