xbps/lib/repository_sync_index.c
Juan RP a77727887d Improvements for xbps_fetch_file and xbps_repository_sync_index.
xbps_fetch_file:
	- A temp file is created with .part extension to improve resuming.
	- Files are downloaded in cwd.
	- Switch to futimens(2) and fsync(2).

xbps_repository_sync_index:
	- Do not create local repodir in metadir if it already exists.
	- Simplify the code thanks to new xbps_fetch_file().
2012-11-11 11:29:49 +01:00

185 lines
5.1 KiB
C

/*-
* Copyright (c) 2009-2012 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "xbps_api_impl.h"
#include "fetch.h"
/**
* @file lib/repository_sync_index.c
* @brief Repository package index synchronization routines
* @defgroup reposync Repository package index synchronization functions
*
* Functions to manipulate repository package index plist file
* synchronizations.
*/
char HIDDEN *
xbps_get_remote_repo_string(const char *uri)
{
struct url *url;
size_t i;
char *p;
if ((url = fetchParseURL(uri)) == NULL)
return NULL;
/*
* Replace '.' ':' and '/' characters with underscores, so that
* provided URL:
*
* http://nocturno.local:8080/repo/x86_64
*
* becomes:
*
* http___nocturno_local_8080_repo_x86_64
*
*/
if (url->port != 0)
p = xbps_xasprintf("%s://%s:%u%s", url->scheme,
url->host, url->port, url->doc);
else
p = xbps_xasprintf("%s://%s%s", url->scheme,
url->host, url->doc);
fetchFreeURL(url);
for (i = 0; i < strlen(p); i++) {
if (p[i] == '.' || p[i] == '/' || p[i] == ':')
p[i] = '_';
}
return p;
}
/*
* Returns -1 on error, 0 if transfer was not necessary (local/remote
* size and/or mtime match) and 1 if downloaded successfully.
*/
int
xbps_repository_sync_pkg_index(struct xbps_handle *xhp,
const char *uri,
const char *plistf)
{
prop_array_t array;
const char *fetchstr = NULL;
char *rpidx, *lrepodir, *uri_fixedp, *lrepofile;
int rv = 0;
assert(uri != NULL);
rpidx = uri_fixedp = lrepodir = lrepofile = NULL;
/* ignore non remote repositories */
if (!xbps_check_is_repository_uri_remote(uri))
return 0;
uri_fixedp = xbps_get_remote_repo_string(uri);
if (uri_fixedp == NULL)
return -1;
/*
* Remote repository plist index full URL.
*/
rpidx = xbps_xasprintf("%s/%s", uri, plistf);
/*
* Full path to repository directory to store the plist
* index file.
*/
lrepodir = xbps_xasprintf("%s/%s", xhp->metadir, uri_fixedp);
/*
* Full path to the local repository index file.
*/
lrepofile = xbps_xasprintf("%s/%s", lrepodir, plistf);
/*
* Create repodir in metadir.
*/
if (access(lrepodir, R_OK|X_OK|W_OK) == -1) {
if ((rv = xbps_mkpath(lrepodir, 0755)) == -1) {
xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL,
errno, NULL, NULL,
"[reposync] failed to create repodir `%s': %s",
lrepodir, strerror(errno));
goto out;
}
}
if (chdir(lrepodir) == -1) {
xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL,
errno, NULL, NULL,
"[reposync] failed to change dir to repodir `%s': %s",
lrepodir, strerror(errno));
rv = -1;
goto out;
}
/* reposync start cb */
xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC, 0, uri, plistf, NULL);
/*
* Download plist index file from repository.
*/
if ((rv = xbps_fetch_file(xhp, rpidx, NULL)) == -1) {
/* reposync error cb */
fetchstr = xbps_fetch_error_string();
xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL,
fetchLastErrCode != 0 ? fetchLastErrCode : errno,
NULL, NULL,
"[reposync] failed to fetch file `%s': %s",
rpidx, fetchstr ? fetchstr : strerror(errno));
goto out;
} else if (rv == 0) {
goto out;
} else {
rv = 0;
}
/*
* Make sure that downloaded plist file can be internalized, i.e
* some HTTP servers don't return proper errors and sometimes
* you get an HTML ASCII file :-)
*/
array = prop_array_internalize_from_zfile(lrepofile);
if (array == NULL) {
xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL, 0, NULL, NULL,
"[reposync] downloaded file `%s' is not valid.", rpidx);
(void)unlink(lrepofile);
(void)remove(lrepodir);
rv = -1;
goto out;
}
prop_object_release(array);
out:
if (rpidx)
free(rpidx);
if (lrepodir)
free(lrepodir);
if (lrepofile)
free(lrepofile);
if (uri_fixedp)
free(uri_fixedp);
return rv;
}