#include <sys/stat.h>
#include <sys/param.h>
#include <stdio.h>
#include <errno.h>
#include "pkg.h"
#include "private/pkg.h"
#include "private/event.h"
#include "private/utils.h"
#include "private/fetch.h"
int
file_open(struct pkg_repo *repo, struct fetch_item *fi)
{
struct stat st;
const char *u = fi->url;
size_t len = strlen(u);
if (len > 5)
u += 5;
if (len < 8) {
pkg_emit_error("Invalid url: %s'\n', "
"file://<absolutepath> expected", fi->url);
return (EPKG_FATAL);
}
if (strncmp(u, "//", 2) != 0) {
pkg_emit_error("invalid url: '%s'\n", fi->url);
return (EPKG_FATAL);
}
u+=2;
if (*u != '/') {
char fqdn[256]="";
char *path = strchr(u+1, '/');
if (path == NULL) {
pkg_emit_error("Invalid url: '%s',\n"
"file:///<path> or file://localhost/<path> expected.", fi->url);
return (EPKG_FATAL);
}
strncat(fqdn, u, MIN(255, path-u));
if (0 != strncmp("localhost", fqdn, sizeof(fqdn))) {
pkg_emit_error("Invalid url: '%s'\n"
"file:///<path> or file://localhost/<path> expected.", fi->url);
return (EPKG_FATAL);
}
u = path;
}
if (stat(u, &st) == -1) {
if (!repo->silent)
pkg_emit_error("%s: %s", fi->url,
strerror(errno));
return (EPKG_FATAL);
}
fi->size = st.st_size;
if (st.st_mtime <= fi->mtime)
return (EPKG_UPTODATE);
pkg_dbg(PKG_DBG_FETCH, 1, "mtime: local %ld, remote %ld", st.st_mtime, fi->mtime);
repo->fh = fopen(u, "re");
if (repo->fh == NULL)
return (EPKG_FATAL);
fi->mtime = st.st_mtime;
return (EPKG_OK);
}
void
fh_close(struct pkg_repo *repo)
{
if (repo->fh != NULL)
fclose(repo->fh);
repo->fh = NULL;
}
int
stdio_fetch(struct pkg_repo *repo, int dest, struct fetch_item *fi)
{
char buf[8192];
size_t buflen = 0, left = 0;
off_t done = 0, r;
pkg_emit_fetch_begin(fi->url);
pkg_emit_progress_start(NULL);
if (fi->offset > 0)
done += fi->offset;
buflen = sizeof(buf);
left = sizeof(buf);
if (fi->size > 0)
left = fi->size - done;
while ((r = fread(buf, 1, left < buflen ? left : buflen, repo->fh)) > 0) {
if (write(dest, buf, r) != r) {
pkg_emit_errno("write", "");
return (EPKG_FATAL);
}
done += r;
if (fi->size > 0) {
left -= r;
pkg_dbg(PKG_DBG_FETCH, 1, "Read status: %jd over %jd", (intmax_t)done, (intmax_t)fi->size);
} else
pkg_dbg(PKG_DBG_FETCH, 1, "Read status: %jd", (intmax_t)done);
if (fi->size > 0)
pkg_emit_progress_tick(done, fi->size);
}
if (ferror(repo->fh)) {
pkg_emit_error("An error occurred while fetching package");
return(EPKG_FATAL);
}
return (EPKG_OK);
}