2008-12-19 10:31:53 +01:00
|
|
|
/*-
|
2009-02-03 03:18:00 +01:00
|
|
|
* Copyright (c) 2008-2009 Juan Romero Pardines.
|
2008-12-19 10:31:53 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2008-12-20 23:55:15 +01:00
|
|
|
#include <libgen.h>
|
2008-12-27 12:56:51 +01:00
|
|
|
#include <unistd.h>
|
2008-12-19 10:31:53 +01:00
|
|
|
|
2008-12-21 23:57:02 +01:00
|
|
|
#include <xbps_api.h>
|
2009-02-05 14:46:09 +01:00
|
|
|
#include "util.h"
|
2008-12-19 10:31:53 +01:00
|
|
|
|
|
|
|
typedef struct repository_info {
|
|
|
|
const char *index_version;
|
|
|
|
const char *location_local;
|
|
|
|
const char *location_remote;
|
2008-12-22 15:21:29 +01:00
|
|
|
uint64_t total_pkgs;
|
2008-12-19 10:31:53 +01:00
|
|
|
} repo_info_t;
|
|
|
|
|
2008-12-23 12:28:21 +01:00
|
|
|
static bool sanitize_localpath(char *, const char *);
|
2008-12-27 12:56:51 +01:00
|
|
|
static prop_dictionary_t getrepolist_dict(const char *);
|
2008-12-19 10:31:53 +01:00
|
|
|
static bool pkgindex_getinfo(prop_dictionary_t, repo_info_t *);
|
|
|
|
static void usage(void);
|
|
|
|
|
2008-12-27 22:19:04 +01:00
|
|
|
static char *plist;
|
|
|
|
|
2008-12-19 10:31:53 +01:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2009-02-05 14:46:09 +01:00
|
|
|
printf("Usage: xbps-repo [options] [action] [arguments]\n\n"
|
2008-12-19 23:10:24 +01:00
|
|
|
" Available actions:\n"
|
2009-02-05 14:46:09 +01:00
|
|
|
" add, list, remove, search, show\n"
|
2008-12-27 12:56:51 +01:00
|
|
|
" Actions with arguments:\n"
|
2009-02-05 15:31:54 +01:00
|
|
|
" add\t\t<URI>\n"
|
2009-02-05 14:46:09 +01:00
|
|
|
" remove\t<URI>\n"
|
2008-12-27 12:56:51 +01:00
|
|
|
" search\t<string>\n"
|
|
|
|
" show\t<pkgname>\n"
|
|
|
|
" Options shared by all actions:\n"
|
|
|
|
" -r\t\t<rootdir>\n"
|
2008-12-19 23:10:24 +01:00
|
|
|
"\n"
|
2008-12-19 10:31:53 +01:00
|
|
|
" Examples:\n"
|
2009-02-05 14:46:09 +01:00
|
|
|
" $ xbps-repo add /path/to/directory\n"
|
|
|
|
" $ xbps-repo add http://www.location.org/xbps-repo\n"
|
|
|
|
" $ xbps-repo list\n"
|
|
|
|
" $ xbps-repo remove /path/to/directory\n"
|
|
|
|
" $ xbps-repo search klibc\n"
|
|
|
|
" $ xbps-repo show klibc\n");
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-19 10:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
pkgindex_getinfo(prop_dictionary_t dict, repo_info_t *ri)
|
|
|
|
{
|
2008-12-21 01:50:19 +01:00
|
|
|
assert(dict != NULL || ri != NULL);
|
2008-12-19 10:31:53 +01:00
|
|
|
|
|
|
|
if (!prop_dictionary_get_cstring_nocopy(dict,
|
|
|
|
"pkgindex-version", &ri->index_version))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!prop_dictionary_get_cstring_nocopy(dict,
|
|
|
|
"location-local", &ri->location_local))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* This one is optional, thus don't panic */
|
|
|
|
prop_dictionary_get_cstring_nocopy(dict, "location-remote",
|
|
|
|
&ri->location_remote);
|
|
|
|
|
|
|
|
if (!prop_dictionary_get_uint64(dict, "total-pkgs",
|
|
|
|
&ri->total_pkgs))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Reject empty repositories, how could this happen? :-) */
|
|
|
|
if (ri->total_pkgs <= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-20 05:49:41 +01:00
|
|
|
static prop_dictionary_t
|
2008-12-27 12:56:51 +01:00
|
|
|
getrepolist_dict(const char *root)
|
2008-12-20 05:49:41 +01:00
|
|
|
{
|
|
|
|
prop_dictionary_t dict;
|
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
xbps_set_rootdir(root);
|
|
|
|
|
2008-12-27 22:19:04 +01:00
|
|
|
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
|
|
|
|
if (plist == NULL)
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-23 12:28:21 +01:00
|
|
|
|
|
|
|
dict = prop_dictionary_internalize_from_file(plist);
|
2008-12-20 05:49:41 +01:00
|
|
|
if (dict == NULL) {
|
2008-12-21 02:20:21 +01:00
|
|
|
printf("ERROR: cannot find repository plist file (%s).\n",
|
2008-12-20 05:49:41 +01:00
|
|
|
strerror(errno));
|
2008-12-27 22:19:04 +01:00
|
|
|
free(plist);
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-20 05:49:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
2008-12-23 12:28:21 +01:00
|
|
|
static bool
|
|
|
|
sanitize_localpath(char *buf, const char *path)
|
2008-12-20 23:55:15 +01:00
|
|
|
{
|
2008-12-23 12:28:21 +01:00
|
|
|
char *dirnp, *basenp, *dir, *base, *tmp;
|
2008-12-27 22:56:47 +01:00
|
|
|
bool rv = false;
|
2008-12-20 23:55:15 +01:00
|
|
|
|
|
|
|
dir = strdup(path);
|
|
|
|
if (dir == NULL)
|
2008-12-23 12:28:21 +01:00
|
|
|
return false;
|
2008-12-20 23:55:15 +01:00
|
|
|
|
|
|
|
base = strdup(path);
|
2008-12-23 12:28:21 +01:00
|
|
|
if (base == NULL) {
|
|
|
|
free(dir);
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-20 23:55:15 +01:00
|
|
|
|
|
|
|
dirnp = dirname(dir);
|
|
|
|
if (strcmp(dirnp, ".") == 0)
|
2008-12-27 22:56:47 +01:00
|
|
|
goto out;
|
2008-12-20 23:55:15 +01:00
|
|
|
|
|
|
|
basenp = basename(base);
|
|
|
|
if (strcmp(basenp, base) == 0)
|
2008-12-27 22:56:47 +01:00
|
|
|
goto out;
|
2008-12-20 23:55:15 +01:00
|
|
|
|
2008-12-23 12:28:21 +01:00
|
|
|
tmp = strncpy(buf, dirnp, PATH_MAX - 1);
|
|
|
|
if (sizeof(*tmp) >= PATH_MAX)
|
2008-12-27 22:56:47 +01:00
|
|
|
goto out;
|
2008-12-20 23:55:15 +01:00
|
|
|
|
2008-12-23 12:28:21 +01:00
|
|
|
buf[strlen(buf) + 1] = '\0';
|
|
|
|
if (strcmp(dirnp, "/"))
|
|
|
|
strncat(buf, "/", 1);
|
|
|
|
strncat(buf, basenp, PATH_MAX - strlen(buf) - 1);
|
2008-12-27 22:56:47 +01:00
|
|
|
rv = true;
|
|
|
|
|
|
|
|
out:
|
2008-12-20 23:55:15 +01:00
|
|
|
free(dir);
|
|
|
|
free(base);
|
2008-12-22 05:00:15 +01:00
|
|
|
|
2008-12-27 22:56:47 +01:00
|
|
|
return rv;
|
2008-12-20 23:55:15 +01:00
|
|
|
}
|
|
|
|
|
2008-12-19 10:31:53 +01:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
prop_dictionary_t dict;
|
|
|
|
repo_info_t *rinfo = NULL;
|
2008-12-27 22:19:04 +01:00
|
|
|
char dpkgidx[PATH_MAX], *root = NULL;
|
2009-02-05 14:46:09 +01:00
|
|
|
int c;
|
2008-12-27 12:56:51 +01:00
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "r:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'r':
|
|
|
|
/* To specify the root directory */
|
2008-12-27 22:19:04 +01:00
|
|
|
root = optarg;
|
2008-12-27 12:56:51 +01:00
|
|
|
xbps_set_rootdir(root);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
2008-12-19 10:31:53 +01:00
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 1)
|
2008-12-19 10:31:53 +01:00
|
|
|
usage();
|
|
|
|
|
2009-02-05 14:46:09 +01:00
|
|
|
if (strcasecmp(argv[0], "add") == 0) {
|
2008-12-19 23:10:24 +01:00
|
|
|
/* Adds a new repository to the pool. */
|
2008-12-27 12:56:51 +01:00
|
|
|
if (argc != 2)
|
2008-12-19 23:10:24 +01:00
|
|
|
usage();
|
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
if (!sanitize_localpath(dpkgidx, argv[1]))
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-19 10:31:53 +01:00
|
|
|
|
2008-12-20 23:55:15 +01:00
|
|
|
/* Temp buffer to verify pkgindex file. */
|
2008-12-27 22:19:04 +01:00
|
|
|
plist = xbps_append_full_path(false, dpkgidx, XBPS_PKGINDEX);
|
|
|
|
if (plist == NULL)
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-19 10:31:53 +01:00
|
|
|
|
2008-12-27 22:19:04 +01:00
|
|
|
dict = prop_dictionary_internalize_from_file(plist);
|
2008-12-19 10:31:53 +01:00
|
|
|
if (dict == NULL) {
|
|
|
|
printf("Directory %s does not contain any "
|
2008-12-20 23:55:15 +01:00
|
|
|
"xbps pkgindex file.\n", dpkgidx);
|
2008-12-27 22:19:04 +01:00
|
|
|
free(plist);
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-19 10:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rinfo = malloc(sizeof(*rinfo));
|
2008-12-22 05:00:15 +01:00
|
|
|
if (rinfo == NULL) {
|
2008-12-23 12:28:21 +01:00
|
|
|
prop_object_release(dict);
|
2008-12-27 22:19:04 +01:00
|
|
|
free(plist);
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-22 05:00:15 +01:00
|
|
|
}
|
2008-12-19 10:31:53 +01:00
|
|
|
|
|
|
|
if (!pkgindex_getinfo(dict, rinfo)) {
|
2008-12-27 22:19:04 +01:00
|
|
|
printf("'%s' is incomplete.\n", plist);
|
2008-12-23 12:28:21 +01:00
|
|
|
prop_object_release(dict);
|
2008-12-20 23:55:15 +01:00
|
|
|
free(rinfo);
|
2008-12-27 22:19:04 +01:00
|
|
|
free(plist);
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-19 10:31:53 +01:00
|
|
|
}
|
|
|
|
|
2008-12-20 23:55:15 +01:00
|
|
|
if (!xbps_register_repository(dpkgidx)) {
|
2008-12-19 10:31:53 +01:00
|
|
|
printf("ERROR: couldn't register repository (%s)\n",
|
|
|
|
strerror(errno));
|
2008-12-23 12:28:21 +01:00
|
|
|
prop_object_release(dict);
|
2008-12-20 23:55:15 +01:00
|
|
|
free(rinfo);
|
2008-12-27 22:19:04 +01:00
|
|
|
free(plist);
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-19 10:31:53 +01:00
|
|
|
}
|
|
|
|
|
2008-12-22 15:21:29 +01:00
|
|
|
printf("Added repository at %s (%s) with %ju packages.\n",
|
2008-12-19 10:31:53 +01:00
|
|
|
rinfo->location_local, rinfo->index_version,
|
|
|
|
rinfo->total_pkgs);
|
2008-12-23 13:05:01 +01:00
|
|
|
|
2008-12-23 12:28:21 +01:00
|
|
|
prop_object_release(dict);
|
2008-12-19 10:31:53 +01:00
|
|
|
free(rinfo);
|
2008-12-27 22:19:04 +01:00
|
|
|
free(plist);
|
2008-12-19 23:10:24 +01:00
|
|
|
|
2009-02-05 14:46:09 +01:00
|
|
|
} else if (strcasecmp(argv[0], "list") == 0) {
|
2008-12-19 23:10:24 +01:00
|
|
|
/* Lists all repositories registered in pool. */
|
2008-12-27 12:56:51 +01:00
|
|
|
if (argc != 1)
|
2008-12-19 23:10:24 +01:00
|
|
|
usage();
|
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
dict = getrepolist_dict(root);
|
2008-12-26 10:13:03 +01:00
|
|
|
(void)xbps_callback_array_iter_in_dict(dict,
|
2009-02-05 15:31:54 +01:00
|
|
|
"repository-list", list_strings_in_array, NULL);
|
2008-12-23 12:28:21 +01:00
|
|
|
prop_object_release(dict);
|
2008-12-27 22:19:04 +01:00
|
|
|
free(plist);
|
2008-12-19 23:10:24 +01:00
|
|
|
|
2009-02-05 14:46:09 +01:00
|
|
|
} else if ((strcasecmp(argv[0], "rm") == 0) ||
|
|
|
|
(strcasecmp(argv[0], "remove") == 0)) {
|
2008-12-20 23:55:15 +01:00
|
|
|
/* Remove a repository from the pool. */
|
2008-12-27 12:56:51 +01:00
|
|
|
if (argc != 2)
|
2008-12-20 23:55:15 +01:00
|
|
|
usage();
|
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
if (!sanitize_localpath(dpkgidx, argv[1]))
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-20 23:55:15 +01:00
|
|
|
|
|
|
|
if (!xbps_unregister_repository(dpkgidx)) {
|
|
|
|
if (errno == ENODEV)
|
|
|
|
printf("Repository '%s' not actually "
|
|
|
|
"registered.\n", dpkgidx);
|
|
|
|
else
|
|
|
|
printf("ERROR: couldn't unregister "
|
|
|
|
"repository (%s)\n", strerror(errno));
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-20 23:55:15 +01:00
|
|
|
}
|
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
} else if (strcasecmp(argv[0], "search") == 0) {
|
2008-12-21 05:32:13 +01:00
|
|
|
/* Search for a package by looking at short_desc. */
|
2008-12-27 12:56:51 +01:00
|
|
|
if (argc != 2)
|
2008-12-21 05:32:13 +01:00
|
|
|
usage();
|
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
dict = getrepolist_dict(root);
|
2008-12-26 10:13:03 +01:00
|
|
|
(void)xbps_callback_array_iter_in_dict(dict,
|
2009-02-05 15:31:54 +01:00
|
|
|
"repository-list", search_string_in_pkgs, argv[1]);
|
2008-12-23 12:28:21 +01:00
|
|
|
prop_object_release(dict);
|
2008-12-27 22:19:04 +01:00
|
|
|
free(plist);
|
2008-12-21 05:32:13 +01:00
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
} else if (strcasecmp(argv[0], "show") == 0) {
|
2008-12-19 23:10:24 +01:00
|
|
|
/* Shows info about a binary package. */
|
2008-12-27 12:56:51 +01:00
|
|
|
if (argc != 2)
|
2008-12-19 23:10:24 +01:00
|
|
|
usage();
|
|
|
|
|
2008-12-27 12:56:51 +01:00
|
|
|
dict = getrepolist_dict(root);
|
2008-12-26 10:13:03 +01:00
|
|
|
if (xbps_callback_array_iter_in_dict(dict, "repository-list",
|
2009-02-05 15:31:54 +01:00
|
|
|
show_pkg_info_from_repolist, argv[1]) != 0) {
|
2008-12-23 13:05:01 +01:00
|
|
|
prop_object_release(dict);
|
2008-12-27 22:19:04 +01:00
|
|
|
free(plist);
|
2008-12-20 23:55:15 +01:00
|
|
|
printf("ERROR: unable to locate package '%s'.\n",
|
2008-12-27 12:56:51 +01:00
|
|
|
argv[1]);
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-12-20 05:49:41 +01:00
|
|
|
}
|
2008-12-23 12:28:21 +01:00
|
|
|
prop_object_release(dict);
|
2008-12-27 22:19:04 +01:00
|
|
|
free(plist);
|
2008-12-20 23:55:15 +01:00
|
|
|
|
2008-12-19 23:10:24 +01:00
|
|
|
} else {
|
|
|
|
usage();
|
2008-12-19 10:31:53 +01:00
|
|
|
}
|
|
|
|
|
2008-12-29 15:56:40 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2008-12-19 10:31:53 +01:00
|
|
|
}
|