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