1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-22 15:58:50 -05:00

lib/dload: abstract out helper function to set utimes

This greatly simplifies the cleanup fallthrough in our download function
and we'll be able to reuse this for signatures.

Signed-off-by: Dave Reisner <d@falconindy.com>
Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dave Reisner 2011-03-25 09:15:30 -04:00 committed by Dan McGee
parent 98c8ab18ff
commit a9fb4d9d5b

View File

@ -142,6 +142,18 @@ static int curl_gethost(const char *url, char *buffer)
return 0; return 0;
} }
static int utimes_long(const char *path, long time)
{
if(time != -1) {
struct timeval tv[2];
memset(&tv, 0, sizeof(tv));
tv[0].tv_sec = tv[1].tv_sec = time;
return utimes(path, tv);
}
return 0;
}
static int curl_download_internal(const char *url, const char *localpath, static int curl_download_internal(const char *url, const char *localpath,
int force) int force)
{ {
@ -265,34 +277,22 @@ static int curl_download_internal(const char *url, const char *localpath,
goto cleanup; goto cleanup;
} }
fclose(localf);
localf = NULL;
/* set the times on the file to the same as that of the remote file */
if(remote_time != -1) {
struct timeval tv[2];
memset(&tv, 0, sizeof(tv));
tv[0].tv_sec = tv[1].tv_sec = remote_time;
utimes(tempfile, tv);
}
rename(tempfile, destfile);
ret = 0; ret = 0;
cleanup: cleanup:
if(localf != NULL) {
fclose(localf);
utimes_long(tempfile, remote_time);
}
/* TODO: A signature download will need to return success here as well before
* we're willing to rotate the new file into place. */
if(ret == 0) {
rename(tempfile, destfile);
}
FREE(tempfile); FREE(tempfile);
FREE(destfile); FREE(destfile);
if(localf != NULL) {
/* if we still had a local file open, we got interrupted. set the mtimes on
* the file accordingly. */
fflush(localf);
if(remote_time != -1) {
struct timeval tv[2];
memset(&tv, 0, sizeof(tv));
tv[0].tv_sec = tv[1].tv_sec = remote_time;
futimes(fileno(localf), tv);
}
fclose(localf);
}
/* restore the old signal handlers */ /* restore the old signal handlers */
sigaction(SIGINT, &sig_int[OLD], NULL); sigaction(SIGINT, &sig_int[OLD], NULL);