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