paragraph for gnunet devs that don't know how to use the web
[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 Affero 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file fs/gnunet-fs.c
20  * @brief special file-sharing functions
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_fs_service.h"
25
26 /**
27  * Return value.
28  */
29 static int ret;
30
31 /**
32  * Handle to FS service.
33  */
34 static struct GNUNET_FS_Handle *fs;
35
36 /**
37  * Option -i given?
38  */
39 static int list_indexed_files;
40
41 /**
42  * Option -v given?
43  */
44 static unsigned int verbose;
45
46
47 /**
48  * Print indexed filenames to stdout.
49  *
50  * @param cls closure
51  * @param filename the name of the file
52  * @param file_id hash of the contents of the indexed file
53  * @return GNUNET_OK to continue iteration
54  */
55 static int
56 print_indexed (void *cls, const char *filename, const struct GNUNET_HashCode * file_id)
57 {
58   if (NULL == filename)
59   {
60     GNUNET_FS_stop (fs);
61     fs = NULL;
62     return GNUNET_OK;
63   }
64   if (verbose)
65     FPRINTF (stdout, "%s: %s\n", GNUNET_h2s (file_id), filename);
66   else
67     FPRINTF (stdout, "%s\n", filename);
68   return GNUNET_OK;
69 }
70
71
72 /**
73  * Main function that will be run by the scheduler.
74  *
75  * @param cls closure
76  * @param args remaining command-line arguments
77  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
78  * @param cfg configuration
79  */
80 static void
81 run (void *cls, char *const *args, const char *cfgfile,
82      const struct GNUNET_CONFIGURATION_Handle *cfg)
83 {
84   if (list_indexed_files)
85   {
86     fs = GNUNET_FS_start (cfg, "gnunet-fs", NULL, NULL, GNUNET_FS_FLAGS_NONE,
87                           GNUNET_FS_OPTIONS_END);
88     if (NULL == fs)
89     {
90       ret = 1;
91       return;
92     }
93     if (NULL == GNUNET_FS_get_indexed_files (fs, &print_indexed, NULL))
94     {
95       ret = 2;
96       GNUNET_FS_stop (fs);
97       fs = NULL;
98       return;
99     }
100   }
101 }
102
103 /**
104  * The main function to access special file-sharing functions.
105  *
106  * @param argc number of arguments from the command line
107  * @param argv command line arguments
108  * @return 0 ok, 1 on error
109  */
110 int
111 main (int argc, char *const *argv)
112 {
113   struct GNUNET_GETOPT_CommandLineOption options[] = {
114
115     GNUNET_GETOPT_option_flag ('i',
116                                   "list-indexed",
117                                   gettext_noop ("print a list of all indexed files"),
118                                   &list_indexed_files),
119
120     GNUNET_GETOPT_option_verbose (&verbose),
121     GNUNET_GETOPT_OPTION_END
122   };
123
124   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
125     return 2;
126   ret = (GNUNET_OK ==
127          GNUNET_PROGRAM_run (argc, argv, "gnunet-fs [OPTIONS]",
128                              gettext_noop ("Special file-sharing operations"),
129                              options, &run, NULL)) ? ret : 1;
130   GNUNET_free ((void*) argv);
131   return ret;
132 }
133
134 /* end of gnunet-fs.c */