1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-23 00:08:50 -05:00

Remove pmserver_t abstraction

Remove what was a pretty weird abstraction in the libalpm backend. Instead
of parsing server URLs as we get them (of which we don't usually use more
than a handful anyway), wait until they are actually used, which allows us
to store them as a simple string list instead. This allows us to remove a
lot of code, and will greatly simplify the continuing refactoring of the
download code.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2008-01-22 22:49:45 -06:00
parent 81a2a06818
commit f159203f6f
9 changed files with 38 additions and 214 deletions

View File

@ -36,7 +36,6 @@ libalpm_la_SOURCES = \
md5.h md5.c \ md5.h md5.c \
package.h package.c \ package.h package.c \
remove.h remove.c \ remove.h remove.c \
server.h server.c \
sync.h sync.c \ sync.h sync.c \
trans.h trans.c \ trans.h trans.c \
util.h util.c util.h util.c

View File

@ -45,7 +45,6 @@ typedef struct __pmdb_t pmdb_t;
typedef struct __pmpkg_t pmpkg_t; typedef struct __pmpkg_t pmpkg_t;
typedef struct __pmdelta_t pmdelta_t; typedef struct __pmdelta_t pmdelta_t;
typedef struct __pmgrp_t pmgrp_t; typedef struct __pmgrp_t pmgrp_t;
typedef struct __pmserver_t pmserver_t;
typedef struct __pmtrans_t pmtrans_t; typedef struct __pmtrans_t pmtrans_t;
typedef struct __pmsyncpkg_t pmsyncpkg_t; typedef struct __pmsyncpkg_t pmsyncpkg_t;
typedef struct __pmdepend_t pmdepend_t; typedef struct __pmdepend_t pmdepend_t;

View File

@ -41,7 +41,6 @@
#include "log.h" #include "log.h"
#include "util.h" #include "util.h"
#include "error.h" #include "error.h"
#include "server.h"
#include "dload.h" #include "dload.h"
#include "handle.h" #include "handle.h"
#include "cache.h" #include "cache.h"
@ -192,14 +191,9 @@ int SYMEXPORT alpm_db_setserver(pmdb_t *db, const char *url)
} }
if(url && strlen(url)) { if(url && strlen(url)) {
pmserver_t *server; db->servers = alpm_list_add(db->servers, strdup(url));
if((server = _alpm_server_new(url)) == NULL) { _alpm_log(PM_LOG_DEBUG, "adding new server URL to database '%s': %s\n",
/* pm_errno is set by _alpm_server_new */ db->treename, url);
return(-1);
}
db->servers = alpm_list_add(db->servers, server);
_alpm_log(PM_LOG_DEBUG, "adding new server to database '%s': protocol '%s', server '%s', path '%s'\n",
db->treename, server->s_url->scheme, server->s_url->host, server->s_url->doc);
} else { } else {
FREELIST(db->servers); FREELIST(db->servers);
_alpm_log(PM_LOG_DEBUG, "serverlist flushed for '%s'\n", db->treename); _alpm_log(PM_LOG_DEBUG, "serverlist flushed for '%s'\n", db->treename);
@ -317,8 +311,7 @@ const char SYMEXPORT *alpm_db_get_name(const pmdb_t *db)
*/ */
const char SYMEXPORT *alpm_db_get_url(const pmdb_t *db) const char SYMEXPORT *alpm_db_get_url(const pmdb_t *db)
{ {
char path[PATH_MAX]; char *url;
pmserver_t *s;
ALPM_LOG_FUNC; ALPM_LOG_FUNC;
@ -326,10 +319,9 @@ const char SYMEXPORT *alpm_db_get_url(const pmdb_t *db)
ASSERT(handle != NULL, return(NULL)); ASSERT(handle != NULL, return(NULL));
ASSERT(db != NULL, return(NULL)); ASSERT(db != NULL, return(NULL));
s = (pmserver_t*)db->servers->data; url = (char*)db->servers->data;
snprintf(path, PATH_MAX, "%s://%s%s", s->s_url->scheme, s->s_url->host, s->s_url->doc); return(url);
return strdup(path);
} }
@ -451,17 +443,12 @@ pmdb_t *_alpm_db_new(const char *dbpath, const char *treename)
void _alpm_db_free(pmdb_t *db) void _alpm_db_free(pmdb_t *db)
{ {
alpm_list_t *tmp;
ALPM_LOG_FUNC; ALPM_LOG_FUNC;
/* cleanup pkgcache */ /* cleanup pkgcache */
_alpm_db_free_pkgcache(db); _alpm_db_free_pkgcache(db);
/* cleanup server list */ /* cleanup server list */
for(tmp = db->servers; tmp; tmp = alpm_list_next(tmp)) { FREELIST(db->servers);
_alpm_server_free(tmp->data);
}
alpm_list_free(db->servers);
FREE(db->path); FREE(db->path);
FREE(db); FREE(db);

View File

@ -34,48 +34,37 @@
#include "util.h" #include "util.h"
#include "error.h" #include "error.h"
#include "handle.h" #include "handle.h"
#include "server.h"
/* remove filename info from "s_url->doc" and return it */
static char *strip_filename(pmserver_t *server)
{
char *p = NULL, *fname = NULL;
if(!server) {
return(NULL);
}
p = strrchr(server->s_url->doc, '/');
if(p && *(++p)) {
fname = strdup(p);
_alpm_log(PM_LOG_DEBUG, "stripping '%s' from '%s'\n",
fname, server->s_url->doc);
*p = 0;
}
/* s_url->doc now contains ONLY path information. return value
* if the file information from the original URL */
return(fname);
}
/* Return a 'struct url' for this server, for downloading 'filename'. */ /* Return a 'struct url' for this server, for downloading 'filename'. */
static struct url *url_for_file(pmserver_t *server, const char *filename) static struct url *url_for_file(const char *url, const char *filename)
{ {
struct url *ret = NULL; struct url *ret = NULL;
char *doc = NULL; char *buf = NULL;
int doclen = 0; int len;
doclen = strlen(server->s_url->doc) + strlen(filename) + 2; /* print url + filename into a buffer */
CALLOC(doc, doclen, sizeof(char), RET_ERR(PM_ERR_MEMORY, NULL)); len = strlen(url) + strlen(filename) + 2;
CALLOC(buf, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, NULL));
snprintf(buf, len, "%s/%s", url, filename);
ret = downloadParseURL(buf);
FREE(buf);
if(!ret) {
_alpm_log(PM_LOG_ERROR, _("url '%s' is invalid\n"), buf);
RET_ERR(PM_ERR_SERVER_BAD_URL, NULL);
}
/* if no URL scheme specified, assume HTTP */
if(strlen(ret->scheme) == 0) {
_alpm_log(PM_LOG_WARNING, _("url scheme not specified, assuming HTTP\n"));
strcpy(ret->scheme, SCHEME_HTTP);
}
/* add a user & password for anonymous FTP */
if(strcmp(ret->scheme,SCHEME_FTP) == 0 && strlen(ret->user) == 0) {
strcpy(ret->user, "anonymous");
strcpy(ret->pwd, "libalpm@guest");
}
snprintf(doc, doclen, "%s/%s", server->s_url->doc, filename);
ret = downloadMakeURL(server->s_url->scheme,
server->s_url->host,
server->s_url->port,
doc,
server->s_url->user,
server->s_url->pwd);
FREE(doc);
return(ret); return(ret);
} }
@ -150,7 +139,7 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
} }
for(i = servers; i && !done; i = i->next) { for(i = servers; i && !done; i = i->next) {
pmserver_t *server = i->data; const char *server = i->data;
/* get each file in the list */ /* get each file in the list */
for(lp = files; lp; lp = lp->next) { for(lp = files; lp; lp = lp->next) {
@ -393,43 +382,23 @@ int _alpm_downloadfiles_forreal(alpm_list_t *servers, const char *localpath,
*/ */
char SYMEXPORT *alpm_fetch_pkgurl(const char *url) char SYMEXPORT *alpm_fetch_pkgurl(const char *url)
{ {
pmserver_t *server; /* TODO this method will not work at all right now */
char *filename, *filepath; char *filename, *filepath;
const char *cachedir; const char *cachedir;
ALPM_LOG_FUNC; ALPM_LOG_FUNC;
if(strstr(url, "://") == NULL) { filename = NULL;
_alpm_log(PM_LOG_DEBUG, "Invalid URL passed to alpm_fetch_pkgurl\n");
return(NULL);
}
server = _alpm_server_new(url);
if(!server) {
return(NULL);
}
/* strip path information from the filename */
filename = strip_filename(server);
if(!filename) {
_alpm_log(PM_LOG_ERROR, _("URL does not contain a file for download\n"));
return(NULL);
}
/* find a valid cache dir to download to */ /* find a valid cache dir to download to */
cachedir = _alpm_filecache_setup(); cachedir = _alpm_filecache_setup();
/* TODO this seems like needless complexity just to download one file */
alpm_list_t *servers = alpm_list_add(NULL, server);
/* download the file */ /* download the file */
if(_alpm_download_single_file(filename, servers, cachedir, 0, NULL)) { if(_alpm_download_single_file(NULL, NULL, cachedir, 0, NULL)) {
_alpm_log(PM_LOG_WARNING, _("failed to download %s\n"), url); _alpm_log(PM_LOG_WARNING, _("failed to download %s\n"), url);
return(NULL); return(NULL);
} }
_alpm_log(PM_LOG_DEBUG, "successfully downloaded %s\n", filename); _alpm_log(PM_LOG_DEBUG, "successfully downloaded %s\n", url);
alpm_list_free(servers);
_alpm_server_free(server);
/* we should be able to find the file the second time around */ /* we should be able to find the file the second time around */
filepath = _alpm_filecache_find(filename); filepath = _alpm_filecache_find(filename);

View File

@ -39,7 +39,6 @@
#include "error.h" #include "error.h"
#include "trans.h" #include "trans.h"
#include "alpm.h" #include "alpm.h"
#include "server.h"
/* global var for handle (private to libalpm) */ /* global var for handle (private to libalpm) */
pmhandle_t *handle = NULL; pmhandle_t *handle = NULL;

View File

@ -10,6 +10,7 @@ lib/libalpm/conflict.c
lib/libalpm/db.c lib/libalpm/db.c
lib/libalpm/delta.c lib/libalpm/delta.c
lib/libalpm/deps.c lib/libalpm/deps.c
lib/libalpm/dload.c
lib/libalpm/error.c lib/libalpm/error.c
lib/libalpm/group.c lib/libalpm/group.c
lib/libalpm/handle.c lib/libalpm/handle.c
@ -17,7 +18,6 @@ lib/libalpm/log.c
lib/libalpm/md5.c lib/libalpm/md5.c
lib/libalpm/package.c lib/libalpm/package.c
lib/libalpm/remove.c lib/libalpm/remove.c
lib/libalpm/server.c
lib/libalpm/sync.c lib/libalpm/sync.c
lib/libalpm/trans.c lib/libalpm/trans.c
lib/libalpm/util.c lib/libalpm/util.c

View File

@ -1,89 +0,0 @@
/*
* server.c
*
* Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <download.h>
/* libalpm */
#include "server.h"
#include "alpm_list.h"
#include "error.h"
#include "log.h"
#include "alpm.h"
#include "util.h"
#include "handle.h"
#include "package.h"
pmserver_t *_alpm_server_new(const char *url)
{
struct url *u;
pmserver_t *server;
ALPM_LOG_FUNC;
CALLOC(server, 1, sizeof(pmserver_t), RET_ERR(PM_ERR_MEMORY, NULL));
u = downloadParseURL(url);
if(!u) {
_alpm_log(PM_LOG_ERROR, _("url '%s' is invalid, ignoring\n"), url);
RET_ERR(PM_ERR_SERVER_BAD_URL, NULL);
}
if(strlen(u->scheme) == 0) {
_alpm_log(PM_LOG_WARNING, _("url scheme not specified, assuming http\n"));
strcpy(u->scheme, "http");
}
if(strcmp(u->scheme,"ftp") == 0 && strlen(u->user) == 0) {
strcpy(u->user, "anonymous");
strcpy(u->pwd, "libalpm@guest");
}
/* remove trailing slashes, just to clean up the rest of the code */
for(int i = strlen(u->doc) - 1; u->doc[i] == '/'; --i)
u->doc[i] = '\0';
server->s_url = u;
return server;
}
void _alpm_server_free(pmserver_t *server)
{
ALPM_LOG_FUNC;
if(server == NULL) {
return;
}
/* free memory */
downloadFreeURL(server->s_url);
FREE(server);
}
/* vim: set ts=2 sw=2 noet: */

View File

@ -1,39 +0,0 @@
/*
* server.h
*
* Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _ALPM_SERVER_H
#define _ALPM_SERVER_H
#include "alpm_list.h"
#include "alpm.h"
#include <time.h>
#include <download.h>
/* Servers */
struct __pmserver_t {
/* useless abstraction now? */
struct url *s_url;
};
pmserver_t *_alpm_server_new(const char *url);
void _alpm_server_free(pmserver_t *server);
#endif /* _ALPM_SERVER_H */
/* vim: set ts=2 sw=2 noet: */

View File

@ -44,7 +44,6 @@
#include "util.h" #include "util.h"
#include "handle.h" #include "handle.h"
#include "alpm.h" #include "alpm.h"
#include "server.h"
#include "dload.h" #include "dload.h"
#include "delta.h" #include "delta.h"