/ libpkg / pkg_add.c
pkg_add.c
   1  /*-
   2   * Copyright (c) 2011-2026 Baptiste Daroussin <bapt@FreeBSD.org>
   3   * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
   4   * Copyright (c) 2016, Vsevolod Stakhov
   5   * Copyright (c) 2024, Future Crew, LLC
   6   *                     Author: Gleb Popov <arrowd@FreeBSD.org>
   7   *
   8   * SPDX-License-Identifier: BSD-2-Clause
   9   */
  10  
  11  #ifdef HAVE_CONFIG_H
  12  #include "pkg_config.h"
  13  #endif
  14  
  15  #include <archive.h>
  16  #include <archive_entry.h>
  17  #include <assert.h>
  18  #include <libgen.h>
  19  #include <string.h>
  20  #include <errno.h>
  21  #include <fcntl.h>
  22  #include <glob.h>
  23  #include <pwd.h>
  24  #include <grp.h>
  25  #include <sys/time.h>
  26  #include <sys/wait.h>
  27  #include <time.h>
  28  #include <xstring.h>
  29  
  30  #include <dirent.h>
  31  
  32  #include <sys/types.h>
  33  #include <sys/extattr.h>
  34  
  35  #include "pkg.h"
  36  #include "private/event.h"
  37  #include "private/utils.h"
  38  #include "private/pkg.h"
  39  #include "private/pkgdb.h"
  40  #include "private/add.h"
  41  
  42  #if defined(UF_NOUNLINK)
  43  #define NOCHANGESFLAGS	(UF_IMMUTABLE | UF_APPEND | UF_NOUNLINK | SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)
  44  #else
  45  #define NOCHANGESFLAGS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
  46  #endif
  47  
  48  struct external_merge_tmp_file {
  49  	int fd;
  50  	const char *template;
  51  	char path[MAXPATHLEN];
  52  	const char *content;
  53  	size_t content_len;
  54  };
  55  
  56  static merge_status
  57  merge_with_external_tool(const char *merge_tool, struct pkg_config_file *lcf,
  58      size_t lcf_len, struct pkg_config_file *rcf, char *localconf, char **mergedconf)
  59  {
  60  	pid_t wait_res;
  61  	int status;
  62  	FILE *inout[2];
  63  
  64  	char const *tmpdir = getenv("TMPDIR");
  65  	if (tmpdir == NULL)
  66  		tmpdir = "/tmp";
  67  
  68  	int output_fd;
  69  	char output_path[MAXPATHLEN];
  70  	off_t output_sz;
  71  
  72  	strlcpy(output_path, tmpdir, sizeof(output_path));
  73  	strlcat(output_path, "/OUTPUT.XXXXXXXXXX", sizeof(output_path));
  74  	output_fd = mkstemp(output_path);
  75  	if (output_fd == -1) {
  76  		pkg_emit_error("Can't create %s", output_path);
  77  		return MERGE_FAILED;
  78  	}
  79  	close(output_fd);
  80  
  81  	struct external_merge_tmp_file tmp_files[] = {
  82  		{-1, "/BASE.XXXXXXXXXX", {0}, lcf->content, lcf_len},
  83  		{-1, "/LOCAL.XXXXXXXXXX", {0}, localconf, strlen(localconf)},
  84  		{-1, "/REMOTE.XXXXXXXXXX", {0}, rcf->content, strlen(rcf->content)}
  85  	};
  86  	bool tmp_files_ok = true;
  87  	for (int i = 0; i < NELEM(tmp_files); i++) {
  88  		int copied = strlcpy(tmp_files[i].path, tmpdir, sizeof(tmp_files[i].path));
  89  		if (copied >= sizeof(tmp_files[i].path)) {
  90  			pkg_emit_error("Temporary path too long: %s", tmp_files[i].path);
  91  			return MERGE_FAILED;
  92  		}
  93  		copied = strlcat(tmp_files[i].path, tmp_files[i].template, sizeof(tmp_files[i].path));
  94  		if (copied >= sizeof(tmp_files[i].path)) {
  95  			pkg_emit_error("Temporary path too long: %s", tmp_files[i].path);
  96  			return MERGE_FAILED;
  97  		}
  98  
  99  		tmp_files[i].fd = mkstemp(tmp_files[i].path);
 100  		if (tmp_files[i].fd == -1) {
 101  			pkg_emit_error("Can't create %s", tmp_files[i].path);
 102  			tmp_files_ok = false;
 103  			break;
 104  		}
 105  		if (write(tmp_files[i].fd, tmp_files[i].content, tmp_files[i].content_len) == -1) {
 106  			pkg_emit_error("Failed to write %s", tmp_files[i].path);
 107  			tmp_files_ok = false;
 108  			break;
 109  		}
 110  		close(tmp_files[i].fd);
 111  		tmp_files[i].fd = -1;
 112  	}
 113  	if (!tmp_files_ok) {
 114  		for (int i = 0; i < NELEM(tmp_files); i++) {
 115  			if (tmp_files[i].fd != -1)
 116  				close(tmp_files[i].fd);
 117  			if (strlen(tmp_files[i].path))
 118  				unlink(tmp_files[i].path);
 119  		}
 120  		return MERGE_FAILED;
 121  	}
 122  
 123  	char command[MAXPATHLEN];
 124  	int i = 0;
 125  	int j = 0;
 126  	for (; *merge_tool != '\0' && i < (int)sizeof(command) - 1; i++, merge_tool++) {
 127  		if (*merge_tool != '%') {
 128  			command[i] = *merge_tool;
 129  			continue;
 130  		}
 131  		merge_tool++;
 132  		int tmp_files_index;
 133  		switch (*merge_tool) {
 134  		case 'b':
 135  			tmp_files_index = 0;
 136  			break;
 137  		case 'l':
 138  			tmp_files_index = 1;
 139  			break;
 140  		case 'r':
 141  			tmp_files_index = 2;
 142  			break;
 143  		case 'n':
 144  			j = strlcpy(&command[i], RELATIVE_PATH(rcf->path), sizeof(command) - i);
 145  			if (j == 0) {
 146  				pkg_emit_error("Max path len hit");
 147  				return MERGE_FAILED;
 148  			}
 149  			i += j - 1;
 150  			continue;
 151  		case 'o':
 152  			j = strlcpy(&command[i], output_path, sizeof(command) - i);
 153  			if (j == 0) {
 154  				pkg_emit_error("Max path len hit");
 155  				return MERGE_FAILED;
 156  			}
 157  			i += j - 1;
 158  			continue;
 159  		default:
 160  			pkg_emit_error("Unknown format string in the MERGETOOL command");
 161  			merge_tool--;
 162  			continue;
 163  		}
 164  		j = strlcpy(&command[i], tmp_files[tmp_files_index].path, sizeof(command) - i);
 165  		if (j == 0) {
 166  			pkg_emit_error("Max path len hit");
 167  			return MERGE_FAILED;
 168  		}
 169  		i += j - 1;
 170  	}
 171  	command[i] = '\0';
 172  
 173  	pid_t pid = process_spawn_pipe(inout, command);
 174  	wait_res = waitpid(pid, &status, 0);
 175  
 176  	fclose(inout[0]);
 177  	fclose(inout[1]);
 178  	for (int i = 0; i < NELEM(tmp_files); i++) {
 179  		unlink(tmp_files[i].path);
 180  	}
 181  
 182  	if (wait_res == -1 || WIFSIGNALED(status) || WEXITSTATUS(status)) {
 183  		unlink(output_path);
 184  		pkg_emit_error("External merge tool failed, retrying with builtin algorithm");
 185  		return MERGE_FAILED;
 186  	}
 187  
 188  	file_to_bufferat(AT_FDCWD, output_path, mergedconf, &output_sz);
 189  	unlink(output_path);
 190  
 191  	return MERGE_SUCCESS;
 192  }
 193  
 194  static void
 195  attempt_to_merge(int rootfd, struct pkg_config_file *rcf, struct pkg *local,
 196      bool merge, const char *merge_tool)
 197  {
 198  	const struct pkg_file *lf = NULL;
 199  	struct stat st;
 200  	xstring *newconf;
 201  	struct pkg_config_file *lcf = NULL;
 202  	size_t lcf_len;
 203  
 204  	char *localconf = NULL;
 205  	off_t sz;
 206  	char *localsum;
 207  
 208  	if (local == NULL) {
 209  		pkg_debug(3, "No local package");
 210  		if (fstatat(rootfd, RELATIVE_PATH(rcf->path), &st, 0) == 0) {
 211  			rcf->status = MERGE_NOT_LOCAL;
 212  		}
 213  		return;
 214  	}
 215  
 216  	if (!pkg_is_config_file(local, rcf->path, &lf, &lcf)) {
 217  		pkg_debug(3, "Corresponding file in local package not a config file");
 218  		rcf->status = MERGE_FAILED;
 219  		return;
 220  	}
 221  
 222  	if (lcf->content == NULL) {
 223  		pkg_debug(3, "Empty configuration content for local package");
 224  		return;
 225  	}
 226  
 227  	pkg_debug(1, "Config file found %s", rcf->path);
 228  	if (file_to_bufferat(rootfd, RELATIVE_PATH(rcf->path), &localconf, &sz) != EPKG_OK)
 229  		return;
 230  
 231  	pkg_debug(2, "size: %jd vs %jd", (intmax_t)sz, (intmax_t)strlen(lcf->content));
 232  
 233  	lcf_len = strlen(lcf->content);
 234  	if (sz == lcf_len) {
 235  		pkg_debug(2, "Ancient vanilla and deployed conf are the same size testing checksum");
 236  		localsum = pkg_checksum_data(localconf, sz,
 237  		    PKG_HASH_TYPE_SHA256_HEX);
 238  		if (localsum != NULL && lf->sum != NULL && STREQ(localsum, lf->sum)) {
 239  			pkg_debug(2, "Checksum are the same %jd", (intmax_t)strlen(localconf));
 240  			free(localsum);
 241  			goto ret;
 242  		}
 243  		if (lf->sum == NULL)
 244  			pkg_emit_error("3way merge: no checksum for the original local file");
 245  		free(localsum);
 246  		pkg_debug(2, "Checksum are different %jd", (intmax_t)strlen(localconf));
 247  	}
 248  	rcf->status = MERGE_FAILED;
 249  	if (!merge) {
 250  		goto ret;
 251  	}
 252  
 253  	pkg_debug(1, "Attempting to merge %s", rcf->path);
 254  	if (merge_tool) {
 255  		char* mergedconf = NULL;
 256  		rcf->status = merge_with_external_tool(merge_tool, lcf, lcf_len, rcf, localconf, &mergedconf);
 257  		rcf->newcontent = mergedconf;
 258  		if (rcf->status == MERGE_SUCCESS)
 259  			goto ret;
 260  	}
 261  	newconf = xstring_new();
 262  	if (merge_3way(lcf->content, localconf, rcf->content, newconf) != 0) {
 263  		xstring_free(newconf);
 264  		pkg_emit_error("Unable to merge configuration file: %s", rcf->path);
 265  	} else {
 266  		char *conf = xstring_get(newconf);
 267  		rcf->newcontent = conf;
 268  		rcf->status = MERGE_SUCCESS;
 269  	}
 270  ret:
 271  	free(localconf);
 272  }
 273  
 274  static int
 275  set_chflags(int fd, const char *path, u_long fflags)
 276  {
 277  #ifdef HAVE_CHFLAGSAT
 278  	if (getenv("INSTALL_AS_USER"))
 279  		return (EPKG_OK);
 280  	if (fflags == 0)
 281  		return (EPKG_OK);
 282  	if (chflagsat(fd, RELATIVE_PATH(path), fflags, AT_SYMLINK_NOFOLLOW) == -1) {
 283  		pkg_fatal_errno("Failed to chflags %s", path);
 284  	}
 285  #endif
 286  	return (EPKG_OK);
 287  }
 288  int
 289  set_attrsat(int fd, const char *path, mode_t perm, uid_t uid, gid_t gid,
 290      const struct timespec *ats, const struct timespec *mts)
 291  {
 292  	struct stat st;
 293  	struct timespec times[2];
 294  
 295  	times[0] = *ats;
 296  	times[1] = *mts;
 297  	if (utimensat(fd, RELATIVE_PATH(path), times,
 298  	    AT_SYMLINK_NOFOLLOW) == -1 && errno != EOPNOTSUPP){
 299  		pkg_fatal_errno("Failed to set time on %s", path);
 300  	}
 301  
 302  	if (getenv("INSTALL_AS_USER") == NULL) {
 303  		if (fchownat(fd, RELATIVE_PATH(path), uid, gid,
 304  				AT_SYMLINK_NOFOLLOW) == -1) {
 305  			if (errno == ENOTSUP) {
 306  				if (fchownat(fd, RELATIVE_PATH(path), uid, gid, 0) == -1) {
 307  					pkg_fatal_errno("Failed to chown(fallback) %s", path);
 308  				}
 309  			}
 310  			else {
 311  				pkg_fatal_errno("Failed to chown %s", path);
 312  			}
 313  		}
 314  	}
 315  
 316  	/* zfs drops the setuid on fchownat */
 317  	if (fchmodat(fd, RELATIVE_PATH(path), perm, AT_SYMLINK_NOFOLLOW) == -1) {
 318  		if (errno == ENOTSUP) {
 319  			/*
 320  			 * Executing fchmodat on a symbolic link results in
 321  			 * ENOENT (file not found) on platforms that do not
 322  			 * support AT_SYMLINK_NOFOLLOW. The file mode of
 323  			 * symlinks cannot be modified via file descriptor
 324  			 * reference on these systems. The lchmod function is
 325  			 * also not an option because it is not a posix
 326  			 * standard, nor is implemented everywhere. Since
 327  			 * symlink permissions have never been evaluated and
 328  			 * thus cosmetic, just skip them on these systems.
 329  			 */
 330  			if (fstatat(fd, RELATIVE_PATH(path), &st, AT_SYMLINK_NOFOLLOW) == -1) {
 331  				pkg_fatal_errno("Failed to get file status %s", path);
 332  			}
 333  			if (!S_ISLNK(st.st_mode)) {
 334  				if (fchmodat(fd, RELATIVE_PATH(path), perm, 0) == -1) {
 335  					pkg_fatal_errno("Failed to chmod(fallback) %s", path);
 336  				}
 337  			}
 338  		}
 339  		else {
 340  			pkg_fatal_errno("Failed to chmod %s", path);
 341  		}
 342  	}
 343  
 344  	return (EPKG_OK);
 345  }
 346  
 347  static void
 348  fill_timespec_buf(const struct stat *aest, struct timespec tspec[2])
 349  {
 350  #ifdef HAVE_STRUCT_STAT_ST_MTIM
 351  	tspec[0].tv_sec = aest->st_atim.tv_sec;
 352  	tspec[0].tv_nsec = 0;
 353  	tspec[1].tv_sec = aest->st_mtim.tv_sec;
 354  	tspec[1].tv_nsec = 0;
 355  #else
 356  # if defined(_DARWIN_C_SOURCE) || defined(__APPLE__)
 357  	tspec[0].tv_sec = aest->st_atimespec.tv_sec;
 358  	tspec[0].tv_nsec = 0;
 359  	tspec[1].tv_sec = aest->st_mtimespec.tv_sec;
 360  	tspec[1].tv_nsec = 0;
 361  # else
 362  	/* Portable unix version */
 363  	tspec[0].tv_sec = aest->st_atime;
 364  	tspec[0].tv_nsec = 0;
 365  	tspec[1].tv_sec = aest->st_mtime;
 366  	tspec[1].tv_nsec = 0;
 367  # endif
 368  #endif
 369  }
 370  
 371  static void
 372  reopen_tempdir(int rootfd, struct tempdir *t)
 373  {
 374  	if (t->fd != -1)
 375  		return;
 376  	t->fd = openat(rootfd, RELATIVE_PATH(t->temp), O_DIRECTORY|O_CLOEXEC);
 377  }
 378  
 379  static struct tempdir *
 380  get_tempdir(struct pkg_add_context *context, const char *path, tempdirs_t *tempdirs)
 381  {
 382  	struct tempdir *tmpdir = NULL;
 383  
 384  	vec_foreach(*tempdirs, i) {
 385  		tmpdir = tempdirs->d[i];
 386  		if (strncmp(tmpdir->name, path, tmpdir->len) == 0 && path[tmpdir->len] == '/') {
 387  			reopen_tempdir(context->rootfd, tmpdir);
 388  			return (tmpdir);
 389  		}
 390  	}
 391  
 392  	tmpdir = open_tempdir(context, path);
 393  	if (tmpdir != NULL)
 394  		vec_push(tempdirs, tmpdir);
 395  
 396  	return (tmpdir);
 397  }
 398  
 399  static void
 400  close_tempdir(struct tempdir *t)
 401  {
 402  	if (t == NULL)
 403  		return;
 404  	if (t->fd != -1)
 405  		close(t->fd);
 406  	t->fd = -1;
 407  }
 408  
 409  static int
 410  create_dir(struct pkg_add_context *context, struct pkg_dir *d,
 411      tempdirs_t *tempdirs)
 412  {
 413  	struct stat st;
 414  	struct tempdir *tmpdir;
 415  	const char *path;
 416  	int error, fd;
 417  
 418  	tmpdir = get_tempdir(context, d->path, tempdirs);
 419  	if (tmpdir == NULL) {
 420  		fd = context->rootfd;
 421  		path = d->path;
 422  	} else {
 423  		fd = tmpdir->fd;
 424  		path = d->path + tmpdir->len;
 425  	}
 426  
 427  	if (mkdirat(fd, RELATIVE_PATH(path), 0755) == -1) {
 428  		if (!mkdirat_p(fd, RELATIVE_PATH(path))) {
 429  			close_tempdir(tmpdir);
 430  			return (EPKG_FATAL);
 431  		}
 432  	}
 433  
 434  	if (fstatat(fd, RELATIVE_PATH(path), &st, 0) == -1) {
 435  		if (errno != ENOENT) {
 436  			close_tempdir(tmpdir);
 437  			pkg_fatal_errno("Failed to stat directory %s", d->path);
 438  		}
 439  		/* path is a dangling symlink. */
 440  		error = unlinkat(fd, RELATIVE_PATH(path), 0);
 441  		if (error != 0) {
 442  			close_tempdir(tmpdir);
 443  			pkg_fatal_errno("Failed to unlink dangling symlink %s",
 444  			    d->path);
 445  		}
 446  		if (mkdirat(fd, RELATIVE_PATH(path), 0755) == -1) {
 447  			if (tmpdir != NULL) {
 448  				close_tempdir(tmpdir);
 449  				pkg_fatal_errno(
 450  				    "Failed to create directory %s/%s",
 451  				    tmpdir->temp, path);
 452  			} else {
 453  				pkg_fatal_errno("Failed to create directory %s",
 454  				    path);
 455  			}
 456  		}
 457  		if (fstatat(fd, RELATIVE_PATH(path), &st, 0) == -1) {
 458  			close_tempdir(tmpdir);
 459  			pkg_fatal_errno("Failed to stat directory %s", d->path);
 460  		}
 461  	}
 462  
 463  	if (st.st_uid == d->uid && st.st_gid == d->gid &&
 464  	    (st.st_mode & ~S_IFMT) == (d->perm & ~S_IFMT)) {
 465  		d->noattrs = true;
 466  	}
 467  
 468  	close_tempdir(tmpdir);
 469  	return (EPKG_OK);
 470  }
 471  
 472  /* In case of directories create the dir and extract the creds */
 473  static int
 474  do_extract_dir(struct pkg_add_context* context, struct archive *a __unused, struct archive_entry *ae,
 475      const char *path, struct pkg *local __unused, tempdirs_t *tempdirs)
 476  {
 477  	struct pkg_dir *d;
 478  	const struct stat *aest;
 479  	unsigned long clear;
 480  
 481  	d = pkg_get_dir(context->pkg, path);
 482  	if (d == NULL) {
 483  		pkg_emit_error("Directory %s not specified in the manifest, skipping",
 484  				path);
 485  		return (EPKG_OK);
 486  	}
 487  	aest = archive_entry_stat(ae);
 488  	d->perm = aest->st_mode;
 489  	d->uid = get_uid_from_uname(archive_entry_uname(ae));
 490  	d->gid = get_gid_from_gname(archive_entry_gname(ae));
 491  	free(d->uname);
 492  	d->uname = xstrdup(archive_entry_uname(ae));
 493  	free(d->gname);
 494  	d->gname = xstrdup(archive_entry_gname(ae));
 495  	fill_timespec_buf(aest, d->time);
 496  	archive_entry_fflags(ae, &d->fflags, &clear);
 497  
 498  	if (create_dir(context, d, tempdirs) == EPKG_FATAL) {
 499  		return (EPKG_FATAL);
 500  	}
 501  
 502  	metalog_add(PKG_METALOG_DIR, RELATIVE_PATH(path),
 503  	    archive_entry_uname(ae), archive_entry_gname(ae),
 504  	    aest->st_mode & ~S_IFDIR, d->fflags, NULL);
 505  
 506  	return (EPKG_OK);
 507  }
 508  
 509  
 510  static bool
 511  try_mkdir(int fd, const char *path)
 512  {
 513  	char *p = get_dirname(xstrdup(path));
 514  
 515  	if (!mkdirat_p(fd, RELATIVE_PATH(p))) {
 516  		free(p);
 517  		return (false);
 518  	}
 519  	free(p);
 520  	return (true);
 521  }
 522  
 523  static int
 524  create_symlinks(struct pkg_add_context *context, struct pkg_file *f, const char *target, tempdirs_t *tempdirs)
 525  {
 526  	struct tempdir *tmpdir = NULL;
 527  	int fd;
 528  	const char *path;
 529  	bool tried_mkdir = false;
 530  
 531  	tmpdir = get_tempdir(context, f->path, tempdirs);
 532  	if (tmpdir == NULL && errno == 0) {
 533  		char temppath[MAXPATHLEN] = { 0 };
 534  		hidden_tempfile(temppath, sizeof(temppath), f->path);
 535  		f->temppath = xstrdup(temppath);
 536  	}
 537  	if (tmpdir == NULL) {
 538  		if (f->temppath == NULL) {
 539  			pkg_emit_error("Failed to create symlink %s: "
 540  			    "no temporary path", f->path);
 541  			return (EPKG_FATAL);
 542  		}
 543  		fd = context->rootfd;
 544  		path = f->temppath;
 545  	} else {
 546  		fd = tmpdir->fd;
 547  		path = f->path + tmpdir->len;
 548  	}
 549  retry:
 550  	if (symlinkat(target, fd, RELATIVE_PATH(path)) == -1) {
 551  		if (!tried_mkdir) {
 552  			if (!try_mkdir(fd, path)) {
 553  				close_tempdir(tmpdir);
 554  				return (EPKG_FATAL);
 555  			}
 556  			tried_mkdir = true;
 557  			goto retry;
 558  		}
 559  
 560  		pkg_fatal_errno("Failed to create symlink: %s", path);
 561  	}
 562  
 563  	if (set_attrsat(fd, path, f->perm, f->uid, f->gid,
 564  	    &f->time[0], &f->time[1]) != EPKG_OK) {
 565  		close_tempdir(tmpdir);
 566  		return (EPKG_FATAL);
 567  	}
 568  	if (tmpdir != NULL)
 569  		set_chflags(fd, path, f->fflags);
 570  	close_tempdir(tmpdir);
 571  
 572  	return (EPKG_OK);
 573  }
 574  
 575  /* In case of a symlink create it directly with a random name */
 576  static int
 577  do_extract_symlink(struct pkg_add_context *context, struct archive *a __unused,
 578      struct archive_entry *ae, const char *path, struct pkg *local __unused,
 579      tempdirs_t *tempdirs)
 580  {
 581  	struct pkg_file *f;
 582  	const struct stat *aest;
 583  	unsigned long clear;
 584  
 585  	f = pkg_get_file(context->pkg, path);
 586  	if (f == NULL) {
 587  		pkg_emit_error("Symlink %s not specified in the manifest", path);
 588  		return (EPKG_FATAL);
 589  	}
 590  
 591  	aest = archive_entry_stat(ae);
 592  	f->uid = get_uid_from_uname(archive_entry_uname(ae));
 593  	f->gid = get_gid_from_gname(archive_entry_gname(ae));
 594  	free(f->uname);
 595  	f->uname = xstrdup(archive_entry_uname(ae));
 596  	free(f->gname);
 597  	f->gname = xstrdup(archive_entry_gname(ae));
 598  	f->perm = aest->st_mode;
 599  	fill_timespec_buf(aest, f->time);
 600  	archive_entry_fflags(ae, &f->fflags, &clear);
 601  
 602  	if (create_symlinks(context, f, archive_entry_symlink(ae), tempdirs) == EPKG_FATAL)
 603  		return (EPKG_FATAL);
 604  
 605  	metalog_add(PKG_METALOG_LINK, RELATIVE_PATH(path),
 606  	    archive_entry_uname(ae), archive_entry_gname(ae),
 607  	    aest->st_mode & ~S_IFLNK, f->fflags, archive_entry_symlink(ae));
 608  
 609  	return (EPKG_OK);
 610  }
 611  
 612  static int
 613  create_hardlink(struct pkg_add_context *context, struct pkg_file *f, const char *path, tempdirs_t *tempdirs)
 614  {
 615  	bool tried_mkdir = false;
 616  	struct pkg_file *fh;
 617  	int fd, fdh;
 618  	const char *pathfrom, *pathto;
 619  	struct tempdir *tmpdir = NULL;
 620  	struct tempdir *tmphdir = NULL;
 621  
 622  	tmpdir = get_tempdir(context, f->path, tempdirs);
 623  	if (tmpdir == NULL && errno == 0) {
 624  		char temppath[MAXPATHLEN] = { 0 };
 625  		hidden_tempfile(temppath, sizeof(temppath), f->path);
 626  		f->temppath = xstrdup(temppath);
 627  	}
 628  	if (tmpdir != NULL) {
 629  		fd = tmpdir->fd;
 630  	} else {
 631  		fd = context->rootfd;
 632  	}
 633  	fh = pkg_get_file(context->pkg, path);
 634  	if (fh == NULL) {
 635  		close_tempdir(tmpdir);
 636  		pkg_emit_error("Can't find the file %s is supposed to be"
 637  		    " hardlinked to %s", f->path, path);
 638  		return (EPKG_FATAL);
 639  	}
 640  	if (fh->temppath == NULL) {
 641  		vec_foreach(*tempdirs, i) {
 642  			if (strncmp(tempdirs->d[i]->name, fh->path, tempdirs->d[i]->len) == 0 &&
 643  			    fh->path[tempdirs->d[i]->len] == '/' ) {
 644  				tmphdir = tempdirs->d[i];
 645  				reopen_tempdir(context->rootfd, tmphdir);
 646  				break;
 647  			}
 648  		}
 649  	}
 650  	if (tmpdir == NULL) {
 651  		if (f->temppath == NULL) {
 652  			close_tempdir(tmpdir);
 653  			pkg_emit_error("Failed to create hardlink %s: "
 654  			    "no temporary path", f->path);
 655  			return (EPKG_FATAL);
 656  		}
 657  		pathto = f->temppath;
 658  		fd = context->rootfd;
 659  	} else {
 660  		pathto = f->path + tmpdir->len;
 661  		fd = tmpdir->fd;
 662  	}
 663  
 664  	if (tmphdir == NULL) {
 665  		if (fh->temppath == NULL) {
 666  			close_tempdir(tmpdir);
 667  			pkg_emit_error("Failed to create hardlink %s -> %s: "
 668  			    "no temporary path for source", f->path, path);
 669  			return (EPKG_FATAL);
 670  		}
 671  		pathfrom = fh->temppath;
 672  		fdh = context->rootfd;
 673  	} else {
 674  		pathfrom = fh->path + tmphdir->len;
 675  		fdh = tmphdir->fd;
 676  	}
 677  
 678  retry:
 679  	if (linkat(fdh, RELATIVE_PATH(pathfrom),
 680  	    fd, RELATIVE_PATH(pathto), 0) == -1) {
 681  		if (!tried_mkdir) {
 682  			if (!try_mkdir(fd, pathto)) {
 683  				close_tempdir(tmpdir);
 684  				close_tempdir(tmphdir);
 685  				return (EPKG_FATAL);
 686  			}
 687  			tried_mkdir = true;
 688  			goto retry;
 689  		}
 690  
 691  		close_tempdir(tmpdir);
 692  		close_tempdir(tmphdir);
 693  		pkg_fatal_errno("Failed to create hardlink: %s <-> %s", pathfrom, pathto);
 694  	}
 695  	close_tempdir(tmpdir);
 696  	close_tempdir(tmphdir);
 697  
 698  	return (EPKG_OK);
 699  }
 700  
 701  static int
 702  do_extract_hardlink(struct pkg_add_context *context, struct archive *a __unused, struct archive_entry *ae,
 703      const char *path, struct pkg *local __unused, tempdirs_t *tempdirs)
 704  {
 705  	struct pkg_file *f;
 706  	const struct stat *aest;
 707  	const char *lp;
 708  
 709  	f = pkg_get_file(context->pkg, path);
 710  	if (f == NULL) {
 711  		pkg_emit_error("Hardlink %s not specified in the manifest", path);
 712  		return (EPKG_FATAL);
 713  	}
 714  	lp = archive_entry_hardlink(ae);
 715  	aest = archive_entry_stat(ae);
 716  
 717  	if (create_hardlink(context, f, lp, tempdirs) == EPKG_FATAL)
 718  		return (EPKG_FATAL);
 719  
 720  	metalog_add(PKG_METALOG_FILE, RELATIVE_PATH(path),
 721  	    archive_entry_uname(ae), archive_entry_gname(ae),
 722  	    aest->st_mode & ~S_IFREG, 0, NULL);
 723  
 724  	return (EPKG_OK);
 725  }
 726  
 727  static int
 728  open_tempfile(int rootfd, const char *path, int perm)
 729  {
 730  	int fd;
 731  	bool tried_mkdir = false;
 732  
 733  retry:
 734  	fd = openat(rootfd, RELATIVE_PATH(path), O_CREAT|O_WRONLY|O_EXCL, perm);
 735  	if (fd == -1) {
 736  		if (!tried_mkdir) {
 737  			if (!try_mkdir(rootfd, path))
 738  				return (-2);
 739  			tried_mkdir = true;
 740  			goto retry;
 741  		}
 742  		return (-1);
 743  	}
 744  	return (fd);
 745  }
 746  
 747  static int
 748  create_regfile(struct pkg_add_context *context, struct pkg_file *f, struct archive *a,
 749      struct archive_entry *ae, int fromfd, struct pkg *local, tempdirs_t *tempdirs)
 750  {
 751  	int fd = -1;
 752  	size_t len;
 753  	char buf[32768];
 754  	char *path;
 755  	struct tempdir *tmpdir = NULL;
 756  	const char *attrname;
 757  	void *attrval;
 758  	size_t attrsz;
 759  
 760  	tmpdir = get_tempdir(context, f->path, tempdirs);
 761  	if (tmpdir == NULL && errno == 0) {
 762  		char temppath[MAXPATHLEN] = { 0 };
 763  		hidden_tempfile(temppath, sizeof(temppath), f->path);
 764  		f->temppath = xstrdup(temppath);
 765  	}
 766  
 767  	if (tmpdir != NULL) {
 768  		fd = open_tempfile(tmpdir->fd, f->path + tmpdir->len, f->perm);
 769  	} else if (f->temppath != NULL) {
 770  		fd = open_tempfile(context->rootfd, f->temppath, f->perm);
 771  	} else {
 772  		pkg_emit_error("Failed to create temporary file for %s", f->path);
 773  		return (EPKG_FATAL);
 774  	}
 775  	if (fd == -2) {
 776  		close_tempdir(tmpdir);
 777  		return (EPKG_FATAL);
 778  	}
 779  
 780  	if (fd == -1) {
 781  		if (tmpdir != NULL) {
 782  			close_tempdir(tmpdir);
 783  			pkg_fatal_errno("Failed to create temporary file '%s/%s' for %s", tmpdir->name, f->path + tmpdir->len, f->path);
 784  		}
 785  		pkg_fatal_errno("Failed to create temporary file for %s", f->path);
 786  	}
 787  
 788  	if (fromfd == -1) {
 789  		/* check if this is a config file */
 790  		f->config = pkghash_get_value(context->pkg->config_files_hash, f->path);
 791  		if (f->config) {
 792  			const char *cfdata;
 793  			bool merge = pkg_object_bool(pkg_config_get("AUTOMERGE"));
 794  			const char *merge_tool = pkg_object_string(pkg_config_get("MERGETOOL"));
 795  
 796  			pkg_debug(1, "Populating config_file %s", f->path);
 797  			if (archive_entry_size(ae) < 0) {
 798  				pkg_emit_error("Invalid config file size for %s", f->path);
 799  				return (EPKG_FATAL);
 800  			}
 801  			len = archive_entry_size(ae);
 802  			f->config->content = xmalloc(len + 1);
 803  			archive_read_data(a, f->config->content, len);
 804  			f->config->content[len] = '\0';
 805  			cfdata = f->config->content;
 806  			attempt_to_merge(context->rootfd, f->config, local, merge, merge_tool);
 807  			if (f->config->status == MERGE_SUCCESS)
 808  				cfdata = f->config->newcontent;
 809  			dprintf(fd, "%s", cfdata);
 810  			if (f->config->newcontent != NULL)
 811  				free(f->config->newcontent);
 812  		} else {
 813  			if (ftruncate(fd, archive_entry_size(ae)) == -1) {
 814  				close(fd);
 815  				close_tempdir(tmpdir);
 816  				pkg_fatal_errno("Failed to truncate file: %s", f->path);
 817  			}
 818  		}
 819  
 820  		if (archive_entry_xattr_reset(ae)) {
 821  			attrname = NULL;
 822  			attrval = NULL;
 823  			attrsz = 0;
 824  
 825  			while (archive_entry_xattr_next(ae, &attrname,
 826  			    &attrval, &attrsz) == ARCHIVE_OK) {
 827  				assert(attrname != NULL);
 828  				assert(attrval != NULL);
 829  				assert(attrsz > 0);
 830  
 831  				if (!strncmp(attrname, "system.", 7)) {
 832  					extattr_set_fd(fd,
 833  					    EXTATTR_NAMESPACE_SYSTEM,
 834  					    attrname+7, attrval, attrsz);
 835  				}
 836  			}
 837  		}
 838  
 839  		if (!f->config && archive_read_data_into_fd(a, fd) != ARCHIVE_OK) {
 840  			close(fd);
 841  			close_tempdir(tmpdir);
 842  			pkg_emit_error("Failed to extract %s from package: %s",
 843  			    f->path, archive_error_string(a));
 844  			return (EPKG_FATAL);
 845  		}
 846  	} else {
 847  		while ((len = read(fromfd, buf, sizeof(buf))) > 0)
 848  			if (write(fd, buf, len) == -1) {
 849  				pkg_errno("Failed to write file: %s", f->temppath);
 850  			}
 851  	}
 852  	if (fd != -1)
 853  		close(fd);
 854  	if (tmpdir == NULL) {
 855  		fd = context->rootfd;
 856  		path = f->temppath;
 857  	} else {
 858  		fd = tmpdir->fd;
 859  		path = f->path + tmpdir->len;
 860  	}
 861  
 862  	if (set_attrsat(fd, path, f->perm, f->uid, f->gid,
 863  	    &f->time[0], &f->time[1]) != EPKG_OK) {
 864  		close_tempdir(tmpdir);
 865  		return (EPKG_FATAL);
 866  	}
 867  	if (tmpdir != NULL)
 868  		set_chflags(fd, path, f->fflags);
 869  
 870  	close_tempdir(tmpdir);
 871  	return (EPKG_OK);
 872  }
 873  
 874  static int
 875  do_extract_regfile(struct pkg_add_context *context, struct archive *a, struct archive_entry *ae,
 876      const char *path, struct pkg *local, tempdirs_t *tempdirs)
 877  {
 878  	struct pkg_file *f;
 879  	const struct stat *aest;
 880  	unsigned long clear;
 881  
 882  	f = pkg_get_file(context->pkg, path);
 883  	if (f == NULL) {
 884  		pkg_emit_error("File %s not specified in the manifest", path);
 885  		return (EPKG_FATAL);
 886  	}
 887  
 888  	aest = archive_entry_stat(ae);
 889  	f->perm = aest->st_mode;
 890  	f->uid = get_uid_from_uname(archive_entry_uname(ae));
 891  	f->gid = get_gid_from_gname(archive_entry_gname(ae));
 892  	free(f->uname);
 893  	f->uname = xstrdup(archive_entry_uname(ae));
 894  	free(f->gname);
 895  	f->gname = xstrdup(archive_entry_gname(ae));
 896  	fill_timespec_buf(aest, f->time);
 897  	archive_entry_fflags(ae, &f->fflags, &clear);
 898  
 899  	if (create_regfile(context, f, a, ae, -1, local, tempdirs) == EPKG_FATAL)
 900  		return (EPKG_FATAL);
 901  
 902  	metalog_add(PKG_METALOG_FILE, RELATIVE_PATH(path),
 903  	    archive_entry_uname(ae), archive_entry_gname(ae),
 904  	    aest->st_mode & ~S_IFREG, f->fflags, NULL);
 905  
 906  	return (EPKG_OK);
 907  }
 908  
 909  static int
 910  do_extract(struct archive *a, struct archive_entry *ae,
 911      int nfiles, struct pkg *local, tempdirs_t *tempdirs,
 912      struct pkg_add_context *context)
 913  {
 914  	int	retcode = EPKG_OK;
 915  	int	ret = 0, cur_file = 0;
 916  	char	path[MAXPATHLEN];
 917  	int (*extract_cb)(struct pkg_add_context *context, struct archive *a,
 918  	    struct archive_entry *ae, const char *path, struct pkg *local,
 919  	    tempdirs_t *tempdirs);
 920  
 921  #ifndef HAVE_ARC4RANDOM
 922  	srand(time(NULL));
 923  #endif
 924  
 925  	if (nfiles == 0)
 926  		return (EPKG_OK);
 927  
 928  	pkg_emit_extract_begin(context->pkg);
 929  	pkg_emit_progress_start(NULL);
 930  
 931  	do {
 932  		pkg_absolutepath(archive_entry_pathname(ae), path, sizeof(path), true);
 933  		if (match_ucl_lists(path,
 934  		    pkg_config_get("FILES_IGNORE_GLOB"),
 935  		    pkg_config_get("FILES_IGNORE_REGEX")))
 936  			continue;
 937  		switch (archive_entry_filetype(ae)) {
 938  		case AE_IFDIR:
 939  			extract_cb = do_extract_dir;
 940  			break;
 941  		case AE_IFLNK:
 942  			extract_cb = do_extract_symlink;
 943  			break;
 944  		case 0: /* HARDLINKS */
 945  			extract_cb = do_extract_hardlink;
 946  			break;
 947  		case AE_IFREG:
 948  			extract_cb = do_extract_regfile;
 949  			break;
 950  		case AE_IFMT:
 951  		case AE_IFSOCK:
 952  		case AE_IFCHR:
 953  		case AE_IFIFO:
 954  		case AE_IFBLK:
 955  		default:
 956  			pkg_emit_error("Archive contains an unsupported filetype (%d): %s", archive_entry_filetype(ae), path);
 957  			retcode = EPKG_FATAL;
 958  			goto cleanup;
 959  		}
 960  
 961  		if (extract_cb(context, a, ae, path, local, tempdirs) != EPKG_OK) {
 962  			retcode = EPKG_FATAL;
 963  			goto cleanup;
 964  		}
 965  		if (archive_entry_filetype(ae) != AE_IFDIR) {
 966  			pkg_emit_progress_tick(cur_file++, nfiles);
 967  		}
 968  	} while ((ret = archive_read_next_header(a, &ae)) == ARCHIVE_OK);
 969  	pkg_emit_progress_tick(cur_file++, nfiles);
 970  
 971  	if (ret != ARCHIVE_EOF) {
 972  		pkg_emit_error("archive_read_next_header(): %s",
 973  		    archive_error_string(a));
 974  		retcode = EPKG_FATAL;
 975  	}
 976  
 977  cleanup:
 978  	pkg_emit_progress_tick(nfiles, nfiles);
 979  	pkg_emit_extract_finished(context->pkg);
 980  
 981  	return (retcode);
 982  }
 983  
 984  static void
 985  backup_file_if_needed(struct pkg *p, struct pkg_file *f)
 986  {
 987  	char path[MAXPATHLEN];
 988  	struct stat st;
 989  	char *sum;
 990  	pkg_checksum_type_t t;
 991  
 992  	if (fstatat(p->rootfd, RELATIVE_PATH(f->path), &st,
 993  	    AT_SYMLINK_NOFOLLOW) == -1)
 994  		return;
 995  
 996  	if (S_ISLNK(st.st_mode))
 997  		return;
 998  
 999  	if (S_ISREG(st.st_mode)) {
1000  		t = pkg_checksum_file_get_type(f->sum, -1);
1001  		sum = pkg_checksum_generate_fileat(p->rootfd,
1002  		    RELATIVE_PATH(f->path), t);
1003  		if (sum == NULL)
1004  			return;
1005  
1006  		if (STREQ(sum, f->sum)) {
1007  			free(sum);
1008  			return;
1009  		}
1010  		free(sum);
1011  	}
1012  
1013  	snprintf(path, sizeof(path), "%s.pkgsave", f->path);
1014  	renameat(p->rootfd, RELATIVE_PATH(f->path),
1015  	    p->rootfd, RELATIVE_PATH(path));
1016  }
1017  
1018  static int
1019  pkg_extract_finalize(struct pkg *pkg, tempdirs_t *tempdirs)
1020  {
1021  	struct stat st;
1022  	struct pkg_file *f = NULL;
1023  	struct pkg_dir *d = NULL;
1024  	char path[MAXPATHLEN + 8];
1025  	const char *fto;
1026  #ifdef HAVE_CHFLAGSAT
1027  	bool install_as_user;
1028  
1029  	install_as_user = (getenv("INSTALL_AS_USER") != NULL);
1030  #endif
1031  
1032  
1033  	if (tempdirs != NULL) {
1034  		vec_foreach(*tempdirs, i) {
1035  			struct tempdir *t = tempdirs->d[i];
1036  			if (fstatat(pkg->rootfd, RELATIVE_PATH(t->name),
1037  			    &st, AT_SYMLINK_NOFOLLOW) == 0)
1038  				unlinkat(pkg->rootfd,
1039  				    RELATIVE_PATH(t->name), 0);
1040  			if (renameat(pkg->rootfd, RELATIVE_PATH(t->temp),
1041  			    pkg->rootfd, RELATIVE_PATH(t->name)) != 0) {
1042  				pkg_fatal_errno("Failed to rename %s -> %s",
1043  				    t->temp, t->name);
1044  			}
1045  			free(t);
1046  		}
1047  	}
1048  	while (pkg_files(pkg, &f) == EPKG_OK) {
1049  
1050  		if (match_ucl_lists(f->path,
1051  		    pkg_config_get("FILES_IGNORE_GLOB"),
1052  		    pkg_config_get("FILES_IGNORE_REGEX")))
1053  			continue;
1054  		append_touched_file(f->path);
1055  		if (f->temppath == NULL)
1056  			continue;
1057  		fto = f->path;
1058  		if (f->config && f->config->status == MERGE_FAILED &&
1059  		    f->previous != PKG_FILE_NONE) {
1060  			snprintf(path, sizeof(path), "%s.pkgnew", f->path);
1061  			fto = path;
1062  		}
1063  
1064  		if (f->config && f->config->status == MERGE_NOT_LOCAL) {
1065  			backup_file_if_needed(pkg, f);
1066  		}
1067  
1068  		/*
1069  		 * enforce an unlink of the file to workaround a bug that
1070  		 * results in renameat returning 0 of the from file is hardlink
1071  		 * on the to file, but the to file is not removed
1072  		 */
1073  		if (f->previous != PKG_FILE_NONE &&
1074  		    fstatat(pkg->rootfd, RELATIVE_PATH(fto), &st,
1075  		    AT_SYMLINK_NOFOLLOW) != -1) {
1076  #ifdef HAVE_CHFLAGSAT
1077  			if (!install_as_user && st.st_flags & NOCHANGESFLAGS) {
1078  				chflagsat(pkg->rootfd, RELATIVE_PATH(fto), 0,
1079  				    AT_SYMLINK_NOFOLLOW);
1080  			}
1081  #endif
1082  			/* if the files does not belong to any package, we do save it */
1083  			if (f->previous == PKG_FILE_SAVE) {
1084  				backup_file_if_needed(pkg, f);
1085  			}
1086  			unlinkat(pkg->rootfd, RELATIVE_PATH(fto), 0);
1087  		}
1088  		if (renameat(pkg->rootfd, RELATIVE_PATH(f->temppath),
1089  		    pkg->rootfd, RELATIVE_PATH(fto)) == -1) {
1090  			char pkgnew[MAXPATHLEN + 8];
1091  			snprintf(pkgnew, sizeof(pkgnew), "%s.pkgnew", f->path);
1092  			if (renameat(pkg->rootfd, RELATIVE_PATH(f->temppath),
1093  			    pkg->rootfd, RELATIVE_PATH(pkgnew)) == -1) {
1094  				pkg_fatal_errno("Failed to rename %s -> %s",
1095  				    f->temppath, fto);
1096  			}
1097  			pkg_emit_notice("Cannot install %s, "
1098  					"installed as %s", f->path, pkgnew);
1099  		}
1100  
1101  		if (set_chflags(pkg->rootfd, fto, f->fflags) != EPKG_OK)
1102  			return (EPKG_FATAL);
1103  	}
1104  
1105  	while (pkg_dirs(pkg, &d) == EPKG_OK) {
1106  		append_touched_dir(d->path);
1107  		if (d->noattrs)
1108  			continue;
1109  		if (set_attrsat(pkg->rootfd, d->path, d->perm,
1110  		    d->uid, d->gid, &d->time[0], &d->time[1]) != EPKG_OK)
1111  			return (EPKG_FATAL);
1112  		if (set_chflags(pkg->rootfd, d->path, d->fflags) != EPKG_OK)
1113  			return (EPKG_FATAL);
1114  	}
1115  	if (tempdirs != NULL)
1116  		vec_free(tempdirs);
1117  
1118  	return (EPKG_OK);
1119  }
1120  
1121  static char *
1122  pkg_globmatch(char *pattern, const char *name)
1123  {
1124  	glob_t g;
1125  	int i;
1126  	char *buf, *buf2;
1127  	char *path = NULL;
1128  
1129  	if (glob(pattern, 0, NULL, &g) == GLOB_NOMATCH) {
1130  		globfree(&g);
1131  
1132  		return (NULL);
1133  	}
1134  
1135  	for (i = 0; i < g.gl_pathc; i++) {
1136  		/* the version starts here */
1137  		buf = strrchr(g.gl_pathv[i], '-');
1138  		if (buf == NULL)
1139  			continue;
1140  		buf2 = strrchr(g.gl_pathv[i], '/');
1141  		if (buf2 == NULL)
1142  			buf2 = g.gl_pathv[i];
1143  		else
1144  			buf2++;
1145  		/* ensure we have match the proper name */
1146  		if (strncmp(buf2, name, buf - buf2) != 0)
1147  			continue;
1148  		if (path == NULL) {
1149  			path = g.gl_pathv[i];
1150  			continue;
1151  		}
1152  		if (pkg_version_cmp(g.gl_pathv[i], path) == 1)
1153  			path = g.gl_pathv[i];
1154  	}
1155  	if (path)
1156  		path = xstrdup(path);
1157  	globfree(&g);
1158  
1159  	return (path);
1160  }
1161  
1162  bool
1163  append_pkg_if_newer(pkgs_t *localpkgs, struct pkg *p)
1164  {
1165  	/* only keep the highest version if we find one */
1166  	struct pkg **lp = pkgs_insert_sorted(localpkgs, p);
1167  	if (lp != NULL) {
1168  		if (pkg_version_cmp((*lp)->version, p->version) == -1) {
1169  			pkg_free(*lp);
1170  			*lp = p;
1171  			return (true);
1172  		}
1173  		return (false);
1174  	}
1175  	return (true);
1176  }
1177  
1178  static charv_t system_shlibs_cache = vec_init();
1179  static bool system_shlibs_scanned = false;
1180  
1181  static charv_t *
1182  get_system_shlibs(void)
1183  {
1184  	if (!system_shlibs_scanned) {
1185  		scan_system_shlibs(&system_shlibs_cache, ctx.pkg_rootdir);
1186  		system_shlibs_scanned = true;
1187  	}
1188  	return (&system_shlibs_cache);
1189  }
1190  
1191  /*
1192   * Select a provider package from a symlink directory.
1193   * dirpath: e.g. "/repo/shlibs/libfoo.so.1"
1194   * ext:     package extension including dot (e.g. ".pkg")
1195   *
1196   * Selection logic:
1197   * - Single entry → use it
1198   * - Multiple → sort alphabetically, pick first (lowest NN. prefix = highest priority)
1199   * Returns xasprintf'd full path or NULL.
1200   */
1201  static char *
1202  select_provider_from_dir(const char *dirpath, const char *ext)
1203  {
1204  	DIR *d = opendir(dirpath);
1205  	if (d == NULL)
1206  		return (NULL);
1207  
1208  	charv_t entries = vec_init();
1209  	char fullpath[MAXPATHLEN];
1210  	struct dirent *de;
1211  	while ((de = readdir(d)) != NULL) {
1212  		if (de->d_name[0] == '.')
1213  			continue;
1214  		if (!str_ends_with(de->d_name, ext))
1215  			continue;
1216  		/* Skip dangling symlinks */
1217  		snprintf(fullpath, sizeof(fullpath), "%s/%s",
1218  		    dirpath, de->d_name);
1219  		if (access(fullpath, F_OK) != 0)
1220  			continue;
1221  		vec_push(&entries, xstrdup(de->d_name));
1222  	}
1223  	closedir(d);
1224  
1225  	if (entries.len == 0) {
1226  		vec_free(&entries);
1227  		return (NULL);
1228  	}
1229  
1230  	char *selected = NULL;
1231  
1232  	if (entries.len == 1) {
1233  		selected = entries.d[0];
1234  		entries.d[0] = NULL;
1235  	} else {
1236  		/* Sort alphabetically, pick first (lowest NN. prefix wins) */
1237  		qsort(entries.d, entries.len, sizeof(char *), char_cmp);
1238  		selected = entries.d[0];
1239  		entries.d[0] = NULL;
1240  	}
1241  
1242  	char *result = NULL;
1243  	if (selected != NULL) {
1244  		xasprintf(&result, "%s/%s", dirpath, selected);
1245  		free(selected);
1246  	}
1247  
1248  	vec_free_and_free(&entries, free);
1249  	return (result);
1250  }
1251  
1252  static int
1253  pkg_add_check_pkg_archive(struct pkgdb *db, struct pkg *pkg,
1254  	const char *path, int flags, const char *location)
1255  {
1256  	const char	*arch;
1257  	int	ret, retcode;
1258  	struct pkg_dep	*dep = NULL;
1259  	char	bd[MAXPATHLEN];
1260  	char	dpath[MAXPATHLEN], *ppath;
1261  	const char	*ext = NULL;
1262  	struct pkg	*pkg_inst = NULL;
1263  	bool	fromstdin;
1264  
1265  	arch = pkg->abi != NULL ? pkg->abi : pkg->altabi;
1266  
1267  	if (!is_valid_abi(arch, true) && (flags & PKG_ADD_FORCE) == 0) {
1268  		return (EPKG_FATAL);
1269  	}
1270  
1271  	if ((flags & PKG_ADD_FORCE) == 0 && !is_valid_os_version(pkg)) {
1272  		return (EPKG_FATAL);
1273  	}
1274  
1275  	/* XX check */
1276  	ret = pkg_try_installed(db, pkg->name, &pkg_inst, PKG_LOAD_BASIC);
1277  	if (ret == EPKG_OK) {
1278  		if ((flags & PKG_ADD_FORCE) == 0 &&
1279  		    pkg_version_cmp(pkg_inst->version, pkg->version) >= 0) {
1280  			pkg_emit_already_installed(pkg_inst);
1281  			pkg_free(pkg_inst);
1282  			pkg_inst = NULL;
1283  			return (EPKG_INSTALLED);
1284  		}
1285  		if (pkg_inst->locked) {
1286  			pkg_emit_locked(pkg_inst);
1287  			pkg_free(pkg_inst);
1288  			pkg_inst = NULL;
1289  			return (EPKG_LOCKED);
1290  		}
1291  		pkg_emit_notice("package %s is already installed, forced "
1292  		    "install", pkg->name);
1293  		pkg_free(pkg_inst);
1294  		pkg_inst = NULL;
1295  	} else if (ret != EPKG_END) {
1296  		return (ret);
1297  	}
1298  
1299  	/*
1300  	 * Check for dependencies by searching the same directory as
1301  	 * the package archive we're reading.  Of course, if we're
1302  	 * reading from a file descriptor or a unix domain socket or
1303  	 * whatever, there's no valid directory to search.
1304  	 */
1305  	fromstdin = STREQ(path, "-");
1306  	strlcpy(bd, path, sizeof(bd));
1307  	if (!fromstdin) {
1308  		/* In-place truncate bd to the directory components. */
1309  		char *basedir = strrchr(bd, '/');
1310  		if (NULL == basedir) {
1311  			bd[0]='.';
1312  			bd[1]='\0';
1313  		} else {
1314  			*basedir = '\0';
1315  		}
1316  		if ((ext = strrchr(path, '.')) == NULL) {
1317  			pkg_emit_error("%s has no extension", path);
1318  			return (EPKG_FATAL);
1319  		}
1320  	}
1321  
1322  	retcode = EPKG_FATAL;
1323  	pkg_emit_add_deps_begin(pkg);
1324  
1325  	while (pkg_deps(pkg, &dep) == EPKG_OK) {
1326  		dpath[0] = '\0';
1327  
1328  		if (pkg_is_installed(db, dep->name) == EPKG_OK)
1329  			continue;
1330  
1331  		if (fromstdin) {
1332  			pkg_emit_missing_dep(pkg, dep);
1333  			if ((flags & PKG_ADD_FORCE_MISSING) == 0)
1334  				goto cleanup;
1335  			continue;
1336  		}
1337  
1338  		if (dep->version != NULL && dep->version[0] != '\0') {
1339  			snprintf(dpath, sizeof(dpath), "%s/%s-%s%s", bd,
1340  				dep->name, dep->version, ext);
1341  		}
1342  
1343  		if (strlen(dpath) == 0 || access(dpath, F_OK) != 0) {
1344  			snprintf(dpath, sizeof(dpath), "%s/%s-*%s", bd,
1345  			    dep->name, ext);
1346  			ppath = pkg_globmatch(dpath, dep->name);
1347  			if (ppath == NULL) {
1348  				pkg_emit_missing_dep(pkg, dep);
1349  				if ((flags & PKG_ADD_FORCE_MISSING) == 0)
1350  					goto cleanup;
1351  				continue;
1352  			}
1353  			strlcpy(dpath, ppath, sizeof(dpath));
1354  			free(ppath);
1355  		}
1356  
1357  		if ((flags & PKG_ADD_UPGRADE) == 0 &&
1358  				access(dpath, F_OK) == 0) {
1359  			ret = pkg_add(db, dpath, PKG_ADD_AUTOMATIC, location);
1360  
1361  			if (ret != EPKG_OK)
1362  				goto cleanup;
1363  		} else {
1364  			pkg_emit_missing_dep(pkg, dep);
1365  			if ((flags & PKG_ADD_FORCE_MISSING) == 0)
1366  				goto cleanup;
1367  		}
1368  	}
1369  
1370  	/*
1371  	 * Phase 2: Resolve shlibs_required via symlink directory layout.
1372  	 * Look in basedir/../shlibs/<shlibname>/ for provider packages.
1373  	 */
1374  	if (!fromstdin && (flags & PKG_ADD_UPGRADE) == 0) {
1375  		char parentdir[MAXPATHLEN];
1376  		strlcpy(parentdir, bd, sizeof(parentdir));
1377  		char *pslash = strrchr(parentdir, '/');
1378  		if (pslash != NULL)
1379  			*pslash = '\0';
1380  
1381  		charv_t *system_shlibs = get_system_shlibs();
1382  
1383  		vec_foreach(pkg->shlibs_required, si) {
1384  			const char *shlibname = pkg->shlibs_required.d[si];
1385  
1386  			if (charv_search(system_shlibs, shlibname) != NULL)
1387  				continue;
1388  
1389  			if (pkgdb_is_shlib_provided(db, shlibname))
1390  				continue;
1391  
1392  			char provdir[MAXPATHLEN];
1393  			snprintf(provdir, sizeof(provdir), "%s/shlibs/%s",
1394  			    parentdir, shlibname);
1395  			char *provider = select_provider_from_dir(
1396  			    provdir, ext);
1397  
1398  			if (provider == NULL) {
1399  				pkg_emit_error("Missing shlib %s required by %s",
1400  				    pkg->shlibs_required.d[si], pkg->name);
1401  				if ((flags & PKG_ADD_FORCE_MISSING) == 0)
1402  					goto cleanup;
1403  				continue;
1404  			}
1405  			ret = pkg_add(db, provider, PKG_ADD_AUTOMATIC,
1406  			    location);
1407  			free(provider);
1408  			if (ret != EPKG_OK && ret != EPKG_INSTALLED)
1409  				goto cleanup;
1410  		}
1411  
1412  		/*
1413  		 * Phase 3: Resolve abstract requires via symlink directory
1414  		 * layout.  Look in basedir/../provides/<label>/ for providers.
1415  		 */
1416  		vec_foreach(pkg->requires, ri) {
1417  			const char *req = pkg->requires.d[ri];
1418  
1419  			if (pkgdb_is_provided(db, req))
1420  				continue;
1421  
1422  			char provdir[MAXPATHLEN];
1423  			snprintf(provdir, sizeof(provdir), "%s/provides/%s",
1424  			    parentdir, req);
1425  			char *provider = select_provider_from_dir(
1426  			    provdir, ext);
1427  
1428  			if (provider == NULL) {
1429  				pkg_emit_error(
1430  				    "Missing provide %s required by %s",
1431  				    req, pkg->name);
1432  				if ((flags & PKG_ADD_FORCE_MISSING) == 0)
1433  					goto cleanup;
1434  				continue;
1435  			}
1436  			ret = pkg_add(db, provider, PKG_ADD_AUTOMATIC,
1437  			    location);
1438  			free(provider);
1439  			if (ret != EPKG_OK && ret != EPKG_INSTALLED)
1440  				goto cleanup;
1441  		}
1442  	}
1443  
1444  	retcode = EPKG_OK;
1445  cleanup:
1446  	pkg_emit_add_deps_finished(pkg);
1447  
1448  	return (retcode);
1449  }
1450  
1451  static int
1452  pkg_add_cleanup_old(struct pkgdb *db, struct pkg *old, struct pkg *new, struct triggers *t, int flags)
1453  {
1454  	struct pkg_file *f;
1455  	int ret = EPKG_OK;
1456  
1457  	pkg_start_stop_rc_scripts(old, PKG_RC_STOP);
1458  
1459  	/*
1460  	 * Execute pre deinstall scripts
1461  	 */
1462  	if ((flags & PKG_ADD_NOSCRIPT) == 0) {
1463  		bool noexec = ((flags & PKG_ADD_NOEXEC) == PKG_ADD_NOEXEC);
1464  		ret = pkg_lua_script_run(old, PKG_LUA_PRE_DEINSTALL, (old != NULL));
1465  		if (ret != EPKG_OK && ctx.developer_mode) {
1466  			return (ret);
1467  		} else {
1468  			ret = pkg_script_run(old, PKG_SCRIPT_PRE_DEINSTALL, (old != NULL),
1469  			    noexec);
1470  			if (ret != EPKG_OK && (ctx.developer_mode || noexec)) {
1471  				return (ret);
1472  			} else {
1473  				ret = EPKG_OK;
1474  			}
1475  		}
1476  	}
1477  
1478  	/* Now remove files that no longer exist in the new package */
1479  	if (new != NULL) {
1480  		f = NULL;
1481  		while (pkg_files(old, &f) == EPKG_OK) {
1482  			if (!pkg_has_file(new, f->path) || match_ucl_lists(f->path,
1483  			    pkg_config_get("FILES_IGNORE_GLOB"),
1484  			    pkg_config_get("FILES_IGNORE_REGEX"))) {
1485  				pkg_debug(2, "File %s is not in the new package", f->path);
1486  				pkg_maybe_backup_library(db, old, f->path);
1487  				trigger_is_it_a_cleanup(t, f->path);
1488  				pkg_delete_file(old, f);
1489  			}
1490  		}
1491  
1492  		pkg_delete_dirs(db, old, new);
1493  	}
1494  
1495  	return (ret);
1496  }
1497  
1498  void
1499  pkg_rollback_pkg(struct pkg *p)
1500  {
1501  	struct pkg_file *f = NULL;
1502  
1503  	while (pkg_files(p, &f) == EPKG_OK) {
1504  		if (match_ucl_lists(f->path,
1505  		    pkg_config_get("FILES_IGNORE_GLOB"),
1506  		    pkg_config_get("FILES_IGNORE_REGEX")))
1507  			continue;
1508  		if (f->temppath  != NULL) {
1509  			unlinkat(p->rootfd, f->temppath, 0);
1510  		}
1511  	}
1512  }
1513  
1514  void
1515  pkg_rollback_cb(void *data)
1516  {
1517  	pkg_rollback_pkg((struct pkg *)data);
1518  }
1519  
1520  int
1521  pkg_add_triggers(void)
1522  {
1523  	return (triggers_execute(NULL));
1524  }
1525  
1526  
1527  static int
1528  populate_config_file_contents(struct archive *a, struct archive_entry *ae,
1529  	struct pkg *pkg)
1530  {
1531  	int	retcode = EPKG_OK;
1532  	int	ret = 0;
1533  	char	path[MAXPATHLEN];
1534  
1535  	do {
1536  		if (archive_entry_filetype(ae) != AE_IFREG) {
1537  			continue;
1538  		}
1539  
1540  		pkg_absolutepath(archive_entry_pathname(ae), path, sizeof(path), true);
1541  
1542  		struct pkg_config_file *config = pkghash_get_value(pkg->config_files_hash, path);
1543  		if (config == NULL) {
1544  			continue;
1545  		}
1546  
1547  		if (archive_entry_size(ae) < 0) {
1548  			pkg_emit_error("Invalid config file size for %s", path);
1549  			return (EPKG_FATAL);
1550  		}
1551  		size_t len = archive_entry_size(ae);
1552  		config->content = xmalloc(len + 1);
1553  		archive_read_data(a, config->content, len);
1554  		config->content[len] = '\0';
1555  	} while ((ret = archive_read_next_header(a, &ae)) == ARCHIVE_OK);
1556  
1557  	if (ret != ARCHIVE_EOF) {
1558  		pkg_emit_error("archive_read_next_header(): %s",
1559  		    archive_error_string(a));
1560  		retcode = EPKG_FATAL;
1561  	}
1562  
1563  	return (retcode);
1564  }
1565  
1566  static int
1567  pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
1568      const char *reloc, struct pkg *remote,
1569      struct pkg *local, struct triggers *t)
1570  {
1571  	struct archive		*a;
1572  	struct archive_entry	*ae;
1573  	struct pkg		*pkg = NULL;
1574  	xstring			*message = NULL;
1575  	struct pkg_message	*msg;
1576  	struct pkg_file		*f;
1577  	const char		*msgstr;
1578  	bool			 extract = true, openxact = false;
1579  	int			 retcode = EPKG_OK;
1580  	int			 ret;
1581  	int			 nfiles;
1582  	tempdirs_t		 tempdirs = vec_init();
1583  	struct pkg_add_context	 context;
1584  
1585  	memset(&context, 0, sizeof(context));
1586  
1587  	assert(path != NULL);
1588  
1589  	/*
1590  	 * Open the package archive file, read all the meta files and set the
1591  	 * current archive_entry to the first non-meta file.
1592  	 * If there is no non-meta files, EPKG_END is returned.
1593  	 */
1594  	ret = pkg_open2(&pkg, &a, &ae, path, 0, -1);
1595  	context.pkg = pkg;
1596  	context.localpkg = local;
1597  	if (ret == EPKG_END)
1598  		extract = false;
1599  	else if (ret != EPKG_OK) {
1600  		retcode = ret;
1601  		goto cleanup;
1602  	}
1603  	if ((flags & (PKG_ADD_UPGRADE | PKG_ADD_SPLITTED_UPGRADE)) !=
1604  	    PKG_ADD_UPGRADE)
1605  		pkg_emit_install_begin(pkg);
1606  	else
1607  		pkg_emit_upgrade_begin(pkg, local);
1608  
1609  	if (pkg_is_valid(pkg) != EPKG_OK) {
1610  		pkg_emit_error("the package is not valid");
1611  		return (EPKG_FATAL);
1612  	}
1613  
1614  	if (flags & PKG_ADD_AUTOMATIC)
1615  		pkg->automatic = true;
1616  
1617  	if (flags & PKG_ADD_REGISTER_ONLY)
1618  		extract = false;
1619  
1620  	/*
1621  	 * Additional checks for non-remote package
1622  	 */
1623  	if (remote == NULL) {
1624  		ret = pkg_add_check_pkg_archive(db, pkg, path, flags, reloc);
1625  		if (ret != EPKG_OK) {
1626  			/* Do not return error on installed package */
1627  			retcode = (ret == EPKG_INSTALLED ? EPKG_OK : ret);
1628  			goto cleanup;
1629  		}
1630  	}
1631  	else {
1632  		if (remote->repo != NULL) {
1633  			/* Save reponame */
1634  			pkg_kv_add(&pkg->annotations, "repository", remote->repo->name, "annotation");
1635  			pkg_kv_add(&pkg->annotations, "repo_type", remote->repo->ops->type, "annotation");
1636  		}
1637  
1638  		free(pkg->digest);
1639  		pkg->digest = xstrdup(remote->digest);
1640  		/* only preserve flags if -A has not been passed */
1641  		if ((flags & PKG_ADD_AUTOMATIC) == 0)
1642  			pkg->automatic = remote->automatic;
1643  	}
1644  
1645  	if (reloc != NULL)
1646  		pkg_kv_add(&pkg->annotations, "relocated", reloc, "annotation");
1647  
1648  	pkg_open_root_fd(pkg);
1649  	context.rootfd = pkg->rootfd;
1650  	/* analyse previous files */
1651  	f = NULL;
1652  	while (pkg_files(pkg, &f) == EPKG_OK) {
1653  		if (match_ucl_lists(f->path,
1654  		    pkg_config_get("FILES_IGNORE_GLOB"),
1655  		    pkg_config_get("FILES_IGNORE_REGEX"))) {
1656  			continue;
1657  		}
1658  		if (faccessat(pkg->rootfd, RELATIVE_PATH(f->path), F_OK, 0) == 0) {
1659  			f->previous = PKG_FILE_EXIST;
1660  			if (!pkgdb_file_exists(db, f->path)) {
1661  				f->previous = PKG_FILE_SAVE;
1662  			}
1663  		}
1664  	}
1665  
1666  	/*
1667  	 * Register the package before installing it in case there are problems
1668  	 * that could be caught here.
1669  	 */
1670  	retcode = pkgdb_register_pkg(db, pkg, flags & PKG_ADD_FORCE, NULL);
1671  	if (retcode != EPKG_OK)
1672  		goto cleanup;
1673  	openxact = true;
1674  
1675  	/*
1676  	 * Execute pre-install scripts
1677  	 */
1678  	if ((flags & PKG_ADD_NOSCRIPT) == 0) {
1679  		if ((retcode = pkg_lua_script_run(pkg, PKG_LUA_PRE_INSTALL, (local != NULL))) != EPKG_OK)
1680  			goto cleanup;
1681  		if ((retcode = pkg_script_run(pkg, PKG_SCRIPT_PRE_INSTALL, (local != NULL),
1682  			    ((flags & PKG_ADD_NOEXEC) == PKG_ADD_NOEXEC))) != EPKG_OK)
1683  			goto cleanup;
1684  	}
1685  
1686  	/*
1687  	 * Execute per-package pre-install triggers
1688  	 */
1689  	if (t != NULL)
1690  		triggers_execute_perpackage(t, pkg, TRIGGER_PHASE_PRE_INSTALL, (local != NULL));
1691  
1692  	/* add the user and group if necessary */
1693  
1694  	nfiles = pkghash_count(pkg->filehash) + pkghash_count(pkg->dirhash);
1695  	/*
1696  	 * Extract the files on disk.
1697  	 */
1698  	if (extract) {
1699  		pkg_register_cleanup_callback(pkg_rollback_cb, pkg);
1700  		retcode = do_extract(a, ae, nfiles, local, &tempdirs, &context);
1701  		pkg_unregister_cleanup_callback(pkg_rollback_cb, pkg);
1702  		if (retcode != EPKG_OK) {
1703  			/* If the add failed, clean up (silently) */
1704  			pkg_rollback_pkg(pkg);
1705  			pkg_delete_dirs(db, pkg, NULL);
1706  			goto cleanup;
1707  		}
1708  	} else if (flags & PKG_ADD_REGISTER_ONLY && nfiles > 0) {
1709  		/* Need to populate config file contents so they can be stored in
1710  		   the database */
1711  		retcode = populate_config_file_contents(a, ae, pkg);
1712  		if (retcode != EPKG_OK) {
1713  			goto cleanup;
1714  		}
1715  	}
1716  
1717  	/*
1718  	 * If this was a split upgrade, the old package has been entirely
1719  	 * removed already.
1720  	 */
1721  	if (local != NULL && (flags & PKG_ADD_SPLITTED_UPGRADE) == 0) {
1722  		pkg_open_root_fd(local);
1723  		pkg_debug(1, "Cleaning up old version");
1724  		if (pkg_add_cleanup_old(db, local, pkg, t, flags) != EPKG_OK) {
1725  			retcode = EPKG_FATAL;
1726  			goto cleanup;
1727  		}
1728  	}
1729  
1730  
1731  	/* Update configuration file content with db with newer versions */
1732  	pkgdb_update_config_file_content(pkg, db->sqlite);
1733  
1734  	if (extract)
1735  		retcode = pkg_extract_finalize(pkg, &tempdirs);
1736  
1737  	pkgdb_register_finale(db, retcode, NULL);
1738  	openxact = false;
1739  
1740  	/*
1741  	 * Execute post install scripts
1742  	 */
1743  
1744  	if (retcode != EPKG_OK)
1745  		goto cleanup;
1746  	if ((flags & PKG_ADD_NOSCRIPT) == 0) {
1747  		bool noexec = ((flags & PKG_ADD_NOEXEC) == PKG_ADD_NOEXEC);
1748  		pkg_lua_script_run(pkg, PKG_LUA_POST_INSTALL, (local != NULL));
1749  		retcode = pkg_script_run(pkg, PKG_SCRIPT_POST_INSTALL, (local != NULL),
1750  		    noexec);
1751  		if (retcode != EPKG_OK && noexec)
1752  			goto cleanup;
1753  		retcode = EPKG_OK;
1754  	}
1755  
1756  	/*
1757  	 * Execute per-package post-install triggers
1758  	 */
1759  	if (t != NULL)
1760  		triggers_execute_perpackage(t, pkg, TRIGGER_PHASE_POST_INSTALL, (local != NULL));
1761  
1762  	/*
1763  	 * start the different related services if the users do want that
1764  	 * and that the service is running
1765  	 */
1766  
1767  	pkg_start_stop_rc_scripts(pkg, PKG_RC_START);
1768  
1769  	if ((flags & (PKG_ADD_UPGRADE | PKG_ADD_SPLITTED_UPGRADE)) !=
1770  	    PKG_ADD_UPGRADE)
1771  		pkg_emit_install_finished(pkg, local);
1772  	else
1773  		pkg_emit_upgrade_finished(pkg, local);
1774  
1775  	vec_foreach(pkg->message, i) {
1776  		msg = pkg->message.d[i];
1777  		msgstr = NULL;
1778  		if (msg->type == PKG_MESSAGE_ALWAYS) {
1779  			msgstr = msg->str;
1780  		} else if (local != NULL &&
1781  		     msg->type == PKG_MESSAGE_UPGRADE) {
1782  			if (msg->maximum_version == NULL &&
1783  			    msg->minimum_version == NULL) {
1784  				msgstr = msg->str;
1785  			} else if (msg->maximum_version == NULL) {
1786  				if (pkg_version_cmp(local->version, msg->minimum_version) == 1) {
1787  					msgstr = msg->str;
1788  				}
1789  			} else if (msg->minimum_version == NULL) {
1790  				if (pkg_version_cmp(local->version, msg->maximum_version) == -1) {
1791  					msgstr = msg->str;
1792  				}
1793  			} else if (pkg_version_cmp(local->version, msg->maximum_version) == -1 &&
1794  				    pkg_version_cmp(local->version, msg->minimum_version) == 1) {
1795  				msgstr = msg->str;
1796  			}
1797  		} else if (local == NULL &&
1798  		    msg->type == PKG_MESSAGE_INSTALL) {
1799  			msgstr = msg->str;
1800  		}
1801  		if (msgstr != NULL) {
1802  			if (message == NULL) {
1803  				message = xstring_new();
1804  				pkg_fprintf(message->fp, "=====\nMessage from "
1805  				    "%n-%v:\n\n", pkg, pkg);
1806  			}
1807  			fprintf(message->fp, "--\n%s\n", msgstr);
1808  		}
1809  	}
1810  	if (pkg_has_message(pkg) && message != NULL) {
1811  		fflush(message->fp);
1812  		pkg_emit_message(message->buf);
1813  		xstring_free(message);
1814  	}
1815  
1816  cleanup:
1817  	if (openxact)
1818  		pkgdb_register_finale(db, retcode, NULL);
1819  	if (a != NULL) {
1820  		archive_read_close(a);
1821  		archive_read_free(a);
1822  	}
1823  
1824  	pkg_free(pkg);
1825  
1826  	return (retcode);
1827  }
1828  
1829  int
1830  pkg_add(struct pkgdb *db, const char *path, unsigned flags,
1831      const char *location)
1832  {
1833  	return pkg_add_common(db, path, flags, location, NULL, NULL, NULL);
1834  }
1835  
1836  int
1837  pkg_add_from_remote(struct pkgdb *db, const char *path, unsigned flags,
1838      const char *location, struct pkg *rp, struct triggers *t)
1839  {
1840  	return pkg_add_common(db, path, flags, location, rp, NULL, t);
1841  }
1842  
1843  int
1844  pkg_add_upgrade(struct pkgdb *db, const char *path, unsigned flags,
1845      const char *location,
1846      struct pkg *rp, struct pkg *lp, struct triggers *t)
1847  {
1848  	if (pkgdb_ensure_loaded(db, lp,
1849  	    PKG_LOAD_FILES|PKG_LOAD_SCRIPTS|PKG_LOAD_DIRS|PKG_LOAD_LUA_SCRIPTS) != EPKG_OK)
1850  		return (EPKG_FATAL);
1851  
1852  	return pkg_add_common(db, path, flags, location, rp, lp, t);
1853  }
1854  
1855  static int
1856  pkg_group_dump(int fd, struct pkg *pkg)
1857  {
1858  	ucl_object_t *o, *seq;
1859  	struct pkg_dep	*dep = NULL;
1860  
1861  	if (pkg->type != PKG_GROUP_REMOTE)
1862  		return (EPKG_FATAL);
1863  	o = ucl_object_typed_new(UCL_OBJECT);
1864  	ucl_object_insert_key(o, ucl_object_fromstring(pkg->name), "name", 0, false);
1865  	ucl_object_insert_key(o, ucl_object_fromstring(pkg->comment), "comment", 0, false);
1866  	seq = ucl_object_typed_new(UCL_ARRAY);
1867  	while (pkg_deps(pkg, &dep) == EPKG_OK)
1868  		ucl_array_append(seq, ucl_object_fromstring(dep->name));
1869  	ucl_object_insert_key(o, seq, "depends", 0, false);
1870  	ucl_object_emit_fd(o, UCL_EMIT_CONFIG, fd);
1871  	return (EPKG_OK);
1872  }
1873  
1874  int
1875  pkg_add_group(struct pkg *pkg)
1876  {
1877  	char temp[MAXPATHLEN];
1878  	int dfd = pkg_get_dbdirfd();
1879  	mkdirat(dfd, "groups", 0755);
1880  	int gfd = openat(dfd, "groups", O_DIRECTORY|O_CLOEXEC);
1881  	hidden_tempfile(temp, MAXPATHLEN, pkg->name);
1882  	int fd = openat(gfd, temp, O_CREAT|O_EXCL|O_WRONLY, 0644);
1883  	if (fd == -1) {
1884  		pkg_emit_errno("Unable to create group file %s", pkg->name);
1885  		return (EPKG_FATAL);
1886  	}
1887  	pkg_group_dump(fd, pkg);
1888  	close(fd);
1889  	if (renameat(gfd, temp, gfd, pkg->name) == -1) {
1890  		unlinkat(gfd, temp, 0);
1891  		pkg_emit_errno("Unable to create group file %s", pkg->name);
1892  		return (EPKG_FATAL);
1893  	}
1894  	return (EPKG_OK);
1895  }
1896  
1897  int
1898  pkg_add_fromdir(struct pkg *pkg, const char *src, struct pkgdb *db __unused)
1899  {
1900  	struct stat st;
1901  	struct pkg_dir *d = NULL;
1902  	struct pkg_file *f = NULL;
1903  	char target[MAXPATHLEN];
1904  	struct passwd *pw, pwent;
1905  	struct group *gr, grent;
1906  	int err, fd, fromfd;
1907  	int retcode;
1908  	hardlinks_t hardlinks = vec_init();
1909  	const char *path;
1910  	char buffer[1024];
1911  	size_t link_len;
1912  	bool install_as_user;
1913  	tempdirs_t tempdirs = vec_init();
1914  	struct pkg_add_context context;
1915  
1916  	memset(&context, 0, sizeof(context));
1917  
1918  	install_as_user = (getenv("INSTALL_AS_USER") != NULL);
1919  
1920  	fromfd = open(src, O_DIRECTORY);
1921  	if (fromfd == -1) {
1922  		pkg_fatal_errno("Unable to open source directory '%s'", src);
1923  	}
1924  	pkg_open_root_fd(pkg);
1925  	context.pkg = pkg;
1926  	context.rootfd = pkg->rootfd;
1927  
1928  	while (pkg_dirs(pkg, &d) == EPKG_OK) {
1929  		if (fstatat(fromfd, RELATIVE_PATH(d->path), &st, 0) == -1) {
1930  			close(fromfd);
1931  			pkg_fatal_errno("%s%s", src, d->path);
1932  		}
1933  		if (d->perm == 0)
1934  			d->perm = st.st_mode & ~S_IFMT;
1935  		if (d->uname != NULL) {
1936  			err = getpwnam_r(d->uname, &pwent, buffer,
1937  			    sizeof(buffer), &pw);
1938  			if (err != 0) {
1939  				pkg_emit_error("getpwnam_r(%s): %s", d->uname, strerror(err));
1940  				retcode = EPKG_FATAL;
1941  				goto cleanup;
1942  			}
1943  			d->uid = pwent.pw_uid;
1944  		} else {
1945  			d->uid = install_as_user ? st.st_uid : 0;
1946  		}
1947  		if (d->gname != NULL) {
1948  			err = getgrnam_r(d->gname, &grent, buffer,
1949  			    sizeof(buffer), &gr);
1950  			if (err != 0) {
1951  				pkg_emit_error("getgrnam_r(%s): %s", d->gname, strerror(err));
1952  				retcode = EPKG_FATAL;
1953  				goto cleanup;
1954  			}
1955  			d->gid = grent.gr_gid;
1956  		} else {
1957  			d->gid = st.st_gid;
1958  		}
1959  #ifdef HAVE_STRUCT_STAT_ST_MTIM
1960  		d->time[0] = st.st_atim;
1961  		d->time[1] = st.st_mtim;
1962  #else
1963  #if defined(_DARWIN_C_SOURCE) || defined(__APPLE__)
1964  		d->time[0] = st.st_atimespec;
1965  		d->time[1] = st.st_mtimespec;
1966  #else
1967  		d->time[0].tv_sec = st.st_atime;
1968  		d->time[0].tv_nsec = 0;
1969  		d->time[1].tv_sec = st.st_mtime;
1970  		d->time[1].tv_nsec = 0;
1971  #endif
1972  #endif
1973  
1974  		if (create_dir(&context, d, &tempdirs) == EPKG_FATAL) {
1975  			retcode = EPKG_FATAL;
1976  			goto cleanup;
1977  		}
1978  	}
1979  
1980  	while (pkg_files(pkg, &f) == EPKG_OK) {
1981  		if (match_ucl_lists(f->path,
1982  		    pkg_config_get("FILES_IGNORE_GLOB"),
1983  		    pkg_config_get("FILES_IGNORE_REGEX")))
1984  			continue;
1985  		if (fstatat(fromfd, RELATIVE_PATH(f->path), &st,
1986  		    AT_SYMLINK_NOFOLLOW) == -1) {
1987  			vec_free_and_free(&hardlinks, free);
1988  			close(fromfd);
1989  			pkg_fatal_errno("%s%s", src, f->path);
1990  		}
1991  		if (f->uname != NULL) {
1992  			err = getpwnam_r(f->uname, &pwent, buffer,
1993  			    sizeof(buffer), &pw);
1994  			if (err != 0) {
1995  				pkg_emit_error("getpwnam_r(%s): %s", f->uname, strerror(err));
1996  				retcode = EPKG_FATAL;
1997  				goto cleanup;
1998  			}
1999  			f->uid = pwent.pw_uid;
2000  		} else {
2001  			f->uid = install_as_user ? st.st_uid : 0;
2002  		}
2003  
2004  		if (f->gname != NULL) {
2005  			err = getgrnam_r(f->gname, &grent, buffer,
2006  			    sizeof(buffer), &gr);
2007  			if (err != 0) {
2008  				pkg_emit_error("getgrnam_r(%s): %s", f->gname, strerror(err));
2009  				retcode = EPKG_FATAL;
2010  				goto cleanup;
2011  			}
2012  			f->gid = grent.gr_gid;
2013  		} else {
2014  			f->gid = st.st_gid;
2015  		}
2016  
2017  		if (f->perm == 0)
2018  			f->perm = st.st_mode & ~S_IFMT;
2019  		if (f->uid == 0 && install_as_user)
2020  			f->uid = st.st_uid;
2021  #ifdef HAVE_STRUCT_STAT_ST_MTIM
2022  		f->time[0] = st.st_atim;
2023  		f->time[1] = st.st_mtim;
2024  #else
2025  #if defined(_DARWIN_C_SOURCE) || defined(__APPLE__)
2026  		f->time[0] = st.st_atimespec;
2027  		f->time[1] = st.st_mtimespec;
2028  #else
2029  		f->time[0].tv_sec = st.st_atime;
2030  		f->time[0].tv_nsec = 0;
2031  		f->time[1].tv_sec = st.st_mtime;
2032  		f->time[1].tv_nsec = 0;
2033  #endif
2034  #endif
2035  
2036  		if (S_ISLNK(st.st_mode)) {
2037  			if ((link_len = readlinkat(fromfd,
2038  			    RELATIVE_PATH(f->path), target,
2039  			    sizeof(target))) == -1) {
2040  				vec_free_and_free(&hardlinks, free);
2041  				close(fromfd);
2042  				pkg_fatal_errno("Unable to read symlinks "
2043  				    "'%s'", f->path);
2044  			}
2045  			target[link_len] = '\0';
2046  			if (create_symlinks(&context, f, target, &tempdirs) == EPKG_FATAL) {
2047  				retcode = EPKG_FATAL;
2048  				goto cleanup;
2049  			}
2050  		} else if (S_ISREG(st.st_mode)) {
2051  			if ((fd = openat(fromfd, RELATIVE_PATH(f->path),
2052  			    O_RDONLY)) == -1) {
2053  				vec_free_and_free(&hardlinks, free);
2054  				close(fromfd);
2055  				pkg_fatal_errno("Unable to open source file"
2056  				    " '%s'", RELATIVE_PATH(f->path));
2057  			}
2058  			path = NULL;
2059  			vec_foreach(hardlinks, i) {
2060  				struct hardlink *hit = hardlinks.d[i];
2061  				if (hit->ino == st.st_ino &&
2062  				    hit->dev == st.st_dev) {
2063  					path = hit->path;
2064  					break;
2065  				}
2066  			}
2067  			if (path != NULL) {
2068  				if (create_hardlink(&context, f, path, &tempdirs) == EPKG_FATAL) {
2069  					close(fd);
2070  					retcode = EPKG_FATAL;
2071  					goto cleanup;
2072  				}
2073  			} else {
2074  				if (create_regfile(&context, f, NULL, NULL, fd, NULL, &tempdirs) == EPKG_FATAL) {
2075  					close(fd);
2076  					retcode = EPKG_FATAL;
2077  					goto cleanup;
2078  				}
2079  				struct hardlink *h = xcalloc(1, sizeof(*h));
2080  				h->ino = st.st_ino;
2081  				h->dev = st.st_dev;
2082  				h->path = f->path;
2083  				vec_push(&hardlinks, h);
2084  			}
2085  			close(fd);
2086  		} else {
2087  			pkg_emit_error("Invalid file type");
2088  			retcode = EPKG_FATAL;
2089  			goto cleanup;
2090  		}
2091  	}
2092  
2093  	retcode = pkg_extract_finalize(pkg, &tempdirs);
2094  
2095  cleanup:
2096  	vec_free_and_free(&hardlinks, free);
2097  	close(fromfd);
2098  	return (retcode);
2099  }
2100  
2101  /*static bool
2102  belong_to_self(struct pkg_add_context *context, const char *path)
2103  {
2104  	struct pkgdb_it *it = NULL;
2105  	struct pkg *p = NULL;
2106  	if (context->db != NULL && (it = pkgdb_query_which(context->db, path, false)) != NULL) {
2107  		if (pkgdb_it_next(it, &p, PKG_LOAD_BASIC) != EPKG_OK) {
2108  			pkgdb_it_free(it);
2109  			fprintf(stderr, "mais non\n");
2110  			return (false);
2111  		}
2112  		pkgdb_it_free(it);
2113  		if (STREQ(p->uid, context->pkg->uid)) {
2114  			pkg_free(p);
2115  			fprintf(stderr, "mais oui\n");
2116  			return (true);
2117  		}
2118  		pkg_free(p);
2119  	}
2120  	fprintf(stderr, "mais nope\n");
2121  	return (false);
2122  }*/
2123  
2124  struct tempdir *
2125  open_tempdir(struct pkg_add_context *context, const char *path)
2126  {
2127  	struct stat st;
2128  	struct pkg *localpkg;
2129  	char walk[MAXPATHLEN];
2130  	char *dir;
2131  	size_t cnt = 0, len;
2132  	struct tempdir *t;
2133  	int rootfd;
2134  
2135  	rootfd = context->rootfd;
2136  	localpkg = context->localpkg;
2137  
2138  	strlcpy(walk, path, sizeof(walk));
2139  	while ((dir = strrchr(walk, '/')) != NULL) {
2140  		*dir = '\0';
2141  		cnt++;
2142  		/* accept symlinks pointing to directories */
2143  		len = strlen(walk);
2144  		if (len == 0 && cnt == 1)
2145  			break;
2146  		if (len > 0) {
2147  			int flag;
2148  
2149  			flag = localpkg == NULL ? 0 : AT_SYMLINK_NOFOLLOW;
2150  			if (fstatat(rootfd, RELATIVE_PATH(walk), &st,
2151  			    flag) == -1) {
2152  				/*
2153  				 * A hack to ensure that intermediate
2154  				 * directories not registered with a package are
2155  				 * still logged in the metalog.  This is not
2156  				 * really the right place, but to implement this
2157  				 * properly we need to clean up uses of
2158  				 * try_mkdir().
2159  				 */
2160  				metalog_add(PKG_METALOG_DIR,
2161  				    RELATIVE_PATH(walk),
2162  				    "root", "wheel", 0755, 0, NULL);
2163  				continue;
2164  			}
2165  			if (S_ISLNK(st.st_mode) &&
2166  			    localpkg != NULL &&
2167  			    pkghash_get(localpkg->filehash, walk) == NULL &&
2168  			    fstatat(rootfd, RELATIVE_PATH(walk), &st, 0) == -1)
2169  				continue;
2170  			if (S_ISDIR(st.st_mode) && cnt == 1)
2171  				break;
2172  			if (!S_ISDIR(st.st_mode))
2173  				continue;
2174  		}
2175  		*dir = '/';
2176  		t = xcalloc(1, sizeof(*t));
2177  		hidden_tempfile(t->temp, sizeof(t->temp), walk);
2178  		if (mkdirat(rootfd, RELATIVE_PATH(t->temp), 0755) == -1) {
2179  			pkg_errno("Failed to create temporary directory: %s", t->temp);
2180  			free(t);
2181  			return (NULL);
2182  		}
2183  
2184  		strlcpy(t->name, walk, sizeof(t->name));
2185  		t->len = strlen(t->name);
2186  		t->fd = openat(rootfd, RELATIVE_PATH(t->temp), O_DIRECTORY|O_CLOEXEC);
2187  		if (t->fd == -1) {
2188  			pkg_errno("Failed to open directory %s", t->temp);
2189  			free(t);
2190  			return (NULL);
2191  		}
2192  		return (t);
2193  	}
2194  	errno = 0;
2195  	return (NULL);
2196  }