error handling
[oweals/gnunet.git] / src / fs / gnunet-unindex.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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-unindex.c
22  * @brief unindex files published on GNUnet
23  * @author Christian Grothoff
24  * @author Krista Bennett
25  * @author James Blackwell
26  * @author Igor Wronsky
27  */
28 #include "platform.h"
29 #include "gnunet_fs_service.h"
30
31 static int ret;
32
33 static unsigned int verbose;
34
35 static const struct GNUNET_CONFIGURATION_Handle *cfg;
36
37 static struct GNUNET_FS_Handle *ctx;
38
39 static struct GNUNET_FS_UnindexContext *uc;
40
41
42 static void
43 cleanup_task (void *cls)
44 {
45   GNUNET_FS_stop (ctx);
46   ctx = NULL;
47 }
48
49
50 static void
51 shutdown_task (void *cls)
52 {
53   struct GNUNET_FS_UnindexContext *u;
54
55   if (uc != NULL)
56   {
57     u = uc;
58     uc = NULL;
59     GNUNET_FS_unindex_stop (u);
60   }
61 }
62
63
64 /**
65  * Called by FS client to give information about the progress of an
66  * operation.
67  *
68  * @param cls closure
69  * @param info details about the event, specifying the event type
70  *        and various bits about the event
71  * @return client-context (for the next progress call
72  *         for this operation; should be set to NULL for
73  *         SUSPEND and STOPPED events).  The value returned
74  *         will be passed to future callbacks in the respective
75  *         field in the GNUNET_FS_ProgressInfo struct.
76  */
77 static void *
78 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
79 {
80   const char *s;
81
82   switch (info->status)
83   {
84   case GNUNET_FS_STATUS_UNINDEX_START:
85     break;
86
87   case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
88     if (verbose)
89     {
90       s = GNUNET_STRINGS_relative_time_to_string (info->value.unindex.eta,
91                                                   GNUNET_YES);
92       fprintf (stdout,
93                _ ("Unindexing at %llu/%llu (%s remaining)\n"),
94                (unsigned long long) info->value.unindex.completed,
95                (unsigned long long) info->value.unindex.size,
96                s);
97     }
98     break;
99
100   case GNUNET_FS_STATUS_UNINDEX_ERROR:
101     fprintf (stderr,
102              _ ("Error unindexing: %s.\n"),
103              info->value.unindex.specifics.error.message);
104     GNUNET_SCHEDULER_shutdown ();
105     break;
106
107   case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
108     fprintf (stdout, "%s", _ ("Unindexing done.\n"));
109     GNUNET_SCHEDULER_shutdown ();
110     break;
111
112   case GNUNET_FS_STATUS_UNINDEX_STOPPED:
113     GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
114     break;
115
116   default:
117     fprintf (stderr, _ ("Unexpected status: %d\n"), info->status);
118     break;
119   }
120   return NULL;
121 }
122
123
124 /**
125  * Main function that will be run by the scheduler.
126  *
127  * @param cls closure
128  * @param args remaining command-line arguments
129  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
130  * @param c configuration
131  */
132 static void
133 run (void *cls,
134      char *const *args,
135      const char *cfgfile,
136      const struct GNUNET_CONFIGURATION_Handle *c)
137 {
138   /* check arguments */
139   if ((args[0] == NULL) || (args[1] != NULL))
140   {
141     printf (_ ("You must specify one and only one filename for unindexing.\n"));
142     ret = -1;
143     return;
144   }
145   cfg = c;
146   ctx = GNUNET_FS_start (cfg,
147                          "gnunet-unindex",
148                          &progress_cb,
149                          NULL,
150                          GNUNET_FS_FLAGS_NONE,
151                          GNUNET_FS_OPTIONS_END);
152   if (NULL == ctx)
153   {
154     fprintf (stderr, _ ("Could not initialize `%s' subsystem.\n"), "FS");
155     ret = 1;
156     return;
157   }
158   uc = GNUNET_FS_unindex_start (ctx, args[0], NULL);
159   if (NULL == uc)
160   {
161     fprintf (stderr, "%s", _ ("Could not start unindex operation.\n"));
162     GNUNET_FS_stop (ctx);
163     return;
164   }
165   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
166 }
167
168
169 /**
170  * The main function to unindex content.
171  *
172  * @param argc number of arguments from the command line
173  * @param argv command line arguments
174  * @return 0 ok, 1 on error
175  */
176 int
177 main (int argc, char *const *argv)
178 {
179   struct GNUNET_GETOPT_CommandLineOption options[] = {
180     GNUNET_GETOPT_option_verbose (&verbose),
181
182     GNUNET_GETOPT_OPTION_END
183   };
184
185   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
186     return 2;
187
188   ret = (GNUNET_OK ==
189          GNUNET_PROGRAM_run (
190            argc,
191            argv,
192            "gnunet-unindex [OPTIONS] FILENAME",
193            gettext_noop (
194              "Unindex a file that was previously indexed with gnunet-publish."),
195            options,
196            &run,
197            NULL))
198         ? ret
199         : 1;
200   GNUNET_free ((void *) argv);
201   return ret;
202 }
203
204
205 /* end of gnunet-unindex.c */