aboutsummaryrefslogtreecommitdiffstats
path: root/src/url.c
diff options
context:
space:
mode:
authorTimo Teras <timo.teras@iki.fi>2009-07-31 16:08:09 +0300
committerTimo Teras <timo.teras@iki.fi>2009-07-31 16:08:09 +0300
commitea901526648c43ef8e961b3d7e051b9ca14b65ca (patch)
treecea5a96dc83518a94f79dddd645f3e4404179530 /src/url.c
parent67108bf07a67811ea91fc965f3f1592a4a70044e (diff)
downloadapk-tools-ea901526648c43ef8e961b3d7e051b9ca14b65ca.tar.gz
apk-tools-ea901526648c43ef8e961b3d7e051b9ca14b65ca.tar.bz2
apk-tools-ea901526648c43ef8e961b3d7e051b9ca14b65ca.tar.xz
apk: use *at instead of chdir+normal file syscall
this way we never change cwd, and relative filenames are always parsed consistently. this also helps filename construction in many places. this patch also changes '--root' to override location of all configuration to be in the new root. previously it depended on the file which one was used.
Diffstat (limited to 'src/url.c')
-rw-r--r--src/url.c21
1 files changed, 12 insertions, 9 deletions
diff --git a/src/url.c b/src/url.c
index e09ec4c..be5e285 100644
--- a/src/url.c
+++ b/src/url.c
@@ -11,6 +11,7 @@
#include <stdio.h>
#include <fcntl.h>
+#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
@@ -61,7 +62,7 @@ static int fork_wget(const char *url)
struct apk_istream *apk_istream_from_url(const char *url)
{
if (apk_url_local_file(url) != NULL)
- return apk_istream_from_file(apk_url_local_file(url));
+ return apk_istream_from_file(AT_FDCWD, apk_url_local_file(url));
return apk_istream_from_fd(fork_wget(url));
}
@@ -74,16 +75,19 @@ struct apk_istream *apk_istream_from_url_gz(const char *file)
struct apk_bstream *apk_bstream_from_url(const char *url)
{
if (apk_url_local_file(url))
- return apk_bstream_from_file(url);
+ return apk_bstream_from_file(AT_FDCWD, url);
return apk_bstream_from_fd(fork_wget(url));
}
-int apk_url_download(const char *url, const char *file)
+int apk_url_download(const char *url, int atfd, const char *file)
{
pid_t pid;
- int status;
- char tmp[256];
+ int status, fd;
+
+ fd = openat(atfd, file, O_CREAT|O_RDWR|O_TRUNC, 0644);
+ if (fd < 0)
+ return -errno;
pid = fork();
if (pid == -1)
@@ -92,15 +96,14 @@ int apk_url_download(const char *url, const char *file)
if (pid == 0) {
setsid();
dup2(open("/dev/null", O_RDONLY), STDIN_FILENO);
- snprintf(tmp, sizeof(tmp), "%s.backup", file);
- rename(file, tmp);
- execlp("wget", "wget", "-q", "-O", file, url, NULL);
+ dup2(fd, STDOUT_FILENO);
+ execlp("wget", "wget", "-q", "-O", "-", url, NULL);
exit(0);
}
waitpid(pid, &status, 0);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
- unlink(file);
+ unlinkat(atfd, file, 0);
return -1;
}