minor bugfixes
[oweals/gnunet.git] / src / fs / gnunet-unindex.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
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 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 /**
43  * Called by FS client to give information about the progress of an 
44  * operation.
45  *
46  * @param cls closure
47  * @param info details about the event, specifying the event type
48  *        and various bits about the event
49  * @return client-context (for the next progress call
50  *         for this operation; should be set to NULL for
51  *         SUSPEND and STOPPED events).  The value returned
52  *         will be passed to future callbacks in the respective
53  *         field in the GNUNET_FS_ProgressInfo struct.
54  */
55 static void *
56 progress_cb (void *cls,
57              const struct GNUNET_FS_ProgressInfo *info)
58 {
59   char *s;
60
61   switch (info->status)
62     {
63     case GNUNET_FS_STATUS_UNINDEX_START:
64       break;
65     case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
66       if (verbose)
67         {
68           s = GNUNET_STRINGS_relative_time_to_string(info->value.unindex.eta);
69           fprintf (stdout,
70                    _("Unindexing at %llu/%llu (%s remaining)\n"),
71                    (unsigned long long) info->value.unindex.completed,
72                    (unsigned long long) info->value.unindex.size,
73                    s);
74           GNUNET_free (s);
75         }
76       break;
77     case GNUNET_FS_STATUS_UNINDEX_ERROR:
78       fprintf (stderr,
79                _("Error unindexing: %s.\n"),
80                info->value.unindex.specifics.error.message);
81       GNUNET_FS_unindex_stop (uc);      
82       break;
83     case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
84       fprintf (stdout,
85                _("Unindexing done.\n"));
86       GNUNET_FS_unindex_stop (uc);
87       break;
88     case GNUNET_FS_STATUS_UNINDEX_STOPPED:
89       GNUNET_FS_stop (ctx);
90       break;      
91     default:
92       fprintf (stderr,
93                _("Unexpected status: %d\n"),
94                info->status);
95       break;
96     }
97   return NULL;
98 }
99
100
101 /**
102  * Main function that will be run by the scheduler.
103  *
104  * @param cls closure
105  * @param sched the scheduler to use
106  * @param args remaining command-line arguments
107  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
108  * @param c configuration
109  */
110 static void
111 run (void *cls,
112      struct GNUNET_SCHEDULER_Handle *sched,
113      char *const *args,
114      const char *cfgfile,
115      const struct GNUNET_CONFIGURATION_Handle *c)
116 {
117   /* check arguments */
118   if ( (args[0] == NULL) || (args[1] != NULL) ) 
119     {
120       printf (_
121               ("You must specify one and only one filename for unindexing.\n"));
122       ret = -1;
123       return;
124     }
125   cfg = c;
126   ctx = GNUNET_FS_start (sched,
127                          cfg,
128                          "gnunet-unindex",
129                          &progress_cb,
130                          NULL,
131                          GNUNET_FS_FLAGS_NONE,
132                          GNUNET_FS_OPTIONS_END);
133   if (NULL == ctx)
134     {
135       fprintf (stderr,
136                _("Could not initialize `%s' subsystem.\n"),
137                "FS");
138       ret = 1;
139       return;
140     }
141   uc = GNUNET_FS_unindex_start (ctx,
142                                 args[0]);
143   if (NULL == uc)
144     {
145       fprintf (stderr,
146                _("Could not start unindex operation.\n"));
147       GNUNET_FS_stop (ctx);
148     }
149 }
150
151
152 /**
153  * gnunet-unindex command line options
154  */
155 static struct GNUNET_GETOPT_CommandLineOption options[] = {
156   {'V', "verbose", NULL,
157    gettext_noop ("be verbose (print progress information)"),
158    0, &GNUNET_GETOPT_set_one, &verbose},
159   GNUNET_GETOPT_OPTION_END
160 };
161
162
163 /**
164  * The main function to unindex content.
165  *
166  * @param argc number of arguments from the command line
167  * @param argv command line arguments
168  * @return 0 ok, 1 on error
169  */
170 int
171 main (int argc, char *const *argv)
172 {
173   return (GNUNET_OK ==
174           GNUNET_PROGRAM_run (argc,
175                               argv,
176                               "gnunet-unindex",
177                               gettext_noop
178                               ("Unindex files."),
179                               options, &run, NULL)) ? ret : 1;
180 }
181
182 /* end of gnunet-unindex.c */