fe2ae8af66508b573f6ced3a73f729d7554ebd40
[oweals/gnunet.git] / src / fs / gnunet-directory.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-directory.c
22  * @brief display content of GNUnet directories
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_fs_service.h"
27
28 static int ret;
29
30 /**
31  * Print a meta data entry.
32  *
33  * @param cls closure (unused)
34  * @param type type of the keyword
35  * @param format format of data
36  * @param data_mime_type mime type of data
37  * @param data value of the meta data
38  * @param data_size number of bytes in data
39  * @return always 0 (to continue iterating)
40  */
41 static int
42 item_printer (void *cls,
43               const char *plugin_name,
44               enum EXTRACTOR_MetaType type, 
45               enum EXTRACTOR_MetaFormat format,
46               const char *data_mime_type,
47               const char *data,
48               size_t data_size)
49 {
50   if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
51        (format != EXTRACTOR_METAFORMAT_C_STRING) )
52     return 0;
53   printf ("\t%20s: %s\n",
54           dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
55                     EXTRACTOR_metatype_to_string (type)), data);
56   return 0;
57 }
58
59
60
61 /**
62  * Print an entry in a directory.
63  *
64  * @param cls closure (not used)
65  * @param filename name of the file in the directory
66  * @param uri URI of the file
67  * @param meta metadata for the file; metadata for
68  *        the directory if everything else is NULL/zero
69  * @param length length of the available data for the file
70  *           (of type size_t since data must certainly fit
71  *            into memory; if files are larger than size_t
72  *            permits, then they will certainly not be
73  *            embedded with the directory itself).
74  * @param data data available for the file (length bytes)
75  */
76 static void
77 print_entry (void *cls,
78              const char *filename,
79              const struct GNUNET_FS_Uri *uri,
80              const struct GNUNET_CONTAINER_MetaData *meta,
81              size_t length,
82              const void *data)
83 {
84   char *string;
85
86   string = GNUNET_FS_uri_to_string (uri);
87   printf ("%s:\n", string);
88   GNUNET_free (string);
89   GNUNET_CONTAINER_meta_data_iterate (meta,
90                                       &item_printer,
91                                       NULL);
92 }
93
94
95 /**
96  * Main function that will be run by the scheduler.
97  *
98  * @param cls closure
99  * @param sched the scheduler to use
100  * @param args remaining command-line arguments
101  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
102  * @param cfg configuration
103  */
104 static void
105 run (void *cls,
106      struct GNUNET_SCHEDULER_Handle *sched,
107      char *const *args,
108      const char *cfgfile,
109      const struct GNUNET_CONFIGURATION_Handle *cfg)
110 {
111   struct GNUNET_DISK_MapHandle *map;
112   struct GNUNET_DISK_FileHandle *h;
113   void *data;
114   size_t len;
115   uint64_t size;
116   const char *filename;
117   int i;
118
119   if (NULL == args[0])
120     {
121       fprintf (stderr,
122                _("You must specify a filename to inspect."));
123       ret = 1;
124       return;
125     }
126   i = 0;
127   while (NULL != (filename = args[i++]))
128     {
129       if ( (GNUNET_OK !=
130             GNUNET_DISK_file_size (filename,
131                                    &size,
132                                    GNUNET_YES)) ||
133            (NULL == (h = GNUNET_DISK_file_open (filename,
134                                                 GNUNET_DISK_OPEN_READ,
135                                                 GNUNET_DISK_PERM_NONE))) )
136         {
137           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
138                       _("Failed to read directory `%s'\n"),
139                       filename);
140           ret = 1;
141           continue;
142         }
143       len = (size_t) size;
144       data = GNUNET_DISK_file_map (h,
145                                    &map,
146                                    GNUNET_DISK_MAP_TYPE_READ,
147                                    len);
148       GNUNET_assert (NULL != data);
149       GNUNET_FS_directory_list_contents (len,
150                                          data,
151                                          0, 
152                                          &print_entry,
153                                          NULL);
154       GNUNET_DISK_file_unmap (map);
155       GNUNET_DISK_file_close (h);
156     }
157 }
158
159 /**
160  * gnunet-directory command line options
161  */
162 static struct GNUNET_GETOPT_CommandLineOption options[] = {
163   GNUNET_GETOPT_OPTION_END
164 };
165
166
167 /**
168  * The main function to inspect GNUnet directories.
169  *
170  * @param argc number of arguments from the command line
171  * @param argv command line arguments
172  * @return 0 ok, 1 on error
173  */
174 int
175 main (int argc, char *const *argv)
176 {
177   return (GNUNET_OK ==
178           GNUNET_PROGRAM_run (argc,
179                               argv,
180                               "gnunet-directory",
181                               gettext_noop
182                               ("Display GNUnet directories."),
183                               options, &run, NULL)) ? ret : 1;
184 }
185
186 /* end of gnunet-directory.c */