Bug Summary

File:libexec/got-index-pack/got-index-pack.c
Warning:line 722, column 10
Potential leak of memory pointed to by 'packidx.hdr.fanout_table'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd6.9 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name got-index-pack.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/local/lib/clang/11.1.0 -I /home/ben/Projects/got/libexec/got-index-pack/../../include -I /home/ben/Projects/got/libexec/got-index-pack/../../lib -D GOT_LIBEXECDIR=/home/ben/bin -D GOT_VERSION=0.53-current -internal-isystem /usr/local/lib/clang/11.1.0/include -internal-externc-isystem /usr/include -O0 -fdebug-compilation-dir /home/ben/Projects/got/libexec/got-index-pack/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /home/ben/Projects/got/scan/2021-05-28-230913-68537-1 -x c /home/ben/Projects/got/libexec/got-index-pack/got-index-pack.c
1/*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/queue.h>
19#include <sys/stat.h>
20#include <sys/time.h>
21#include <sys/types.h>
22#include <sys/uio.h>
23#include <sys/mman.h>
24
25#include <stdint.h>
26#include <errno(*__errno()).h>
27#include <imsg.h>
28#include <limits.h>
29#include <signal.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <ctype.h>
34#include <sha1.h>
35#include <endian.h>
36#include <fcntl.h>
37#include <unistd.h>
38#include <zlib.h>
39#include <err.h>
40#include <assert.h>
41#include <dirent.h>
42
43#include "got_error.h"
44#include "got_object.h"
45
46#include "got_lib_sha1.h"
47#include "got_lib_delta.h"
48#include "got_lib_inflate.h"
49#include "got_lib_object.h"
50#include "got_lib_object_parse.h"
51#include "got_lib_object_idset.h"
52#include "got_lib_privsep.h"
53#include "got_lib_pack.h"
54#include "got_lib_delta_cache.h"
55
56#ifndef nitems
57#define nitems(_a)(sizeof((_a)) / sizeof((_a)[0])) (sizeof((_a)) / sizeof((_a)[0]))
58#endif
59
60struct got_indexed_object {
61 struct got_object_id id;
62
63 /*
64 * Has this object been fully resolved?
65 * If so, we know its ID, otherwise we don't and 'id' is invalid.
66 */
67 int valid;
68
69 /* Offset of type+size field for this object in pack file. */
70 off_t off;
71
72 /* Type+size values parsed from pack file. */
73 uint8_t type;
74 uint64_t size;
75
76 /* Length of on-disk type+size data. */
77 size_t tslen;
78
79 /* Length of object data following type+size. */
80 size_t len;
81
82 uint32_t crc;
83
84 union {
85 struct {
86 /* For ref deltas. */
87 struct got_object_id ref_id;
88 } ref;
89 struct {
90 /* For offset deltas. */
91 off_t base_offset;
92 size_t base_offsetlen;
93 } ofs;
94 } delta;
95};
96
97static void
98putbe32(char *b, uint32_t n)
99{
100 b[0] = n >> 24;
101 b[1] = n >> 16;
102 b[2] = n >> 8;
103 b[3] = n >> 0;
104}
105
106static const struct got_error *
107get_obj_type_label(const char **label, int obj_type)
108{
109 const struct got_error *err = NULL((void *)0);
110
111 switch (obj_type) {
112 case GOT_OBJ_TYPE_BLOB3:
113 *label = GOT_OBJ_LABEL_BLOB"blob";
114 break;
115 case GOT_OBJ_TYPE_TREE2:
116 *label = GOT_OBJ_LABEL_TREE"tree";
117 break;
118 case GOT_OBJ_TYPE_COMMIT1:
119 *label = GOT_OBJ_LABEL_COMMIT"commit";
120 break;
121 case GOT_OBJ_TYPE_TAG4:
122 *label = GOT_OBJ_LABEL_TAG"tag";
123 break;
124 default:
125 *label = NULL((void *)0);
126 err = got_error(GOT_ERR_OBJ_TYPE11);
127 break;
128 }
129
130 return err;
131}
132
133static const struct got_error *
134read_checksum(uint32_t *crc, SHA1_CTX *sha1_ctx, int fd, size_t len)
135{
136 uint8_t buf[8192];
137 size_t n;
138 ssize_t r;
139
140 for (n = len; n > 0; n -= r){
141 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
142 if (r == -1)
143 return got_error_from_errno("read");
144 if (r == 0)
145 break;
146 if (crc)
147 *crc = crc32(*crc, buf, r);
148 if (sha1_ctx)
149 SHA1Update(sha1_ctx, buf, r);
150 }
151
152 return NULL((void *)0);
153}
154
155static const struct got_error *
156read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
157{
158 uint8_t buf[8192];
159 size_t n, r;
160
161 for (n = len; n > 0; n -= r) {
162 r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
163 if (r == 0) {
164 if (feof(f)(!__isthreaded ? (((f)->_flags & 0x0020) != 0) : (feof
)(f))
)
165 return NULL((void *)0);
166 return got_ferror(f, GOT_ERR_IO6);
167 }
168 SHA1Update(ctx, buf, r);
169 }
170
171 return NULL((void *)0);
172}
173
174static const struct got_error *
175read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
176 FILE *tmpfile, SHA1_CTX *pack_sha1_ctx)
177{
178 const struct got_error *err = NULL((void *)0);
179 SHA1_CTX ctx;
180 uint8_t *data = NULL((void *)0);
181 size_t datalen = 0;
182 ssize_t n;
183 char *header;
184 size_t headerlen;
185 const char *obj_label;
186 size_t mapoff = obj->off;
187 struct got_inflate_checksum csum;
188
189 csum.input_sha1 = pack_sha1_ctx;
190 csum.input_crc = &obj->crc;
191
192 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
193 &obj->tslen, pack, obj->off);
194 if (err)
195 return err;
196
197 if (pack->map) {
198 obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
199 SHA1Update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
200 mapoff += obj->tslen;
201 } else {
202 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
203 if (lseek(pack->fd, obj->off, SEEK_SET0) == -1)
204 return got_error_from_errno("lseek");
205 err = read_checksum(&obj->crc, pack_sha1_ctx,
206 pack->fd, obj->tslen);
207 if (err)
208 return err;
209 }
210
211 switch (obj->type) {
212 case GOT_OBJ_TYPE_BLOB3:
213 case GOT_OBJ_TYPE_COMMIT1:
214 case GOT_OBJ_TYPE_TREE2:
215 case GOT_OBJ_TYPE_TAG4:
216 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX(8 * 1024 * 1024)) {
217 if (fseek(tmpfile, 0L, SEEK_SET0) == -1) {
218 err = got_error_from_errno("fseek");
219 break;
220 }
221 if (pack->map) {
222 err = got_inflate_to_file_mmap(&datalen,
223 &obj->len, &csum, pack->map, mapoff,
224 pack->filesize - mapoff, tmpfile);
225 } else {
226 err = got_inflate_to_file_fd(&datalen,
227 &obj->len, &csum, pack->fd, tmpfile);
228 }
229 } else {
230 if (pack->map) {
231 err = got_inflate_to_mem_mmap(&data, &datalen,
232 &obj->len, &csum, pack->map, mapoff,
233 pack->filesize - mapoff);
234 } else {
235 err = got_inflate_to_mem_fd(&data, &datalen,
236 &obj->len, &csum, obj->size, pack->fd);
237 }
238 }
239 if (err)
240 break;
241 SHA1Init(&ctx);
242 err = get_obj_type_label(&obj_label, obj->type);
243 if (err) {
244 free(data);
245 break;
246 }
247 if (asprintf(&header, "%s %lld", obj_label,
248 (long long)obj->size) == -1) {
249 err = got_error_from_errno("asprintf");
250 free(data);
251 break;
252 }
253 headerlen = strlen(header) + 1;
254 SHA1Update(&ctx, header, headerlen);
255 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX(8 * 1024 * 1024)) {
256 err = read_file_sha1(&ctx, tmpfile, datalen);
257 if (err)
258 break;
259 } else
260 SHA1Update(&ctx, data, datalen);
261 SHA1Final(obj->id.sha1, &ctx);
262 free(header);
263 free(data);
264 break;
265 case GOT_OBJ_TYPE_REF_DELTA7:
266 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH20);
267 if (pack->map) {
268 if (mapoff + SHA1_DIGEST_LENGTH20 >= pack->filesize) {
269 err = got_error(GOT_ERR_BAD_PACKFILE16);
270 break;
271 }
272 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
273 SHA1_DIGEST_LENGTH20);
274 obj->crc = crc32(obj->crc, pack->map + mapoff,
275 SHA1_DIGEST_LENGTH20);
276 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
277 SHA1_DIGEST_LENGTH20);
278 mapoff += SHA1_DIGEST_LENGTH20;
279 err = got_inflate_to_mem_mmap(NULL((void *)0), &datalen,
280 &obj->len, &csum, pack->map, mapoff,
281 pack->filesize - mapoff);
282 if (err)
283 break;
284 } else {
285 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
286 SHA1_DIGEST_LENGTH20);
287 if (n == -1) {
288 err = got_error_from_errno("read");
289 break;
290 }
291 if (n < sizeof(obj->id)) {
292 err = got_error(GOT_ERR_BAD_PACKFILE16);
293 break;
294 }
295 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
296 SHA1_DIGEST_LENGTH20);
297 SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
298 SHA1_DIGEST_LENGTH20);
299 err = got_inflate_to_mem_fd(NULL((void *)0), &datalen, &obj->len,
300 &csum, obj->size, pack->fd);
301 if (err)
302 break;
303 }
304 obj->len += SHA1_DIGEST_LENGTH20;
305 break;
306 case GOT_OBJ_TYPE_OFFSET_DELTA6:
307 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH20);
308 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
309 &obj->delta.ofs.base_offsetlen, pack, obj->off,
310 obj->tslen);
311 if (err)
312 break;
313
314 if (pack->map) {
315 obj->crc = crc32(obj->crc, pack->map + mapoff,
316 obj->delta.ofs.base_offsetlen);
317 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
318 obj->delta.ofs.base_offsetlen);
319 mapoff += obj->delta.ofs.base_offsetlen;
320 err = got_inflate_to_mem_mmap(NULL((void *)0), &datalen,
321 &obj->len, &csum, pack->map, mapoff,
322 pack->filesize - mapoff);
323 if (err)
324 break;
325 } else {
326 /*
327 * XXX Seek back and get CRC and SHA1 of on-disk
328 * offset bytes.
329 */
330 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET0)
331 == -1) {
332 err = got_error_from_errno("lseek");
333 break;
334 }
335 err = read_checksum(&obj->crc, pack_sha1_ctx,
336 pack->fd, obj->delta.ofs.base_offsetlen);
337 if (err)
338 break;
339
340 err = got_inflate_to_mem_fd(NULL((void *)0), &datalen, &obj->len,
341 &csum, obj->size, pack->fd);
342 if (err)
343 break;
344 }
345 obj->len += obj->delta.ofs.base_offsetlen;
346 break;
347 default:
348 err = got_error(GOT_ERR_OBJ_TYPE11);
349 break;
350 }
351
352 return err;
353}
354
355static const struct got_error *
356hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
357{
358 ssize_t w;
359
360 SHA1Update(ctx, buf, len);
361
362 w = write(fd, buf, len);
363 if (w == -1)
364 return got_error_from_errno("write");
365 if (w != len)
366 return got_error(GOT_ERR_IO6);
367
368 return NULL((void *)0);
369}
370
371static const struct got_error *
372resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
373 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
374 FILE *delta_accum_file)
375{
376 const struct got_error *err = NULL((void *)0);
377 struct got_delta_chain deltas;
378 struct got_delta *delta;
379 uint8_t *buf = NULL((void *)0);
380 size_t len = 0;
381 SHA1_CTX ctx;
382 char *header = NULL((void *)0);
383 size_t headerlen;
384 uint64_t max_size;
385 int base_obj_type;
386 const char *obj_label;
387
388 deltas.nentries = 0;
389 SIMPLEQ_INIT(&deltas.entries)do { (&deltas.entries)->sqh_first = ((void *)0); (&
deltas.entries)->sqh_last = &(&deltas.entries)->
sqh_first; } while (0)
;
390
391 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
392 obj->off, obj->tslen, obj->type, obj->size,
393 GOT_DELTA_CHAIN_RECURSION_MAX500);
394 if (err)
395 goto done;
396
397 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
398 if (err)
399 goto done;
400 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX(8 * 1024 * 1024)) {
401 rewind(tmpfile);
402 rewind(delta_base_file);
403 rewind(delta_accum_file);
404 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
405 pack, tmpfile, delta_base_file, delta_accum_file);
406 if (err)
407 goto done;
408 } else {
409 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
410 &deltas, pack);
411 }
412 if (err)
413 goto done;
414
415 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
416 if (err)
417 goto done;
418 err = get_obj_type_label(&obj_label, base_obj_type);
419 if (err)
420 goto done;
421 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
422 err = got_error_from_errno("asprintf");
423 goto done;
424 }
425 headerlen = strlen(header) + 1;
426 SHA1Init(&ctx);
427 SHA1Update(&ctx, header, headerlen);
428 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX(8 * 1024 * 1024)) {
429 err = read_file_sha1(&ctx, tmpfile, len);
430 if (err)
431 goto done;
432 } else
433 SHA1Update(&ctx, buf, len);
434 SHA1Final(obj->id.sha1, &ctx);
435done:
436 free(buf);
437 free(header);
438 while (!SIMPLEQ_EMPTY(&deltas.entries)(((&deltas.entries)->sqh_first) == ((void *)0))) {
439 delta = SIMPLEQ_FIRST(&deltas.entries)((&deltas.entries)->sqh_first);
440 SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry)do { if (((&deltas.entries)->sqh_first = (&deltas.
entries)->sqh_first->entry.sqe_next) == ((void *)0)) (&
deltas.entries)->sqh_last = &(&deltas.entries)->
sqh_first; } while (0)
;
441 free(delta);
442 }
443 return err;
444}
445
446/* Determine the slot in the pack index a given object ID should use. */
447static int
448find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
449{
450 u_int8_t id0 = sha1[0];
451 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff])(__uint32_t)(__builtin_constant_p(packidx->hdr.fanout_table
[0xff]) ? (__uint32_t)(((__uint32_t)(packidx->hdr.fanout_table
[0xff]) & 0xff) << 24 | ((__uint32_t)(packidx->hdr
.fanout_table[0xff]) & 0xff00) << 8 | ((__uint32_t)
(packidx->hdr.fanout_table[0xff]) & 0xff0000) >>
8 | ((__uint32_t)(packidx->hdr.fanout_table[0xff]) & 0xff000000
) >> 24) : __swap32md(packidx->hdr.fanout_table[0xff
]))
;
452 int left = 0, right = nindexed - 1;
453 int cmp = 0, i = 0;
454
455 if (id0 > 0)
456 left = be32toh(packidx->hdr.fanout_table[id0 - 1])(__uint32_t)(__builtin_constant_p(packidx->hdr.fanout_table
[id0 - 1]) ? (__uint32_t)(((__uint32_t)(packidx->hdr.fanout_table
[id0 - 1]) & 0xff) << 24 | ((__uint32_t)(packidx->
hdr.fanout_table[id0 - 1]) & 0xff00) << 8 | ((__uint32_t
)(packidx->hdr.fanout_table[id0 - 1]) & 0xff0000) >>
8 | ((__uint32_t)(packidx->hdr.fanout_table[id0 - 1]) &
0xff000000) >> 24) : __swap32md(packidx->hdr.fanout_table
[id0 - 1]))
;
457
458 while (left <= right) {
459 struct got_packidx_object_id *oid;
460
461 i = ((left + right) / 2);
462 oid = &packidx->hdr.sorted_ids[i];
463
464 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH20);
465 if (cmp == 0)
466 return -1; /* object already indexed */
467 else if (cmp > 0)
468 left = i + 1;
469 else if (cmp < 0)
470 right = i - 1;
471 }
472
473 return left;
474}
475
476#if 0
477static void
478print_packidx(struct got_packidx *packidx)
479{
480 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff])(__uint32_t)(__builtin_constant_p(packidx->hdr.fanout_table
[0xff]) ? (__uint32_t)(((__uint32_t)(packidx->hdr.fanout_table
[0xff]) & 0xff) << 24 | ((__uint32_t)(packidx->hdr
.fanout_table[0xff]) & 0xff00) << 8 | ((__uint32_t)
(packidx->hdr.fanout_table[0xff]) & 0xff0000) >>
8 | ((__uint32_t)(packidx->hdr.fanout_table[0xff]) & 0xff000000
) >> 24) : __swap32md(packidx->hdr.fanout_table[0xff
]))
;
481 int i;
482
483 fprintf(stderr(&__sF[2]), "object IDs:\n");
484 for (i = 0; i < nindexed; i++) {
485 char hex[SHA1_DIGEST_STRING_LENGTH(20 * 2 + 1)];
486 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
487 hex, sizeof(hex));
488 fprintf(stderr(&__sF[2]), "%s\n", hex);
489 }
490 fprintf(stderr(&__sF[2]), "\n");
491
492 fprintf(stderr(&__sF[2]), "object offsets:\n");
493 for (i = 0; i < nindexed; i++) {
494 uint32_t offset = be32toh(packidx->hdr.offsets[i])(__uint32_t)(__builtin_constant_p(packidx->hdr.offsets[i])
? (__uint32_t)(((__uint32_t)(packidx->hdr.offsets[i]) &
0xff) << 24 | ((__uint32_t)(packidx->hdr.offsets[i]
) & 0xff00) << 8 | ((__uint32_t)(packidx->hdr.offsets
[i]) & 0xff0000) >> 8 | ((__uint32_t)(packidx->hdr
.offsets[i]) & 0xff000000) >> 24) : __swap32md(packidx
->hdr.offsets[i]))
;
495 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX0x80000000) {
496 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK0x7fffffff;
497 fprintf(stderr(&__sF[2]), "%u -> %llu\n", offset,
498 be64toh(packidx->hdr.large_offsets[j])(__uint64_t)(__builtin_constant_p(packidx->hdr.large_offsets
[j]) ? (__uint64_t)((((__uint64_t)(packidx->hdr.large_offsets
[j]) & 0xff) << 56) | ((__uint64_t)(packidx->hdr
.large_offsets[j]) & 0xff00ULL) << 40 | ((__uint64_t
)(packidx->hdr.large_offsets[j]) & 0xff0000ULL) <<
24 | ((__uint64_t)(packidx->hdr.large_offsets[j]) & 0xff000000ULL
) << 8 | ((__uint64_t)(packidx->hdr.large_offsets[j]
) & 0xff00000000ULL) >> 8 | ((__uint64_t)(packidx->
hdr.large_offsets[j]) & 0xff0000000000ULL) >> 24 | (
(__uint64_t)(packidx->hdr.large_offsets[j]) & 0xff000000000000ULL
) >> 40 | ((__uint64_t)(packidx->hdr.large_offsets[j
]) & 0xff00000000000000ULL) >> 56) : __swap64md(packidx
->hdr.large_offsets[j]))
);
499 } else
500 fprintf(stderr(&__sF[2]), "%u\n", offset);
501 }
502 fprintf(stderr(&__sF[2]), "\n");
503
504 fprintf(stderr(&__sF[2]), "fanout table:");
505 for (i = 0; i <= 0xff; i++)
506 fprintf(stderr(&__sF[2]), " %u", be32toh(packidx->hdr.fanout_table[i])(__uint32_t)(__builtin_constant_p(packidx->hdr.fanout_table
[i]) ? (__uint32_t)(((__uint32_t)(packidx->hdr.fanout_table
[i]) & 0xff) << 24 | ((__uint32_t)(packidx->hdr.
fanout_table[i]) & 0xff00) << 8 | ((__uint32_t)(packidx
->hdr.fanout_table[i]) & 0xff0000) >> 8 | ((__uint32_t
)(packidx->hdr.fanout_table[i]) & 0xff000000) >>
24) : __swap32md(packidx->hdr.fanout_table[i]))
);
507 fprintf(stderr(&__sF[2]), "\n");
508}
509#endif
510
511static void
512add_indexed_object(struct got_packidx *packidx, uint32_t idx,
513 struct got_indexed_object *obj)
514{
515 int i;
516
517 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
518 SHA1_DIGEST_LENGTH20);
519 packidx->hdr.crc32[idx] = htobe32(obj->crc)(__uint32_t)(__builtin_constant_p(obj->crc) ? (__uint32_t)
(((__uint32_t)(obj->crc) & 0xff) << 24 | ((__uint32_t
)(obj->crc) & 0xff00) << 8 | ((__uint32_t)(obj->
crc) & 0xff0000) >> 8 | ((__uint32_t)(obj->crc) &
0xff000000) >> 24) : __swap32md(obj->crc))
;
520 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX0x80000000)
521 packidx->hdr.offsets[idx] = htobe32(obj->off)(__uint32_t)(__builtin_constant_p(obj->off) ? (__uint32_t)
(((__uint32_t)(obj->off) & 0xff) << 24 | ((__uint32_t
)(obj->off) & 0xff00) << 8 | ((__uint32_t)(obj->
off) & 0xff0000) >> 8 | ((__uint32_t)(obj->off) &
0xff000000) >> 24) : __swap32md(obj->off))
;
522 else {
523 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |(__uint32_t)(__builtin_constant_p(packidx->nlargeobj | 0x80000000
) ? (__uint32_t)(((__uint32_t)(packidx->nlargeobj | 0x80000000
) & 0xff) << 24 | ((__uint32_t)(packidx->nlargeobj
| 0x80000000) & 0xff00) << 8 | ((__uint32_t)(packidx
->nlargeobj | 0x80000000) & 0xff0000) >> 8 | ((__uint32_t
)(packidx->nlargeobj | 0x80000000) & 0xff000000) >>
24) : __swap32md(packidx->nlargeobj | 0x80000000))
524 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)(__uint32_t)(__builtin_constant_p(packidx->nlargeobj | 0x80000000
) ? (__uint32_t)(((__uint32_t)(packidx->nlargeobj | 0x80000000
) & 0xff) << 24 | ((__uint32_t)(packidx->nlargeobj
| 0x80000000) & 0xff00) << 8 | ((__uint32_t)(packidx
->nlargeobj | 0x80000000) & 0xff0000) >> 8 | ((__uint32_t
)(packidx->nlargeobj | 0x80000000) & 0xff000000) >>
24) : __swap32md(packidx->nlargeobj | 0x80000000))
;
525 packidx->hdr.large_offsets[packidx->nlargeobj] =
526 htobe64(obj->off)(__uint64_t)(__builtin_constant_p(obj->off) ? (__uint64_t)
((((__uint64_t)(obj->off) & 0xff) << 56) | ((__uint64_t
)(obj->off) & 0xff00ULL) << 40 | ((__uint64_t)(obj
->off) & 0xff0000ULL) << 24 | ((__uint64_t)(obj->
off) & 0xff000000ULL) << 8 | ((__uint64_t)(obj->
off) & 0xff00000000ULL) >> 8 | ((__uint64_t)(obj->
off) & 0xff0000000000ULL) >> 24 | ((__uint64_t)(obj
->off) & 0xff000000000000ULL) >> 40 | ((__uint64_t
)(obj->off) & 0xff00000000000000ULL) >> 56) : __swap64md
(obj->off))
;
527 packidx->nlargeobj++;
528 }
529
530 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
531 uint32_t n = be32toh(packidx->hdr.fanout_table[i])(__uint32_t)(__builtin_constant_p(packidx->hdr.fanout_table
[i]) ? (__uint32_t)(((__uint32_t)(packidx->hdr.fanout_table
[i]) & 0xff) << 24 | ((__uint32_t)(packidx->hdr.
fanout_table[i]) & 0xff00) << 8 | ((__uint32_t)(packidx
->hdr.fanout_table[i]) & 0xff0000) >> 8 | ((__uint32_t
)(packidx->hdr.fanout_table[i]) & 0xff000000) >>
24) : __swap32md(packidx->hdr.fanout_table[i]))
;
532 packidx->hdr.fanout_table[i] = htobe32(n + 1)(__uint32_t)(__builtin_constant_p(n + 1) ? (__uint32_t)(((__uint32_t
)(n + 1) & 0xff) << 24 | ((__uint32_t)(n + 1) &
0xff00) << 8 | ((__uint32_t)(n + 1) & 0xff0000) >>
8 | ((__uint32_t)(n + 1) & 0xff000000) >> 24) : __swap32md
(n + 1))
;
533 }
534}
535
536static int
537indexed_obj_cmp(const void *pa, const void *pb)
538{
539 struct got_indexed_object *a, *b;
540
541 a = (struct got_indexed_object *)pa;
542 b = (struct got_indexed_object *)pb;
543 return got_object_id_cmp(&a->id, &b->id);
544}
545
546static void
547make_packidx(struct got_packidx *packidx, int nobj,
548 struct got_indexed_object *objects)
549{
550 struct got_indexed_object *obj;
551 int i;
552 uint32_t idx = 0;
553
554 qsort(objects, nobj, sizeof(struct got_indexed_object),
555 indexed_obj_cmp);
556
557 memset(packidx->hdr.fanout_table, 0,
558 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS(0xff + 1) * sizeof(uint32_t));
559 packidx->nlargeobj = 0;
560
561 for (i = 0; i < nobj; i++) {
562 obj = &objects[i];
563 if (obj->valid)
564 add_indexed_object(packidx, idx++, obj);
565 }
566}
567
568static void
569update_packidx(struct got_packidx *packidx, int nobj,
570 struct got_indexed_object *obj)
571{
572 int idx;
573 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff])(__uint32_t)(__builtin_constant_p(packidx->hdr.fanout_table
[0xff]) ? (__uint32_t)(((__uint32_t)(packidx->hdr.fanout_table
[0xff]) & 0xff) << 24 | ((__uint32_t)(packidx->hdr
.fanout_table[0xff]) & 0xff00) << 8 | ((__uint32_t)
(packidx->hdr.fanout_table[0xff]) & 0xff0000) >>
8 | ((__uint32_t)(packidx->hdr.fanout_table[0xff]) & 0xff000000
) >> 24) : __swap32md(packidx->hdr.fanout_table[0xff
]))
;
574
575 idx = find_object_idx(packidx, obj->id.sha1);
576 if (idx == -1) {
577 char hex[SHA1_DIGEST_STRING_LENGTH(20 * 2 + 1)];
578 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
579 return; /* object already indexed */
580 }
581
582 memmove(&packidx->hdr.sorted_ids[idx + 1],
583 &packidx->hdr.sorted_ids[idx],
584 sizeof(struct got_packidx_object_id) * (nindexed - idx));
585 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
586 sizeof(uint32_t) * (nindexed - idx));
587
588 add_indexed_object(packidx, idx, obj);
589}
590
591static const struct got_error *
592send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
593 int nobj_indexed, int nobj_loose, int nobj_resolved)
594{
595 struct got_imsg_index_pack_progress iprogress;
596
597 iprogress.nobj_total = nobj_total;
598 iprogress.nobj_indexed = nobj_indexed;
599 iprogress.nobj_loose = nobj_loose;
600 iprogress.nobj_resolved = nobj_resolved;
601
602 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
603 &iprogress, sizeof(iprogress)) == -1)
604 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
605
606 return got_privsep_flush_imsg(ibuf);
607}
608
609static const struct got_error *
610send_index_pack_done(struct imsgbuf *ibuf)
611{
612 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL((void *)0), 0) == -1)
613 return got_error_from_errno("imsg_compose FETCH");
614 return got_privsep_flush_imsg(ibuf);
615}
616
617
618static const struct got_error *
619index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
620 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
621 struct imsgbuf *ibuf)
622{
623 const struct got_error *err;
624 struct got_packfile_hdr hdr;
625 struct got_packidx packidx;
626 char buf[8];
627 char pack_sha1[SHA1_DIGEST_LENGTH20];
628 int nobj, nvalid, nloose, nresolved = 0, i;
629 struct got_indexed_object *objects = NULL((void *)0), *obj;
630 SHA1_CTX ctx;
631 uint8_t packidx_hash[SHA1_DIGEST_LENGTH20];
632 ssize_t r, w;
633 int pass, have_ref_deltas = 0, first_delta_idx = -1;
634 size_t mapoff = 0;
635 int p_indexed = 0, last_p_indexed = -1;
636 int p_resolved = 0, last_p_resolved = -1;
637
638 /* Require that pack file header and SHA1 trailer are present. */
639 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH20)
1
Assuming the condition is false
2
Taking false branch
640 return got_error_msg(GOT_ERR_BAD_PACKFILE16,
641 "short pack file");
642
643 if (pack->map) {
3
Assuming field 'map' is non-null
4
Taking true branch
644 memcpy(&hdr, pack->map, sizeof(hdr));
645 mapoff += sizeof(hdr);
646 } else {
647 r = read(pack->fd, &hdr, sizeof(hdr));
648 if (r == -1)
649 return got_error_from_errno("read");
650 if (r < sizeof(hdr))
651 return got_error_msg(GOT_ERR_BAD_PACKFILE16,
652 "short pack file");
653 }
654
655 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE)(__uint32_t)(__builtin_constant_p(0x5041434b) ? (__uint32_t)(
((__uint32_t)(0x5041434b) & 0xff) << 24 | ((__uint32_t
)(0x5041434b) & 0xff00) << 8 | ((__uint32_t)(0x5041434b
) & 0xff0000) >> 8 | ((__uint32_t)(0x5041434b) &
0xff000000) >> 24) : __swap32md(0x5041434b))
)
5
'?' condition is true
6
Assuming the condition is false
7
Taking false branch
656 return got_error_msg(GOT_ERR_BAD_PACKFILE16,
657 "bad packfile signature");
658 if (hdr.version != htobe32(GOT_PACKFILE_VERSION)(__uint32_t)(__builtin_constant_p(2) ? (__uint32_t)(((__uint32_t
)(2) & 0xff) << 24 | ((__uint32_t)(2) & 0xff00)
<< 8 | ((__uint32_t)(2) & 0xff0000) >> 8 | (
(__uint32_t)(2) & 0xff000000) >> 24) : __swap32md(2
))
)
8
'?' condition is true
9
Assuming the condition is false
10
Taking false branch
659 return got_error_msg(GOT_ERR_BAD_PACKFILE16,
660 "bad packfile version");
661 nobj = be32toh(hdr.nobjects)(__uint32_t)(__builtin_constant_p(hdr.nobjects) ? (__uint32_t
)(((__uint32_t)(hdr.nobjects) & 0xff) << 24 | ((__uint32_t
)(hdr.nobjects) & 0xff00) << 8 | ((__uint32_t)(hdr.
nobjects) & 0xff0000) >> 8 | ((__uint32_t)(hdr.nobjects
) & 0xff000000) >> 24) : __swap32md(hdr.nobjects))
;
11
'?' condition is false
662 if (nobj == 0)
12
Assuming 'nobj' is not equal to 0
13
Taking false branch
663 return got_error_msg(GOT_ERR_BAD_PACKFILE16,
664 "bad packfile with zero objects");
665
666 /* We compute the SHA1 of pack file contents and verify later on. */
667 SHA1Init(&ctx);
668 SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
669
670 /*
671 * Create an in-memory pack index which will grow as objects
672 * IDs in the pack file are discovered. Only fields used to
673 * read deltified objects will be needed by the pack.c library
674 * code, so setting up just a pack index header is sufficient.
675 */
676 memset(&packidx, 0, sizeof(packidx));
677 packidx.hdr.magic = malloc(sizeof(uint32_t));
678 if (packidx.hdr.magic == NULL((void *)0))
14
Assuming field 'magic' is not equal to NULL
15
Taking false branch
679 return got_error_from_errno("calloc");
680 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC)(__uint32_t)(__builtin_constant_p(0xff744f63) ? (__uint32_t)(
((__uint32_t)(0xff744f63) & 0xff) << 24 | ((__uint32_t
)(0xff744f63) & 0xff00) << 8 | ((__uint32_t)(0xff744f63
) & 0xff0000) >> 8 | ((__uint32_t)(0xff744f63) &
0xff000000) >> 24) : __swap32md(0xff744f63))
;
16
'?' condition is true
681 packidx.hdr.version = malloc(sizeof(uint32_t));
682 if (packidx.hdr.version == NULL((void *)0)) {
17
Assuming field 'version' is not equal to NULL
18
Taking false branch
683 err = got_error_from_errno("malloc");
684 goto done;
685 }
686 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION)(__uint32_t)(__builtin_constant_p(2) ? (__uint32_t)(((__uint32_t
)(2) & 0xff) << 24 | ((__uint32_t)(2) & 0xff00)
<< 8 | ((__uint32_t)(2) & 0xff0000) >> 8 | (
(__uint32_t)(2) & 0xff000000) >> 24) : __swap32md(2
))
;
19
'?' condition is true
687 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS(0xff + 1),
20
Memory is allocated
688 sizeof(uint32_t));
689 if (packidx.hdr.fanout_table == NULL((void *)0)) {
21
Assuming field 'fanout_table' is not equal to NULL
22
Taking false branch
690 err = got_error_from_errno("calloc");
691 goto done;
692 }
693 packidx.hdr.sorted_ids = calloc(nobj,
694 sizeof(struct got_packidx_object_id));
695 if (packidx.hdr.sorted_ids == NULL((void *)0)) {
23
Assuming field 'sorted_ids' is not equal to NULL
24
Taking false branch
696 err = got_error_from_errno("calloc");
697 goto done;
698 }
699 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
700 if (packidx.hdr.crc32 == NULL((void *)0)) {
25
Assuming field 'crc32' is not equal to NULL
26
Taking false branch
701 err = got_error_from_errno("calloc");
702 goto done;
703 }
704 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
705 if (packidx.hdr.offsets == NULL((void *)0)) {
27
Assuming field 'offsets' is not equal to NULL
28
Taking false branch
706 err = got_error_from_errno("calloc");
707 goto done;
708 }
709 /* Large offsets table is empty for pack files < 2 GB. */
710 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX0x80000000) {
29
Assuming field 'filesize' is < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX
30
Taking false branch
711 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
712 if (packidx.hdr.large_offsets == NULL((void *)0)) {
713 err = got_error_from_errno("calloc");
714 goto done;
715 }
716 }
717
718 nvalid = 0;
719 nloose = 0;
720 objects = calloc(nobj, sizeof(struct got_indexed_object));
721 if (objects == NULL((void *)0))
31
Assuming 'objects' is equal to NULL
32
Taking true branch
722 return got_error_from_errno("calloc");
33
Potential leak of memory pointed to by 'packidx.hdr.fanout_table'
723
724 /*
725 * First pass: locate all objects and identify un-deltified objects.
726 *
727 * When this pass has completed we will know offset, type, size, and
728 * CRC information for all objects in this pack file. We won't know
729 * any of the actual object IDs of deltified objects yet since we
730 * will not yet attempt to combine deltas.
731 */
732 pass = 1;
733 for (i = 0; i < nobj; i++) {
734 /* Don't send too many progress privsep messages. */
735 p_indexed = ((i + 1) * 100) / nobj;
736 if (p_indexed != last_p_indexed) {
737 err = send_index_pack_progress(ibuf, nobj, i + 1,
738 nloose, 0);
739 if (err)
740 goto done;
741 last_p_indexed = p_indexed;
742 }
743
744 obj = &objects[i];
745 obj->crc = crc32(0L, NULL((void *)0), 0);
746
747 /* Store offset to type+size information for this object. */
748 if (pack->map) {
749 obj->off = mapoff;
750 } else {
751 obj->off = lseek(pack->fd, 0, SEEK_CUR1);
752 if (obj->off == -1) {
753 err = got_error_from_errno("lseek");
754 goto done;
755 }
756 }
757
758 err = read_packed_object(pack, obj, tmpfile, &ctx);
759 if (err)
760 goto done;
761
762 if (pack->map) {
763 mapoff += obj->tslen + obj->len;
764 } else {
765 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
766 SEEK_SET0) == -1) {
767 err = got_error_from_errno("lseek");
768 goto done;
769 }
770 }
771
772 if (obj->type == GOT_OBJ_TYPE_BLOB3 ||
773 obj->type == GOT_OBJ_TYPE_TREE2 ||
774 obj->type == GOT_OBJ_TYPE_COMMIT1 ||
775 obj->type == GOT_OBJ_TYPE_TAG4) {
776 obj->valid = 1;
777 nloose++;
778 } else {
779 if (first_delta_idx == -1)
780 first_delta_idx = i;
781 if (obj->type == GOT_OBJ_TYPE_REF_DELTA7)
782 have_ref_deltas = 1;
783 }
784 }
785 nvalid = nloose;
786
787 /*
788 * Having done a full pass over the pack file and can now
789 * verify its checksum.
790 */
791 SHA1Final(pack_sha1, &ctx);
792 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH20) != 0) {
793 err = got_error_msg(GOT_ERR_BAD_PACKFILE16,
794 "pack file checksum mismatch");
795 goto done;
796 }
797
798 /* Verify the SHA1 checksum stored at the end of the pack file. */
799 if (pack->map) {
800 memcpy(pack_sha1_expected, pack->map +
801 pack->filesize - SHA1_DIGEST_LENGTH20,
802 SHA1_DIGEST_LENGTH20);
803 } else {
804 ssize_t n;
805 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH20, SEEK_END2) == -1) {
806 err = got_error_from_errno("lseek");
807 goto done;
808 }
809 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH20);
810 if (n == -1) {
811 err = got_error_from_errno("read");
812 goto done;
813 }
814 if (n != SHA1_DIGEST_LENGTH20) {
815 err = got_error(GOT_ERR_IO6);
816 goto done;
817 }
818 }
819 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH20) != 0) {
820 err = got_error_msg(GOT_ERR_BAD_PACKFILE16,
821 "bad checksum in pack file trailer");
822 goto done;
823 }
824
825 if (first_delta_idx == -1)
826 first_delta_idx = 0;
827
828 /* In order to resolve ref deltas we need an in-progress pack index. */
829 if (have_ref_deltas)
830 make_packidx(&packidx, nobj, objects);
831
832 /*
833 * Second pass: We can now resolve deltas to compute the IDs of
834 * objects which appear in deltified form. Because deltas can be
835 * chained this pass may require a couple of iterations until all
836 * IDs of deltified objects have been discovered.
837 */
838 pass++;
839 while (nvalid != nobj) {
840 int n = 0;
841 /*
842 * This loop will only run once unless the pack file
843 * contains ref deltas which refer to objects located
844 * later in the pack file, which is unusual.
845 * Offset deltas can always be resolved in one pass
846 * unless the packfile is corrupt.
847 */
848 for (i = first_delta_idx; i < nobj; i++) {
849 obj = &objects[i];
850 if (obj->type != GOT_OBJ_TYPE_REF_DELTA7 &&
851 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA6)
852 continue;
853
854 if (obj->valid)
855 continue;
856
857 if (pack->map == NULL((void *)0) && lseek(pack->fd,
858 obj->off + obj->tslen, SEEK_SET0) == -1) {
859 err = got_error_from_errno("lseek");
860 goto done;
861 }
862
863 err = resolve_deltified_object(pack, &packidx, obj,
864 tmpfile, delta_base_file, delta_accum_file);
865 if (err) {
866 if (err->code != GOT_ERR_NO_OBJ17)
867 goto done;
868 /*
869 * We cannot resolve this object yet because
870 * a delta base is unknown. Try again later.
871 */
872 continue;
873 }
874
875 obj->valid = 1;
876 n++;
877 if (have_ref_deltas)
878 update_packidx(&packidx, nobj, obj);
879 /* Don't send too many progress privsep messages. */
880 p_resolved = ((nresolved + n) * 100) / nobj;
881 if (p_resolved != last_p_resolved) {
882 err = send_index_pack_progress(ibuf, nobj,
883 nobj, nloose, nresolved + n);
884 if (err)
885 goto done;
886 last_p_resolved = p_resolved;
887 }
888
889 }
890 if (pass++ > 3 && n == 0) {
891 static char msg[64];
892 snprintf(msg, sizeof(msg), "could not resolve "
893 "any of deltas; packfile could be corrupt");
894 err = got_error_msg(GOT_ERR_BAD_PACKFILE16, msg);
895 goto done;
896
897 }
898 nresolved += n;
899 nvalid += nresolved;
900 }
901
902 if (nloose + nresolved != nobj) {
903 static char msg[64];
904 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
905 nloose + nresolved, nobj);
906 err = got_error_msg(GOT_ERR_BAD_PACKFILE16, msg);
907 goto done;
908 }
909
910 err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved);
911 if (err)
912 goto done;
913
914 make_packidx(&packidx, nobj, objects);
915
916 free(objects);
917 objects = NULL((void *)0);
918
919 SHA1Init(&ctx);
920 putbe32(buf, GOT_PACKIDX_V2_MAGIC0xff744f63);
921 putbe32(buf + 4, GOT_PACKIDX_VERSION2);
922 err = hwrite(idxfd, buf, 8, &ctx);
923 if (err)
924 goto done;
925 err = hwrite(idxfd, packidx.hdr.fanout_table,
926 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS(0xff + 1) * sizeof(uint32_t), &ctx);
927 if (err)
928 goto done;
929 err = hwrite(idxfd, packidx.hdr.sorted_ids,
930 nobj * SHA1_DIGEST_LENGTH20, &ctx);
931 if (err)
932 goto done;
933 err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
934 if (err)
935 goto done;
936 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
937 &ctx);
938 if (err)
939 goto done;
940 if (packidx.nlargeobj > 0) {
941 err = hwrite(idxfd, packidx.hdr.large_offsets,
942 packidx.nlargeobj * sizeof(uint64_t), &ctx);
943 if (err)
944 goto done;
945 }
946 err = hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH20, &ctx);
947 if (err)
948 goto done;
949
950 SHA1Final(packidx_hash, &ctx);
951 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
952 if (w == -1) {
953 err = got_error_from_errno("write");
954 goto done;
955 }
956 if (w != sizeof(packidx_hash)) {
957 err = got_error(GOT_ERR_IO6);
958 goto done;
959 }
960done:
961 free(objects);
962 free(packidx.hdr.magic);
963 free(packidx.hdr.version);
964 free(packidx.hdr.fanout_table);
965 free(packidx.hdr.sorted_ids);
966 free(packidx.hdr.offsets);
967 free(packidx.hdr.large_offsets);
968 return err;
969}
970
971int
972main(int argc, char **argv)
973{
974 const struct got_error *err = NULL((void *)0), *close_err;
975 struct imsgbuf ibuf;
976 struct imsg imsg;
977 size_t i;
978 int idxfd = -1, tmpfd = -1;
979 FILE *tmpfiles[3];
980 struct got_pack pack;
981 uint8_t pack_hash[SHA1_DIGEST_LENGTH20];
982 off_t packfile_size;
983#if 0
984 static int attached;
985 while (!attached)
986 sleep(1);
987#endif
988
989 for (i = 0; i < nitems(tmpfiles)(sizeof((tmpfiles)) / sizeof((tmpfiles)[0])); i++)
990 tmpfiles[i] = NULL((void *)0);
991
992 memset(&pack, 0, sizeof(pack));
993 pack.fd = -1;
994 pack.delta_cache = got_delta_cache_alloc(500,
995 GOT_DELTA_RESULT_SIZE_CACHED_MAX(8 * 1024 * 1024));
996 if (pack.delta_cache == NULL((void *)0)) {
997 err = got_error_from_errno("got_delta_cache_alloc");
998 goto done;
999 }
1000
1001 imsg_init(&ibuf, GOT_IMSG_FD_CHILD(2 + 1));
1002#ifndef PROFILE
1003 /* revoke access to most system calls */
1004 if (pledge("stdio recvfd", NULL((void *)0)) == -1) {
1005 err = got_error_from_errno("pledge");
1006 got_privsep_send_error(&ibuf, err);
1007 return 1;
1008 }
1009#endif
1010 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1011 if (err)
1012 goto done;
1013 if (imsg.hdr.type == GOT_IMSG_STOP)
1014 goto done;
1015 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
1016 err = got_error(GOT_ERR_PRIVSEP_MSG39);
1017 goto done;
1018 }
1019 if (imsg.hdr.len - IMSG_HEADER_SIZEsizeof(struct imsg_hdr) != sizeof(pack_hash)) {
1020 err = got_error(GOT_ERR_PRIVSEP_LEN36);
1021 goto done;
1022 }
1023 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
1024 pack.fd = imsg.fd;
1025
1026 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1027 if (err)
1028 goto done;
1029 if (imsg.hdr.type == GOT_IMSG_STOP)
1030 goto done;
1031 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
1032 err = got_error(GOT_ERR_PRIVSEP_MSG39);
1033 goto done;
1034 }
1035 if (imsg.hdr.len - IMSG_HEADER_SIZEsizeof(struct imsg_hdr) != 0) {
1036 err = got_error(GOT_ERR_PRIVSEP_LEN36);
1037 goto done;
1038 }
1039 idxfd = imsg.fd;
1040
1041 for (i = 0; i < nitems(tmpfiles)(sizeof((tmpfiles)) / sizeof((tmpfiles)[0])); i++) {
1042 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1043 if (err)
1044 goto done;
1045 if (imsg.hdr.type == GOT_IMSG_STOP)
1046 goto done;
1047 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
1048 err = got_error(GOT_ERR_PRIVSEP_MSG39);
1049 goto done;
1050 }
1051 if (imsg.hdr.len - IMSG_HEADER_SIZEsizeof(struct imsg_hdr) != 0) {
1052 err = got_error(GOT_ERR_PRIVSEP_LEN36);
1053 goto done;
1054 }
1055 tmpfd = imsg.fd;
1056 tmpfiles[i] = fdopen(tmpfd, "w+");
1057 if (tmpfiles[i] == NULL((void *)0)) {
1058 err = got_error_from_errno("fdopen");
1059 goto done;
1060 }
1061 tmpfd = -1;
1062 }
1063
1064 if (lseek(pack.fd, 0, SEEK_END2) == -1) {
1065 err = got_error_from_errno("lseek");
1066 goto done;
1067 }
1068 packfile_size = lseek(pack.fd, 0, SEEK_CUR1);
1069 if (packfile_size == -1) {
1070 err = got_error_from_errno("lseek");
1071 goto done;
1072 }
1073 pack.filesize = packfile_size; /* XXX off_t vs size_t */
1074
1075 if (lseek(pack.fd, 0, SEEK_SET0) == -1) {
1076 err = got_error_from_errno("lseek");
1077 goto done;
1078 }
1079
1080#ifndef GOT_PACK_NO_MMAP
1081 pack.map = mmap(NULL((void *)0), pack.filesize, PROT_READ0x01, MAP_PRIVATE0x0002,
1082 pack.fd, 0);
1083 if (pack.map == MAP_FAILED((void *)-1))
1084 pack.map = NULL((void *)0); /* fall back to read(2) */
1085#endif
1086 err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
1087 pack_hash, &ibuf);
1088done:
1089 close_err = got_pack_close(&pack);
1090 if (close_err && err == NULL((void *)0))
1091 err = close_err;
1092 if (idxfd != -1 && close(idxfd) == -1 && err == NULL((void *)0))
1093 err = got_error_from_errno("close");
1094 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL((void *)0))
1095 err = got_error_from_errno("close");
1096 for (i = 0; i < nitems(tmpfiles)(sizeof((tmpfiles)) / sizeof((tmpfiles)[0])); i++) {
1097 if (tmpfiles[i] != NULL((void *)0) && fclose(tmpfiles[i]) == EOF(-1) &&
1098 err == NULL((void *)0))
1099 err = got_error_from_errno("close");
1100 }
1101
1102 if (err == NULL((void *)0))
1103 err = send_index_pack_done(&ibuf);
1104 if (err) {
1105 got_privsep_send_error(&ibuf, err);
1106 fprintf(stderr(&__sF[2]), "%s: %s\n", getprogname(), err->msg);
1107 got_privsep_send_error(&ibuf, err);
1108 exit(1);
1109 }
1110
1111 exit(0);
1112}