diff options
Diffstat (limited to 'src/cli/fs/gnunet-fs.c')
-rw-r--r-- | src/cli/fs/gnunet-fs.c | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/src/cli/fs/gnunet-fs.c b/src/cli/fs/gnunet-fs.c new file mode 100644 index 000000000..21e3c4a40 --- /dev/null +++ b/src/cli/fs/gnunet-fs.c | |||
@@ -0,0 +1,191 @@ | |||
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 | |||
27 | #include "gnunet_fs_service.h" | ||
28 | |||
29 | /** | ||
30 | * Return value. | ||
31 | */ | ||
32 | static int ret; | ||
33 | |||
34 | /** | ||
35 | * Handle to FS service. | ||
36 | */ | ||
37 | static struct GNUNET_FS_Handle *fs; | ||
38 | |||
39 | /** | ||
40 | * Handle for the index listing operation. | ||
41 | */ | ||
42 | static struct GNUNET_FS_GetIndexedContext *gic; | ||
43 | |||
44 | /** | ||
45 | * Option -i given? | ||
46 | */ | ||
47 | static int list_indexed_files; | ||
48 | |||
49 | /** | ||
50 | * Option -v given? | ||
51 | */ | ||
52 | static unsigned int verbose; | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Print indexed filenames to stdout. | ||
57 | * | ||
58 | * @param cls closure | ||
59 | * @param filename the name of the file | ||
60 | * @param file_id hash of the contents of the indexed file | ||
61 | * @return #GNUNET_OK to continue iteration | ||
62 | */ | ||
63 | static enum GNUNET_GenericReturnValue | ||
64 | print_indexed (void *cls, | ||
65 | const char *filename, | ||
66 | const struct GNUNET_HashCode *file_id) | ||
67 | { | ||
68 | if (NULL == filename) | ||
69 | { | ||
70 | gic = NULL; | ||
71 | GNUNET_SCHEDULER_shutdown (); | ||
72 | return GNUNET_OK; | ||
73 | } | ||
74 | if (verbose) | ||
75 | fprintf (stdout, | ||
76 | "%s: %s\n", | ||
77 | GNUNET_h2s (file_id), | ||
78 | filename); | ||
79 | else | ||
80 | fprintf (stdout, | ||
81 | "%s\n", | ||
82 | filename); | ||
83 | return GNUNET_OK; | ||
84 | } | ||
85 | |||
86 | |||
87 | /** | ||
88 | * Function run on shutdown. | ||
89 | * | ||
90 | * @param cls NULL | ||
91 | */ | ||
92 | static void | ||
93 | do_shutdown (void *cls) | ||
94 | { | ||
95 | (void) cls; | ||
96 | if (NULL != gic) | ||
97 | { | ||
98 | GNUNET_FS_get_indexed_files_cancel (gic); | ||
99 | gic = NULL; | ||
100 | } | ||
101 | if (NULL != fs) | ||
102 | { | ||
103 | GNUNET_FS_stop (fs); | ||
104 | fs = NULL; | ||
105 | } | ||
106 | } | ||
107 | |||
108 | |||
109 | /** | ||
110 | * Main function that will be run by the scheduler. | ||
111 | * | ||
112 | * @param cls closure | ||
113 | * @param args remaining command-line arguments | ||
114 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
115 | * @param cfg configuration | ||
116 | */ | ||
117 | static void | ||
118 | run (void *cls, | ||
119 | char *const *args, | ||
120 | const char *cfgfile, | ||
121 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
122 | { | ||
123 | if (! list_indexed_files) | ||
124 | return; | ||
125 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, | ||
126 | NULL); | ||
127 | fs = GNUNET_FS_start (cfg, | ||
128 | "gnunet-fs", | ||
129 | NULL, | ||
130 | NULL, | ||
131 | GNUNET_FS_FLAGS_NONE, | ||
132 | GNUNET_FS_OPTIONS_END); | ||
133 | if (NULL == fs) | ||
134 | { | ||
135 | ret = 1; | ||
136 | return; | ||
137 | } | ||
138 | gic = GNUNET_FS_get_indexed_files (fs, | ||
139 | &print_indexed, | ||
140 | NULL); | ||
141 | if (NULL == gic) | ||
142 | { | ||
143 | ret = 2; | ||
144 | GNUNET_SCHEDULER_shutdown (); | ||
145 | return; | ||
146 | } | ||
147 | } | ||
148 | |||
149 | |||
150 | /** | ||
151 | * The main function to access special file-sharing functions. | ||
152 | * | ||
153 | * @param argc number of arguments from the command line | ||
154 | * @param argv command line arguments | ||
155 | * @return 0 ok, 1 on error | ||
156 | */ | ||
157 | int | ||
158 | main (int argc, | ||
159 | char *const *argv) | ||
160 | { | ||
161 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
162 | GNUNET_GETOPT_option_flag ('i', | ||
163 | "list-indexed", | ||
164 | gettext_noop ( | ||
165 | "print a list of all indexed files"), | ||
166 | &list_indexed_files), | ||
167 | |||
168 | GNUNET_GETOPT_option_verbose (&verbose), | ||
169 | GNUNET_GETOPT_OPTION_END | ||
170 | }; | ||
171 | |||
172 | if (GNUNET_OK != | ||
173 | GNUNET_STRINGS_get_utf8_args (argc, argv, | ||
174 | &argc, &argv)) | ||
175 | return 2; | ||
176 | ret = (GNUNET_OK == | ||
177 | GNUNET_PROGRAM_run (argc, | ||
178 | argv, | ||
179 | "gnunet-fs [OPTIONS]", | ||
180 | gettext_noop ("Special file-sharing operations"), | ||
181 | options, | ||
182 | &run, | ||
183 | NULL)) | ||
184 | ? ret | ||
185 | : 1; | ||
186 | GNUNET_free_nz ((void *) argv); | ||
187 | return ret; | ||
188 | } | ||
189 | |||
190 | |||
191 | /* end of gnunet-fs.c */ | ||