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