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