gnunet-fs.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2011 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-fs.c
  19. * @brief special file-sharing functions
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_fs_service.h"
  24. /**
  25. * Return value.
  26. */
  27. static int ret;
  28. /**
  29. * Handle to FS service.
  30. */
  31. static struct GNUNET_FS_Handle *fs;
  32. /**
  33. * Option -i given?
  34. */
  35. static int list_indexed_files;
  36. /**
  37. * Option -v given?
  38. */
  39. static int verbose;
  40. /**
  41. * Print indexed filenames to stdout.
  42. *
  43. * @param cls closure
  44. * @param filename the name of the file
  45. * @param file_id hash of the contents of the indexed file
  46. * @return GNUNET_OK to continue iteration
  47. */
  48. static int
  49. print_indexed (void *cls, const char *filename, const struct GNUNET_HashCode * file_id)
  50. {
  51. if (NULL == filename)
  52. {
  53. GNUNET_FS_stop (fs);
  54. fs = NULL;
  55. return GNUNET_OK;
  56. }
  57. if (verbose)
  58. FPRINTF (stdout, "%s: %s\n", GNUNET_h2s (file_id), filename);
  59. else
  60. FPRINTF (stdout, "%s\n", filename);
  61. return GNUNET_OK;
  62. }
  63. /**
  64. * Main function that will be run by the scheduler.
  65. *
  66. * @param cls closure
  67. * @param args remaining command-line arguments
  68. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  69. * @param cfg configuration
  70. */
  71. static void
  72. run (void *cls, char *const *args, const char *cfgfile,
  73. const struct GNUNET_CONFIGURATION_Handle *cfg)
  74. {
  75. if (list_indexed_files)
  76. {
  77. fs = GNUNET_FS_start (cfg, "gnunet-fs", NULL, NULL, GNUNET_FS_FLAGS_NONE,
  78. GNUNET_FS_OPTIONS_END);
  79. if (NULL == fs)
  80. {
  81. ret = 1;
  82. return;
  83. }
  84. if (NULL == GNUNET_FS_get_indexed_files (fs, &print_indexed, NULL))
  85. {
  86. ret = 2;
  87. GNUNET_FS_stop (fs);
  88. fs = NULL;
  89. return;
  90. }
  91. }
  92. }
  93. /**
  94. * The main function to access special file-sharing functions.
  95. *
  96. * @param argc number of arguments from the command line
  97. * @param argv command line arguments
  98. * @return 0 ok, 1 on error
  99. */
  100. int
  101. main (int argc, char *const *argv)
  102. {
  103. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  104. {'i', "list-indexed", NULL,
  105. gettext_noop ("print a list of all indexed files"), 0,
  106. &GNUNET_GETOPT_set_one, &list_indexed_files},
  107. GNUNET_GETOPT_OPTION_VERBOSE (&verbose),
  108. GNUNET_GETOPT_OPTION_END
  109. };
  110. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  111. return 2;
  112. ret = (GNUNET_OK ==
  113. GNUNET_PROGRAM_run (argc, argv, "gnunet-fs [OPTIONS]",
  114. gettext_noop ("Special file-sharing operations"),
  115. options, &run, NULL)) ? ret : 1;
  116. GNUNET_free ((void*) argv);
  117. return ret;
  118. }
  119. /* end of gnunet-fs.c */