Verify bittorrent .torrent metainfo files.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.5KB

  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "metainfo.h"
  7. #include "verify.h"
  8. #include "showinfo.h"
  9. #include "opts.h"
  10. #ifndef PROGRAM_NAME
  11. #define PROGRAM_NAME "torrent-verify"
  12. #endif
  13. static_assert((sizeof(long long) >= 8), "Size of long long is less than 8, cannot compile");
  14. void usage() {
  15. fprintf(stderr, "Usage: " PROGRAM_NAME " [-h | -i | -s | -f CHAR] [-n] [-v data_path] [--] .torrent_file...\n");
  16. exit(EXIT_FAILURE);
  17. }
  18. void help() {
  19. printf(
  20. "Usage:\n"
  21. " " PROGRAM_NAME " [options] <.torrent file>\n"
  22. "\n"
  23. "OPTIONS:\n"
  24. " -h print this help text\n"
  25. " -i show info about the torrent file\n"
  26. " -v PATH verify the torrent file, pass in the path of the files\n"
  27. " -s don't write any output\n"
  28. " -n Don't use torrent name as a folder when verifying\n"
  29. " -f CHAR Show info from the .torrent file, as an input for a script\n"
  30. " Valid CHARs are: i - Info hash\n"
  31. "\n"
  32. "EXIT CODE\n"
  33. " If no error, exit code is 0. In verify mode exit code is 0 if it's\n"
  34. " verified correctly, otherwise non-zero\n"
  35. #ifdef BUILD_INFO
  36. "\n"
  37. BUILD_HASH " (" BUILD_DATE ")\n"
  38. #ifdef MT
  39. "MultiThread support\n"
  40. #endif
  41. #ifdef HTTP_TORRENT
  42. "HTTP Torrent support\n"
  43. #endif
  44. #endif
  45. );
  46. exit(EXIT_SUCCESS);
  47. }
  48. int main(int argc, char** argv) {
  49. if (opts_parse(argc, argv) == -1)
  50. usage();
  51. if (opt_help)
  52. help();
  53. if (optind >= argc) {
  54. fprintf(stderr, "Provide at least one torrent file"
  55. #ifdef HTTP_TORRENT
  56. " or an http link to a torrent file"
  57. #endif
  58. "\n");
  59. usage();
  60. }
  61. int exit_code = EXIT_SUCCESS;
  62. for (int i = optind; i < argc; i++) {
  63. metainfo_t m;
  64. if (metainfo_create(&m, argv[i]) == -1) {
  65. return EXIT_FAILURE;
  66. }
  67. if (opt_showinfo && !opt_silent) {
  68. showinfo(&m);
  69. }
  70. if (opt_scriptformat_info != OPT_SCRIPTFORMAT_NONE) {
  71. showinfo_script(&m);
  72. }
  73. if (opt_data_path) { /* Verify */
  74. int verify_result = verify(&m, opt_data_path, !opt_no_use_dir);
  75. if (verify_result != 0) {
  76. if (!opt_silent)
  77. printf("Torrent verify failed: %s\n", strerror(verify_result));
  78. exit_code = EXIT_FAILURE;
  79. } else {
  80. if (!opt_silent)
  81. printf("Torrent verified successfully\n");
  82. }
  83. }
  84. metainfo_destroy(&m);
  85. }
  86. return exit_code;
  87. }