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