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) if (argc < 1)
usage(); usage();
if ((rv = xbps_repository_pool_init()) != 0) { rv = xbps_repository_pool_init();
if (rv != ENOENT) { if (rv != 0 && rv != ENOENT) {
fprintf(stderr, fprintf(stderr, "E: cannot get repository list pool! %s\n",
"E: cannot get repository list pool! %s\n", strerror(rv));
strerror(rv)); exit(EXIT_FAILURE);
exit(EXIT_FAILURE);
}
} }
if (strcasecmp(argv[0], "add") == 0) { if (strcasecmp(argv[0], "add") == 0) {

View File

@ -40,6 +40,44 @@ struct repoinfo {
uint64_t totalpkgs; 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 * static struct repoinfo *
pkgindex_verify(const char *plist, const char *uri) pkgindex_verify(const char *plist, const char *uri)
{ {
@ -102,10 +140,10 @@ out:
int int
unregister_repository(const char *uri) unregister_repository(const char *uri)
{ {
char idxstr[PATH_MAX]; const char *idxstr = NULL;
int rv = 0; int rv = 0;
if (!realpath(uri, idxstr)) if ((idxstr = sanitize_url(uri)) == NULL)
return errno; return errno;
if ((rv = xbps_repository_unregister(idxstr)) != 0) { if ((rv = xbps_repository_unregister(idxstr)) != 0) {
@ -124,14 +162,15 @@ int
register_repository(const char *uri) register_repository(const char *uri)
{ {
struct repoinfo *rpi = NULL; struct repoinfo *rpi = NULL;
char *metadir, *plist, idxstr[PATH_MAX]; const char *idxstr = NULL;
char *metadir, *plist;
int rv = 0; int rv = 0;
if (xbps_check_is_repo_string_remote(uri)) { if ((idxstr = sanitize_url(uri)) == NULL)
if (!realpath(uri, idxstr)) return errno;
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); rv = xbps_repository_sync_pkg_index(idxstr);
if (rv == -1) { if (rv == -1) {
fprintf(stderr, fprintf(stderr,
@ -146,9 +185,6 @@ register_repository(const char *uri)
plist = xbps_get_pkg_index_plist(idxstr); plist = xbps_get_pkg_index_plist(idxstr);
} else { } else {
if (!realpath(uri, idxstr))
return errno;
/* /*
* Create metadir if necessary. * Create metadir if necessary.
*/ */
@ -171,7 +207,7 @@ register_repository(const char *uri)
if (plist == NULL) if (plist == NULL)
return errno; return errno;
if ((rpi = pkgindex_verify(plist, uri)) == NULL) if ((rpi = pkgindex_verify(plist, idxstr)) == NULL)
goto out; goto out;
rv = xbps_repository_register(idxstr); rv = xbps_repository_register(idxstr);