- reworked sync_synctree() to make use of alpm_db_update()

- dropped unpack()
This commit is contained in:
Aurelien Foret 2005-03-16 21:21:06 +00:00
parent 2ce1105900
commit ab7ca5dc72
3 changed files with 21 additions and 104 deletions

View File

@ -156,52 +156,44 @@ static int sync_cleancache(int level)
static int sync_synctree(list_t *syncs)
{
char path[PATH_MAX];
mode_t oldmask;
list_t *files = NULL;
char *root, *dbpath;
list_t *i;
int success = 0;
int ret = 0;
alpm_get_option(PM_OPT_ROOT, (long *)&root);
alpm_get_option(PM_OPT_DBPATH, (long *)&dbpath);
for(i = syncs; i; i = i->next) {
char path[PATH_MAX];
list_t *files = NULL;
sync_t *sync = (sync_t *)i->data;
/* build a one-element list */
snprintf(path, PATH_MAX, "%s"PM_EXT_DB, sync->treename);
files = list_add(files, strdup(path));
success = 1;
if(downloadfiles(sync->servers, pmo_dbpath, files)) {
snprintf(path, PATH_MAX, "%s%s", root, dbpath);
if(downloadfiles(sync->servers, path, files)) {
fprintf(stderr, "failed to synchronize %s\n", sync->treename);
success = 0;
FREELIST(files);
ret--;
continue;
}
FREELIST(files);
snprintf(path, PATH_MAX, "%s/%s"PM_EXT_DB, pmo_dbpath, sync->treename);
if(success) {
char ldir[PATH_MAX];
snprintf(ldir, PATH_MAX, "%s/%s", pmo_dbpath, sync->treename);
/* remove the old dir */
vprint("removing %s (if it exists)\n", ldir);
rmrf(ldir);
/* make the new dir */
oldmask = umask(0000);
mkdir(ldir, 0755);
umask(oldmask);
/* uncompress the sync database */
vprint("Unpacking %s...\n", path);
if(unpack(path, ldir, NULL)) {
return(1);
}
snprintf(path, PATH_MAX, "%s%s/%s"PM_EXT_DB, root, dbpath, sync->treename);
if(alpm_db_update(sync->treename, path) == -1) {
fprintf(stderr, "error: %s\n", alpm_strerror(pm_errno));
ret--;
}
/* remove the .tar.gz */
unlink(path);
}
return(!success);
return(ret);
}
static int sync_search(list_t *syncs, list_t *targets)

View File

@ -28,84 +28,11 @@
#include <fcntl.h>
#include <ctype.h>
#include <dirent.h>
#include <zlib.h>
#include <libtar.h>
#include <unistd.h>
/* pacman */
#include "util.h"
/* borrowed and modified from Per Liden's pkgutils (http://crux.nu) */
long gzopen_frontend(char *pathname, int oflags, int mode)
{
char *gzoflags;
int fd;
gzFile gzf;
switch (oflags & O_ACCMODE) {
case O_WRONLY:
gzoflags = "w";
break;
case O_RDONLY:
gzoflags = "r";
break;
case O_RDWR:
default:
errno = EINVAL;
return -1;
}
if((fd = open(pathname, oflags, mode)) == -1) {
return -1;
}
if((oflags & O_CREAT) && fchmod(fd, mode)) {
return -1;
}
if(!(gzf = gzdopen(fd, gzoflags))) {
errno = ENOMEM;
return -1;
}
return (long)gzf;
}
int unpack(char *archive, const char *prefix, const char *fn)
{
TAR *tar = NULL;
char expath[PATH_MAX];
tartype_t gztype = {
(openfunc_t) gzopen_frontend,
(closefunc_t)gzclose,
(readfunc_t) gzread,
(writefunc_t)gzwrite
};
/* open the .tar.gz package */
if(tar_open(&tar, archive, &gztype, O_RDONLY, 0, TAR_GNU) == -1) {
perror(archive);
return(1);
}
while(!th_read(tar)) {
if(fn && strcmp(fn, th_get_pathname(tar))) {
if(TH_ISREG(tar) && tar_skip_regfile(tar)) {
char errorstr[255];
snprintf(errorstr, 255, "bad tar archive: %s", archive);
perror(errorstr);
tar_close(tar);
return(1);
}
continue;
}
snprintf(expath, PATH_MAX, "%s/%s", prefix, th_get_pathname(tar));
if(tar_extract_file(tar, expath)) {
fprintf(stderr, "could not extract %s: %s\n", th_get_pathname(tar), strerror(errno));
}
if(fn) break;
}
tar_close(tar);
return(0);
}
/* does the same thing as 'mkdir -p' */
int makepath(char *path)
{

View File

@ -26,10 +26,8 @@
fprintf(stderr, "malloc failure: could not allocate %d bytes\n", b); \
exit(1); }} else p = NULL; } while(0)
#define FREE(p) do { if (p) { free(p); (p)= NULL; }} while(0)
#define FREE(p) do { if (p) { free(p); (p) = NULL; }} while(0)
long gzopen_frontend(char *pathname, int oflags, int mode);
int unpack(char *archive, const char *prefix, const char *fn);
int makepath(char *path);
int rmrf(char *path);
void indentprint(char *str, int indent);