pacman/src/pacsync.c

273 lines
6.9 KiB
C
Raw Normal View History

2002-08-09 18:03:48 +00:00
/*
2003-09-03 02:09:29 +00:00
* pacsync.c
2002-08-09 18:03:48 +00:00
*
* Copyright (c) 2002 by Judd Vinet <jvinet@zeroflux.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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include "config.h"
#include <stdio.h>
2003-09-03 02:09:29 +00:00
#include <stdlib.h>
2002-08-09 18:03:48 +00:00
#include <string.h>
2003-05-30 19:56:46 +00:00
#include <sys/stat.h>
2002-08-09 18:03:48 +00:00
#include <unistd.h>
#include <ftplib.h>
/* pacman */
#include "list.h"
#include "package.h"
#include "db.h"
#include "util.h"
#include "pacsync.h"
#include "pacman.h"
static int log_progress(netbuf *ctl, int xfered, void *arg);
static char sync_fnm[25];
2003-03-17 19:36:48 +00:00
static int offset;
2002-08-09 18:03:48 +00:00
/* pacman options */
extern char *pmo_root;
2003-09-03 02:09:29 +00:00
extern char *pmo_dbpath;
2003-05-30 19:56:46 +00:00
extern unsigned short pmo_nopassiveftp;
2002-09-16 05:22:13 +00:00
/* sync servers */
extern PMList *pmc_syncs;
2003-09-03 02:09:29 +00:00
extern int maxcols;
2002-08-09 18:03:48 +00:00
int sync_synctree()
{
char ldir[PATH_MAX] = "";
char path[PATH_MAX];
mode_t oldmask;
PMList *files = NULL;
2002-09-16 05:22:13 +00:00
PMList *i;
int success = 0;
2003-09-03 02:09:29 +00:00
2002-09-16 05:22:13 +00:00
for(i = pmc_syncs; i; i = i->next) {
sync_t *sync = (sync_t*)i->data;
2003-09-03 02:09:29 +00:00
snprintf(ldir, PATH_MAX, "%s%s", pmo_root, pmo_dbpath);
2002-09-16 05:22:13 +00:00
/* build a one-element list */
snprintf(path, PATH_MAX, "%s.db.tar.gz", sync->treename);
files = list_add(files, strdup(path));
success = 1;
if(downloadfiles(sync->servers, ldir, files)) {
fprintf(stderr, "failed to synchronize %s\n", sync->treename);
success = 0;
}
2002-08-09 18:03:48 +00:00
list_free(files);
2002-09-16 05:22:13 +00:00
files = NULL;
snprintf(path, PATH_MAX, "%s/%s.db.tar.gz", ldir, sync->treename);
if(success) {
2003-09-03 02:09:29 +00:00
snprintf(ldir, PATH_MAX, "%s%s/%s", pmo_root, pmo_dbpath, sync->treename);
2002-09-16 05:22:13 +00:00
/* 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)) {
return(1);
}
}
/* remove the .tar.gz */
unlink(path);
2002-08-09 18:03:48 +00:00
}
2002-09-16 05:22:13 +00:00
return(!success);
2002-08-09 18:03:48 +00:00
}
2002-09-16 05:22:13 +00:00
int downloadfiles(PMList *servers, char *localpath, PMList *files)
2002-08-09 18:03:48 +00:00
{
int fsz;
netbuf *control = NULL;
PMList *lp;
2002-09-16 05:22:13 +00:00
int done = 0;
PMList *complete = NULL;
PMList *i;
2002-08-09 18:03:48 +00:00
if(files == NULL) {
return(0);
}
2002-09-16 05:22:13 +00:00
for(i = servers; i && !done; i = i->next) {
server_t *server = (server_t*)i->data;
2003-09-03 02:09:29 +00:00
if(!strcmp(server->protocol, "ftp")) {
2002-09-16 05:22:13 +00:00
FtpInit();
if(!FtpConnect(server->server, &control)) {
fprintf(stderr, "error: cannot connect to %s\n", server->server);
continue;
}
if(!FtpLogin("anonymous", "arch@guest", control)) {
fprintf(stderr, "error: anonymous login failed\n");
FtpQuit(control);
continue;
}
if(!FtpChdir(server->path, control)) {
fprintf(stderr, "error: could not cwd to %s: %s\n", server->path,
FtpLastResponse(control));
continue;
}
2003-05-30 19:56:46 +00:00
if(!pmo_nopassiveftp) {
if(!FtpOptions(FTPLIB_CONNMODE, FTPLIB_PASSIVE, control)) {
fprintf(stderr, "warning: failed to set passive mode\n");
}
} else {
vprint("FTP passive mode not set\n");
}
2002-08-09 18:03:48 +00:00
}
2002-09-16 05:22:13 +00:00
/* get each file in the list */
for(lp = files; lp; lp = lp->next) {
char output[PATH_MAX];
int j;
char *fn = (char*)lp->data;
2003-03-17 19:36:48 +00:00
struct stat st;
2002-09-16 05:22:13 +00:00
if(is_in(fn, complete)) {
continue;
}
2003-03-17 19:36:48 +00:00
snprintf(output, PATH_MAX, "%s/%s.part", localpath, fn);
2002-09-16 05:22:13 +00:00
strncpy(sync_fnm, lp->data, 24);
for(j = strlen(sync_fnm); j < 24; j++) {
sync_fnm[j] = ' ';
}
sync_fnm[24] = '\0';
2003-09-03 02:09:29 +00:00
offset = 0;
2002-09-16 05:22:13 +00:00
2003-09-03 02:09:29 +00:00
if(!strcmp(server->protocol, "ftp")) {
if(!FtpSize(fn, &fsz, FTPLIB_IMAGE, control)) {
fprintf(stderr, "warning: failed to get filesize for %s\n", fn);
}
if(!stat(output, &st)) {
offset = (int)st.st_size;
if(!FtpRestart(offset, control)) {
fprintf(stderr, "warning: failed to resume download -- restarting\n");
/* can't resume: */
/* unlink the file in order to restart download from scratch */
unlink(output);
2003-07-12 00:06:30 +00:00
}
2003-09-03 02:09:29 +00:00
}
/* set up our progress bar's callback */
FtpOptions(FTPLIB_CALLBACK, (long)log_progress, control);
FtpOptions(FTPLIB_IDLETIME, (long)1000, control);
FtpOptions(FTPLIB_CALLBACKARG, (long)&fsz, control);
FtpOptions(FTPLIB_CALLBACKBYTES, (10*1024), control);
2003-07-12 00:06:30 +00:00
2003-09-03 02:09:29 +00:00
if(!FtpGet(output, lp->data, FTPLIB_IMAGE, control)) {
fprintf(stderr, "\nfailed downloading %s from %s: %s\n",
fn, server->server, FtpLastResponse(control));
/* we leave the partially downloaded file in place so it can be resumed later */
} else {
char completefile[PATH_MAX];
log_progress(control, fsz-offset, &fsz);
complete = list_add(complete, fn);
/* rename "output.part" file to "output" file */
snprintf(completefile, PATH_MAX, "%s/%s", localpath, fn);
rename(output, completefile);
2003-03-17 19:36:48 +00:00
}
2003-09-03 02:09:29 +00:00
printf("\n");
fflush(stdout);
} else if(!strcmp(server->protocol, "file")) {
2002-09-16 05:22:13 +00:00
/* local repository, just copy the file */
char src[PATH_MAX], dest[PATH_MAX];
snprintf(src, PATH_MAX, "%s%s", server->path, fn);
snprintf(dest, PATH_MAX, "%s/%s", localpath, fn);
vprint("copying %s to %s\n", src, dest);
if(copyfile(src, dest)) {
fprintf(stderr, "failed copying %s\n", src);
} else {
char out[56];
2003-09-03 02:09:29 +00:00
printf(" %s [", sync_fnm);
2002-09-16 05:22:13 +00:00
strncpy(out, server->path, 33);
printf("%s", out);
2003-09-03 02:09:29 +00:00
for(j = strlen(out); j < maxcols-44; j++) {
2002-09-16 05:22:13 +00:00
printf(" ");
}
fputs("] 100% | LOCAL\n", stdout);
fflush(stdout);
complete = list_add(complete, fn);
}
}
}
if(list_count(complete) == list_count(files)) {
done = 1;
2002-08-09 18:03:48 +00:00
}
2002-09-16 05:22:13 +00:00
2003-09-03 02:09:29 +00:00
if(!strcmp(server->protocol, "ftp")) {
2002-09-16 05:22:13 +00:00
FtpQuit(control);
2002-08-09 18:03:48 +00:00
}
}
2002-09-16 05:22:13 +00:00
return(!done);
2002-08-09 18:03:48 +00:00
}
static int log_progress(netbuf *ctl, int xfered, void *arg)
{
2003-03-17 19:36:48 +00:00
int fsz = *(int*)arg;
int pct = ((float)(xfered+offset) / fsz) * 100;
2003-09-03 02:09:29 +00:00
int i, cur;
char *cenv = NULL;
2003-03-17 19:36:48 +00:00
2003-09-03 02:09:29 +00:00
cenv = getenv("COLUMNS");
if(cenv) {
maxcols = atoi(cenv);
2003-03-17 19:36:48 +00:00
}
2003-09-03 02:09:29 +00:00
printf(" %s [", sync_fnm);
cur = (int)((maxcols-44)*pct/100);
for(i = 0; i < maxcols-44; i++) {
(i < cur) ? printf("#") : printf(" ");
2003-03-17 19:36:48 +00:00
}
2003-09-03 02:09:29 +00:00
printf("] %3d%% | %6dK\r", pct, ((xfered+offset)/1024));
2003-03-17 19:36:48 +00:00
fflush(stdout);
return(1);
2002-08-09 18:03:48 +00:00
}
2003-09-03 02:09:29 +00:00
/* Test for existance of a package in a PMList* of syncpkg_t*
* If found, return a pointer to the respective syncpkg_t*
2003-05-30 19:56:46 +00:00
*/
2003-09-03 02:09:29 +00:00
syncpkg_t* find_pkginsync(char *needle, PMList *haystack)
2003-05-30 19:56:46 +00:00
{
2003-09-03 02:09:29 +00:00
PMList *i;
2003-05-30 19:56:46 +00:00
syncpkg_t *sync;
int found = 0;
2003-09-03 02:09:29 +00:00
for(i = haystack; i && !found; i = i->next) {
sync = (syncpkg_t*)i->data;
if(sync && !strcmp(sync->pkg->name, needle)) {
2003-05-30 19:56:46 +00:00
found = 1;
}
}
2003-09-03 02:09:29 +00:00
if(!found) {
sync = NULL;
}
2003-05-30 19:56:46 +00:00
2003-09-03 02:09:29 +00:00
return sync;
2003-05-30 19:56:46 +00:00
}
2002-08-09 18:03:48 +00:00
/* vim: set ts=2 sw=2 noet: */