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.

96 lines
2.3KB

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