#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#ifdef HAVE_LIBCURL
#include <curl/curl.h>
#endif
#include "dload.h"
#include "alpm_list.h"
#include "alpm.h"
#include "log.h"
#include "util.h"
#include "handle.h"
#ifdef HAVE_LIBCURL
static off_t prevprogress;
static const char *get_filename(const char *url)
{
char *filename = strrchr(url, '/');
if(filename != NULL) {
filename++;
}
return filename;
}
static char *get_fullpath(const char *path, const char *filename,
const char *suffix)
{
char *filepath;
size_t len = strlen(path) + strlen(filename) + strlen(suffix) + 1;
CALLOC(filepath, len, sizeof(char), return NULL);
snprintf(filepath, len, "%s%s%s", path, filename, suffix);
return filepath;
}
enum {
ABORT_SIGINT = 1,
ABORT_OVER_MAXFILESIZE
};
static int dload_interrupted;
static void inthandler(int UNUSED signum)
{
dload_interrupted = ABORT_SIGINT;
}
static int curl_progress(void *file, double dltotal, double dlnow,
double UNUSED ultotal, double UNUSED ulnow)
{
struct dload_payload *payload = (struct dload_payload *)file;
off_t current_size, total_size;
if(dload_interrupted) {
return 1;
}
current_size = payload->initial_size + (off_t)dlnow;
if(payload->max_size && current_size > payload->max_size) {
dload_interrupted = ABORT_OVER_MAXFILESIZE;
return 1;
}
if(payload->handle->dlcb == NULL) {
return 0;
}
total_size = payload->initial_size + (off_t)dltotal;
if(DOUBLE_EQ(dltotal, 0.0) || prevprogress == total_size) {
return 0;
}
if(prevprogress == 0) {
payload->handle->dlcb(payload->remote_name, 0, (off_t)dltotal);
}
payload->handle->dlcb(payload->remote_name, current_size, total_size);
prevprogress = current_size;
return 0;
}
static int curl_gethost(const char *url, char *buffer)
{
size_t hostlen;
char *p;
if(strncmp(url, "file://", 7) == 0) {
strcpy(buffer, _("disk"));
} else {
p = strstr(url, "//");
if(!p) {
return 1;
}
p += 2;
hostlen = strcspn(p, "/");
if(hostlen > 255) {
return
|