wmclockmon: add missing `const` qualifiers
[dockapps.git] / wmget / retrieve.c
blobd58b7cbc75ef6ef530f8d8df87b0d2c4f8763cef
1 /*
2 wmget - A background download manager as a Window Maker dock app
3 Copyright (c) 2001-2003 Aaron Trickey <aaron@amtrickey.net>
5 Permission is hereby granted, free of charge, to any person
6 obtaining a copy of this software and associated documentation files
7 (the "Software"), to deal in the Software without restriction,
8 including without limitation the rights to use, copy, modify, merge,
9 publish, distribute, sublicense, and/or sell copies of the Software,
10 and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 ********************************************************************
25 retrieve.c - Child process code for performing downloads
27 When the server [server.c] accepts a download job, it fork()s a
28 new process which in turn invokes this file's retrieve(). This
29 code takes care of setting up and invoking libcurl to grab the
30 data. The shared memory segment (shmem, wmget.h) is inherited from
31 the server and contains the Job structure through which the child
32 process communicates with the server.
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <string.h>
40 #include <sys/types.h>
42 #include <curl/curl.h>
43 #include <curl/easy.h>
45 #include "wmget.h"
47 #if LIBCURL_VERSION_NUM >= 0x070905
48 # define PROGRESS double
49 #else
50 # define PROGRESS size_t
51 #endif
53 static int progress_callback (
54 void *data,
55 PROGRESS total,
56 PROGRESS progress,
57 PROGRESS unused1,
58 PROGRESS unused2)
60 Job *job = data;
62 (void)unused1;
63 (void)unused2;
65 if (job->stop_request) {
66 /* Abort transfer. */
67 job->status = J_STOPPING;
68 return 1;
71 if (!total) {
72 debug ("Total bytes unknown!");
73 total = progress * 2 + 1; /* just to make the bar halfway */
75 job->progress = (unsigned long)progress;
76 job->prog_max = (unsigned long)total;
78 debug ("progress_callback (%d/%d)", job->progress, job->prog_max);
80 return 0;
84 void write_error_file (Job *job, const char *msg)
86 char error_file_name[MAXPATHLEN + 1];
87 FILE *error_file;
89 strcpy (error_file_name, job->options.save_to);
90 strncat (error_file_name, ".ERROR",
91 MAXPATHLEN - strlen (error_file_name));
93 if (!(error_file = fopen (error_file_name, "w")))
94 return;
96 fprintf (error_file, "Download failed:\n");
97 fprintf (error_file, " From URL: %s\n", job->source_url);
98 fprintf (error_file, " To file: %s\n", job->options.save_to);
99 fprintf (error_file, " Error: %s\n", msg);
100 fprintf (error_file, " (" WMGET_VERSION_BANNER ")\n");
101 fclose (error_file);
105 int retrieve (Job *job)
107 CURL *curl;
108 CURLcode rc;
109 FILE *outfp;
110 JobOptions *opts;
112 debug ("Retrieval process %d running job:", getpid());
113 debug_dump_job (job);
115 if (job->options.continue_from) {
116 outfp = fopen (job->options.save_to, "a");
117 } else {
118 outfp = fopen (job->options.save_to, "w");
121 if (!outfp) {
122 error_sys ("could not open `%s' for output",
123 job->options.save_to);
124 return 1;
127 curl = curl_easy_init ();
128 if (!curl) {
129 error ("could not initialize libcurl");
130 write_error_file (job, "could not initialize libcurl");
131 fclose (outfp);
132 return 1;
135 curl_easy_setopt (curl, CURLOPT_FILE, outfp);
136 curl_easy_setopt (curl, CURLOPT_URL, job->source_url);
137 curl_easy_setopt (
138 curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
139 curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, (void *)job);
140 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, job->error);
141 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0);
143 if (job->options.continue_from) {
144 curl_easy_setopt (curl, CURLOPT_RESUME_FROM,
145 (long)job->options.continue_from);
148 /* Now load the job's user-configurable parameters:
150 opts = &job->options;
152 if (opts->proxy[0]) {
153 curl_easy_setopt (curl, CURLOPT_PROXY, opts->proxy);
156 if (opts->follow) {
157 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
158 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, opts->follow);
159 } else {
160 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 0);
163 if (opts->user_agent[0]) {
164 curl_easy_setopt (curl, CURLOPT_USERAGENT, opts->user_agent);
167 if (opts->use_ascii) {
168 curl_easy_setopt (curl, CURLOPT_TRANSFERTEXT, 1);
171 if (opts->referer[0]) {
172 curl_easy_setopt (curl, CURLOPT_REFERER, opts->referer);
175 if (opts->include) {
176 curl_easy_setopt (curl, CURLOPT_HEADER, 1);
179 if (opts->interface[0]) {
180 curl_easy_setopt (curl, CURLOPT_INTERFACE, opts->interface);
183 if (opts->proxy_auth[0]) {
184 curl_easy_setopt (curl, CURLOPT_PROXYUSERPWD, opts->proxy_auth);
187 if (opts->auth[0]) {
188 curl_easy_setopt (curl, CURLOPT_USERPWD, opts->auth);
192 /* If wmget is verbose, set libcurl to verbose too...
194 if (output_level () > OL_NORMAL)
195 curl_easy_setopt (curl, CURLOPT_VERBOSE, 1);
198 /* Finally, perform the download:
200 job->status = J_RUNNING;
201 rc = curl_easy_perform (curl);
203 if (rc) {
204 if (job->status == J_STOPPING) {
205 info ("aborted by user");
206 job->status = J_COMPLETE;
208 } else {
209 error (job->error);
210 write_error_file (job, job->error);
211 job->status = J_COMPLETE;
213 } else {
214 job->status = J_COMPLETE;
217 curl_easy_cleanup (curl);
218 fclose (outfp);
220 if (rc)
221 return 1;
223 return 0;