clang -cc1 -cc1 -triple amd64-unknown-openbsd6.9 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name object.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/tog/../include -I /home/ben/Projects/got/tog/../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/tog/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/tog/../lib/object.c
| 1 |  | 
| 2 |   | 
| 3 |   | 
| 4 |   | 
| 5 |   | 
| 6 |   | 
| 7 |   | 
| 8 |   | 
| 9 |   | 
| 10 |   | 
| 11 |   | 
| 12 |   | 
| 13 |   | 
| 14 |   | 
| 15 |   | 
| 16 |   | 
| 17 | #include <sys/types.h> | 
| 18 | #include <sys/stat.h> | 
| 19 | #include <sys/queue.h> | 
| 20 | #include <sys/uio.h> | 
| 21 | #include <sys/socket.h> | 
| 22 | #include <sys/wait.h> | 
| 23 | #include <sys/resource.h> | 
| 24 |   | 
| 25 | #include <errno.h> | 
| 26 | #include <fcntl.h> | 
| 27 | #include <stdio.h> | 
| 28 | #include <stdlib.h> | 
| 29 | #include <string.h> | 
| 30 | #include <stdint.h> | 
| 31 | #include <sha1.h> | 
| 32 | #include <unistd.h> | 
| 33 | #include <zlib.h> | 
| 34 | #include <ctype.h> | 
| 35 | #include <libgen.h> | 
| 36 | #include <limits.h> | 
| 37 | #include <imsg.h> | 
| 38 | #include <time.h> | 
| 39 |   | 
| 40 | #include "got_error.h" | 
| 41 | #include "got_object.h" | 
| 42 | #include "got_repository.h" | 
| 43 | #include "got_opentemp.h" | 
| 44 | #include "got_path.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_privsep.h" | 
| 51 | #include "got_lib_object_idcache.h" | 
| 52 | #include "got_lib_object_cache.h" | 
| 53 | #include "got_lib_object_parse.h" | 
| 54 | #include "got_lib_pack.h" | 
| 55 | #include "got_lib_repository.h" | 
| 56 |   | 
| 57 | #ifndef MIN | 
| 58 | #define	MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b)) | 
| 59 | #endif | 
| 60 |   | 
| 61 | struct got_object_id * | 
| 62 | got_object_get_id(struct got_object *obj) | 
| 63 | { | 
| 64 | 	return &obj->id; | 
| 65 | } | 
| 66 |   | 
| 67 | const struct got_error * | 
| 68 | got_object_get_id_str(char **outbuf, struct got_object *obj) | 
| 69 | { | 
| 70 | 	return got_object_id_str(outbuf, &obj->id); | 
| 71 | } | 
| 72 |   | 
| 73 | const struct got_error * | 
| 74 | got_object_get_type(int *type, struct got_repository *repo, | 
| 75 |     struct got_object_id *id) | 
| 76 | { | 
| 77 | 	const struct got_error *err = NULL; | 
| 78 | 	struct got_object *obj; | 
| 79 |   | 
| 80 | 	err = got_object_open(&obj, repo, id); | 
| 81 | 	if (err) | 
| 82 | 		return err; | 
| 83 |   | 
| 84 | 	switch (obj->type) { | 
| 85 | 	case GOT_OBJ_TYPE_COMMIT: | 
| 86 | 	case GOT_OBJ_TYPE_TREE: | 
| 87 | 	case GOT_OBJ_TYPE_BLOB: | 
| 88 | 	case GOT_OBJ_TYPE_TAG: | 
| 89 | 		*type = obj->type; | 
| 90 | 		break; | 
| 91 | 	default: | 
| 92 | 		err = got_error(GOT_ERR_OBJ_TYPE); | 
| 93 | 		break; | 
| 94 | 	} | 
| 95 |   | 
| 96 | 	got_object_close(obj); | 
| 97 | 	return err; | 
| 98 | } | 
| 99 |   | 
| 100 | const struct got_error * | 
| 101 | got_object_get_path(char **path, struct got_object_id *id, | 
| 102 |     struct got_repository *repo) | 
| 103 | { | 
| 104 | 	const struct got_error *err = NULL; | 
| 105 | 	char *hex = NULL; | 
| 106 | 	char *path_objects; | 
| 107 |   | 
| 108 | 	*path = NULL; | 
| 109 |   | 
| 110 | 	path_objects = got_repo_get_path_objects(repo); | 
| 111 | 	if (path_objects == NULL) | 
| 112 | 		return got_error_from_errno("got_repo_get_path_objects"); | 
| 113 |   | 
| 114 | 	err = got_object_id_str(&hex, id); | 
| 115 | 	if (err) | 
| 116 | 		goto done; | 
| 117 |   | 
| 118 | 	if (asprintf(path, "%s/%.2x/%s", path_objects, | 
| 119 | 	    id->sha1[0], hex + 2) == -1) | 
| 120 | 		err = got_error_from_errno("asprintf"); | 
| 121 |   | 
| 122 | done: | 
| 123 | 	free(hex); | 
| 124 | 	free(path_objects); | 
| 125 | 	return err; | 
| 126 | } | 
| 127 |   | 
| 128 | const struct got_error * | 
| 129 | got_object_open_loose_fd(int *fd, struct got_object_id *id, | 
| 130 |     struct got_repository *repo) | 
| 131 | { | 
| 132 | 	const struct got_error *err = NULL; | 
| 133 | 	char *path; | 
| 134 |   | 
| 135 | 	err = got_object_get_path(&path, id, repo); | 
| 136 | 	if (err) | 
 | 30  |  | Assuming pointer value is null |  |  
  | 
 | 31  |  | Assuming 'err' is null, which participates in a condition later |  |  
  | 
 |  | 
| 137 | 		return err; | 
| 138 | 	*fd = open(path, O_RDONLY | O_NOFOLLOW); | 
| 139 | 	if (*fd == -1) { | 
 | 33  |  | Assuming the condition is false |  |  
  | 
 |  | 
| 140 | 		err = got_error_from_errno2("open", path); | 
| 141 | 		goto done; | 
| 142 | 	} | 
| 143 | done: | 
| 144 | 	free(path); | 
| 145 | 	return err; | 
 | 35  |  | Returning null pointer (loaded from 'err'), which participates in a condition later |  |  
  | 
| 146 | } | 
| 147 |   | 
| 148 | static const struct got_error * | 
| 149 | request_packed_object(struct got_object **obj, struct got_pack *pack, int idx, | 
| 150 |     struct got_object_id *id) | 
| 151 | { | 
| 152 | 	const struct got_error *err = NULL; | 
| 153 | 	struct imsgbuf *ibuf = pack->privsep_child->ibuf; | 
| 154 |   | 
| 155 | 	err = got_privsep_send_packed_obj_req(ibuf, idx, id); | 
| 156 | 	if (err) | 
| 157 | 		return err; | 
| 158 |   | 
| 159 | 	err = got_privsep_recv_obj(obj, ibuf); | 
| 160 | 	if (err) | 
| 161 | 		return err; | 
| 162 |   | 
| 163 | 	memcpy(&(*obj)->id, id, sizeof((*obj)->id)); | 
| 164 |   | 
| 165 | 	return NULL; | 
| 166 | } | 
| 167 |   | 
| 168 | static const struct got_error * | 
| 169 | request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen, | 
| 170 |     int outfd, struct got_pack *pack, int idx, struct got_object_id *id) | 
| 171 | { | 
| 172 | 	const struct got_error *err = NULL; | 
| 173 | 	struct imsgbuf *ibuf = pack->privsep_child->ibuf; | 
| 174 | 	int outfd_child; | 
| 175 | 	int basefd, accumfd;  | 
| 176 |   | 
| 177 | 	basefd = got_opentempfd(); | 
| 178 | 	if (basefd == -1) | 
| 179 | 		return got_error_from_errno("got_opentempfd"); | 
| 180 |   | 
| 181 | 	accumfd = got_opentempfd(); | 
| 182 | 	if (accumfd == -1) { | 
| 183 | 		close(basefd); | 
| 184 | 		return got_error_from_errno("got_opentempfd"); | 
| 185 | 	} | 
| 186 |   | 
| 187 | 	outfd_child = dup(outfd); | 
| 188 | 	if (outfd_child == -1) { | 
| 189 | 		err = got_error_from_errno("dup"); | 
| 190 | 		close(basefd); | 
| 191 | 		close(accumfd); | 
| 192 | 		return err; | 
| 193 | 	} | 
| 194 |   | 
| 195 | 	err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id); | 
| 196 | 	if (err) { | 
| 197 | 		close(basefd); | 
| 198 | 		close(accumfd); | 
| 199 | 		close(outfd_child); | 
| 200 | 		return err; | 
| 201 | 	} | 
| 202 |   | 
| 203 | 	err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child); | 
| 204 | 	if (err) { | 
| 205 | 		close(basefd); | 
| 206 | 		close(accumfd); | 
| 207 | 		return err; | 
| 208 | 	} | 
| 209 |   | 
| 210 |   | 
| 211 | 	err = got_privsep_send_tmpfd(pack->privsep_child->ibuf, | 
| 212 | 	    basefd); | 
| 213 | 	if (err) { | 
| 214 | 		close(accumfd); | 
| 215 | 		return err; | 
| 216 | 	} | 
| 217 |   | 
| 218 | 	err = got_privsep_send_tmpfd(pack->privsep_child->ibuf, | 
| 219 | 	    accumfd); | 
| 220 | 	if (err) | 
| 221 | 		return err; | 
| 222 |   | 
| 223 | 	err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf); | 
| 224 | 	if (err) | 
| 225 | 		return err; | 
| 226 |   | 
| 227 | 	return NULL; | 
| 228 | } | 
| 229 |   | 
| 230 | static void | 
| 231 | set_max_datasize(void) | 
| 232 | { | 
| 233 | 	struct rlimit rl; | 
| 234 |   | 
| 235 | 	if (getrlimit(RLIMIT_DATA, &rl) != 0) | 
| 236 | 		return; | 
| 237 |   | 
| 238 | 	rl.rlim_cur = rl.rlim_max; | 
| 239 | 	setrlimit(RLIMIT_DATA, &rl); | 
| 240 | } | 
| 241 |   | 
| 242 | static const struct got_error * | 
| 243 | start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx) | 
| 244 | { | 
| 245 | 	const struct got_error *err = NULL; | 
| 246 | 	int imsg_fds[2]; | 
| 247 | 	pid_t pid; | 
| 248 | 	struct imsgbuf *ibuf; | 
| 249 |   | 
| 250 | 	ibuf = calloc(1, sizeof(*ibuf)); | 
| 251 | 	if (ibuf == NULL) | 
| 252 | 		return got_error_from_errno("calloc"); | 
| 253 |   | 
| 254 | 	pack->privsep_child = calloc(1, sizeof(*pack->privsep_child)); | 
| 255 | 	if (pack->privsep_child == NULL) { | 
| 256 | 		err = got_error_from_errno("calloc"); | 
| 257 | 		free(ibuf); | 
| 258 | 		return err; | 
| 259 | 	} | 
| 260 |   | 
| 261 | 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) { | 
| 262 | 		err = got_error_from_errno("socketpair"); | 
| 263 | 		goto done; | 
| 264 | 	} | 
| 265 |   | 
| 266 | 	pid = fork(); | 
| 267 | 	if (pid == -1) { | 
| 268 | 		err = got_error_from_errno("fork"); | 
| 269 | 		goto done; | 
| 270 | 	} else if (pid == 0) { | 
| 271 | 		set_max_datasize(); | 
| 272 | 		got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK, | 
| 273 | 		    pack->path_packfile); | 
| 274 | 		 | 
| 275 | 	} | 
| 276 |   | 
| 277 | 	if (close(imsg_fds[1]) == -1) | 
| 278 | 		return got_error_from_errno("close"); | 
| 279 | 	pack->privsep_child->imsg_fd = imsg_fds[0]; | 
| 280 | 	pack->privsep_child->pid = pid; | 
| 281 | 	imsg_init(ibuf, imsg_fds[0]); | 
| 282 | 	pack->privsep_child->ibuf = ibuf; | 
| 283 |   | 
| 284 | 	err = got_privsep_init_pack_child(ibuf, pack, packidx); | 
| 285 | 	if (err) { | 
| 286 | 		const struct got_error *child_err; | 
| 287 | 		err = got_privsep_send_stop(pack->privsep_child->imsg_fd); | 
| 288 | 		child_err = got_privsep_wait_for_child( | 
| 289 | 		    pack->privsep_child->pid); | 
| 290 | 		if (child_err && err == NULL) | 
| 291 | 			err = child_err; | 
| 292 | 	} | 
| 293 | done: | 
| 294 | 	if (err) { | 
| 295 | 		free(ibuf); | 
| 296 | 		free(pack->privsep_child); | 
| 297 | 		pack->privsep_child = NULL; | 
| 298 | 	} | 
| 299 | 	return err; | 
| 300 | } | 
| 301 |   | 
| 302 | static const struct got_error * | 
| 303 | read_packed_object_privsep(struct got_object **obj, | 
| 304 |     struct got_repository *repo, struct got_pack *pack, | 
| 305 |     struct got_packidx *packidx, int idx, struct got_object_id *id) | 
| 306 | { | 
| 307 | 	const struct got_error *err = NULL; | 
| 308 |   | 
| 309 | 	if (pack->privsep_child == NULL) { | 
| 310 | 		err = start_pack_privsep_child(pack, packidx); | 
| 311 | 		if (err) | 
| 312 | 			return err; | 
| 313 | 	} | 
| 314 |   | 
| 315 | 	return request_packed_object(obj, pack, idx, id); | 
| 316 | } | 
| 317 |   | 
| 318 | static const struct got_error * | 
| 319 | read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen, | 
| 320 |     int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx, | 
| 321 |     struct got_object_id *id) | 
| 322 | { | 
| 323 | 	const struct got_error *err = NULL; | 
| 324 |   | 
| 325 | 	if (pack->privsep_child == NULL) { | 
| 326 | 		err = start_pack_privsep_child(pack, packidx); | 
| 327 | 		if (err) | 
| 328 | 			return err; | 
| 329 | 	} | 
| 330 |   | 
| 331 | 	return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack, | 
| 332 | 	    idx, id); | 
| 333 | } | 
| 334 |   | 
| 335 | static const struct got_error * | 
| 336 | open_packed_object(struct got_object **obj, struct got_object_id *id, | 
| 337 |     struct got_repository *repo) | 
| 338 | { | 
| 339 | 	const struct got_error *err = NULL; | 
| 340 | 	struct got_pack *pack = NULL; | 
| 341 | 	struct got_packidx *packidx = NULL; | 
| 342 | 	int idx; | 
| 343 | 	char *path_packfile; | 
| 344 |   | 
| 345 | 	err = got_repo_search_packidx(&packidx, &idx, repo, id); | 
| 346 | 	if (err) | 
| 347 | 		return err; | 
| 348 |   | 
| 349 | 	err = got_packidx_get_packfile_path(&path_packfile, packidx); | 
| 350 | 	if (err) | 
| 351 | 		return err; | 
| 352 |   | 
| 353 | 	pack = got_repo_get_cached_pack(repo, path_packfile); | 
| 354 | 	if (pack == NULL) { | 
| 355 | 		err = got_repo_cache_pack(&pack, repo, path_packfile, packidx); | 
| 356 | 		if (err) | 
| 357 | 			goto done; | 
| 358 | 	} | 
| 359 |   | 
| 360 | 	err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id); | 
| 361 | 	if (err) | 
| 362 | 		goto done; | 
| 363 | done: | 
| 364 | 	free(path_packfile); | 
| 365 | 	return err; | 
| 366 | } | 
| 367 |   | 
| 368 | static const struct got_error * | 
| 369 | request_object(struct got_object **obj, struct got_repository *repo, int fd) | 
| 370 | { | 
| 371 | 	const struct got_error *err = NULL; | 
| 372 | 	struct imsgbuf *ibuf; | 
| 373 |   | 
| 374 | 	ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf; | 
| 375 |   | 
| 376 | 	err = got_privsep_send_obj_req(ibuf, fd); | 
| 377 | 	if (err) | 
| 378 | 		return err; | 
| 379 |   | 
| 380 | 	return got_privsep_recv_obj(obj, ibuf); | 
| 381 | } | 
| 382 |   | 
| 383 | static const struct got_error * | 
| 384 | request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd, | 
| 385 |     struct got_repository *repo, int infd) | 
| 386 | { | 
| 387 | 	const struct got_error *err = NULL; | 
| 388 | 	struct imsgbuf *ibuf; | 
| 389 | 	int outfd_child; | 
| 390 |   | 
| 391 | 	ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf; | 
| 392 |   | 
| 393 | 	outfd_child = dup(outfd); | 
| 394 | 	if (outfd_child == -1) | 
| 395 | 		return got_error_from_errno("dup"); | 
| 396 |   | 
| 397 | 	err = got_privsep_send_raw_obj_req(ibuf, infd); | 
| 398 | 	if (err) | 
| 399 | 		return err; | 
| 400 |   | 
| 401 | 	err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child); | 
| 402 | 	if (err) | 
| 403 | 		return err; | 
| 404 |   | 
| 405 | 	return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf); | 
| 406 | } | 
| 407 |   | 
| 408 | static const struct got_error * | 
| 409 | start_read_object_child(struct got_repository *repo) | 
| 410 | { | 
| 411 | 	const struct got_error *err = NULL; | 
| 412 | 	int imsg_fds[2]; | 
| 413 | 	pid_t pid; | 
| 414 | 	struct imsgbuf *ibuf; | 
| 415 |   | 
| 416 | 	ibuf = calloc(1, sizeof(*ibuf)); | 
| 417 | 	if (ibuf == NULL) | 
| 418 | 		return got_error_from_errno("calloc"); | 
| 419 |   | 
| 420 | 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) { | 
| 421 | 		err = got_error_from_errno("socketpair"); | 
| 422 | 		free(ibuf); | 
| 423 | 		return err; | 
| 424 | 	} | 
| 425 |   | 
| 426 | 	pid = fork(); | 
| 427 | 	if (pid == -1) { | 
| 428 | 		err = got_error_from_errno("fork"); | 
| 429 | 		free(ibuf); | 
| 430 | 		return err; | 
| 431 | 	} | 
| 432 | 	else if (pid == 0) { | 
| 433 | 		got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT, | 
| 434 | 		    repo->path); | 
| 435 | 		 | 
| 436 | 	} | 
| 437 |   | 
| 438 | 	if (close(imsg_fds[1]) == -1) { | 
| 439 | 		err = got_error_from_errno("close"); | 
| 440 | 		free(ibuf); | 
| 441 | 		return err; | 
| 442 | 	} | 
| 443 |   | 
| 444 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd = | 
| 445 | 	    imsg_fds[0]; | 
| 446 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid; | 
| 447 | 	imsg_init(ibuf, imsg_fds[0]); | 
| 448 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf; | 
| 449 |   | 
| 450 | 	return NULL; | 
| 451 | } | 
| 452 |   | 
| 453 | static const struct got_error * | 
| 454 | read_object_header_privsep(struct got_object **obj, struct got_repository *repo, | 
| 455 |     int obj_fd) | 
| 456 | { | 
| 457 | 	const struct got_error *err; | 
| 458 |   | 
| 459 | 	if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1) | 
| 460 | 		return request_object(obj, repo, obj_fd); | 
| 461 |   | 
| 462 | 	err = start_read_object_child(repo); | 
| 463 | 	if (err) { | 
| 464 | 		close(obj_fd); | 
| 465 | 		return err; | 
| 466 | 	} | 
| 467 |   | 
| 468 | 	return request_object(obj, repo, obj_fd); | 
| 469 | } | 
| 470 |   | 
| 471 | static const struct got_error * | 
| 472 | read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen, | 
| 473 |     int outfd, struct got_repository *repo, int obj_fd) | 
| 474 | { | 
| 475 | 	const struct got_error *err; | 
| 476 |   | 
| 477 | 	if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1) | 
| 478 | 		return request_raw_object(outbuf, size, hdrlen, outfd, repo, | 
| 479 | 		    obj_fd); | 
| 480 |   | 
| 481 | 	err = start_read_object_child(repo); | 
| 482 | 	if (err) | 
| 483 | 		return err; | 
| 484 |   | 
| 485 | 	return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd); | 
| 486 | } | 
| 487 |   | 
| 488 | const struct got_error * | 
| 489 | got_object_open(struct got_object **obj, struct got_repository *repo, | 
| 490 |     struct got_object_id *id) | 
| 491 | { | 
| 492 | 	const struct got_error *err = NULL; | 
| 493 | 	int fd; | 
| 494 |   | 
| 495 | 	*obj = got_repo_get_cached_object(repo, id); | 
| 496 | 	if (*obj != NULL) { | 
| 497 | 		(*obj)->refcnt++; | 
| 498 | 		return NULL; | 
| 499 | 	} | 
| 500 |   | 
| 501 | 	err = open_packed_object(obj, id, repo); | 
| 502 | 	if (err && err->code != GOT_ERR_NO_OBJ) | 
| 503 | 		return err; | 
| 504 | 	if (*obj) { | 
| 505 | 		(*obj)->refcnt++; | 
| 506 | 		return got_repo_cache_object(repo, id, *obj); | 
| 507 | 	} | 
| 508 |   | 
| 509 | 	err = got_object_open_loose_fd(&fd, id, repo); | 
| 510 | 	if (err) { | 
| 511 | 		if (err->code == GOT_ERR_ERRNO && errno == ENOENT) | 
| 512 | 			err = got_error_no_obj(id); | 
| 513 | 		return err; | 
| 514 | 	} | 
| 515 |   | 
| 516 | 	err = read_object_header_privsep(obj, repo, fd); | 
| 517 | 	if (err) | 
| 518 | 		return err; | 
| 519 |   | 
| 520 | 	memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH); | 
| 521 |   | 
| 522 | 	(*obj)->refcnt++; | 
| 523 | 	return got_repo_cache_object(repo, id, *obj); | 
| 524 | } | 
| 525 |   | 
| 526 | const struct got_error * | 
| 527 | got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo, | 
| 528 |     struct got_object_id *id, size_t blocksize) | 
| 529 | { | 
| 530 | 	const struct got_error *err = NULL; | 
| 531 | 	struct got_packidx *packidx = NULL; | 
| 532 | 	int idx; | 
| 533 | 	uint8_t *outbuf = NULL; | 
| 534 | 	int outfd = -1; | 
| 535 | 	off_t size = 0; | 
| 536 | 	size_t hdrlen = 0; | 
| 537 | 	char *path_packfile = NULL; | 
| 538 |   | 
| 539 | 	*obj = NULL; | 
| 540 |   | 
| 541 | 	outfd = got_opentempfd(); | 
| 542 | 	if (outfd == -1) | 
| 543 | 		return got_error_from_errno("got_opentempfd"); | 
| 544 |   | 
| 545 | 	err = got_repo_search_packidx(&packidx, &idx, repo, id); | 
| 546 | 	if (err == NULL) { | 
| 547 | 		struct got_pack *pack = NULL; | 
| 548 |   | 
| 549 | 		err = got_packidx_get_packfile_path(&path_packfile, packidx); | 
| 550 | 		if (err) | 
| 551 | 			goto done; | 
| 552 |   | 
| 553 | 		pack = got_repo_get_cached_pack(repo, path_packfile); | 
| 554 | 		if (pack == NULL) { | 
| 555 | 			err = got_repo_cache_pack(&pack, repo, path_packfile, | 
| 556 | 			    packidx); | 
| 557 | 			if (err) | 
| 558 | 				goto done; | 
| 559 | 		} | 
| 560 | 		err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen, | 
| 561 | 		    outfd, pack, packidx, idx, id); | 
| 562 | 	} else if (err->code == GOT_ERR_NO_OBJ) { | 
| 563 | 		int fd; | 
| 564 |   | 
| 565 | 		err = got_object_open_loose_fd(&fd, id, repo); | 
| 566 | 		if (err) | 
| 567 | 			goto done; | 
| 568 | 		err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd, | 
| 569 | 		    repo, fd); | 
| 570 | 	} | 
| 571 |   | 
| 572 | 	if (hdrlen > size) { | 
| 573 | 		err = got_error(GOT_ERR_BAD_OBJ_HDR); | 
| 574 | 		goto done; | 
| 575 | 	} | 
| 576 |   | 
| 577 | 	*obj = calloc(1, sizeof(**obj)); | 
| 578 | 	if (*obj == NULL) { | 
| 579 | 		err = got_error_from_errno("calloc"); | 
| 580 | 		goto done; | 
| 581 | 	} | 
| 582 |   | 
| 583 | 	(*obj)->read_buf = malloc(blocksize); | 
| 584 | 	if ((*obj)->read_buf == NULL) { | 
| 585 | 		err = got_error_from_errno("malloc"); | 
| 586 | 		goto done; | 
| 587 | 	} | 
| 588 |   | 
| 589 | 	if (outbuf) { | 
| 590 | 		if (close(outfd) == -1) { | 
| 591 | 			err = got_error_from_errno("close"); | 
| 592 | 			goto done; | 
| 593 | 		} | 
| 594 | 		outfd = -1; | 
| 595 | 		(*obj)->f = fmemopen(outbuf, hdrlen + size, "r"); | 
| 596 | 		if ((*obj)->f == NULL) { | 
| 597 | 			err = got_error_from_errno("fdopen"); | 
| 598 | 			goto done; | 
| 599 | 		} | 
| 600 | 		(*obj)->data = outbuf; | 
| 601 | 	} else { | 
| 602 | 		struct stat sb; | 
| 603 | 		if (fstat(outfd, &sb) == -1) { | 
| 604 | 			err = got_error_from_errno("fstat"); | 
| 605 | 			goto done; | 
| 606 | 		} | 
| 607 |   | 
| 608 | 		if (sb.st_size != size) { | 
| 609 | 			err = got_error(GOT_ERR_PRIVSEP_LEN); | 
| 610 | 			goto done; | 
| 611 | 		} | 
| 612 |   | 
| 613 | 		(*obj)->f = fdopen(outfd, "r"); | 
| 614 | 		if ((*obj)->f == NULL) { | 
| 615 | 			err = got_error_from_errno("fdopen"); | 
| 616 | 			goto done; | 
| 617 | 		} | 
| 618 | 		outfd = -1; | 
| 619 | 		(*obj)->data = NULL; | 
| 620 | 	} | 
| 621 | 	(*obj)->hdrlen = hdrlen; | 
| 622 | 	(*obj)->size = size; | 
| 623 | 	(*obj)->blocksize = blocksize; | 
| 624 | done: | 
| 625 | 	free(path_packfile); | 
| 626 | 	if (err) { | 
| 627 | 		if (*obj) { | 
| 628 | 			got_object_raw_close(*obj); | 
| 629 | 			*obj = NULL; | 
| 630 | 		} | 
| 631 | 		if (outfd != -1) | 
| 632 | 			close(outfd); | 
| 633 | 		free(outbuf); | 
| 634 | 	} | 
| 635 | 	return err; | 
| 636 | } | 
| 637 |   | 
| 638 | void | 
| 639 | got_object_raw_rewind(struct got_raw_object *obj) | 
| 640 | { | 
| 641 | 	if (obj->f) | 
| 642 | 		rewind(obj->f); | 
| 643 | } | 
| 644 |   | 
| 645 | size_t | 
| 646 | got_object_raw_get_hdrlen(struct got_raw_object *obj) | 
| 647 | { | 
| 648 | 	return obj->hdrlen; | 
| 649 | } | 
| 650 |   | 
| 651 | const uint8_t * | 
| 652 | got_object_raw_get_read_buf(struct got_raw_object *obj) | 
| 653 | { | 
| 654 | 	return obj->read_buf; | 
| 655 | } | 
| 656 |   | 
| 657 | const struct got_error * | 
| 658 | got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj) | 
| 659 | { | 
| 660 | 	size_t n; | 
| 661 |   | 
| 662 | 	n = fread(obj->read_buf, 1, obj->blocksize, obj->f); | 
| 663 | 	if (n == 0 && ferror(obj->f)) | 
| 664 | 		return got_ferror(obj->f, GOT_ERR_IO); | 
| 665 | 	*outlenp = n; | 
| 666 | 	return NULL; | 
| 667 | } | 
| 668 |   | 
| 669 | const struct got_error * | 
| 670 | got_object_raw_close(struct got_raw_object *obj) | 
| 671 | { | 
| 672 | 	const struct got_error *err = NULL; | 
| 673 |   | 
| 674 | 	free(obj->read_buf); | 
| 675 | 	if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL) | 
| 676 | 		err = got_error_from_errno("fclose"); | 
| 677 | 	free(obj->data); | 
| 678 | 	free(obj); | 
| 679 | 	return err; | 
| 680 | } | 
| 681 |   | 
| 682 | const struct got_error * | 
| 683 | got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo, | 
| 684 |     const char *id_str) | 
| 685 | { | 
| 686 | 	struct got_object_id id; | 
| 687 |   | 
| 688 | 	if (!got_parse_sha1_digest(id.sha1, id_str)) | 
| 689 | 		return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR); | 
| 690 |   | 
| 691 | 	return got_object_open(obj, repo, &id); | 
| 692 | } | 
| 693 |   | 
| 694 | const struct got_error * | 
| 695 | got_object_resolve_id_str(struct got_object_id **id, | 
| 696 |     struct got_repository *repo, const char *id_str) | 
| 697 | { | 
| 698 | 	const struct got_error *err = NULL; | 
| 699 | 	struct got_object *obj; | 
| 700 |   | 
| 701 | 	err = got_object_open_by_id_str(&obj, repo, id_str); | 
| 702 | 	if (err) | 
| 703 | 		return err; | 
| 704 |   | 
| 705 | 	*id = got_object_id_dup(got_object_get_id(obj)); | 
| 706 | 	got_object_close(obj); | 
| 707 | 	if (*id == NULL) | 
| 708 | 		return got_error_from_errno("got_object_id_dup"); | 
| 709 |   | 
| 710 | 	return NULL; | 
| 711 | } | 
| 712 |   | 
| 713 | static const struct got_error * | 
| 714 | request_packed_commit(struct got_commit_object **commit, struct got_pack *pack, | 
| 715 |     int pack_idx, struct got_object_id *id) | 
| 716 | { | 
| 717 | 	const struct got_error *err = NULL; | 
| 718 |   | 
| 719 | 	err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id, | 
| 720 | 	    pack_idx); | 
| 721 | 	if (err) | 
| 722 | 		return err; | 
| 723 |   | 
| 724 | 	err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf); | 
| 725 | 	if (err) | 
| 726 | 		return err; | 
| 727 |   | 
| 728 | 	(*commit)->flags |= GOT_COMMIT_FLAG_PACKED; | 
| 729 | 	return NULL; | 
| 730 | } | 
| 731 |   | 
| 732 | static const struct got_error * | 
| 733 | read_packed_commit_privsep(struct got_commit_object **commit, | 
| 734 |     struct got_pack *pack, struct got_packidx *packidx, int idx, | 
| 735 |     struct got_object_id *id) | 
| 736 | { | 
| 737 | 	const struct got_error *err = NULL; | 
| 738 |   | 
| 739 | 	if (pack->privsep_child) | 
| 740 | 		return request_packed_commit(commit, pack, idx, id); | 
| 741 |   | 
| 742 | 	err = start_pack_privsep_child(pack, packidx); | 
| 743 | 	if (err) | 
| 744 | 		return err; | 
| 745 |   | 
| 746 | 	return request_packed_commit(commit, pack, idx, id); | 
| 747 | } | 
| 748 |   | 
| 749 | static const struct got_error * | 
| 750 | request_commit(struct got_commit_object **commit, struct got_repository *repo, | 
| 751 |     int fd) | 
| 752 | { | 
| 753 | 	const struct got_error *err = NULL; | 
| 754 | 	struct imsgbuf *ibuf; | 
| 755 |   | 
| 756 | 	ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf; | 
| 757 |   | 
| 758 | 	err = got_privsep_send_commit_req(ibuf, fd, NULL, -1); | 
| 759 | 	if (err) | 
| 760 | 		return err; | 
| 761 |   | 
| 762 | 	return got_privsep_recv_commit(commit, ibuf); | 
| 763 | } | 
| 764 |   | 
| 765 | static const struct got_error * | 
| 766 | read_commit_privsep(struct got_commit_object **commit, int obj_fd, | 
| 767 |     struct got_repository *repo) | 
| 768 | { | 
| 769 | 	const struct got_error *err; | 
| 770 | 	int imsg_fds[2]; | 
| 771 | 	pid_t pid; | 
| 772 | 	struct imsgbuf *ibuf; | 
| 773 |   | 
| 774 | 	if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1) | 
| 775 | 		return request_commit(commit, repo, obj_fd); | 
| 776 |   | 
| 777 | 	ibuf = calloc(1, sizeof(*ibuf)); | 
| 778 | 	if (ibuf == NULL) | 
| 779 | 		return got_error_from_errno("calloc"); | 
| 780 |   | 
| 781 | 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) { | 
| 782 | 		err = got_error_from_errno("socketpair"); | 
| 783 | 		free(ibuf); | 
| 784 | 		return err; | 
| 785 | 	} | 
| 786 |   | 
| 787 | 	pid = fork(); | 
| 788 | 	if (pid == -1) { | 
| 789 | 		err = got_error_from_errno("fork"); | 
| 790 | 		free(ibuf); | 
| 791 | 		return err; | 
| 792 | 	} | 
| 793 | 	else if (pid == 0) { | 
| 794 | 		got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT, | 
| 795 | 		    repo->path); | 
| 796 | 		 | 
| 797 | 	} | 
| 798 |   | 
| 799 | 	if (close(imsg_fds[1]) == -1) { | 
| 800 | 		err = got_error_from_errno("close"); | 
| 801 | 		free(ibuf); | 
| 802 | 		return err; | 
| 803 | 	} | 
| 804 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd = | 
| 805 | 	    imsg_fds[0]; | 
| 806 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid; | 
| 807 | 	imsg_init(ibuf, imsg_fds[0]); | 
| 808 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf; | 
| 809 |   | 
| 810 | 	return request_commit(commit, repo, obj_fd); | 
| 811 | } | 
| 812 |   | 
| 813 |   | 
| 814 | static const struct got_error * | 
| 815 | open_commit(struct got_commit_object **commit, | 
| 816 |     struct got_repository *repo, struct got_object_id *id, int check_cache) | 
| 817 | { | 
| 818 | 	const struct got_error *err = NULL; | 
| 819 | 	struct got_packidx *packidx = NULL; | 
| 820 | 	int idx; | 
| 821 | 	char *path_packfile = NULL; | 
| 822 |   | 
| 823 | 	if (check_cache) { | 
| 824 | 		*commit = got_repo_get_cached_commit(repo, id); | 
| 825 | 		if (*commit != NULL) { | 
| 826 | 			(*commit)->refcnt++; | 
| 827 | 			return NULL; | 
| 828 | 		} | 
| 829 | 	} else | 
| 830 | 		*commit = NULL; | 
| 831 |   | 
| 832 | 	err = got_repo_search_packidx(&packidx, &idx, repo, id); | 
| 833 | 	if (err == NULL) { | 
| 834 | 		struct got_pack *pack = NULL; | 
| 835 |   | 
| 836 | 		err = got_packidx_get_packfile_path(&path_packfile, packidx); | 
| 837 | 		if (err) | 
| 838 | 			return err; | 
| 839 |   | 
| 840 | 		pack = got_repo_get_cached_pack(repo, path_packfile); | 
| 841 | 		if (pack == NULL) { | 
| 842 | 			err = got_repo_cache_pack(&pack, repo, path_packfile, | 
| 843 | 			    packidx); | 
| 844 | 			if (err) | 
| 845 | 				goto done; | 
| 846 | 		} | 
| 847 | 		err = read_packed_commit_privsep(commit, pack, | 
| 848 | 		    packidx, idx, id); | 
| 849 | 	} else if (err->code == GOT_ERR_NO_OBJ) { | 
| 850 | 		int fd; | 
| 851 |   | 
| 852 | 		err = got_object_open_loose_fd(&fd, id, repo); | 
| 853 | 		if (err) | 
| 854 | 			return err; | 
| 855 | 		err = read_commit_privsep(commit, fd, repo); | 
| 856 | 	} | 
| 857 |   | 
| 858 | 	if (err == NULL) { | 
| 859 | 		(*commit)->refcnt++; | 
| 860 | 		err = got_repo_cache_commit(repo, id, *commit); | 
| 861 | 	} | 
| 862 | done: | 
| 863 | 	free(path_packfile); | 
| 864 | 	return err; | 
| 865 | } | 
| 866 |   | 
| 867 | const struct got_error * | 
| 868 | got_object_open_as_commit(struct got_commit_object **commit, | 
| 869 |     struct got_repository *repo, struct got_object_id *id) | 
| 870 | { | 
| 871 | 	*commit = got_repo_get_cached_commit(repo, id); | 
| 872 | 	if (*commit != NULL) { | 
| 873 | 		(*commit)->refcnt++; | 
| 874 | 		return NULL; | 
| 875 | 	} | 
| 876 |   | 
| 877 | 	return open_commit(commit, repo, id, 0); | 
| 878 | } | 
| 879 |   | 
| 880 | const struct got_error * | 
| 881 | got_object_commit_open(struct got_commit_object **commit, | 
| 882 |     struct got_repository *repo, struct got_object *obj) | 
| 883 | { | 
| 884 | 	return open_commit(commit, repo, got_object_get_id(obj), 1); | 
| 885 | } | 
| 886 |   | 
| 887 | const struct got_error * | 
| 888 | got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id) | 
| 889 | { | 
| 890 | 	const struct got_error *err = NULL; | 
| 891 |   | 
| 892 | 	*qid = calloc(1, sizeof(**qid)); | 
| 893 | 	if (*qid == NULL) | 
| 894 | 		return got_error_from_errno("calloc"); | 
| 895 |   | 
| 896 | 	(*qid)->id = got_object_id_dup(id); | 
| 897 | 	if ((*qid)->id == NULL) { | 
| 898 | 		err = got_error_from_errno("got_object_id_dup"); | 
| 899 | 		got_object_qid_free(*qid); | 
| 900 | 		*qid = NULL; | 
| 901 | 		return err; | 
| 902 | 	} | 
| 903 |   | 
| 904 | 	return NULL; | 
| 905 | } | 
| 906 |   | 
| 907 | static const struct got_error * | 
| 908 | request_packed_tree(struct got_tree_object **tree, struct got_pack *pack, | 
| 909 |     int pack_idx, struct got_object_id *id) | 
| 910 | { | 
| 911 | 	const struct got_error *err = NULL; | 
| 912 |   | 
| 913 | 	err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id, | 
| 914 | 	    pack_idx); | 
| 915 | 	if (err) | 
| 916 | 		return err; | 
| 917 |   | 
| 918 | 	return got_privsep_recv_tree(tree, pack->privsep_child->ibuf); | 
| 919 | } | 
| 920 |   | 
| 921 | static const struct got_error * | 
| 922 | read_packed_tree_privsep(struct got_tree_object **tree, | 
| 923 |     struct got_pack *pack, struct got_packidx *packidx, int idx, | 
| 924 |     struct got_object_id *id) | 
| 925 | { | 
| 926 | 	const struct got_error *err = NULL; | 
| 927 |   | 
| 928 | 	if (pack->privsep_child) | 
| 929 | 		return request_packed_tree(tree, pack, idx, id); | 
| 930 |   | 
| 931 | 	err = start_pack_privsep_child(pack, packidx); | 
| 932 | 	if (err) | 
| 933 | 		return err; | 
| 934 |   | 
| 935 | 	return request_packed_tree(tree, pack, idx, id); | 
| 936 | } | 
| 937 |   | 
| 938 | static const struct got_error * | 
| 939 | request_tree(struct got_tree_object **tree, struct got_repository *repo, | 
| 940 |     int fd) | 
| 941 | { | 
| 942 | 	const struct got_error *err = NULL; | 
| 943 | 	struct imsgbuf *ibuf; | 
| 944 |   | 
| 945 | 	ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf; | 
| 946 |   | 
| 947 | 	err = got_privsep_send_tree_req(ibuf, fd, NULL, -1); | 
| 948 | 	if (err) | 
| 949 | 		return err; | 
| 950 |   | 
| 951 | 	return got_privsep_recv_tree(tree, ibuf); | 
| 952 | } | 
| 953 |   | 
| 954 | const struct got_error * | 
| 955 | read_tree_privsep(struct got_tree_object **tree, int obj_fd, | 
| 956 |     struct got_repository *repo) | 
| 957 | { | 
| 958 | 	const struct got_error *err; | 
| 959 | 	int imsg_fds[2]; | 
| 960 | 	pid_t pid; | 
| 961 | 	struct imsgbuf *ibuf; | 
| 962 |   | 
| 963 | 	if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1) | 
| 964 | 		return request_tree(tree, repo, obj_fd); | 
| 965 |   | 
| 966 | 	ibuf = calloc(1, sizeof(*ibuf)); | 
| 967 | 	if (ibuf == NULL) | 
| 968 | 		return got_error_from_errno("calloc"); | 
| 969 |   | 
| 970 | 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) { | 
| 971 | 		err = got_error_from_errno("socketpair"); | 
| 972 | 		free(ibuf); | 
| 973 | 		return err; | 
| 974 | 	} | 
| 975 |   | 
| 976 | 	pid = fork(); | 
| 977 | 	if (pid == -1) { | 
| 978 | 		err = got_error_from_errno("fork"); | 
| 979 | 		free(ibuf); | 
| 980 | 		return err; | 
| 981 | 	} | 
| 982 | 	else if (pid == 0) { | 
| 983 | 		got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE, | 
| 984 | 		    repo->path); | 
| 985 | 		 | 
| 986 | 	} | 
| 987 |   | 
| 988 | 	if (close(imsg_fds[1]) == -1) { | 
| 989 | 		err = got_error_from_errno("close"); | 
| 990 | 		free(ibuf); | 
| 991 | 		return err; | 
| 992 | 	} | 
| 993 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd = | 
| 994 | 	    imsg_fds[0]; | 
| 995 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid; | 
| 996 | 	imsg_init(ibuf, imsg_fds[0]); | 
| 997 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf; | 
| 998 |   | 
| 999 |   | 
| 1000 | 	return request_tree(tree, repo, obj_fd); | 
| 1001 | } | 
| 1002 |   | 
| 1003 | static const struct got_error * | 
| 1004 | open_tree(struct got_tree_object **tree, struct got_repository *repo, | 
| 1005 |     struct got_object_id *id, int check_cache) | 
| 1006 | { | 
| 1007 | 	const struct got_error *err = NULL; | 
| 1008 | 	struct got_packidx *packidx = NULL; | 
| 1009 | 	int idx; | 
| 1010 | 	char *path_packfile = NULL; | 
| 1011 |   | 
| 1012 | 	if (check_cache) { | 
| 1013 | 		*tree = got_repo_get_cached_tree(repo, id); | 
| 1014 | 		if (*tree != NULL) { | 
| 1015 | 			(*tree)->refcnt++; | 
| 1016 | 			return NULL; | 
| 1017 | 		} | 
| 1018 | 	} else | 
| 1019 | 		*tree = NULL; | 
| 1020 |   | 
| 1021 | 	err = got_repo_search_packidx(&packidx, &idx, repo, id); | 
| 1022 | 	if (err == NULL) { | 
| 1023 | 		struct got_pack *pack = NULL; | 
| 1024 |   | 
| 1025 | 		err = got_packidx_get_packfile_path(&path_packfile, packidx); | 
| 1026 | 		if (err) | 
| 1027 | 			return err; | 
| 1028 |   | 
| 1029 | 		pack = got_repo_get_cached_pack(repo, path_packfile); | 
| 1030 | 		if (pack == NULL) { | 
| 1031 | 			err = got_repo_cache_pack(&pack, repo, path_packfile, | 
| 1032 | 			    packidx); | 
| 1033 | 			if (err) | 
| 1034 | 				goto done; | 
| 1035 | 		} | 
| 1036 | 		err = read_packed_tree_privsep(tree, pack, | 
| 1037 | 		    packidx, idx, id); | 
| 1038 | 	} else if (err->code == GOT_ERR_NO_OBJ) { | 
| 1039 | 		int fd; | 
| 1040 |   | 
| 1041 | 		err = got_object_open_loose_fd(&fd, id, repo); | 
| 1042 | 		if (err) | 
| 1043 | 			return err; | 
| 1044 | 		err = read_tree_privsep(tree, fd, repo); | 
| 1045 | 	} | 
| 1046 |   | 
| 1047 | 	if (err == NULL) { | 
| 1048 | 		(*tree)->refcnt++; | 
| 1049 | 		err = got_repo_cache_tree(repo, id, *tree); | 
| 1050 | 	} | 
| 1051 | done: | 
| 1052 | 	free(path_packfile); | 
| 1053 | 	return err; | 
| 1054 | } | 
| 1055 |   | 
| 1056 | const struct got_error * | 
| 1057 | got_object_open_as_tree(struct got_tree_object **tree, | 
| 1058 |     struct got_repository *repo, struct got_object_id *id) | 
| 1059 | { | 
| 1060 | 	*tree = got_repo_get_cached_tree(repo, id); | 
| 1061 | 	if (*tree != NULL) { | 
| 1062 | 		(*tree)->refcnt++; | 
| 1063 | 		return NULL; | 
| 1064 | 	} | 
| 1065 |   | 
| 1066 | 	return open_tree(tree, repo, id, 0); | 
| 1067 | } | 
| 1068 |   | 
| 1069 | const struct got_error * | 
| 1070 | got_object_tree_open(struct got_tree_object **tree, | 
| 1071 |     struct got_repository *repo, struct got_object *obj) | 
| 1072 | { | 
| 1073 | 	return open_tree(tree, repo, got_object_get_id(obj), 1); | 
| 1074 | } | 
| 1075 |   | 
| 1076 | int | 
| 1077 | got_object_tree_get_nentries(struct got_tree_object *tree) | 
| 1078 | { | 
| 1079 | 	return tree->nentries; | 
| 1080 | } | 
| 1081 |   | 
| 1082 | struct got_tree_entry * | 
| 1083 | got_object_tree_get_first_entry(struct got_tree_object *tree) | 
| 1084 | { | 
| 1085 | 	return got_object_tree_get_entry(tree, 0); | 
| 1086 | } | 
| 1087 |   | 
| 1088 | struct got_tree_entry * | 
| 1089 | got_object_tree_get_last_entry(struct got_tree_object *tree) | 
| 1090 | { | 
| 1091 | 	return got_object_tree_get_entry(tree, tree->nentries - 1); | 
| 1092 | } | 
| 1093 |   | 
| 1094 | struct got_tree_entry * | 
| 1095 | got_object_tree_get_entry(struct got_tree_object *tree, int i) | 
| 1096 | { | 
| 1097 | 	if (i < 0 || i >= tree->nentries) | 
| 1098 | 		return NULL; | 
| 1099 | 	return &tree->entries[i]; | 
| 1100 | } | 
| 1101 |   | 
| 1102 | mode_t | 
| 1103 | got_tree_entry_get_mode(struct got_tree_entry *te) | 
| 1104 | { | 
| 1105 | 	return te->mode; | 
| 1106 | } | 
| 1107 |   | 
| 1108 | const char * | 
| 1109 | got_tree_entry_get_name(struct got_tree_entry *te) | 
| 1110 | { | 
| 1111 | 	return &te->name[0]; | 
| 1112 | } | 
| 1113 |   | 
| 1114 | struct got_object_id * | 
| 1115 | got_tree_entry_get_id(struct got_tree_entry *te) | 
| 1116 | { | 
| 1117 | 	return &te->id; | 
| 1118 | } | 
| 1119 |   | 
| 1120 | const struct got_error * | 
| 1121 | got_object_blob_read_to_str(char **s, struct got_blob_object *blob) | 
| 1122 | { | 
| 1123 | 	const struct got_error *err = NULL; | 
| 1124 | 	size_t len, totlen, hdrlen, offset; | 
| 1125 |   | 
| 1126 | 	*s = NULL; | 
| 1127 |   | 
| 1128 | 	hdrlen = got_object_blob_get_hdrlen(blob); | 
| 1129 | 	totlen = 0; | 
| 1130 | 	offset = 0; | 
| 1131 | 	do { | 
| 1132 | 		char *p; | 
| 1133 |   | 
| 1134 | 		err = got_object_blob_read_block(&len, blob); | 
| 1135 | 		if (err) | 
| 1136 | 			return err; | 
| 1137 |   | 
| 1138 | 		if (len == 0) | 
| 1139 | 			break; | 
| 1140 |   | 
| 1141 | 		totlen += len - hdrlen; | 
| 1142 | 		p = realloc(*s, totlen + 1); | 
| 1143 | 		if (p == NULL) { | 
| 1144 | 			err = got_error_from_errno("realloc"); | 
| 1145 | 			free(*s); | 
| 1146 | 			*s = NULL; | 
| 1147 | 			return err; | 
| 1148 | 		} | 
| 1149 | 		*s = p; | 
| 1150 | 		 | 
| 1151 | 		memcpy(*s + offset, | 
| 1152 | 		    got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen); | 
| 1153 | 		hdrlen = 0; | 
| 1154 | 		offset = totlen; | 
| 1155 | 	} while (len > 0); | 
| 1156 |   | 
| 1157 | 	(*s)[totlen] = '\0'; | 
| 1158 | 	return NULL; | 
| 1159 | } | 
| 1160 |   | 
| 1161 | const struct got_error * | 
| 1162 | got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te, | 
| 1163 |     struct got_repository *repo) | 
| 1164 | { | 
| 1165 | 	const struct got_error *err = NULL; | 
| 1166 | 	struct got_blob_object *blob = NULL; | 
| 1167 |   | 
| 1168 | 	*link_target = NULL; | 
| 1169 |   | 
| 1170 | 	if (!got_object_tree_entry_is_symlink(te)) | 
 |  | 
| 1171 | 		return got_error(GOT_ERR_TREE_ENTRY_TYPE); | 
| 1172 |   | 
| 1173 | 	err = got_object_open_as_blob(&blob, repo, | 
 | 16  |  | Calling 'got_object_open_as_blob' |  |  
  | 
| 1174 | 	    got_tree_entry_get_id(te), PATH_MAX); | 
| 1175 | 	if (err) | 
| 1176 | 		return err; | 
| 1177 |   | 
| 1178 | 	err = got_object_blob_read_to_str(link_target, blob); | 
| 1179 | 	got_object_blob_close(blob); | 
| 1180 | 	if (err) { | 
| 1181 | 		free(*link_target); | 
| 1182 | 		*link_target = NULL; | 
| 1183 | 	} | 
| 1184 | 	return err; | 
| 1185 | } | 
| 1186 |   | 
| 1187 | int | 
| 1188 | got_tree_entry_get_index(struct got_tree_entry *te) | 
| 1189 | { | 
| 1190 | 	return te->idx; | 
| 1191 | } | 
| 1192 |   | 
| 1193 | struct got_tree_entry * | 
| 1194 | got_tree_entry_get_next(struct got_tree_object *tree, | 
| 1195 |     struct got_tree_entry *te) | 
| 1196 | { | 
| 1197 | 	return got_object_tree_get_entry(tree, te->idx + 1); | 
| 1198 | } | 
| 1199 |   | 
| 1200 | struct got_tree_entry * | 
| 1201 | got_tree_entry_get_prev(struct got_tree_object *tree, | 
| 1202 |     struct got_tree_entry *te) | 
| 1203 | { | 
| 1204 | 	return got_object_tree_get_entry(tree, te->idx - 1); | 
| 1205 | } | 
| 1206 |   | 
| 1207 | static const struct got_error * | 
| 1208 | request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd, | 
| 1209 |     struct got_pack *pack, struct got_packidx *packidx, int idx, | 
| 1210 |     struct got_object_id *id) | 
| 1211 | { | 
| 1212 | 	const struct got_error *err = NULL; | 
| 1213 | 	int outfd_child; | 
| 1214 | 	int basefd, accumfd;  | 
| 1215 |   | 
| 1216 | 	basefd = got_opentempfd(); | 
| 1217 | 	if (basefd == -1) | 
| 1218 | 		return got_error_from_errno("got_opentempfd"); | 
| 1219 | 	accumfd = got_opentempfd(); | 
| 1220 | 	if (accumfd == -1) | 
| 1221 | 		return got_error_from_errno("got_opentempfd"); | 
| 1222 |   | 
| 1223 | 	outfd_child = dup(outfd); | 
| 1224 | 	if (outfd_child == -1) | 
| 1225 | 		return got_error_from_errno("dup"); | 
| 1226 |   | 
| 1227 | 	err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx); | 
| 1228 | 	if (err) | 
| 1229 | 		return err; | 
| 1230 |   | 
| 1231 | 	err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf, | 
| 1232 | 	    outfd_child); | 
| 1233 | 	if (err) { | 
| 1234 | 		close(basefd); | 
| 1235 | 		close(accumfd); | 
| 1236 | 		return err; | 
| 1237 | 	} | 
| 1238 |   | 
| 1239 | 	err = got_privsep_send_tmpfd(pack->privsep_child->ibuf, | 
| 1240 | 	    basefd); | 
| 1241 | 	if (err) { | 
| 1242 | 		close(accumfd); | 
| 1243 | 		return err; | 
| 1244 | 	} | 
| 1245 |   | 
| 1246 | 	err = got_privsep_send_tmpfd(pack->privsep_child->ibuf, | 
| 1247 | 	    accumfd); | 
| 1248 | 	if (err) | 
| 1249 | 		return err; | 
| 1250 |   | 
| 1251 | 	err = got_privsep_recv_blob(outbuf, size, hdrlen, | 
| 1252 | 	    pack->privsep_child->ibuf); | 
| 1253 | 	if (err) | 
| 1254 | 		return err; | 
| 1255 |   | 
| 1256 | 	if (lseek(outfd, SEEK_SET, 0) == -1) | 
| 1257 | 		err = got_error_from_errno("lseek"); | 
| 1258 |   | 
| 1259 | 	return err; | 
| 1260 | } | 
| 1261 |   | 
| 1262 | static const struct got_error * | 
| 1263 | read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen, | 
| 1264 |     int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx, | 
| 1265 |     struct got_object_id *id) | 
| 1266 | { | 
| 1267 | 	const struct got_error *err = NULL; | 
| 1268 |   | 
| 1269 | 	if (pack->privsep_child == NULL) { | 
| 1270 | 		err = start_pack_privsep_child(pack, packidx); | 
| 1271 | 		if (err) | 
| 1272 | 			return err; | 
| 1273 | 	} | 
| 1274 |   | 
| 1275 | 	return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx, | 
| 1276 | 	    idx, id); | 
| 1277 | } | 
| 1278 |   | 
| 1279 | static const struct got_error * | 
| 1280 | request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd, | 
| 1281 |     int infd, struct imsgbuf *ibuf) | 
| 1282 | { | 
| 1283 | 	const struct got_error *err = NULL; | 
| 1284 | 	int outfd_child; | 
| 1285 |   | 
| 1286 | 	outfd_child = dup(outfd); | 
| 1287 | 	if (outfd_child == -1) | 
| 1288 | 		return got_error_from_errno("dup"); | 
| 1289 |   | 
| 1290 | 	err = got_privsep_send_blob_req(ibuf, infd, NULL, -1); | 
| 1291 | 	if (err) | 
| 1292 | 		return err; | 
| 1293 |   | 
| 1294 | 	err = got_privsep_send_blob_outfd(ibuf, outfd_child); | 
| 1295 | 	if (err) | 
| 1296 | 		return err; | 
| 1297 |   | 
| 1298 | 	err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf); | 
| 1299 | 	if (err) | 
| 1300 | 		return err; | 
| 1301 |   | 
| 1302 | 	if (lseek(outfd, SEEK_SET, 0) == -1) | 
| 1303 | 		return got_error_from_errno("lseek"); | 
| 1304 |   | 
| 1305 | 	return err; | 
| 1306 | } | 
| 1307 |   | 
| 1308 | static const struct got_error * | 
| 1309 | read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen, | 
| 1310 |     int outfd, int infd, struct got_repository *repo) | 
| 1311 | { | 
| 1312 | 	const struct got_error *err; | 
| 1313 | 	int imsg_fds[2]; | 
| 1314 | 	pid_t pid; | 
| 1315 | 	struct imsgbuf *ibuf; | 
| 1316 |   | 
| 1317 | 	if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) { | 
 | 39  |  | Assuming the condition is false |  |  
  | 
 |  | 
| 1318 | 		ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf; | 
| 1319 | 		return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf); | 
| 1320 | 	} | 
| 1321 |   | 
| 1322 | 	ibuf = calloc(1, sizeof(*ibuf)); | 
| 1323 | 	if (ibuf == NULL) | 
 | 41  |  | Assuming 'ibuf' is equal to NULL |  |  
  | 
 |  | 
| 1324 | 		return got_error_from_errno("calloc"); | 
 | 43  |  | Returning without writing to '*hdrlen' |  |  
  | 
 | 44  |  | Returning pointer, which participates in a condition later |  |  
  | 
| 1325 |   | 
| 1326 | 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) { | 
| 1327 | 		err = got_error_from_errno("socketpair"); | 
| 1328 | 		free(ibuf); | 
| 1329 | 		return err; | 
| 1330 | 	} | 
| 1331 |   | 
| 1332 | 	pid = fork(); | 
| 1333 | 	if (pid == -1) { | 
| 1334 | 		err = got_error_from_errno("fork"); | 
| 1335 | 		free(ibuf); | 
| 1336 | 		return err; | 
| 1337 | 	} | 
| 1338 | 	else if (pid == 0) { | 
| 1339 | 		got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB, | 
| 1340 | 		    repo->path); | 
| 1341 | 		 | 
| 1342 | 	} | 
| 1343 |   | 
| 1344 | 	if (close(imsg_fds[1]) == -1) { | 
| 1345 | 		err = got_error_from_errno("close"); | 
| 1346 | 		free(ibuf); | 
| 1347 | 		return err; | 
| 1348 | 	} | 
| 1349 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd = | 
| 1350 | 	    imsg_fds[0]; | 
| 1351 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid; | 
| 1352 | 	imsg_init(ibuf, imsg_fds[0]); | 
| 1353 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf; | 
| 1354 |   | 
| 1355 | 	return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf); | 
| 1356 | } | 
| 1357 |   | 
| 1358 | static const struct got_error * | 
| 1359 | open_blob(struct got_blob_object **blob, struct got_repository *repo, | 
| 1360 |     struct got_object_id *id, size_t blocksize) | 
| 1361 | { | 
| 1362 | 	const struct got_error *err = NULL; | 
| 1363 | 	struct got_packidx *packidx = NULL; | 
| 1364 | 	int idx; | 
| 1365 | 	char *path_packfile = NULL; | 
| 1366 | 	uint8_t *outbuf; | 
| 1367 | 	int outfd; | 
| 1368 | 	size_t size, hdrlen; | 
 | 18  |  | 'hdrlen' declared without an initial value |  |  
  | 
| 1369 | 	struct stat sb; | 
| 1370 |   | 
| 1371 | 	*blob = calloc(1, sizeof(**blob)); | 
| 1372 | 	if (*blob == NULL) | 
 | 19  |  | Assuming the condition is false |  |  
  | 
 |  | 
| 1373 | 		return got_error_from_errno("calloc"); | 
| 1374 |   | 
| 1375 | 	outfd = got_opentempfd(); | 
| 1376 | 	if (outfd == -1) | 
 | 21  |  | Assuming the condition is false |  |  
  | 
 |  | 
| 1377 | 		return got_error_from_errno("got_opentempfd"); | 
| 1378 |   | 
| 1379 | 	(*blob)->read_buf = malloc(blocksize); | 
| 1380 | 	if ((*blob)->read_buf == NULL) { | 
 | 23  |  | Assuming field 'read_buf' is not equal to NULL |  |  
  | 
 |  | 
| 1381 | 		err = got_error_from_errno("malloc"); | 
| 1382 | 		goto done; | 
| 1383 | 	} | 
| 1384 |   | 
| 1385 | 	err = got_repo_search_packidx(&packidx, &idx, repo, id); | 
| 1386 | 	if (err == NULL) { | 
 | 25  |  | Assuming 'err' is not equal to NULL |  |  
  | 
 |  | 
| 1387 | 		struct got_pack *pack = NULL; | 
| 1388 |   | 
| 1389 | 		err = got_packidx_get_packfile_path(&path_packfile, packidx); | 
| 1390 | 		if (err) | 
| 1391 | 			goto done; | 
| 1392 |   | 
| 1393 | 		pack = got_repo_get_cached_pack(repo, path_packfile); | 
| 1394 | 		if (pack == NULL) { | 
| 1395 | 			err = got_repo_cache_pack(&pack, repo, path_packfile, | 
| 1396 | 			    packidx); | 
| 1397 | 			if (err) | 
| 1398 | 				goto done; | 
| 1399 | 		} | 
| 1400 | 		err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd, | 
| 1401 | 		    pack, packidx, idx, id); | 
| 1402 | 	} else if (err->code == GOT_ERR_NO_OBJ) { | 
 | 27  |  | Assuming field 'code' is equal to GOT_ERR_NO_OBJ |  |  
  | 
 |  | 
| 1403 | 		int infd; | 
| 1404 |   | 
| 1405 | 		err = got_object_open_loose_fd(&infd, id, repo); | 
 | 29  |  | Calling 'got_object_open_loose_fd' |  |  
  | 
 | 36  |  | Returning from 'got_object_open_loose_fd' |  |  
  | 
| 1406 | 		if (err) | 
 |  | 
| 1407 | 			goto done; | 
| 1408 | 		err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd, | 
 | 38  |  | Calling 'read_blob_privsep' |  |  
  | 
 | 45  |  | Returning from 'read_blob_privsep' |  |  
  | 
| 1409 | 		    repo); | 
| 1410 | 	} | 
| 1411 | 	if (err) | 
 |  | 
 |  | 
| 1412 | 		goto done; | 
| 1413 |   | 
| 1414 | 	if (hdrlen > size) { | 
 | 48  |  | The left operand of '>' is a garbage value |  
  | 
| 1415 | 		err = got_error(GOT_ERR_BAD_OBJ_HDR); | 
| 1416 | 		goto done; | 
| 1417 | 	} | 
| 1418 |   | 
| 1419 | 	if (outbuf) { | 
| 1420 | 		if (close(outfd) == -1 && err == NULL) | 
| 1421 | 			err = got_error_from_errno("close"); | 
| 1422 | 		outfd = -1; | 
| 1423 | 		(*blob)->f = fmemopen(outbuf, size, "rb"); | 
| 1424 | 		if ((*blob)->f == NULL) { | 
| 1425 | 			err = got_error_from_errno("fmemopen"); | 
| 1426 | 			free(outbuf); | 
| 1427 | 			goto done; | 
| 1428 | 		} | 
| 1429 | 		(*blob)->data = outbuf; | 
| 1430 | 	} else { | 
| 1431 | 		if (fstat(outfd, &sb) == -1) { | 
| 1432 | 			err = got_error_from_errno("fstat"); | 
| 1433 | 			goto done; | 
| 1434 | 		} | 
| 1435 |   | 
| 1436 | 		if (sb.st_size != size) { | 
| 1437 | 			err = got_error(GOT_ERR_PRIVSEP_LEN); | 
| 1438 | 			goto done; | 
| 1439 | 		} | 
| 1440 |   | 
| 1441 | 		(*blob)->f = fdopen(outfd, "rb"); | 
| 1442 | 		if ((*blob)->f == NULL) { | 
| 1443 | 			err = got_error_from_errno("fdopen"); | 
| 1444 | 			close(outfd); | 
| 1445 | 			outfd = -1; | 
| 1446 | 			goto done; | 
| 1447 | 		} | 
| 1448 | 	} | 
| 1449 |   | 
| 1450 | 	(*blob)->hdrlen = hdrlen; | 
| 1451 | 	(*blob)->blocksize = blocksize; | 
| 1452 | 	memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH); | 
| 1453 |   | 
| 1454 | done: | 
| 1455 | 	free(path_packfile); | 
| 1456 | 	if (err) { | 
| 1457 | 		if (*blob) { | 
| 1458 | 			got_object_blob_close(*blob); | 
| 1459 | 			*blob = NULL; | 
| 1460 | 		} else if (outfd != -1) | 
| 1461 | 			close(outfd); | 
| 1462 | 	} | 
| 1463 | 	return err; | 
| 1464 | } | 
| 1465 |   | 
| 1466 | const struct got_error * | 
| 1467 | got_object_open_as_blob(struct got_blob_object **blob, | 
| 1468 |     struct got_repository *repo, struct got_object_id *id, | 
| 1469 |     size_t blocksize) | 
| 1470 | { | 
| 1471 | 	return open_blob(blob, repo, id, blocksize); | 
 |  | 
| 1472 | } | 
| 1473 |   | 
| 1474 | const struct got_error * | 
| 1475 | got_object_blob_open(struct got_blob_object **blob, | 
| 1476 |     struct got_repository *repo, struct got_object *obj, size_t blocksize) | 
| 1477 | { | 
| 1478 | 	return open_blob(blob, repo, got_object_get_id(obj), blocksize); | 
| 1479 | } | 
| 1480 |   | 
| 1481 | const struct got_error * | 
| 1482 | got_object_blob_close(struct got_blob_object *blob) | 
| 1483 | { | 
| 1484 | 	const struct got_error *err = NULL; | 
| 1485 | 	free(blob->read_buf); | 
| 1486 | 	if (blob->f && fclose(blob->f) == EOF) | 
| 1487 | 		err = got_error_from_errno("fclose"); | 
| 1488 | 	free(blob->data); | 
| 1489 | 	free(blob); | 
| 1490 | 	return err; | 
| 1491 | } | 
| 1492 |   | 
| 1493 | void | 
| 1494 | got_object_blob_rewind(struct got_blob_object *blob) | 
| 1495 | { | 
| 1496 | 	if (blob->f) | 
| 1497 | 		rewind(blob->f); | 
| 1498 | } | 
| 1499 |   | 
| 1500 | char * | 
| 1501 | got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size) | 
| 1502 | { | 
| 1503 | 	return got_sha1_digest_to_str(blob->id.sha1, buf, size); | 
| 1504 | } | 
| 1505 |   | 
| 1506 | size_t | 
| 1507 | got_object_blob_get_hdrlen(struct got_blob_object *blob) | 
| 1508 | { | 
| 1509 | 	return blob->hdrlen; | 
| 1510 | } | 
| 1511 |   | 
| 1512 | const uint8_t * | 
| 1513 | got_object_blob_get_read_buf(struct got_blob_object *blob) | 
| 1514 | { | 
| 1515 | 	return blob->read_buf; | 
| 1516 | } | 
| 1517 |   | 
| 1518 | const struct got_error * | 
| 1519 | got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob) | 
| 1520 | { | 
| 1521 | 	size_t n; | 
| 1522 |   | 
| 1523 | 	n = fread(blob->read_buf, 1, blob->blocksize, blob->f); | 
| 1524 | 	if (n == 0 && ferror(blob->f)) | 
| 1525 | 		return got_ferror(blob->f, GOT_ERR_IO); | 
| 1526 | 	*outlenp = n; | 
| 1527 | 	return NULL; | 
| 1528 | } | 
| 1529 |   | 
| 1530 | const struct got_error * | 
| 1531 | got_object_blob_dump_to_file(off_t *filesize, int *nlines, | 
| 1532 |     off_t **line_offsets, FILE *outfile, struct got_blob_object *blob) | 
| 1533 | { | 
| 1534 | 	const struct got_error *err = NULL; | 
| 1535 | 	size_t n, len, hdrlen; | 
| 1536 | 	const uint8_t *buf; | 
| 1537 | 	int i; | 
| 1538 | 	const int alloc_chunksz = 512; | 
| 1539 | 	size_t nalloc = 0; | 
| 1540 | 	off_t off = 0, total_len = 0; | 
| 1541 |   | 
| 1542 | 	if (line_offsets) | 
| 1543 | 		*line_offsets = NULL; | 
| 1544 | 	if (filesize) | 
| 1545 | 		*filesize = 0; | 
| 1546 | 	if (nlines) | 
| 1547 | 		*nlines = 0; | 
| 1548 |   | 
| 1549 | 	hdrlen = got_object_blob_get_hdrlen(blob); | 
| 1550 | 	do { | 
| 1551 | 		err = got_object_blob_read_block(&len, blob); | 
| 1552 | 		if (err) | 
| 1553 | 			return err; | 
| 1554 | 		if (len == 0) | 
| 1555 | 			break; | 
| 1556 | 		buf = got_object_blob_get_read_buf(blob); | 
| 1557 | 		i = hdrlen; | 
| 1558 | 		if (nlines) { | 
| 1559 | 			if (line_offsets && *line_offsets == NULL) { | 
| 1560 | 				 | 
| 1561 | 				*nlines = 1; | 
| 1562 | 				nalloc = alloc_chunksz; | 
| 1563 | 				*line_offsets = calloc(nalloc, | 
| 1564 | 				    sizeof(**line_offsets)); | 
| 1565 | 				if (*line_offsets == NULL) | 
| 1566 | 					return got_error_from_errno("calloc"); | 
| 1567 |   | 
| 1568 | 				 | 
| 1569 | 				while (i < len) { | 
| 1570 | 					if (buf[i] == '\n') | 
| 1571 | 						break; | 
| 1572 | 					i++; | 
| 1573 | 				} | 
| 1574 | 			} | 
| 1575 | 			 | 
| 1576 | 			while (i < len) { | 
| 1577 | 				if (buf[i] != '\n') { | 
| 1578 | 					i++; | 
| 1579 | 					continue; | 
| 1580 | 				} | 
| 1581 | 				(*nlines)++; | 
| 1582 | 				if (line_offsets && nalloc < *nlines) { | 
| 1583 | 					size_t n = *nlines + alloc_chunksz; | 
| 1584 | 					off_t *o = recallocarray(*line_offsets, | 
| 1585 | 					    nalloc, n, sizeof(**line_offsets)); | 
| 1586 | 					if (o == NULL) { | 
| 1587 | 						free(*line_offsets); | 
| 1588 | 						*line_offsets = NULL; | 
| 1589 | 						return got_error_from_errno( | 
| 1590 | 						    "recallocarray"); | 
| 1591 | 					} | 
| 1592 | 					*line_offsets = o; | 
| 1593 | 					nalloc = n; | 
| 1594 | 				} | 
| 1595 | 				if (line_offsets) { | 
| 1596 | 					off = total_len + i - hdrlen + 1; | 
| 1597 | 					(*line_offsets)[*nlines - 1] = off; | 
| 1598 | 				} | 
| 1599 | 				i++; | 
| 1600 | 			} | 
| 1601 | 		} | 
| 1602 | 		 | 
| 1603 | 		n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile); | 
| 1604 | 		if (n != len - hdrlen) | 
| 1605 | 			return got_ferror(outfile, GOT_ERR_IO); | 
| 1606 | 		total_len += len - hdrlen; | 
| 1607 | 		hdrlen = 0; | 
| 1608 | 	} while (len != 0); | 
| 1609 |   | 
| 1610 | 	if (fflush(outfile) != 0) | 
| 1611 | 		return got_error_from_errno("fflush"); | 
| 1612 | 	rewind(outfile); | 
| 1613 |   | 
| 1614 | 	if (filesize) | 
| 1615 | 		*filesize = total_len; | 
| 1616 |   | 
| 1617 | 	return NULL; | 
| 1618 | } | 
| 1619 |   | 
| 1620 | static const struct got_error * | 
| 1621 | request_packed_tag(struct got_tag_object **tag, struct got_pack *pack, | 
| 1622 |     int pack_idx, struct got_object_id *id) | 
| 1623 | { | 
| 1624 | 	const struct got_error *err = NULL; | 
| 1625 |   | 
| 1626 | 	err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id, | 
| 1627 | 	    pack_idx); | 
| 1628 | 	if (err) | 
| 1629 | 		return err; | 
| 1630 |   | 
| 1631 | 	return got_privsep_recv_tag(tag, pack->privsep_child->ibuf); | 
| 1632 | } | 
| 1633 |   | 
| 1634 | static const struct got_error * | 
| 1635 | read_packed_tag_privsep(struct got_tag_object **tag, | 
| 1636 |     struct got_pack *pack, struct got_packidx *packidx, int idx, | 
| 1637 |     struct got_object_id *id) | 
| 1638 | { | 
| 1639 | 	const struct got_error *err = NULL; | 
| 1640 |   | 
| 1641 | 	if (pack->privsep_child) | 
| 1642 | 		return request_packed_tag(tag, pack, idx, id); | 
| 1643 |   | 
| 1644 | 	err = start_pack_privsep_child(pack, packidx); | 
| 1645 | 	if (err) | 
| 1646 | 		return err; | 
| 1647 |   | 
| 1648 | 	return request_packed_tag(tag, pack, idx, id); | 
| 1649 | } | 
| 1650 |   | 
| 1651 | static const struct got_error * | 
| 1652 | request_tag(struct got_tag_object **tag, struct got_repository *repo, | 
| 1653 |     int fd) | 
| 1654 | { | 
| 1655 | 	const struct got_error *err = NULL; | 
| 1656 | 	struct imsgbuf *ibuf; | 
| 1657 |   | 
| 1658 | 	ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf; | 
| 1659 |   | 
| 1660 | 	err = got_privsep_send_tag_req(ibuf, fd, NULL, -1); | 
| 1661 | 	if (err) | 
| 1662 | 		return err; | 
| 1663 |   | 
| 1664 | 	return got_privsep_recv_tag(tag, ibuf); | 
| 1665 | } | 
| 1666 |   | 
| 1667 | static const struct got_error * | 
| 1668 | read_tag_privsep(struct got_tag_object **tag, int obj_fd, | 
| 1669 |     struct got_repository *repo) | 
| 1670 | { | 
| 1671 | 	const struct got_error *err; | 
| 1672 | 	int imsg_fds[2]; | 
| 1673 | 	pid_t pid; | 
| 1674 | 	struct imsgbuf *ibuf; | 
| 1675 |   | 
| 1676 | 	if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1) | 
| 1677 | 		return request_tag(tag, repo, obj_fd); | 
| 1678 |   | 
| 1679 | 	ibuf = calloc(1, sizeof(*ibuf)); | 
| 1680 | 	if (ibuf == NULL) | 
| 1681 | 		return got_error_from_errno("calloc"); | 
| 1682 |   | 
| 1683 | 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) { | 
| 1684 | 		err = got_error_from_errno("socketpair"); | 
| 1685 | 		free(ibuf); | 
| 1686 | 		return err; | 
| 1687 | 	} | 
| 1688 |   | 
| 1689 | 	pid = fork(); | 
| 1690 | 	if (pid == -1) { | 
| 1691 | 		err = got_error_from_errno("fork"); | 
| 1692 | 		free(ibuf); | 
| 1693 | 		return err; | 
| 1694 | 	} | 
| 1695 | 	else if (pid == 0) { | 
| 1696 | 		got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG, | 
| 1697 | 		    repo->path); | 
| 1698 | 		 | 
| 1699 | 	} | 
| 1700 |   | 
| 1701 | 	if (close(imsg_fds[1]) == -1) { | 
| 1702 | 		err = got_error_from_errno("close"); | 
| 1703 | 		free(ibuf); | 
| 1704 | 		return err; | 
| 1705 | 	} | 
| 1706 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd = | 
| 1707 | 	    imsg_fds[0]; | 
| 1708 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid; | 
| 1709 | 	imsg_init(ibuf, imsg_fds[0]); | 
| 1710 | 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf; | 
| 1711 |   | 
| 1712 | 	return request_tag(tag, repo, obj_fd); | 
| 1713 | } | 
| 1714 |   | 
| 1715 | static const struct got_error * | 
| 1716 | open_tag(struct got_tag_object **tag, struct got_repository *repo, | 
| 1717 |     struct got_object_id *id, int check_cache) | 
| 1718 | { | 
| 1719 | 	const struct got_error *err = NULL; | 
| 1720 | 	struct got_packidx *packidx = NULL; | 
| 1721 | 	int idx; | 
| 1722 | 	char *path_packfile = NULL; | 
| 1723 | 	struct got_object *obj = NULL; | 
| 1724 | 	int obj_type = GOT_OBJ_TYPE_ANY; | 
| 1725 |   | 
| 1726 | 	if (check_cache) { | 
| 1727 | 		*tag = got_repo_get_cached_tag(repo, id); | 
| 1728 | 		if (*tag != NULL) { | 
| 1729 | 			(*tag)->refcnt++; | 
| 1730 | 			return NULL; | 
| 1731 | 		} | 
| 1732 | 	} else | 
| 1733 | 		*tag = NULL; | 
| 1734 |   | 
| 1735 | 	err = got_repo_search_packidx(&packidx, &idx, repo, id); | 
| 1736 | 	if (err == NULL) { | 
| 1737 | 		struct got_pack *pack = NULL; | 
| 1738 |   | 
| 1739 | 		err = got_packidx_get_packfile_path(&path_packfile, packidx); | 
| 1740 | 		if (err) | 
| 1741 | 			return err; | 
| 1742 |   | 
| 1743 | 		pack = got_repo_get_cached_pack(repo, path_packfile); | 
| 1744 | 		if (pack == NULL) { | 
| 1745 | 			err = got_repo_cache_pack(&pack, repo, path_packfile, | 
| 1746 | 			    packidx); | 
| 1747 | 			if (err) | 
| 1748 | 				goto done; | 
| 1749 | 		} | 
| 1750 |   | 
| 1751 | 		 | 
| 1752 | 		err = read_packed_object_privsep(&obj, repo, pack, packidx, | 
| 1753 | 		    idx, id); | 
| 1754 | 		if (err) | 
| 1755 | 			goto done; | 
| 1756 | 		obj_type = obj->type; | 
| 1757 | 		got_object_close(obj); | 
| 1758 | 		if (obj_type != GOT_OBJ_TYPE_TAG) { | 
| 1759 | 			err = got_error(GOT_ERR_OBJ_TYPE); | 
| 1760 | 			goto done; | 
| 1761 | 		} | 
| 1762 | 		err = read_packed_tag_privsep(tag, pack, packidx, idx, id); | 
| 1763 | 	} else if (err->code == GOT_ERR_NO_OBJ) { | 
| 1764 | 		int fd; | 
| 1765 |   | 
| 1766 | 		err = got_object_open_loose_fd(&fd, id, repo); | 
| 1767 | 		if (err) | 
| 1768 | 			return err; | 
| 1769 | 		err = read_object_header_privsep(&obj, repo, fd); | 
| 1770 | 		if (err) | 
| 1771 | 			return err; | 
| 1772 | 		obj_type = obj->type; | 
| 1773 | 		got_object_close(obj); | 
| 1774 | 		if (obj_type != GOT_OBJ_TYPE_TAG) | 
| 1775 | 			return got_error(GOT_ERR_OBJ_TYPE); | 
| 1776 |   | 
| 1777 | 		err = got_object_open_loose_fd(&fd, id, repo); | 
| 1778 | 		if (err) | 
| 1779 | 			return err; | 
| 1780 | 		err = read_tag_privsep(tag, fd, repo); | 
| 1781 | 	} | 
| 1782 |   | 
| 1783 | 	if (err == NULL) { | 
| 1784 | 		(*tag)->refcnt++; | 
| 1785 | 		err = got_repo_cache_tag(repo, id, *tag); | 
| 1786 | 	} | 
| 1787 | done: | 
| 1788 | 	free(path_packfile); | 
| 1789 | 	return err; | 
| 1790 | } | 
| 1791 |   | 
| 1792 | const struct got_error * | 
| 1793 | got_object_open_as_tag(struct got_tag_object **tag, | 
| 1794 |     struct got_repository *repo, struct got_object_id *id) | 
| 1795 | { | 
| 1796 | 	*tag = got_repo_get_cached_tag(repo, id); | 
| 1797 | 	if (*tag != NULL) { | 
| 1798 | 		(*tag)->refcnt++; | 
| 1799 | 		return NULL; | 
| 1800 | 	} | 
| 1801 |   | 
| 1802 | 	return open_tag(tag, repo, id, 0); | 
| 1803 | } | 
| 1804 |   | 
| 1805 | const struct got_error * | 
| 1806 | got_object_tag_open(struct got_tag_object **tag, | 
| 1807 |     struct got_repository *repo, struct got_object *obj) | 
| 1808 | { | 
| 1809 | 	return open_tag(tag, repo, got_object_get_id(obj), 1); | 
| 1810 | } | 
| 1811 |   | 
| 1812 | const char * | 
| 1813 | got_object_tag_get_name(struct got_tag_object *tag) | 
| 1814 | { | 
| 1815 | 	return tag->tag; | 
| 1816 | } | 
| 1817 |   | 
| 1818 | int | 
| 1819 | got_object_tag_get_object_type(struct got_tag_object *tag) | 
| 1820 | { | 
| 1821 | 	return tag->obj_type; | 
| 1822 | } | 
| 1823 |   | 
| 1824 | struct got_object_id * | 
| 1825 | got_object_tag_get_object_id(struct got_tag_object *tag) | 
| 1826 | { | 
| 1827 | 	return &tag->id; | 
| 1828 | } | 
| 1829 |   | 
| 1830 | time_t | 
| 1831 | got_object_tag_get_tagger_time(struct got_tag_object *tag) | 
| 1832 | { | 
| 1833 | 	return tag->tagger_time; | 
| 1834 | } | 
| 1835 |   | 
| 1836 | time_t | 
| 1837 | got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag) | 
| 1838 | { | 
| 1839 | 	return tag->tagger_gmtoff; | 
| 1840 | } | 
| 1841 |   | 
| 1842 | const char * | 
| 1843 | got_object_tag_get_tagger(struct got_tag_object *tag) | 
| 1844 | { | 
| 1845 | 	return tag->tagger; | 
| 1846 | } | 
| 1847 |   | 
| 1848 | const char * | 
| 1849 | got_object_tag_get_message(struct got_tag_object *tag) | 
| 1850 | { | 
| 1851 | 	return tag->tagmsg; | 
| 1852 | } | 
| 1853 |   | 
| 1854 | static struct got_tree_entry * | 
| 1855 | find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len) | 
| 1856 | { | 
| 1857 | 	int i; | 
| 1858 |   | 
| 1859 | 	 | 
| 1860 | 	for (i = 0; i < tree->nentries; i++) { | 
| 1861 | 		struct got_tree_entry *te = &tree->entries[i]; | 
| 1862 | 		int cmp = strncmp(te->name, name, len); | 
| 1863 | 		if (cmp < 0) | 
| 1864 | 			continue; | 
| 1865 | 		if (cmp > 0) | 
| 1866 | 			break; | 
| 1867 | 		if (te->name[len] == '\0') | 
| 1868 | 			return te; | 
| 1869 | 	} | 
| 1870 | 	return NULL; | 
| 1871 | } | 
| 1872 |   | 
| 1873 | struct got_tree_entry * | 
| 1874 | got_object_tree_find_entry(struct got_tree_object *tree, const char *name) | 
| 1875 | { | 
| 1876 | 	return find_entry_by_name(tree, name, strlen(name)); | 
| 1877 | } | 
| 1878 |   | 
| 1879 | const struct got_error * | 
| 1880 | got_object_id_by_path(struct got_object_id **id, struct got_repository *repo, | 
| 1881 |     struct got_object_id *commit_id, const char *path) | 
| 1882 | { | 
| 1883 | 	const struct got_error *err = NULL; | 
| 1884 | 	struct got_commit_object *commit = NULL; | 
| 1885 | 	struct got_tree_object *tree = NULL; | 
| 1886 | 	struct got_tree_entry *te = NULL; | 
| 1887 | 	const char *seg, *s; | 
| 1888 | 	size_t seglen; | 
| 1889 |   | 
| 1890 | 	*id = NULL; | 
| 1891 |   | 
| 1892 | 	err = got_object_open_as_commit(&commit, repo, commit_id); | 
| 1893 | 	if (err) | 
| 1894 | 		goto done; | 
| 1895 |   | 
| 1896 | 	 | 
| 1897 | 	if (got_path_is_root_dir(path)) { | 
| 1898 | 		*id = got_object_id_dup(commit->tree_id); | 
| 1899 | 		if (*id == NULL) | 
| 1900 | 			err = got_error_from_errno("got_object_id_dup"); | 
| 1901 | 		goto done; | 
| 1902 | 	} | 
| 1903 |   | 
| 1904 | 	err = got_object_open_as_tree(&tree, repo, commit->tree_id); | 
| 1905 | 	if (err) | 
| 1906 | 		goto done; | 
| 1907 |   | 
| 1908 | 	s = path; | 
| 1909 | 	while (s[0] == '/') | 
| 1910 | 		s++; | 
| 1911 | 	seg = s; | 
| 1912 | 	seglen = 0; | 
| 1913 | 	while (*s) { | 
| 1914 | 		struct got_tree_object *next_tree; | 
| 1915 |   | 
| 1916 | 		if (*s != '/') { | 
| 1917 | 			s++; | 
| 1918 | 			seglen++; | 
| 1919 | 			if (*s) | 
| 1920 | 				continue; | 
| 1921 | 		} | 
| 1922 |   | 
| 1923 | 		te = find_entry_by_name(tree, seg, seglen); | 
| 1924 | 		if (te == NULL) { | 
| 1925 | 			err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY); | 
| 1926 | 			goto done; | 
| 1927 | 		} | 
| 1928 |   | 
| 1929 | 		if (*s == '\0') | 
| 1930 | 			break; | 
| 1931 |   | 
| 1932 | 		seg = s + 1; | 
| 1933 | 		seglen = 0; | 
| 1934 | 		s++; | 
| 1935 | 		if (*s) { | 
| 1936 | 			err = got_object_open_as_tree(&next_tree, repo, | 
| 1937 | 			    &te->id); | 
| 1938 | 			te = NULL; | 
| 1939 | 			if (err) | 
| 1940 | 				goto done; | 
| 1941 | 			got_object_tree_close(tree); | 
| 1942 | 			tree = next_tree; | 
| 1943 | 		} | 
| 1944 | 	} | 
| 1945 |   | 
| 1946 | 	if (te) { | 
| 1947 | 		*id = got_object_id_dup(&te->id); | 
| 1948 | 		if (*id == NULL) | 
| 1949 | 			return got_error_from_errno("got_object_id_dup"); | 
| 1950 | 	} else | 
| 1951 | 		err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY); | 
| 1952 | done: | 
| 1953 | 	if (commit) | 
| 1954 | 		got_object_commit_close(commit); | 
| 1955 | 	if (tree) | 
| 1956 | 		got_object_tree_close(tree); | 
| 1957 | 	return err; | 
| 1958 | } | 
| 1959 |   | 
| 1960 |  | 
| 1961 |   | 
| 1962 |   | 
| 1963 |   | 
| 1964 | static mode_t | 
| 1965 | normalize_mode_for_comparison(mode_t mode) | 
| 1966 | { | 
| 1967 | 	 | 
| 1968 | 	  | 
| 1969 | 	  | 
| 1970 | 	  | 
| 1971 | 	  | 
| 1972 | 	if (S_ISDIR(mode)) | 
| 1973 | 		return mode & S_IFDIR; | 
| 1974 |   | 
| 1975 | 	 | 
| 1976 | 	  | 
| 1977 | 	  | 
| 1978 | 	  | 
| 1979 | 	  | 
| 1980 | 	if (S_ISLNK(mode)) | 
| 1981 | 		return mode & S_IFLNK; | 
| 1982 |   | 
| 1983 | 	 | 
| 1984 | 	return mode & S_IXUSR; | 
| 1985 | } | 
| 1986 |   | 
| 1987 | const struct got_error * | 
| 1988 | got_object_tree_path_changed(int *changed, | 
| 1989 |     struct got_tree_object *tree01, struct got_tree_object *tree02, | 
| 1990 |     const char *path, struct got_repository *repo) | 
| 1991 | { | 
| 1992 | 	const struct got_error *err = NULL; | 
| 1993 | 	struct got_tree_object *tree1 = NULL, *tree2 = NULL; | 
| 1994 | 	struct got_tree_entry *te1 = NULL, *te2 = NULL; | 
| 1995 | 	const char *seg, *s; | 
| 1996 | 	size_t seglen; | 
| 1997 |   | 
| 1998 | 	*changed = 0; | 
| 1999 |   | 
| 2000 | 	 | 
| 2001 | 	if (got_path_is_root_dir(path)) | 
| 2002 | 		return got_error_path(path, GOT_ERR_BAD_PATH); | 
| 2003 |   | 
| 2004 | 	tree1 = tree01; | 
| 2005 | 	tree2 = tree02; | 
| 2006 | 	s = path; | 
| 2007 | 	while (*s == '/') | 
| 2008 | 		s++; | 
| 2009 | 	seg = s; | 
| 2010 | 	seglen = 0; | 
| 2011 | 	while (*s) { | 
| 2012 | 		struct got_tree_object *next_tree1, *next_tree2; | 
| 2013 | 		mode_t mode1, mode2; | 
| 2014 |   | 
| 2015 | 		if (*s != '/') { | 
| 2016 | 			s++; | 
| 2017 | 			seglen++; | 
| 2018 | 			if (*s) | 
| 2019 | 				continue; | 
| 2020 | 		} | 
| 2021 |   | 
| 2022 | 		te1 = find_entry_by_name(tree1, seg, seglen); | 
| 2023 | 		if (te1 == NULL) { | 
| 2024 | 			err = got_error(GOT_ERR_NO_OBJ); | 
| 2025 | 			goto done; | 
| 2026 | 		} | 
| 2027 |   | 
| 2028 | 		if (tree2) | 
| 2029 | 			te2 = find_entry_by_name(tree2, seg, seglen); | 
| 2030 |   | 
| 2031 | 		if (te2) { | 
| 2032 | 			mode1 = normalize_mode_for_comparison(te1->mode); | 
| 2033 | 			mode2 = normalize_mode_for_comparison(te2->mode); | 
| 2034 | 			if (mode1 != mode2) { | 
| 2035 | 				*changed = 1; | 
| 2036 | 				goto done; | 
| 2037 | 			} | 
| 2038 |   | 
| 2039 | 			if (got_object_id_cmp(&te1->id, &te2->id) == 0) { | 
| 2040 | 				*changed = 0; | 
| 2041 | 				goto done; | 
| 2042 | 			} | 
| 2043 | 		} | 
| 2044 |   | 
| 2045 | 		if (*s == '\0') {  | 
| 2046 | 			*changed = 1; | 
| 2047 | 			goto done; | 
| 2048 | 		} | 
| 2049 |   | 
| 2050 | 		seg = s + 1; | 
| 2051 | 		s++; | 
| 2052 | 		seglen = 0; | 
| 2053 | 		if (*s) { | 
| 2054 | 			err = got_object_open_as_tree(&next_tree1, repo, | 
| 2055 | 			    &te1->id); | 
| 2056 | 			te1 = NULL; | 
| 2057 | 			if (err) | 
| 2058 | 				goto done; | 
| 2059 | 			if (tree1 != tree01) | 
| 2060 | 				got_object_tree_close(tree1); | 
| 2061 | 			tree1 = next_tree1; | 
| 2062 |   | 
| 2063 | 			if (te2) { | 
| 2064 | 				err = got_object_open_as_tree(&next_tree2, repo, | 
| 2065 | 				    &te2->id); | 
| 2066 | 				te2 = NULL; | 
| 2067 | 				if (err) | 
| 2068 | 					goto done; | 
| 2069 | 				if (tree2 != tree02) | 
| 2070 | 					got_object_tree_close(tree2); | 
| 2071 | 				tree2 = next_tree2; | 
| 2072 | 			} else if (tree2) { | 
| 2073 | 				if (tree2 != tree02) | 
| 2074 | 					got_object_tree_close(tree2); | 
| 2075 | 				tree2 = NULL; | 
| 2076 | 			} | 
| 2077 | 		} | 
| 2078 | 	} | 
| 2079 | done: | 
| 2080 | 	if (tree1 && tree1 != tree01) | 
| 2081 | 		got_object_tree_close(tree1); | 
| 2082 | 	if (tree2 && tree2 != tree02) | 
| 2083 | 		got_object_tree_close(tree2); | 
| 2084 | 	return err; | 
| 2085 | } | 
| 2086 |   | 
| 2087 | const struct got_error * | 
| 2088 | got_object_tree_entry_dup(struct got_tree_entry **new_te, | 
| 2089 |     struct got_tree_entry *te) | 
| 2090 | { | 
| 2091 | 	const struct got_error *err = NULL; | 
| 2092 |   | 
| 2093 | 	*new_te = calloc(1, sizeof(**new_te)); | 
| 2094 | 	if (*new_te == NULL) | 
| 2095 | 		return got_error_from_errno("calloc"); | 
| 2096 |   | 
| 2097 | 	(*new_te)->mode = te->mode; | 
| 2098 | 	memcpy((*new_te)->name, te->name, sizeof((*new_te)->name)); | 
| 2099 | 	memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id)); | 
| 2100 | 	return err; | 
| 2101 | } | 
| 2102 |   | 
| 2103 | int | 
| 2104 | got_object_tree_entry_is_submodule(struct got_tree_entry *te) | 
| 2105 | { | 
| 2106 | 	return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK); | 
| 2107 | } | 
| 2108 |   | 
| 2109 | int | 
| 2110 | got_object_tree_entry_is_symlink(struct got_tree_entry *te) | 
| 2111 | { | 
| 2112 | 	 | 
| 2113 | 	return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK); | 
| 2114 | } | 
| 2115 |   | 
| 2116 | static const struct got_error * | 
| 2117 | resolve_symlink(char **link_target, const char *path, | 
| 2118 |     struct got_object_id *commit_id, struct got_repository *repo) | 
| 2119 | { | 
| 2120 | 	const struct got_error *err = NULL; | 
| 2121 | 	char buf[PATH_MAX]; | 
| 2122 | 	char *name, *parent_path = NULL; | 
| 2123 | 	struct got_object_id *tree_obj_id = NULL; | 
| 2124 | 	struct got_tree_object *tree = NULL; | 
| 2125 | 	struct got_tree_entry *te = NULL; | 
| 2126 |   | 
| 2127 | 	*link_target = NULL; | 
| 2128 |   | 
| 2129 | 	if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) | 
 | 3  |  | Assuming the condition is false |  |  
  | 
 |  | 
| 2130 | 		return got_error(GOT_ERR_NO_SPACE); | 
| 2131 |   | 
| 2132 | 	name = basename(buf); | 
| 2133 | 	if (name == NULL) | 
 | 5  |  | Assuming 'name' is not equal to NULL |  |  
  | 
 |  | 
| 2134 | 		return got_error_from_errno2("basename", path); | 
| 2135 |   | 
| 2136 | 	err = got_path_dirname(&parent_path, path); | 
| 2137 | 	if (err) | 
 |  | 
 |  | 
| 2138 | 		return err; | 
| 2139 |   | 
| 2140 | 	err = got_object_id_by_path(&tree_obj_id, repo, commit_id, | 
| 2141 | 	    parent_path); | 
| 2142 | 	if (err) { | 
 |  | 
 |  | 
| 2143 | 		if (err->code == GOT_ERR_NO_TREE_ENTRY) { | 
| 2144 | 			 | 
| 2145 | 			err = got_error_path(path, err->code); | 
| 2146 | 		} | 
| 2147 | 		goto done; | 
| 2148 | 	} | 
| 2149 |   | 
| 2150 | 	err = got_object_open_as_tree(&tree, repo, tree_obj_id); | 
| 2151 | 	if (err) | 
 |  | 
| 2152 | 		goto done; | 
| 2153 |   | 
| 2154 | 	te = got_object_tree_find_entry(tree, name); | 
| 2155 | 	if (te == NULL) { | 
 |  | 
| 2156 | 		err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY); | 
| 2157 | 		goto done; | 
| 2158 | 	} | 
| 2159 |   | 
| 2160 | 	if (got_object_tree_entry_is_symlink(te)) { | 
 |  | 
| 2161 | 		err = got_tree_entry_get_symlink_target(link_target, te, repo); | 
 | 14  |  | Calling 'got_tree_entry_get_symlink_target' |  |  
  | 
| 2162 | 		if (err) | 
| 2163 | 			goto done; | 
| 2164 | 		if (!got_path_is_absolute(*link_target)) { | 
| 2165 | 			char *abspath; | 
| 2166 | 			if (asprintf(&abspath, "%s/%s", parent_path, | 
| 2167 | 			    *link_target) == -1) { | 
| 2168 | 				err = got_error_from_errno("asprintf"); | 
| 2169 | 				goto done; | 
| 2170 | 			} | 
| 2171 | 			free(*link_target); | 
| 2172 | 			*link_target = malloc(PATH_MAX); | 
| 2173 | 			if (*link_target == NULL) { | 
| 2174 | 				err = got_error_from_errno("malloc"); | 
| 2175 | 				goto done; | 
| 2176 | 			} | 
| 2177 | 			err = got_canonpath(abspath, *link_target, PATH_MAX); | 
| 2178 | 			free(abspath); | 
| 2179 | 			if (err) | 
| 2180 | 				goto done; | 
| 2181 | 		} | 
| 2182 | 	} | 
| 2183 | done: | 
| 2184 | 	free(tree_obj_id); | 
| 2185 | 	if (tree) | 
| 2186 | 		got_object_tree_close(tree); | 
| 2187 | 	if (err) { | 
| 2188 | 		free(*link_target); | 
| 2189 | 		*link_target = NULL; | 
| 2190 | 	} | 
| 2191 | 	return err; | 
| 2192 | } | 
| 2193 |   | 
| 2194 | const struct got_error * | 
| 2195 | got_object_resolve_symlinks(char **link_target, const char *path, | 
| 2196 |     struct got_object_id *commit_id, struct got_repository *repo) | 
| 2197 | { | 
| 2198 | 	const struct got_error *err = NULL; | 
| 2199 | 	char *next_target = NULL; | 
| 2200 | 	int max_recursion = 40;  | 
| 2201 |   | 
| 2202 | 	*link_target = NULL; | 
| 2203 |   | 
| 2204 | 	do { | 
| 2205 | 		err = resolve_symlink(&next_target, | 
 | 2  |  | Calling 'resolve_symlink' |  |  
  | 
| 2206 | 		    *link_target ? *link_target : path, commit_id, repo); | 
 |  | 
| 2207 | 		if (err) | 
| 2208 | 			break; | 
| 2209 | 		if (next_target) { | 
| 2210 | 			free(*link_target); | 
| 2211 | 			if (--max_recursion == 0) { | 
| 2212 | 				err = got_error_path(path, GOT_ERR_RECURSION); | 
| 2213 | 				*link_target = NULL; | 
| 2214 | 				break; | 
| 2215 | 			} | 
| 2216 | 			*link_target = next_target; | 
| 2217 | 		} | 
| 2218 | 	} while (next_target); | 
| 2219 |   | 
| 2220 | 	return err; | 
| 2221 | } | 
| 2222 |   | 
| 2223 | const struct got_error * | 
| 2224 | got_traverse_packed_commits(struct got_object_id_queue *traversed_commits, | 
| 2225 |     struct got_object_id *commit_id, const char *path, | 
| 2226 |     struct got_repository *repo) | 
| 2227 | { | 
| 2228 | 	const struct got_error *err = NULL; | 
| 2229 | 	struct got_pack *pack = NULL; | 
| 2230 | 	struct got_packidx *packidx = NULL; | 
| 2231 | 	char *path_packfile = NULL; | 
| 2232 | 	struct got_commit_object *changed_commit = NULL; | 
| 2233 | 	struct got_object_id *changed_commit_id = NULL; | 
| 2234 | 	int idx; | 
| 2235 |   | 
| 2236 | 	err = got_repo_search_packidx(&packidx, &idx, repo, commit_id); | 
| 2237 | 	if (err) { | 
| 2238 | 		if (err->code != GOT_ERR_NO_OBJ) | 
| 2239 | 			return err; | 
| 2240 | 		return NULL; | 
| 2241 | 	} | 
| 2242 |   | 
| 2243 | 	err = got_packidx_get_packfile_path(&path_packfile, packidx); | 
| 2244 | 	if (err) | 
| 2245 | 		return err; | 
| 2246 |   | 
| 2247 | 	pack = got_repo_get_cached_pack(repo, path_packfile); | 
| 2248 | 	if (pack == NULL) { | 
| 2249 | 		err = got_repo_cache_pack(&pack, repo, path_packfile, packidx); | 
| 2250 | 		if (err) | 
| 2251 | 			goto done; | 
| 2252 | 	} | 
| 2253 |   | 
| 2254 | 	if (pack->privsep_child == NULL) { | 
| 2255 | 		err = start_pack_privsep_child(pack, packidx); | 
| 2256 | 		if (err) | 
| 2257 | 			goto done; | 
| 2258 | 	} | 
| 2259 |   | 
| 2260 | 	err = got_privsep_send_commit_traversal_request( | 
| 2261 | 	    pack->privsep_child->ibuf, commit_id, idx, path); | 
| 2262 | 	if (err) | 
| 2263 | 		goto done; | 
| 2264 |   | 
| 2265 | 	err = got_privsep_recv_traversed_commits(&changed_commit, | 
| 2266 | 	    &changed_commit_id, traversed_commits, pack->privsep_child->ibuf); | 
| 2267 | 	if (err) | 
| 2268 | 		goto done; | 
| 2269 |   | 
| 2270 | 	if (changed_commit) { | 
| 2271 | 		 | 
| 2272 | 		  | 
| 2273 | 		  | 
| 2274 | 		  | 
| 2275 | 		changed_commit->refcnt++; | 
| 2276 | 		err = got_repo_cache_commit(repo, changed_commit_id, | 
| 2277 | 		    changed_commit); | 
| 2278 | 		got_object_commit_close(changed_commit); | 
| 2279 | 	} | 
| 2280 | done: | 
| 2281 | 	free(path_packfile); | 
| 2282 | 	free(changed_commit_id); | 
| 2283 | 	return err; | 
| 2284 | } |