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.4KB

  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. #endif
  42. );
  43. exit(EXIT_SUCCESS);
  44. }
  45. int main(int argc, char** argv) {
  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. int exit_code = EXIT_SUCCESS;
  55. for (int i = optind; i < argc; i++) {
  56. metainfo_t m;
  57. if (metainfo_create(&m, argv[i]) == -1) {
  58. return EXIT_FAILURE;
  59. }
  60. if (opt_showinfo && !opt_silent) {
  61. showinfo(&m);
  62. }
  63. if (opt_scriptformat_info != OPT_SCRIPTFORMAT_NONE) {
  64. showinfo_script(&m);
  65. }
  66. if (opt_data_path) { /* Verify */
  67. int verify_result = verify(&m, opt_data_path, !opt_no_use_dir);
  68. if (verify_result != 0) {
  69. if (!opt_silent)
  70. printf("Torrent verify failed: %s\n", strerror(verify_result));
  71. exit_code = EXIT_FAILURE;
  72. } else {
  73. if (!opt_silent)
  74. printf("Torrent verified successfully\n");
  75. }
  76. }
  77. metainfo_destroy(&m);
  78. }
  79. return exit_code;
  80. }