xbps-repo: put back sanitize_url() and improve the implementation.

This commit is contained in:
Juan RP 2010-05-20 14:45:12 +02:00
parent 3038fbed2d
commit 8e764dca60
2 changed files with 52 additions and 18 deletions

View File

@ -99,13 +99,11 @@ main(int argc, char **argv)
if (argc < 1)
usage();
if ((rv = xbps_repository_pool_init()) != 0) {
if (rv != ENOENT) {
fprintf(stderr,
"E: cannot get repository list pool! %s\n",
strerror(rv));
exit(EXIT_FAILURE);
}
rv = xbps_repository_pool_init();
if (rv != 0 && rv != ENOENT) {
fprintf(stderr, "E: cannot get repository list pool! %s\n",
strerror(rv));
exit(EXIT_FAILURE);
}
if (strcasecmp(argv[0], "add") == 0) {

View File

@ -40,6 +40,44 @@ struct repoinfo {
uint64_t totalpkgs;
};
static const char *
sanitize_url(const char *path)
{
static char buf[PATH_MAX];
const char *res = NULL;
char *dirnp, *basenp, *dir, *base;
int r = 0;
memset(&buf, 0, sizeof(buf));
if ((dir = strdup(path)) == NULL)
return NULL;
if ((base = strdup(path)) == NULL) {
free(dir);
return NULL;
}
dirnp = dirname(dir);
if (strcmp(dirnp, ".") == 0)
goto out;
basenp = basename(base);
if (strcmp(basenp, base) == 0)
goto out;
r = snprintf(buf, sizeof(buf) - 1, "%s/%s", dirnp, basenp);
if (r == -1 || r >= (int)sizeof(buf) - 1)
goto out;
res = buf;
out:
free(dir);
free(base);
return res;
}
static struct repoinfo *
pkgindex_verify(const char *plist, const char *uri)
{
@ -102,10 +140,10 @@ out:
int
unregister_repository(const char *uri)
{
char idxstr[PATH_MAX];
const char *idxstr = NULL;
int rv = 0;
if (!realpath(uri, idxstr))
if ((idxstr = sanitize_url(uri)) == NULL)
return errno;
if ((rv = xbps_repository_unregister(idxstr)) != 0) {
@ -124,14 +162,15 @@ int
register_repository(const char *uri)
{
struct repoinfo *rpi = NULL;
char *metadir, *plist, idxstr[PATH_MAX];
const char *idxstr = NULL;
char *metadir, *plist;
int rv = 0;
if (xbps_check_is_repo_string_remote(uri)) {
if (!realpath(uri, idxstr))
return errno;
if ((idxstr = sanitize_url(uri)) == NULL)
return errno;
printf("Fetching remote package index at %s...\n", uri);
if (xbps_check_is_repo_string_remote(idxstr)) {
printf("Fetching remote package index at %s...\n", idxstr);
rv = xbps_repository_sync_pkg_index(idxstr);
if (rv == -1) {
fprintf(stderr,
@ -146,9 +185,6 @@ register_repository(const char *uri)
plist = xbps_get_pkg_index_plist(idxstr);
} else {
if (!realpath(uri, idxstr))
return errno;
/*
* Create metadir if necessary.
*/
@ -171,7 +207,7 @@ register_repository(const char *uri)
if (plist == NULL)
return errno;
if ((rpi = pkgindex_verify(plist, uri)) == NULL)
if ((rpi = pkgindex_verify(plist, idxstr)) == NULL)
goto out;
rv = xbps_repository_register(idxstr);