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