2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file fs/gnunet-directory.c
22 * @brief display content of GNUnet directories
23 * @author Christian Grothoff
26 #include "gnunet_fs_service.h"
31 * Print a meta data entry.
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)
43 item_printer (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
44 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
45 const char *data, size_t data_size)
47 if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
49 printf (_("\t<original file embedded in %u bytes of meta data>\n"),
50 (unsigned int) data_size);
53 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
54 (format != EXTRACTOR_METAFORMAT_C_STRING))
56 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
58 printf ("\t%20s: %s\n",
59 dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
60 EXTRACTOR_metatype_to_string (type)), data);
67 * Print an entry in a directory.
69 * @param cls closure (not used)
70 * @param filename name of the file in the directory
71 * @param uri URI of the file
72 * @param meta metadata for the file; metadata for
73 * the directory if everything else is NULL/zero
74 * @param length length of the available data for the file
75 * (of type size_t since data must certainly fit
76 * into memory; if files are larger than size_t
77 * permits, then they will certainly not be
78 * embedded with the directory itself).
79 * @param data data available for the file (length bytes)
82 print_entry (void *cls, const char *filename, const struct GNUNET_FS_Uri *uri,
83 const struct GNUNET_CONTAINER_MetaData *meta, size_t length,
90 GNUNET_CONTAINER_meta_data_get_by_type (meta,
91 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
94 printf (_("Directory `%s' meta data:\n"), name);
95 GNUNET_CONTAINER_meta_data_iterate (meta, &item_printer, NULL);
97 printf (_("Directory `%s' contents:\n"), name);
101 string = GNUNET_FS_uri_to_string (uri);
102 printf ("%s (%s):\n", name, string);
103 GNUNET_free (string);
104 GNUNET_CONTAINER_meta_data_iterate (meta, &item_printer, NULL);
111 * Main function that will be run by the scheduler.
114 * @param args remaining command-line arguments
115 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
116 * @param cfg configuration
119 run (void *cls, char *const *args, const char *cfgfile,
120 const struct GNUNET_CONFIGURATION_Handle *cfg)
122 struct GNUNET_DISK_MapHandle *map;
123 struct GNUNET_DISK_FileHandle *h;
127 const char *filename;
132 fprintf (stderr, _("You must specify a filename to inspect."));
137 while (NULL != (filename = args[i++]))
139 if ((GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES)) ||
142 GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
143 GNUNET_DISK_PERM_NONE))))
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to read directory `%s'\n"),
151 data = GNUNET_DISK_file_map (h, &map, GNUNET_DISK_MAP_TYPE_READ, len);
152 GNUNET_assert (NULL != data);
153 GNUNET_FS_directory_list_contents (len, data, 0, &print_entry, NULL);
155 GNUNET_DISK_file_unmap (map);
156 GNUNET_DISK_file_close (h);
161 * The main function to inspect GNUnet directories.
163 * @param argc number of arguments from the command line
164 * @param argv command line arguments
165 * @return 0 ok, 1 on error
168 main (int argc, char *const *argv)
170 static struct GNUNET_GETOPT_CommandLineOption options[] = {
171 GNUNET_GETOPT_OPTION_END
174 GNUNET_PROGRAM_run (argc, argv, "gnunet-directory [OPTIONS] FILENAME",
176 ("Display contents of a GNUnet directory"),
177 options, &run, NULL)) ? ret : 1;
180 /* end of gnunet-directory.c */