uclient-fetch: truncate output files (unless resuming)
[project/uclient.git] / uclient-fetch.c
index 3f875c9aea0a87feb380caa5aa11e8ccba1c83b4..851994f47b841668ca066a294e9268afff07a123 100644 (file)
@@ -103,7 +103,10 @@ static int open_output_file(const char *path, uint64_t resume_offset)
        if (cur_resume)
                flags = O_RDWR;
        else
-               flags = O_WRONLY | O_EXCL;
+               flags = O_WRONLY | O_TRUNC;
+
+       if (!cur_resume && !output_file)
+               flags |= O_EXCL;
 
        flags |= O_CREAT;
 
@@ -112,7 +115,8 @@ static int open_output_file(const char *path, uint64_t resume_offset)
                        if (!quiet)
                                fprintf(stderr, "Writing to stdout\n");
 
-                       return STDOUT_FILENO;
+                       ret = STDOUT_FILENO;
+                       goto done;
                }
        } else {
                filename = uclient_get_url_filename(path, "index.html");
@@ -136,6 +140,7 @@ static int open_output_file(const char *path, uint64_t resume_offset)
 
        out_offset = resume_offset;
        out_bytes += resume_offset;
+done:
        if (!quiet) {
                progress_init(&pmt, output_file);
                pmt_timer.cb = pmt_update;
@@ -162,12 +167,22 @@ static void header_done_cb(struct uclient *cl)
        uint64_t resume_offset = 0, resume_end, resume_size;
        static int retries;
 
-       if (retries < 10 && uclient_http_redirect(cl)) {
-               if (!quiet)
-                       fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
+       if (retries < 10) {
+               int ret = uclient_http_redirect(cl);
+               if (ret < 0) {
+                       if (!quiet)
+                               fprintf(stderr, "Failed to redirect to %s on %s\n", cl->url->location, cl->url->host);
+                       error_ret = 8;
+                       request_done(cl);
+                       return;
+               }
+               if (ret > 0) {
+                       if (!quiet)
+                               fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
 
-               retries++;
-               return;
+                       retries++;
+                       return;
+               }
        }
 
        if (cl->status_code == 204 && cur_resume) {
@@ -361,6 +376,7 @@ static void eof_cb(struct uclient *cl)
        if (!quiet) {
                pmt_update(&pmt_timer);
                uloop_timeout_cancel(&pmt_timer);
+               fprintf(stderr, "\n");
        }
 
        if (!cl->data_eof) {
@@ -518,7 +534,7 @@ int main(int argc, char **argv)
        signal(SIGPIPE, SIG_IGN);
        init_ustream_ssl();
 
-       while ((ch = getopt_long(argc, argv, "cO:P:qsU:Y:", longopts, &longopt_idx)) != -1) {
+       while ((ch = getopt_long(argc, argv, "cO:P:qsT:U:Y:", longopts, &longopt_idx)) != -1) {
                switch(ch) {
                case 0:
                        switch (longopt_idx) {