Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
Prepare pkgng for multiple remote repositories support
Marin Atanasov Nikolov committed 14 years ago
commit 1c4494284da6ca9827a70ec3ce69e00e54c80e17
parent 0c21ced34149f5d95cf4d560d4f65dbc12697a80
3 files changed +189 -0
modified libpkg/pkg.h
@@ -12,6 +12,7 @@ struct pkg_dir;
struct pkg_conflict;
struct pkg_script;
struct pkg_option;
+
struct pkg_remote_repo;

struct pkgdb;
struct pkgdb_it;
@@ -571,6 +572,42 @@ int pkg_delete(struct pkg *pkg, struct pkgdb *db, int force);
int pkg_repo_fetch(struct pkg *pkg);

/**
+
 * Initializes the remote repositories
+
 */
+
void pkg_remote_repo_init(void);
+

+
/**
+
 * Loads the remote repositories from file
+
 * @return EPKG_OK on success, and EPKG_FATAL on error
+
 */
+
int pkg_remote_repo_load(void);
+

+
/**
+
 * Adds a remote repository
+
 * @param name Name for the repository
+
 * @param url URL of the remote repository
+
 * @return EPKG_OK on success, EPKG_FATAL on error
+
 */
+
int pkg_remote_repo_add(const char *name, const char *url);
+

+
/**
+
 * Get the next repository from the tail
+
 * @return Next repository in the tail
+
 */
+
struct pkg_remote_repo * pkg_remote_repo_next(void);
+

+
/**
+
 * Free the memory used for remote repositories
+
 */
+
void pkg_remote_repo_free(void);
+

+
/**
+
 * Resets the tails and sets the next
+
 * element of the tail to be first one
+
 */
+
void pkg_remote_repo_reset(void);
+

+
/**
 * Get the value of a configuration key
 */
const char * pkg_config(const char *key);
modified libpkg/pkg_private.h
@@ -92,6 +92,12 @@ struct pkg_jobs_node {
	LIST_ENTRY(pkg_jobs_node) entries;
};

+
struct pkg_remote_repo {
+
	char *name;
+
	char *url;
+
	STAILQ_ENTRY(pkg_remote_repo) entries;
+
};
+

int pkg_open2(struct pkg **p, struct archive **a, struct archive_entry **ae, const char *path);
void pkg_freedeps(struct pkg *pkg);
void pkg_freerdeps(struct pkg *pkg);
modified libpkg/pkg_repo.c
@@ -1,3 +1,5 @@
+
#include <err.h>
+
#include <ctype.h>
#include <assert.h>
#include <errno.h>
#include <libgen.h>
@@ -8,6 +10,18 @@
#include "pkg_event.h"
#include "pkg_private.h"

+
/*
+
 * Head of the remote repository tail and initializer
+
 */
+
static STAILQ_HEAD(remote_repo, pkg_remote_repo) rrh;
+
static int rrh_init;
+

+
/**
+
 * Trims any leading and trailing spaces from a string
+
 * @todo Find a better way to deal with such cases :)
+
 */
+
static char * trim_spaces(char *str);
+

int
pkg_repo_fetch(struct pkg *pkg)
{
@@ -58,3 +72,135 @@ pkg_repo_fetch(struct pkg *pkg)

	return (retcode);
}
+

+
void
+
pkg_remote_repo_init(void)
+
{
+
        STAILQ_INIT(&rrh);
+
        rrh_init = 0;
+
}
+

+
int
+
pkg_remote_repo_load(void)
+
{
+
        FILE *fp;
+
        char buf[MAXPATHLEN];
+
        char *repo_name, *repo_url, *tmp;
+
        unsigned int count = 0, line = 0;
+

+
        if ((fp = fopen("/etc/pkg/repositories", "r")) == NULL) {
+
		EMIT_ERRNO("fopen", "/etc/pkg/repositories");
+
		return(EPKG_FATAL);
+
	}
+

+
        while (fgets(buf, MAXPATHLEN, fp)) {
+
                if (buf[0] == '\n' || buf[0] == '#' || buf[0] == ';')
+
                        continue;
+

+
		repo_name = repo_url = tmp = NULL;
+

+
                count = 0;
+
                line++;
+

+
                buf[strlen(buf) - 1] = '\0';
+
                tmp = buf;
+

+
                repo_name = strsep(&tmp, "=");
+
                repo_url  = strsep(&tmp, "=");
+

+
                if ((repo_name == NULL) || (repo_url == NULL)) {
+
                        warnx("Repository name or URL is missing at line %d (ignoring repository)", line);
+
                        continue;
+
                }
+

+
                repo_name = trim_spaces(repo_name);
+
                repo_url  = trim_spaces(repo_url);
+

+
                if (tmp) {
+
                        warnx("Wrong repository format at line %d (ignoring repository)", line);
+
                        continue;
+
                }
+
                
+
                count++;
+

+
                pkg_remote_repo_add(repo_name, repo_url);
+
        }
+

+
        fclose(fp);
+

+
        return(EPKG_OK);
+
}
+

+
int
+
pkg_remote_repo_add(const char *name, const char *url)
+
{
+
        struct pkg_remote_repo *newrepo;
+

+
        if ((newrepo = calloc(1, sizeof(struct pkg_remote_repo))) == NULL) {
+
                EMIT_ERRNO("calloc", "");
+
		return(EPKG_FATAL);
+
        }
+

+
        newrepo->name = strdup(name);
+
        newrepo->url  = strdup(url);
+

+
        assert(newrepo->name != NULL && newrepo->url != NULL);
+
        
+
        STAILQ_INSERT_TAIL(&rrh, newrepo, entries);
+

+
        return(EPKG_OK);
+
}
+

+
struct pkg_remote_repo *
+
pkg_remote_repo_next(void)
+
{
+
        static struct pkg_remote_repo *next;
+
        
+
        if (rrh_init == 0) {
+
                next = STAILQ_FIRST(&rrh);
+
                rrh_init = 1;
+
        } else
+
                next = STAILQ_NEXT(next, entries);
+

+
        return(next);
+
}
+

+
void
+
pkg_remote_repo_free(void)
+
{
+
        struct pkg_remote_repo *n1, *n2;
+

+
        n1 = STAILQ_FIRST(&rrh);
+
        while (n1 != NULL) {
+
                n2 = STAILQ_NEXT(n1, entries);
+
                
+
                if (n1->name != NULL)
+
                        free(n1->name);
+
                if (n1->url != NULL)
+
                        free(n1->url);
+

+
                free(n1);
+
                n1 = n2;
+
        }
+
}
+

+
void
+
pkg_remote_repo_reset(void)
+
{
+
        rrh_init = 0;
+
}
+

+
char *
+
trim_spaces(char *str)
+
{
+
        char *buf;
+
        int   i, len;
+

+
        buf = str;
+
        len = strlen(str);
+
                       
+
        for (i = 0; i < len && isspace(str[i]); i++, buf++) ;
+
                for (i = len - 1; i >= 0 && isspace(str[i]); str[i] = '\0', i--) ;
+

+
        return(buf);
+
}