/ src / repo.c
repo.c
  1  /*-
  2   * Copyright (c) 2011-2024 Baptiste Daroussin <bapt@FreeBSD.org>
  3   *
  4   * Redistribution and use in source and binary forms, with or without
  5   * modification, are permitted provided that the following conditions
  6   * are met:
  7   * 1. Redistributions of source code must retain the above copyright
  8   *    notice, this list of conditions and the following disclaimer
  9   *    in this position and unchanged.
 10   * 2. Redistributions in binary form must reproduce the above copyright
 11   *    notice, this list of conditions and the following disclaimer in the
 12   *    documentation and/or other materials provided with the distribution.
 13   *
 14   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 15   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 16   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 17   * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 18   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 19   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 20   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 21   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 22   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 23   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 24   */
 25  
 26  #include <bsd_compat.h>
 27  #include <getopt.h>
 28  #include <signal.h>
 29  #include <stdio.h>
 30  #include <string.h>
 31  
 32  #if __has_include(<readpassphrase.h>)
 33  #include <readpassphrase.h>
 34  #elif __has_include(<bsd/readpassphrase.h>)
 35  #include <bsd/readpassphrase.h>
 36  #else
 37  #include "readpassphrase_compat.h"
 38  #endif
 39  
 40  #include <unistd.h>
 41  
 42  #include <pkg.h>
 43  #include "pkgcli.h"
 44  
 45  void
 46  usage_repo(void)
 47  {
 48  	fprintf(stderr, "Usage: pkg repo [-hlqs] [-m metafile] [-o output-dir] <repo-path> "
 49  	    "[rsa:<rsa-key>|signing_command: <the command>]\n\n");
 50  	fprintf(stderr, "For more information see 'pkg help repo'.\n");
 51  }
 52  
 53  int
 54  exec_repo(int argc, char **argv)
 55  {
 56  	int	 ch;
 57  	bool	 hash = false;
 58  	bool	 hash_symlink = false;
 59  	struct pkg_repo_create *prc = pkg_repo_create_new();
 60  
 61  	hash = (getenv("PKG_REPO_HASH") != NULL);
 62  	hash_symlink = (getenv("PKG_REPO_SYMLINK") != NULL);
 63  
 64  	struct option longopts[] = {
 65  		{ "groups",	required_argument,	NULL,	'g' },
 66  		{ "hash",	no_argument,		NULL,	'h' },
 67  		{ "list-files", no_argument,		NULL,	'l' },
 68  		{ "meta-file",	required_argument,	NULL,	'm' },
 69  		{ "output-dir", required_argument,	NULL,	'o' },
 70  		{ "quiet",	no_argument,		NULL,	'q' },
 71  		{ "symlink",	no_argument,		NULL,	's' },
 72  		{ NULL,		0,			NULL,	0   },
 73  	};
 74  
 75  	while ((ch = getopt_long(argc, argv, "+hg:lo:qm:s", longopts, NULL)) != -1) {
 76  		switch (ch) {
 77  		case 'g':
 78  			pkg_repo_create_set_groups(prc, optarg);
 79  			break;
 80  		case 'h':
 81  			hash = true;
 82  			break;
 83  		case 'l':
 84  			pkg_repo_create_set_create_filelist(prc, true);
 85  			break;
 86  		case 'o':
 87  			pkg_repo_create_set_output_dir(prc, optarg);
 88  			break;
 89  		case 'q':
 90  			quiet = true;
 91  			break;
 92  		case 'm':
 93  			pkg_repo_create_set_metafile(prc, optarg);
 94  			break;
 95  		case 's':
 96  			hash_symlink = true;
 97  			break;
 98  		default:
 99  			usage_repo();
100  			return (EXIT_FAILURE);
101  		}
102  	}
103  	argc -= optind;
104  	argv += optind;
105  
106  	if (argc < 1) {
107  		pkg_repo_create_free(prc);
108  		usage_repo();
109  		return (EXIT_FAILURE);
110  	}
111  
112  	pkg_repo_create_set_hash(prc, hash);
113  	pkg_repo_create_set_hash_symlink(prc, hash_symlink);
114  	pkg_repo_create_set_sign(prc, argv + 1, argc - 1, password_cb);
115  
116  	if (argc > 2 && !STREQ(argv[1], "signing_command:")) {
117  		pkg_repo_create_free(prc);
118  		usage_repo();
119  		return (EXIT_FAILURE);
120  	}
121  
122  	if (pkg_repo_create(prc, argv[0]) != EPKG_OK) {
123  		printf("Cannot create repository catalogue\n");
124  		pkg_repo_create_free(prc);
125  		return (EXIT_FAILURE);
126  	}
127  
128  	pkg_repo_create_free(prc);
129  	return (EXIT_SUCCESS);
130  }