GNUnet  0.10.x
Functions | Variables
gnunet-download.c File Reference

downloading for files on GNUnet More...

#include "platform.h"
#include "gnunet_fs_service.h"
Include dependency graph for gnunet-download.c:

Go to the source code of this file.

Functions

static void cleanup_task (void *cls)
 
static void shutdown_task (void *cls)
 
static void display_bar (unsigned long long x, unsigned long long n, unsigned int w)
 Display progress bar (if tty). More...
 
static void * progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
 Called by FS client to give information about the progress of an operation. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
 Main function that will be run by the scheduler. More...
 
int main (int argc, char *const *argv)
 The main function to download GNUnet. More...
 

Variables

static int ret
 
static unsigned int verbose
 
static int delete_incomplete
 
static const struct GNUNET_CONFIGURATION_Handlecfg
 
static struct GNUNET_FS_Handlectx
 
static struct GNUNET_FS_DownloadContextdc
 
static unsigned int anonymity = 1
 
static unsigned int parallelism = 16
 
static unsigned int request_parallelism = 4092
 
static int do_recursive
 
static char * filename
 
static int local_only
 

Detailed Description

downloading for files on GNUnet

Author
Christian Grothoff
Krista Bennett
James Blackwell
Igor Wronsky

Definition in file gnunet-download.c.

Function Documentation

◆ cleanup_task()

static void cleanup_task ( void *  cls)
static

Definition at line 57 of file gnunet-download.c.

References GNUNET_FS_stop().

Referenced by progress_cb().

58 {
60  ctx = NULL;
61 }
static struct GNUNET_FS_Handle * ctx
void GNUNET_FS_stop(struct GNUNET_FS_Handle *h)
Close our connection with the file-sharing service.
Definition: fs_api.c:3163
Here is the call graph for this function:
Here is the caller graph for this function:

◆ shutdown_task()

static void shutdown_task ( void *  cls)
static

Definition at line 65 of file gnunet-download.c.

References delete_incomplete, and GNUNET_FS_download_stop().

Referenced by run().

66 {
67  if (NULL != dc)
68  {
70  dc = NULL;
71  }
72 }
void GNUNET_FS_download_stop(struct GNUNET_FS_DownloadContext *dc, int do_delete)
Stop a download (aborts if download is incomplete).
Definition: fs_download.c:2313
static int delete_incomplete
static struct GNUNET_FS_DownloadContext * dc
Here is the call graph for this function:
Here is the caller graph for this function:

◆ display_bar()

static void display_bar ( unsigned long long  x,
unsigned long long  n,
unsigned int  w 
)
static

Display progress bar (if tty).

Parameters
xcurrent position in the download
ntotal size of the download
wdesired number of steps in the progress bar

Definition at line 83 of file gnunet-download.c.

References buf, GNUNET_snprintf(), and p.

Referenced by progress_cb().

86 {
87  char buf[w + 20];
88  unsigned int p;
89  unsigned int endeq;
90  float ratio_complete;
91 
92 #if !WINDOWS
93  if (0 == isatty (1))
94  return;
95 #else
96  if (FILE_TYPE_CHAR != GetFileType (GetStdHandle (STD_OUTPUT_HANDLE)))
97  return;
98 #endif
99  ratio_complete = x/(float)n;
100  endeq = ratio_complete * w;
101  GNUNET_snprintf (buf, sizeof (buf),
102  "%3d%% [", (int)(ratio_complete*100) );
103  for (p=0; p<endeq; p++)
104  strcat (buf, "=");
105  for (p=endeq; p<w; p++)
106  strcat (buf, " ");
107  strcat (buf, "]\r");
108  printf ("%s", buf);
109  fflush(stdout);
110 }
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
static char buf[2048]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ progress_cb()

static void* progress_cb ( void *  cls,
const struct GNUNET_FS_ProgressInfo info 
)
static

Called by FS client to give information about the progress of an operation.

Parameters
clsclosure
infodetails about the event, specifying the event type and various bits about the event
Returns
client-context (for the next progress call for this operation; should be set to NULL for SUSPEND and STOPPED events). The value returned will be passed to future callbacks in the respective field in the struct GNUNET_FS_ProgressInfo

Definition at line 127 of file gnunet-download.c.

References _, cleanup_task(), display_bar(), GNUNET_FS_ProgressInfo::download, duration, FPRINTF, GNUNET_free, GNUNET_FS_STATUS_DOWNLOAD_ACTIVE, GNUNET_FS_STATUS_DOWNLOAD_COMPLETED, GNUNET_FS_STATUS_DOWNLOAD_ERROR, GNUNET_FS_STATUS_DOWNLOAD_INACTIVE, GNUNET_FS_STATUS_DOWNLOAD_PROGRESS, GNUNET_FS_STATUS_DOWNLOAD_START, GNUNET_FS_STATUS_DOWNLOAD_STOPPED, GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_shutdown(), GNUNET_strdup, GNUNET_STRINGS_byte_size_fancy(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, GNUNET_TIME_Relative::rel_value_us, GNUNET_FS_ProgressInfo::status, t, GNUNET_FS_ProgressInfo::value, and verbose.

Referenced by run().

129 {
130  char *s;
131  const char *s2;
132  char *t;
133 
134  switch (info->status)
135  {
137  if (verbose > 1)
138  FPRINTF (stderr,
139  _("Starting download `%s'.\n"),
140  info->value.download.filename);
141  break;
143  if (verbose)
144  {
146  GNUNET_YES));
147  if (info->value.download.specifics.progress.block_download_duration.rel_value_us
148  == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
149  s2 = _("<unknown time>");
150  else
151  s2 = GNUNET_STRINGS_relative_time_to_string (info->value.download.specifics.progress.block_download_duration,
152  GNUNET_YES);
153  t = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed *
154  1000LL /
155  (info->value.download.
156  duration.rel_value_us + 1));
157  FPRINTF (stdout,
158  _("Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"),
159  info->value.download.filename,
160  (unsigned long long) info->value.download.completed,
161  (unsigned long long) info->value.download.size,
162  s,
163  t,
164  s2);
165  GNUNET_free (s);
166  GNUNET_free (t);
167  }
168  else
169  {
170  display_bar (info->value.download.completed,
171  info->value.download.size,
172  60);
173  }
174  break;
176 #if !WINDOWS
177  if (0 != isatty (1))
178  fprintf (stdout, "\n");
179 #else
180  if (FILE_TYPE_CHAR ==
181  GetFileType (GetStdHandle (STD_OUTPUT_HANDLE)))
182  fprintf (stdout, "\n");
183 #endif
184  FPRINTF (stderr, _("Error downloading: %s.\n"),
185  info->value.download.specifics.error.message);
187  break;
189  s = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed * 1000 /
190  (info->value.download.
191  duration.rel_value_us + 1));
192 #if !WINDOWS
193  if (0 != isatty (1))
194  fprintf (stdout, "\n");
195 #else
196  if (FILE_TYPE_CHAR ==
197  GetFileType (GetStdHandle (STD_OUTPUT_HANDLE)))
198  fprintf (stdout, "\n");
199 #endif
200  FPRINTF (stdout,
201  _("Downloading `%s' done (%s/s).\n"),
202  info->value.download.filename, s);
203  GNUNET_free (s);
204  if (info->value.download.dc == dc)
206  break;
208  if (info->value.download.dc == dc)
210  break;
213  break;
214  default:
215  FPRINTF (stderr,
216  _("Unexpected status: %d\n"),
217  info->status);
218  break;
219  }
220  return NULL;
221 }
Notification that we have started this download.
uint64_t rel_value_us
The actual value.
static unsigned int verbose
char * GNUNET_STRINGS_byte_size_fancy(unsigned long long size)
Convert a given filesize into a fancy human-readable format.
Definition: strings.c:174
#define FPRINTF
Definition: plibc.h:683
struct GNUNET_FS_ProgressInfo::@28::@30 download
Values for all "GNUNET_FS_STATUS_DOWNLOAD_*" events.
static void cleanup_task(void *cls)
static struct GNUNET_SCHEDULER_Task * t
Main task.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:524
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
Notification that this download completed.
union GNUNET_FS_ProgressInfo::@28 value
Values that depend on the event type.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1273
enum GNUNET_FS_Status status
Specific status code (determines the event type).
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:727
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
Notification that this download is no longer actively being pursued (back in the queue).
Notification about progress with this download.
Notification that this download is now actively being pursued (as opposed to waiting in the queue)...
static struct GNUNET_TIME_Relative duration
How long do we run the test?
#define GNUNET_YES
Definition: gnunet_common.h:80
Notification that this download was stopped (final event with respect to this action).
static void display_bar(unsigned long long x, unsigned long long n, unsigned int w)
Display progress bar (if tty).
Notification that this download encountered an error.
static struct GNUNET_FS_DownloadContext * dc
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle c 
)
static

Main function that will be run by the scheduler.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cconfiguration

Definition at line 233 of file gnunet-download.c.

References _, anonymity, do_recursive, filename, FPRINTF, GNUNET_free, GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY, GNUNET_FS_DOWNLOAD_OPTION_NONE, GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE, GNUNET_FS_download_start(), GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM, GNUNET_FS_OPTIONS_END, GNUNET_FS_OPTIONS_REQUEST_PARALLELISM, GNUNET_FS_start(), GNUNET_FS_stop(), GNUNET_FS_uri_chk_get_file_size(), GNUNET_FS_uri_destroy(), GNUNET_FS_uri_parse(), GNUNET_FS_uri_test_chk(), GNUNET_FS_uri_test_loc(), GNUNET_SCHEDULER_add_shutdown(), local_only, options, parallelism, progress_cb(), request_parallelism, ret, shutdown_task(), and uri.

Referenced by main().

237 {
238  struct GNUNET_FS_Uri *uri;
239  char *emsg;
241 
242  if (NULL == args[0])
243  {
244  FPRINTF (stderr,
245  "%s",
246  _("You need to specify a URI argument.\n"));
247  return;
248  }
249  uri = GNUNET_FS_uri_parse (args[0], &emsg);
250  if (NULL == uri)
251  {
252  FPRINTF (stderr,
253  _("Failed to parse URI: %s\n"),
254  emsg);
255  GNUNET_free (emsg);
256  ret = 1;
257  return;
258  }
259  if ( (! GNUNET_FS_uri_test_chk (uri)) &&
260  (! GNUNET_FS_uri_test_loc (uri)))
261  {
262  FPRINTF (stderr,
263  "%s",
264  _("Only CHK or LOC URIs supported.\n"));
265  ret = 1;
266  GNUNET_FS_uri_destroy (uri);
267  return;
268  }
269  if (NULL == filename)
270  {
271  FPRINTF (stderr,
272  "%s",
273  _("Target filename must be specified.\n"));
274  ret = 1;
275  GNUNET_FS_uri_destroy (uri);
276  return;
277  }
278  cfg = c;
280  "gnunet-download",
281  &progress_cb, NULL,
284  parallelism,
288  if (NULL == ctx)
289  {
290  FPRINTF (stderr,
291  _("Could not initialize `%s' subsystem.\n"),
292  "FS");
293  GNUNET_FS_uri_destroy (uri);
294  ret = 1;
295  return;
296  }
298  if (do_recursive)
300  if (local_only)
303  uri,
304  NULL,
305  filename,
306  NULL,
307  0,
309  anonymity,
310  options,
311  NULL,
312  NULL);
313  GNUNET_FS_uri_destroy (uri);
314  if (dc == NULL)
315  {
317  ctx = NULL;
318  return;
319  }
321  NULL);
322 }
static unsigned int parallelism
static int ret
static const struct GNUNET_CONFIGURATION_Handle * cfg
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define FPRINTF
Definition: plibc.h:683
struct GNUNET_FS_Uri * GNUNET_FS_uri_parse(const char *uri, char **emsg)
Convert a UTF-8 String to a URI.
Definition: fs_uri.c:635
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1293
static struct GNUNET_FS_Handle * ctx
Last option in the VARARG list.
void GNUNET_FS_stop(struct GNUNET_FS_Handle *h)
Close our connection with the file-sharing service.
Definition: fs_api.c:3163
struct GNUNET_FS_DownloadContext * GNUNET_FS_download_start(struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri, const struct GNUNET_CONTAINER_MetaData *meta, const char *filename, const char *tempname, uint64_t offset, uint64_t length, uint32_t anonymity, enum GNUNET_FS_DownloadOptions options, void *cctx, struct GNUNET_FS_DownloadContext *parent)
Download parts of a file.
Definition: fs_download.c:2126
struct GNUNET_FS_Handle * GNUNET_FS_start(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *client_name, GNUNET_FS_ProgressCallback upcb, void *upcb_cls, enum GNUNET_FS_Flags flags,...)
Setup a connection to the file-sharing service.
Definition: fs_api.c:3097
No special flags set.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
int GNUNET_FS_uri_test_loc(const struct GNUNET_FS_Uri *uri)
Is this a location URI?
Definition: fs_uri.c:1381
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
static unsigned int request_parallelism
static void * progress_cb(void *cls, const struct GNUNET_FS_ProgressInfo *info)
Called by FS client to give information about the progress of an operation.
Only download from the local host, do not access remote systems (no P2P)
static int local_only
static char * filename
No options (use defaults for everything).
void GNUNET_FS_uri_destroy(struct GNUNET_FS_Uri *uri)
Free URI.
Definition: fs_uri.c:670
int GNUNET_FS_uri_test_chk(const struct GNUNET_FS_Uri *uri)
Is this a file (or directory) URI?
Definition: fs_uri.c:1345
GNUNET_FS_DownloadOptions
Options for downloading.
Select the desired amount of parallelism (this option should be followed by an "unsigned int" giving ...
static void shutdown_task(void *cls)
Maximum number of requests that should be pending at a given point in time (invidivual downloads may ...
Do a recursive download (that is, automatically trigger the download of files in directories).
uint64_t GNUNET_FS_uri_chk_get_file_size(const struct GNUNET_FS_Uri *uri)
What is the size of the file that this URI refers to?
Definition: fs_uri.c:1359
A Universal Resource Identifier (URI), opaque.
Definition: fs_api.h:168
static int do_recursive
static unsigned int anonymity
static struct GNUNET_FS_DownloadContext * dc
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const *  argv 
)

The main function to download GNUnet.

Parameters
argcnumber of arguments from the command line
argvcommand line arguments
Returns
0 ok, 1 on error

Definition at line 333 of file gnunet-download.c.

References anonymity, delete_incomplete, do_recursive, filename, gettext_noop, GNUNET_free, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_flag(), GNUNET_GETOPT_option_increment_uint(), GNUNET_GETOPT_option_string(), GNUNET_GETOPT_option_uint(), GNUNET_OK, GNUNET_PROGRAM_run(), GNUNET_STRINGS_get_utf8_args(), local_only, parallelism, request_parallelism, ret, run(), and verbose.

334 {
337  "anonymity",
338  "LEVEL",
339  gettext_noop ("set the desired LEVEL of receiver-anonymity"),
340  &anonymity),
341 
343  "delete-incomplete",
344  gettext_noop ("delete incomplete downloads (when aborted with CTRL-C)"),
346 
348  "no-network",
349  gettext_noop ("only search the local peer (no P2P network search)"),
350  &local_only),
352  "output",
353  "FILENAME",
354  gettext_noop ("write the file to FILENAME"),
355  &filename),
357  "parallelism",
358  "DOWNLOADS",
359  gettext_noop ("set the maximum number of parallel downloads that is allowed"),
360  &parallelism),
362  "request-parallelism",
363  "REQUESTS",
364  gettext_noop ("set the maximum number of parallel requests for blocks that is allowed"),
367  "recursive",
368  gettext_noop ("download a GNUnet directory recursively"),
369  &do_recursive),
371  "verbose",
372  gettext_noop ("be verbose (print progress information)"),
373  &verbose),
375  };
376 
377  if (GNUNET_OK !=
378  GNUNET_STRINGS_get_utf8_args (argc, argv,
379  &argc, &argv))
380  return 2;
381 
382  ret = (GNUNET_OK ==
383  GNUNET_PROGRAM_run (argc, argv,
384  "gnunet-download [OPTIONS] URI",
386  ("Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"),
387  options,
388  &run, NULL)) ? ret : 1;
389  GNUNET_free ((void*) argv);
390  return ret;
391 }
static unsigned int parallelism
static int ret
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
static unsigned int verbose
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1521
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_increment_uint(char shortName, const char *name, const char *description, unsigned int *val)
Increment val each time the option flag is given by one.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
Definition of a command line option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
static unsigned int request_parallelism
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run by the scheduler.
static int local_only
static char * filename
static int delete_incomplete
static int do_recursive
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:361
static unsigned int anonymity
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69
Here is the call graph for this function:

Variable Documentation

◆ ret

int ret
static

Definition at line 31 of file gnunet-download.c.

Referenced by main(), and run().

◆ verbose

unsigned int verbose
static

Definition at line 33 of file gnunet-download.c.

Referenced by main(), and progress_cb().

◆ delete_incomplete

int delete_incomplete
static

Definition at line 35 of file gnunet-download.c.

Referenced by main(), and shutdown_task().

◆ cfg

const struct GNUNET_CONFIGURATION_Handle* cfg
static

Definition at line 37 of file gnunet-download.c.

◆ ctx

struct GNUNET_FS_Handle* ctx
static

Definition at line 39 of file gnunet-download.c.

◆ dc

struct GNUNET_FS_DownloadContext* dc
static

◆ anonymity

unsigned int anonymity = 1
static

◆ parallelism

unsigned int parallelism = 16
static

Definition at line 45 of file gnunet-download.c.

Referenced by adapt_parallelism(), main(), and run().

◆ request_parallelism

unsigned int request_parallelism = 4092
static

Definition at line 47 of file gnunet-download.c.

Referenced by main(), and run().

◆ do_recursive

int do_recursive
static

Definition at line 49 of file gnunet-download.c.

Referenced by main(), and run().

◆ filename

char* filename
static

◆ local_only

int local_only
static

Definition at line 53 of file gnunet-download.c.

Referenced by main(), and run().