gnunet-directory.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/gnunet-directory.c
  19. * @brief display content of GNUnet directories
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_fs_service.h"
  24. static int ret;
  25. /**
  26. * Print a meta data entry.
  27. *
  28. * @param cls closure (unused)
  29. * @param plugin_name name of the plugin that generated the meta data
  30. * @param type type of the keyword
  31. * @param format format of data
  32. * @param data_mime_type mime type of data
  33. * @param data value of the meta data
  34. * @param data_size number of bytes in data
  35. * @return always 0 (to continue iterating)
  36. */
  37. static int
  38. item_printer (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
  39. enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
  40. const char *data, size_t data_size)
  41. {
  42. if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
  43. {
  44. printf (_("\t<original file embedded in %u bytes of meta data>\n"),
  45. (unsigned int) data_size);
  46. return 0;
  47. }
  48. if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
  49. (format != EXTRACTOR_METAFORMAT_C_STRING))
  50. return 0;
  51. if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
  52. return 0;
  53. printf ("\t%20s: %s\n",
  54. dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
  55. EXTRACTOR_metatype_to_string (type)), data);
  56. return 0;
  57. }
  58. /**
  59. * Print an entry in a directory.
  60. *
  61. * @param cls closure (not used)
  62. * @param filename name of the file in the directory
  63. * @param uri URI of the file
  64. * @param meta metadata for the file; metadata for
  65. * the directory if everything else is NULL/zero
  66. * @param length length of the available data for the file
  67. * (of type size_t since data must certainly fit
  68. * into memory; if files are larger than size_t
  69. * permits, then they will certainly not be
  70. * embedded with the directory itself).
  71. * @param data data available for the file (length bytes)
  72. */
  73. static void
  74. print_entry (void *cls, const char *filename, const struct GNUNET_FS_Uri *uri,
  75. const struct GNUNET_CONTAINER_MetaData *meta, size_t length,
  76. const void *data)
  77. {
  78. char *string;
  79. char *name;
  80. name =
  81. GNUNET_CONTAINER_meta_data_get_by_type (meta,
  82. EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
  83. if (uri == NULL)
  84. {
  85. printf (_("Directory `%s' meta data:\n"), name);
  86. GNUNET_CONTAINER_meta_data_iterate (meta, &item_printer, NULL);
  87. printf ("\n");
  88. printf (_("Directory `%s' contents:\n"), name);
  89. GNUNET_free (name);
  90. return;
  91. }
  92. string = GNUNET_FS_uri_to_string (uri);
  93. printf ("%s (%s):\n", name, string);
  94. GNUNET_free (string);
  95. GNUNET_CONTAINER_meta_data_iterate (meta, &item_printer, NULL);
  96. printf ("\n");
  97. GNUNET_free (name);
  98. }
  99. /**
  100. * Main function that will be run by the scheduler.
  101. *
  102. * @param cls closure
  103. * @param args remaining command-line arguments
  104. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  105. * @param cfg configuration
  106. */
  107. static void
  108. run (void *cls, char *const *args, const char *cfgfile,
  109. const struct GNUNET_CONFIGURATION_Handle *cfg)
  110. {
  111. struct GNUNET_DISK_MapHandle *map;
  112. struct GNUNET_DISK_FileHandle *h;
  113. void *data;
  114. size_t len;
  115. uint64_t size;
  116. const char *filename;
  117. int i;
  118. if (NULL == args[0])
  119. {
  120. fprintf (stderr, _("You must specify a filename to inspect."));
  121. ret = 1;
  122. return;
  123. }
  124. i = 0;
  125. while (NULL != (filename = args[i++]))
  126. {
  127. if ((GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES)) ||
  128. (NULL ==
  129. (h =
  130. GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
  131. GNUNET_DISK_PERM_NONE))))
  132. {
  133. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to read directory `%s'\n"),
  134. filename);
  135. ret = 1;
  136. continue;
  137. }
  138. len = (size_t) size;
  139. data = GNUNET_DISK_file_map (h, &map, GNUNET_DISK_MAP_TYPE_READ, len);
  140. GNUNET_assert (NULL != data);
  141. GNUNET_FS_directory_list_contents (len, data, 0, &print_entry, NULL);
  142. printf ("\n");
  143. GNUNET_DISK_file_unmap (map);
  144. GNUNET_DISK_file_close (h);
  145. }
  146. }
  147. /**
  148. * The main function to inspect GNUnet directories.
  149. *
  150. * @param argc number of arguments from the command line
  151. * @param argv command line arguments
  152. * @return 0 ok, 1 on error
  153. */
  154. int
  155. main (int argc, char *const *argv)
  156. {
  157. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  158. GNUNET_GETOPT_OPTION_END
  159. };
  160. return (GNUNET_OK ==
  161. GNUNET_PROGRAM_run (argc, argv, "gnunet-directory [OPTIONS] FILENAME",
  162. gettext_noop
  163. ("Display contents of a GNUnet directory"),
  164. options, &run, NULL)) ? ret : 1;
  165. }
  166. /* end of gnunet-directory.c */