Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Set mtime for remote repo from a remote server.
Vsevolod Stakhov committed 13 years ago
commit 9ea259e3aa7d574fc566939076b5b411a86a901c
parent 0564db0
3 files changed +81 -43
modified libpkg/fetch.c
@@ -98,7 +98,21 @@ pkg_fetch_file(const char *url, const char *dest, time_t t)
		return(EPKG_FATAL);
	}

-
	retcode = pkg_fetch_file_to_fd(url, fd, t);
+
	retcode = pkg_fetch_file_to_fd(url, fd, &t);
+

+
	if (t != 0) {
+
		struct timeval ftimes[2] = {
+
			{
+
			.tv_sec = t,
+
			.tv_usec = 0
+
			},
+
			{
+
			.tv_sec = t,
+
			.tv_usec = 0
+
			}
+
		};
+
		futimes(fd, ftimes);
+
	}

	close(fd);

@@ -110,7 +124,7 @@ pkg_fetch_file(const char *url, const char *dest, time_t t)
}

int
-
pkg_fetch_file_to_fd(const char *url, int dest, time_t t)
+
pkg_fetch_file_to_fd(const char *url, int dest, time_t *t)
{
	FILE *remote = NULL;
	struct url *u;
@@ -145,8 +159,8 @@ pkg_fetch_file_to_fd(const char *url, int dest, time_t t)
	retry = max_retry;

	u = fetchParseURL(url);
-
	if (t != 0)
-
		u->ims_time = t;
+
	if (t != NULL && *t != 0)
+
		u->ims_time = *t;

	doc = u->doc;
	while (remote == NULL) {
@@ -212,11 +226,13 @@ pkg_fetch_file_to_fd(const char *url, int dest, time_t t)
			}
		}
	}
-
	if (t != 0) {
-
		if (st.mtime <= t) {
+
	if (t != NULL) {
+
		if (st.mtime < *t) {
			retcode = EPKG_UPTODATE;
			goto cleanup;
		}
+
		else
+
			*t = st.mtime;
	}

	begin_dl = time(NULL);
modified libpkg/private/pkg.h
@@ -281,7 +281,7 @@ int pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags);
#define PKG_DELETE_UPGRADE (1<<1)
#define PKG_DELETE_NOSCRIPT (1<<2)

-
int pkg_fetch_file_to_fd(const char *url, int dest, time_t t);
+
int pkg_fetch_file_to_fd(const char *url, int dest, time_t *t);
int pkg_repo_fetch(struct pkg *pkg);

int pkg_start_stop_rc_scripts(struct pkg *, pkg_rc_attr attr);
modified libpkg/update.c
@@ -64,20 +64,51 @@ remote_add_indexes(const char *reponame)
	return (ret);
}

+
/* Return opened file descriptor */
+
static int
+
repo_fetch_remote_tmp(const char *reponame, const char *filename, time_t *t)
+
{
+
	char url[MAXPATHLEN];
+
	char tmp[MAXPATHLEN];
+
	int fd;
+
	const char *tmpdir;
+

+
	snprintf(url, MAXPATHLEN, "%s/%s", reponame, filename);
+

+
	tmpdir = getenv("TMPDIR");
+
	if (tmpdir == NULL)
+
		tmpdir = "/tmp";
+
	snprintf(tmp, MAXPATHLEN, "%s/%s.XXXXXX", tmpdir, filename);
+

+
	fd = mkstemp(tmp);
+
	if (fd == -1) {
+
		pkg_emit_error("Could not create temporary file %s, "
+
		    "aborting update.\n", tmp);
+
		return -1;
+
	}
+
	(void)unlink(tmp);
+

+
	if (pkg_fetch_file_to_fd(url, fd, t) != EPKG_OK) {
+
		close(fd);
+
		fd = -1;
+
	}
+

+
	return fd;
+
}
+

int
pkg_update(const char *name, const char *packagesite, bool force)
{
-
	char url[MAXPATHLEN];
+

	struct archive *a = NULL;
	struct archive_entry *ae = NULL;
	char repofile[MAXPATHLEN];
	char repofile_unchecked[MAXPATHLEN];
-
	char tmp[MAXPATHLEN];
	const char *dbdir = NULL;
	const char *repokey;
	unsigned char *sig = NULL;
	int siglen = 0;
-
	int fd, rc = EPKG_FATAL, ret;
+
	int fd = -1, rc = EPKG_FATAL, ret;
	struct stat st;
	time_t t = 0;
	sqlite3 *sqlite = NULL;
@@ -85,28 +116,12 @@ pkg_update(const char *name, const char *packagesite, bool force)
	const char *myarch;
	int64_t res;
	char *bad_abis = NULL;
-
	const char *tmpdir;
	sqlite3_stmt *stmt;
	const char sql[] = ""
	    "INSERT OR REPLACE INTO repodata (key, value) "
	    "VALUES (\"packagesite\", ?1);";

	sqlite3_initialize();
-
	snprintf(url, MAXPATHLEN, "%s/repo.txz", packagesite);
-

-
	tmpdir = getenv("TMPDIR");
-
	if (tmpdir == NULL)
-
		tmpdir = "/tmp";
-
	strlcpy(tmp, tmpdir, sizeof(tmp));
-
	strlcat(tmp, "/repo.txz.XXXXXX", sizeof(tmp));
-

-
	fd = mkstemp(tmp);
-
	if (fd == -1) {
-
		pkg_emit_error("Could not create temporary file %s, "
-
		    "aborting update.\n", tmp);
-
		return (EPKG_FATAL);
-
	}
-
	(void)unlink(tmp);

	if (pkg_config_string(PKG_CONFIG_DBDIR, &dbdir) != EPKG_OK) {
		pkg_emit_error("Cant get dbdir config entry");
@@ -116,18 +131,9 @@ pkg_update(const char *name, const char *packagesite, bool force)
	snprintf(repofile, sizeof(repofile), "%s/%s.sqlite", dbdir, name);
	snprintf(repofile_unchecked, sizeof(repofile_unchecked),
				    "%s.unchecked", repofile);
-
	if (force)
-
		t = 0;		/* Always fetch */
-
	else {
-
		if (stat(repofile, &st) != -1) {
+
	if (!force)
+
		if (stat(repofile, &st) != -1)
			t = st.st_mtime;
-
			/* add 1 minute to the timestamp because
-
			 * repo.sqlite is always newer than repo.txz,
-
			 * 1 minute should be enough.
-
			 */
-
			t += 60;
-
		}
-
	}

	if (t != 0) {
		if (sqlite3_open(repofile, &sqlite) != SQLITE_OK) {
@@ -166,11 +172,6 @@ pkg_update(const char *name, const char *packagesite, bool force)
	if (sqlite != NULL)
		sqlite3_close(sqlite);

-
	rc = pkg_fetch_file_to_fd(url, fd, t);
-

-
	if (rc != EPKG_OK) {
-
		goto cleanup;
-
	}

	/* If the repo.sqlite file exists, test that we can write to
	   it.  If it doesn't exist, assume we can create it */
@@ -182,6 +183,11 @@ pkg_update(const char *name, const char *packagesite, bool force)
		goto cleanup;
	}

+
	if ((fd = repo_fetch_remote_tmp(packagesite, "repo.txz", &t)) == -1) {
+
		rc = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

	a = archive_read_new();
	archive_read_support_filter_all(a);
	archive_read_support_format_tar(a);
@@ -316,12 +322,28 @@ pkg_update(const char *name, const char *packagesite, bool force)
	if ((rc = remote_add_indexes(name)) != EPKG_OK)
		goto cleanup;

+
	/* Set mtime from http request if possible */
+
	if (t != 0) {
+
		struct timeval ftimes[2] = {
+
			{
+
			.tv_sec = t,
+
			.tv_usec = 0
+
			},
+
			{
+
			.tv_sec = t,
+
			.tv_usec = 0
+
			}
+
		};
+
		utimes(repofile, ftimes);
+
	}
+

	rc = EPKG_OK;

	cleanup:
	if (a != NULL)
		archive_read_free(a);
-
	(void)close(fd);
+
	if (fd != -1)
+
		(void)close(fd);

	return (rc);
}