first batch of license fixes (boring)
[oweals/gnunet.git] / src / fs / gnunet-fs.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14 */
15 /**
16  * @file fs/gnunet-fs.c
17  * @brief special file-sharing functions
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include "gnunet_fs_service.h"
22
23 /**
24  * Return value.
25  */
26 static int ret;
27
28 /**
29  * Handle to FS service.
30  */
31 static struct GNUNET_FS_Handle *fs;
32
33 /**
34  * Option -i given?
35  */
36 static int list_indexed_files;
37
38 /**
39  * Option -v given?
40  */
41 static unsigned int verbose;
42
43
44 /**
45  * Print indexed filenames to stdout.
46  *
47  * @param cls closure
48  * @param filename the name of the file
49  * @param file_id hash of the contents of the indexed file
50  * @return GNUNET_OK to continue iteration
51  */
52 static int
53 print_indexed (void *cls, const char *filename, const struct GNUNET_HashCode * file_id)
54 {
55   if (NULL == filename)
56   {
57     GNUNET_FS_stop (fs);
58     fs = NULL;
59     return GNUNET_OK;
60   }
61   if (verbose)
62     FPRINTF (stdout, "%s: %s\n", GNUNET_h2s (file_id), filename);
63   else
64     FPRINTF (stdout, "%s\n", filename);
65   return GNUNET_OK;
66 }
67
68
69 /**
70  * Main function that will be run by the scheduler.
71  *
72  * @param cls closure
73  * @param args remaining command-line arguments
74  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
75  * @param cfg configuration
76  */
77 static void
78 run (void *cls, char *const *args, const char *cfgfile,
79      const struct GNUNET_CONFIGURATION_Handle *cfg)
80 {
81   if (list_indexed_files)
82   {
83     fs = GNUNET_FS_start (cfg, "gnunet-fs", NULL, NULL, GNUNET_FS_FLAGS_NONE,
84                           GNUNET_FS_OPTIONS_END);
85     if (NULL == fs)
86     {
87       ret = 1;
88       return;
89     }
90     if (NULL == GNUNET_FS_get_indexed_files (fs, &print_indexed, NULL))
91     {
92       ret = 2;
93       GNUNET_FS_stop (fs);
94       fs = NULL;
95       return;
96     }
97   }
98 }
99
100 /**
101  * The main function to access special file-sharing functions.
102  *
103  * @param argc number of arguments from the command line
104  * @param argv command line arguments
105  * @return 0 ok, 1 on error
106  */
107 int
108 main (int argc, char *const *argv)
109 {
110   struct GNUNET_GETOPT_CommandLineOption options[] = {
111
112     GNUNET_GETOPT_option_flag ('i',
113                                   "list-indexed",
114                                   gettext_noop ("print a list of all indexed files"),
115                                   &list_indexed_files),
116
117     GNUNET_GETOPT_option_verbose (&verbose),
118     GNUNET_GETOPT_OPTION_END
119   };
120
121   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
122     return 2;
123   ret = (GNUNET_OK ==
124          GNUNET_PROGRAM_run (argc, argv, "gnunet-fs [OPTIONS]",
125                              gettext_noop ("Special file-sharing operations"),
126                              options, &run, NULL)) ? ret : 1;
127   GNUNET_free ((void*) argv);
128   return ret;
129 }
130
131 /* end of gnunet-fs.c */