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.

94 lines
2.3KB

  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] [-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. "\n"
  30. "EXIT CODE\n"
  31. " If no error, exit code is 0. In verify mode exit code is 0 if it's\n"
  32. " verified correctly, otherwise non-zero\n"
  33. #ifdef BUILD_INFO
  34. "\n"
  35. BUILD_HASH " (" BUILD_DATE ")\n"
  36. #ifdef MT
  37. "MultiThread support\n"
  38. #endif
  39. #endif
  40. );
  41. exit(EXIT_SUCCESS);
  42. }
  43. int main(int argc, char** argv) {
  44. if (opts_parse(argc, argv) == -1)
  45. usage();
  46. if (opt_help)
  47. help();
  48. if (optind >= argc) {
  49. fprintf(stderr, "Provide at least one torrent file\n");
  50. usage();
  51. }
  52. /*
  53. if (data_path && optind != argc - 1) {
  54. fprintf(stderr, "If used with -v, only input 1 bittorrent file\n");
  55. usage();
  56. }
  57. */
  58. int exit_code = EXIT_SUCCESS;
  59. for (int i = optind; i < argc; i++) {
  60. metainfo_t m;
  61. if (metainfo_create(&m, argv[i]) == -1) {
  62. return EXIT_FAILURE;
  63. }
  64. if (opt_showinfo && !opt_silent) {
  65. showinfo(&m);
  66. }
  67. if (opt_data_path) { /* Verify */
  68. int verify_result = verify(&m, opt_data_path, !opt_no_use_dir);
  69. if (verify_result != 0) {
  70. if (!opt_silent)
  71. printf("Torrent verify failed: %s\n", strerror(verify_result));
  72. exit_code = EXIT_FAILURE;
  73. } else {
  74. if (!opt_silent)
  75. printf("Torrent verified successfully\n");
  76. }
  77. }
  78. metainfo_destroy(&m);
  79. }
  80. return exit_code;
  81. }