diff options
author | ng0 <ng0@n0.is> | 2019-09-06 22:46:29 +0000 |
---|---|---|
committer | ng0 <ng0@n0.is> | 2019-09-06 22:46:29 +0000 |
commit | 6e599264ad13e8fc105493d74d7c11d46f8739ed (patch) | |
tree | 169bef1ecbade5a659831fb169f3ae6943af127f /src/fs/gnunet-download.c | |
parent | 4f13bc15113021ebf71d5d81e99bc29f8a07fc9c (diff) | |
download | gnunet-6e599264ad13e8fc105493d74d7c11d46f8739ed.tar.gz gnunet-6e599264ad13e8fc105493d74d7c11d46f8739ed.zip |
first step to remove plibc
Diffstat (limited to 'src/fs/gnunet-download.c')
-rw-r--r-- | src/fs/gnunet-download.c | 277 |
1 files changed, 138 insertions, 139 deletions
diff --git a/src/fs/gnunet-download.c b/src/fs/gnunet-download.c index 31e62c603..00e8336a2 100644 --- a/src/fs/gnunet-download.c +++ b/src/fs/gnunet-download.c | |||
@@ -80,33 +80,30 @@ shutdown_task (void *cls) | |||
80 | * @param w desired number of steps in the progress bar | 80 | * @param w desired number of steps in the progress bar |
81 | */ | 81 | */ |
82 | static void | 82 | static void |
83 | display_bar (unsigned long long x, | 83 | display_bar (unsigned long long x, unsigned long long n, unsigned int w) |
84 | unsigned long long n, | ||
85 | unsigned int w) | ||
86 | { | 84 | { |
87 | char buf[w + 20]; | 85 | char buf[w + 20]; |
88 | unsigned int p; | 86 | unsigned int p; |
89 | unsigned int endeq; | 87 | unsigned int endeq; |
90 | float ratio_complete; | 88 | float ratio_complete; |
91 | 89 | ||
92 | #if !WINDOWS | 90 | #if ! WINDOWS |
93 | if (0 == isatty (1)) | 91 | if (0 == isatty (1)) |
94 | return; | 92 | return; |
95 | #else | 93 | #else |
96 | if (FILE_TYPE_CHAR != GetFileType (GetStdHandle (STD_OUTPUT_HANDLE))) | 94 | if (FILE_TYPE_CHAR != GetFileType (GetStdHandle (STD_OUTPUT_HANDLE))) |
97 | return; | 95 | return; |
98 | #endif | 96 | #endif |
99 | ratio_complete = x/(float)n; | 97 | ratio_complete = x / (float) n; |
100 | endeq = ratio_complete * w; | 98 | endeq = ratio_complete * w; |
101 | GNUNET_snprintf (buf, sizeof (buf), | 99 | GNUNET_snprintf (buf, sizeof (buf), "%3d%% [", (int) (ratio_complete * 100)); |
102 | "%3d%% [", (int)(ratio_complete*100) ); | 100 | for (p = 0; p < endeq; p++) |
103 | for (p=0; p<endeq; p++) | ||
104 | strcat (buf, "="); | 101 | strcat (buf, "="); |
105 | for (p=endeq; p<w; p++) | 102 | for (p = endeq; p < w; p++) |
106 | strcat (buf, " "); | 103 | strcat (buf, " "); |
107 | strcat (buf, "]\r"); | 104 | strcat (buf, "]\r"); |
108 | printf ("%s", buf); | 105 | printf ("%s", buf); |
109 | fflush(stdout); | 106 | fflush (stdout); |
110 | } | 107 | } |
111 | 108 | ||
112 | 109 | ||
@@ -124,8 +121,7 @@ display_bar (unsigned long long x, | |||
124 | * field in the `struct GNUNET_FS_ProgressInfo` | 121 | * field in the `struct GNUNET_FS_ProgressInfo` |
125 | */ | 122 | */ |
126 | static void * | 123 | static void * |
127 | progress_cb (void *cls, | 124 | progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info) |
128 | const struct GNUNET_FS_ProgressInfo *info) | ||
129 | { | 125 | { |
130 | char *s; | 126 | char *s; |
131 | const char *s2; | 127 | const char *s2; |
@@ -135,71 +131,75 @@ progress_cb (void *cls, | |||
135 | { | 131 | { |
136 | case GNUNET_FS_STATUS_DOWNLOAD_START: | 132 | case GNUNET_FS_STATUS_DOWNLOAD_START: |
137 | if (verbose > 1) | 133 | if (verbose > 1) |
138 | FPRINTF (stderr, | 134 | fprintf (stderr, |
139 | _("Starting download `%s'.\n"), | 135 | _ ("Starting download `%s'.\n"), |
140 | info->value.download.filename); | 136 | info->value.download.filename); |
141 | break; | 137 | break; |
142 | case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS: | 138 | case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS: |
143 | if (verbose) | 139 | if (verbose) |
144 | { | 140 | { |
145 | s = GNUNET_strdup (GNUNET_STRINGS_relative_time_to_string (info->value.download.eta, | 141 | s = GNUNET_strdup ( |
146 | GNUNET_YES)); | 142 | GNUNET_STRINGS_relative_time_to_string (info->value.download.eta, |
147 | if (info->value.download.specifics.progress.block_download_duration.rel_value_us | 143 | GNUNET_YES)); |
148 | == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) | 144 | if (info->value.download.specifics.progress.block_download_duration |
149 | s2 = _("<unknown time>"); | 145 | .rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) |
146 | s2 = _ ("<unknown time>"); | ||
150 | else | 147 | else |
151 | s2 = GNUNET_STRINGS_relative_time_to_string (info->value.download.specifics.progress.block_download_duration, | 148 | s2 = GNUNET_STRINGS_relative_time_to_string (info->value.download |
152 | GNUNET_YES); | 149 | .specifics.progress |
153 | t = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed * | 150 | .block_download_duration, |
154 | 1000LL / | 151 | GNUNET_YES); |
155 | (info->value.download. | 152 | t = GNUNET_STRINGS_byte_size_fancy ( |
156 | duration.rel_value_us + 1)); | 153 | info->value.download.completed * 1000LL / |
157 | FPRINTF (stdout, | 154 | (info->value.download.duration.rel_value_us + 1)); |
158 | _("Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"), | 155 | fprintf ( |
159 | info->value.download.filename, | 156 | stdout, |
160 | (unsigned long long) info->value.download.completed, | 157 | _ ( |
161 | (unsigned long long) info->value.download.size, | 158 | "Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"), |
162 | s, | 159 | info->value.download.filename, |
163 | t, | 160 | (unsigned long long) info->value.download.completed, |
164 | s2); | 161 | (unsigned long long) info->value.download.size, |
162 | s, | ||
163 | t, | ||
164 | s2); | ||
165 | GNUNET_free (s); | 165 | GNUNET_free (s); |
166 | GNUNET_free (t); | 166 | GNUNET_free (t); |
167 | } | 167 | } |
168 | else | 168 | else |
169 | { | 169 | { |
170 | display_bar (info->value.download.completed, | 170 | display_bar (info->value.download.completed, |
171 | info->value.download.size, | 171 | info->value.download.size, |
172 | 60); | 172 | 60); |
173 | } | 173 | } |
174 | break; | 174 | break; |
175 | case GNUNET_FS_STATUS_DOWNLOAD_ERROR: | 175 | case GNUNET_FS_STATUS_DOWNLOAD_ERROR: |
176 | #if !WINDOWS | 176 | #if ! WINDOWS |
177 | if (0 != isatty (1)) | 177 | if (0 != isatty (1)) |
178 | fprintf (stdout, "\n"); | 178 | fprintf (stdout, "\n"); |
179 | #else | 179 | #else |
180 | if (FILE_TYPE_CHAR == | 180 | if (FILE_TYPE_CHAR == GetFileType (GetStdHandle (STD_OUTPUT_HANDLE))) |
181 | GetFileType (GetStdHandle (STD_OUTPUT_HANDLE))) | ||
182 | fprintf (stdout, "\n"); | 181 | fprintf (stdout, "\n"); |
183 | #endif | 182 | #endif |
184 | FPRINTF (stderr, _("Error downloading: %s.\n"), | 183 | fprintf (stderr, |
184 | _ ("Error downloading: %s.\n"), | ||
185 | info->value.download.specifics.error.message); | 185 | info->value.download.specifics.error.message); |
186 | GNUNET_SCHEDULER_shutdown (); | 186 | GNUNET_SCHEDULER_shutdown (); |
187 | break; | 187 | break; |
188 | case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED: | 188 | case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED: |
189 | s = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed * 1000 / | 189 | s = GNUNET_STRINGS_byte_size_fancy ( |
190 | (info->value.download. | 190 | info->value.download.completed * 1000 / |
191 | duration.rel_value_us + 1)); | 191 | (info->value.download.duration.rel_value_us + 1)); |
192 | #if !WINDOWS | 192 | #if ! WINDOWS |
193 | if (0 != isatty (1)) | 193 | if (0 != isatty (1)) |
194 | fprintf (stdout, "\n"); | 194 | fprintf (stdout, "\n"); |
195 | #else | 195 | #else |
196 | if (FILE_TYPE_CHAR == | 196 | if (FILE_TYPE_CHAR == GetFileType (GetStdHandle (STD_OUTPUT_HANDLE))) |
197 | GetFileType (GetStdHandle (STD_OUTPUT_HANDLE))) | ||
198 | fprintf (stdout, "\n"); | 197 | fprintf (stdout, "\n"); |
199 | #endif | 198 | #endif |
200 | FPRINTF (stdout, | 199 | fprintf (stdout, |
201 | _("Downloading `%s' done (%s/s).\n"), | 200 | _ ("Downloading `%s' done (%s/s).\n"), |
202 | info->value.download.filename, s); | 201 | info->value.download.filename, |
202 | s); | ||
203 | GNUNET_free (s); | 203 | GNUNET_free (s); |
204 | if (info->value.download.dc == dc) | 204 | if (info->value.download.dc == dc) |
205 | GNUNET_SCHEDULER_shutdown (); | 205 | GNUNET_SCHEDULER_shutdown (); |
@@ -212,9 +212,7 @@ progress_cb (void *cls, | |||
212 | case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE: | 212 | case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE: |
213 | break; | 213 | break; |
214 | default: | 214 | default: |
215 | FPRINTF (stderr, | 215 | fprintf (stderr, _ ("Unexpected status: %d\n"), info->status); |
216 | _("Unexpected status: %d\n"), | ||
217 | info->status); | ||
218 | break; | 216 | break; |
219 | } | 217 | } |
220 | return NULL; | 218 | return NULL; |
@@ -241,55 +239,45 @@ run (void *cls, | |||
241 | 239 | ||
242 | if (NULL == args[0]) | 240 | if (NULL == args[0]) |
243 | { | 241 | { |
244 | FPRINTF (stderr, | 242 | fprintf (stderr, "%s", _ ("You need to specify a URI argument.\n")); |
245 | "%s", | ||
246 | _("You need to specify a URI argument.\n")); | ||
247 | return; | 243 | return; |
248 | } | 244 | } |
249 | uri = GNUNET_FS_uri_parse (args[0], &emsg); | 245 | uri = GNUNET_FS_uri_parse (args[0], &emsg); |
250 | if (NULL == uri) | 246 | if (NULL == uri) |
251 | { | 247 | { |
252 | FPRINTF (stderr, | 248 | fprintf (stderr, _ ("Failed to parse URI: %s\n"), emsg); |
253 | _("Failed to parse URI: %s\n"), | ||
254 | emsg); | ||
255 | GNUNET_free (emsg); | 249 | GNUNET_free (emsg); |
256 | ret = 1; | 250 | ret = 1; |
257 | return; | 251 | return; |
258 | } | 252 | } |
259 | if ( (! GNUNET_FS_uri_test_chk (uri)) && | 253 | if ((! GNUNET_FS_uri_test_chk (uri)) && (! GNUNET_FS_uri_test_loc (uri))) |
260 | (! GNUNET_FS_uri_test_loc (uri))) | ||
261 | { | 254 | { |
262 | FPRINTF (stderr, | 255 | fprintf (stderr, "%s", _ ("Only CHK or LOC URIs supported.\n")); |
263 | "%s", | ||
264 | _("Only CHK or LOC URIs supported.\n")); | ||
265 | ret = 1; | 256 | ret = 1; |
266 | GNUNET_FS_uri_destroy (uri); | 257 | GNUNET_FS_uri_destroy (uri); |
267 | return; | 258 | return; |
268 | } | 259 | } |
269 | if (NULL == filename) | 260 | if (NULL == filename) |
270 | { | 261 | { |
271 | FPRINTF (stderr, | 262 | fprintf (stderr, "%s", _ ("Target filename must be specified.\n")); |
272 | "%s", | ||
273 | _("Target filename must be specified.\n")); | ||
274 | ret = 1; | 263 | ret = 1; |
275 | GNUNET_FS_uri_destroy (uri); | 264 | GNUNET_FS_uri_destroy (uri); |
276 | return; | 265 | return; |
277 | } | 266 | } |
278 | cfg = c; | 267 | cfg = c; |
279 | ctx = GNUNET_FS_start (cfg, | 268 | ctx = GNUNET_FS_start (cfg, |
280 | "gnunet-download", | 269 | "gnunet-download", |
281 | &progress_cb, NULL, | 270 | &progress_cb, |
282 | GNUNET_FS_FLAGS_NONE, | 271 | NULL, |
283 | GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM, | 272 | GNUNET_FS_FLAGS_NONE, |
284 | parallelism, | 273 | GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM, |
285 | GNUNET_FS_OPTIONS_REQUEST_PARALLELISM, | 274 | parallelism, |
286 | request_parallelism, | 275 | GNUNET_FS_OPTIONS_REQUEST_PARALLELISM, |
287 | GNUNET_FS_OPTIONS_END); | 276 | request_parallelism, |
277 | GNUNET_FS_OPTIONS_END); | ||
288 | if (NULL == ctx) | 278 | if (NULL == ctx) |
289 | { | 279 | { |
290 | FPRINTF (stderr, | 280 | fprintf (stderr, _ ("Could not initialize `%s' subsystem.\n"), "FS"); |
291 | _("Could not initialize `%s' subsystem.\n"), | ||
292 | "FS"); | ||
293 | GNUNET_FS_uri_destroy (uri); | 281 | GNUNET_FS_uri_destroy (uri); |
294 | ret = 1; | 282 | ret = 1; |
295 | return; | 283 | return; |
@@ -300,16 +288,16 @@ run (void *cls, | |||
300 | if (local_only) | 288 | if (local_only) |
301 | options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY; | 289 | options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY; |
302 | dc = GNUNET_FS_download_start (ctx, | 290 | dc = GNUNET_FS_download_start (ctx, |
303 | uri, | 291 | uri, |
304 | NULL, | 292 | NULL, |
305 | filename, | 293 | filename, |
306 | NULL, | 294 | NULL, |
307 | 0, | 295 | 0, |
308 | GNUNET_FS_uri_chk_get_file_size (uri), | 296 | GNUNET_FS_uri_chk_get_file_size (uri), |
309 | anonymity, | 297 | anonymity, |
310 | options, | 298 | options, |
311 | NULL, | 299 | NULL, |
312 | NULL); | 300 | NULL); |
313 | GNUNET_FS_uri_destroy (uri); | 301 | GNUNET_FS_uri_destroy (uri); |
314 | if (dc == NULL) | 302 | if (dc == NULL) |
315 | { | 303 | { |
@@ -317,8 +305,7 @@ run (void *cls, | |||
317 | ctx = NULL; | 305 | ctx = NULL; |
318 | return; | 306 | return; |
319 | } | 307 | } |
320 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, | 308 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); |
321 | NULL); | ||
322 | } | 309 | } |
323 | 310 | ||
324 | 311 | ||
@@ -332,61 +319,73 @@ run (void *cls, | |||
332 | int | 319 | int |
333 | main (int argc, char *const *argv) | 320 | main (int argc, char *const *argv) |
334 | { | 321 | { |
335 | struct GNUNET_GETOPT_CommandLineOption options[] = { | 322 | struct GNUNET_GETOPT_CommandLineOption options[] = |
336 | GNUNET_GETOPT_option_uint ('a', | 323 | {GNUNET_GETOPT_option_uint ('a', |
337 | "anonymity", | 324 | "anonymity", |
338 | "LEVEL", | 325 | "LEVEL", |
339 | gettext_noop ("set the desired LEVEL of receiver-anonymity"), | 326 | gettext_noop ( |
340 | &anonymity), | 327 | "set the desired LEVEL of receiver-anonymity"), |
341 | 328 | &anonymity), | |
342 | GNUNET_GETOPT_option_flag ('D', | 329 | |
343 | "delete-incomplete", | 330 | GNUNET_GETOPT_option_flag ( |
344 | gettext_noop ("delete incomplete downloads (when aborted with CTRL-C)"), | 331 | 'D', |
345 | &delete_incomplete), | 332 | "delete-incomplete", |
346 | 333 | gettext_noop ("delete incomplete downloads (when aborted with CTRL-C)"), | |
347 | GNUNET_GETOPT_option_flag ('n', | 334 | &delete_incomplete), |
348 | "no-network", | 335 | |
349 | gettext_noop ("only search the local peer (no P2P network search)"), | 336 | GNUNET_GETOPT_option_flag ( |
350 | &local_only), | 337 | 'n', |
351 | GNUNET_GETOPT_option_string ('o', | 338 | "no-network", |
352 | "output", | 339 | gettext_noop ("only search the local peer (no P2P network search)"), |
353 | "FILENAME", | 340 | &local_only), |
354 | gettext_noop ("write the file to FILENAME"), | 341 | GNUNET_GETOPT_option_string ('o', |
355 | &filename), | 342 | "output", |
356 | GNUNET_GETOPT_option_uint ('p', | 343 | "FILENAME", |
357 | "parallelism", | 344 | gettext_noop ("write the file to FILENAME"), |
358 | "DOWNLOADS", | 345 | &filename), |
359 | gettext_noop ("set the maximum number of parallel downloads that is allowed"), | 346 | GNUNET_GETOPT_option_uint ( |
360 | ¶llelism), | 347 | 'p', |
361 | GNUNET_GETOPT_option_uint ('r', | 348 | "parallelism", |
362 | "request-parallelism", | 349 | "DOWNLOADS", |
363 | "REQUESTS", | 350 | gettext_noop ( |
364 | gettext_noop ("set the maximum number of parallel requests for blocks that is allowed"), | 351 | "set the maximum number of parallel downloads that is allowed"), |
365 | &request_parallelism), | 352 | ¶llelism), |
366 | GNUNET_GETOPT_option_flag ('R', | 353 | GNUNET_GETOPT_option_uint ( |
367 | "recursive", | 354 | 'r', |
368 | gettext_noop ("download a GNUnet directory recursively"), | 355 | "request-parallelism", |
369 | &do_recursive), | 356 | "REQUESTS", |
370 | GNUNET_GETOPT_option_increment_uint ('V', | 357 | gettext_noop ( |
371 | "verbose", | 358 | "set the maximum number of parallel requests for blocks that is allowed"), |
372 | gettext_noop ("be verbose (print progress information)"), | 359 | &request_parallelism), |
373 | &verbose), | 360 | GNUNET_GETOPT_option_flag ('R', |
374 | GNUNET_GETOPT_OPTION_END | 361 | "recursive", |
375 | }; | 362 | gettext_noop ( |
376 | 363 | "download a GNUnet directory recursively"), | |
377 | if (GNUNET_OK != | 364 | &do_recursive), |
378 | GNUNET_STRINGS_get_utf8_args (argc, argv, | 365 | GNUNET_GETOPT_option_increment_uint ( |
379 | &argc, &argv)) | 366 | 'V', |
367 | "verbose", | ||
368 | gettext_noop ("be verbose (print progress information)"), | ||
369 | &verbose), | ||
370 | GNUNET_GETOPT_OPTION_END}; | ||
371 | |||
372 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | ||
380 | return 2; | 373 | return 2; |
381 | 374 | ||
382 | ret = (GNUNET_OK == | 375 | ret = |
383 | GNUNET_PROGRAM_run (argc, argv, | 376 | (GNUNET_OK == |
384 | "gnunet-download [OPTIONS] URI", | 377 | GNUNET_PROGRAM_run ( |
385 | gettext_noop | 378 | argc, |
386 | ("Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"), | 379 | argv, |
387 | options, | 380 | "gnunet-download [OPTIONS] URI", |
388 | &run, NULL)) ? ret : 1; | 381 | gettext_noop ( |
389 | GNUNET_free ((void*) argv); | 382 | "Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"), |
383 | options, | ||
384 | &run, | ||
385 | NULL)) | ||
386 | ? ret | ||
387 | : 1; | ||
388 | GNUNET_free ((void *) argv); | ||
390 | return ret; | 389 | return ret; |
391 | } | 390 | } |
392 | 391 | ||