Add -f option to make using this in scripts easier

This commit is contained in:
x3 2021-05-23 18:06:17 +02:00
parent d4951e0a70
commit b6f58089b0
Signed by: x3
GPG Key ID: 7E9961E8AD0E240E
5 changed files with 67 additions and 8 deletions

View File

@ -15,7 +15,7 @@
static_assert((sizeof(long long) >= 8), "Size of long long is less than 8, cannot compile"); static_assert((sizeof(long long) >= 8), "Size of long long is less than 8, cannot compile");
void usage() { void usage() {
fprintf(stderr, "Usage: " PROGRAM_NAME " [-h | -i | -s] [-n] [-v data_path] [--] .torrent_file...\n"); fprintf(stderr, "Usage: " PROGRAM_NAME " [-h | -i | -s | -f CHAR] [-n] [-v data_path] [--] .torrent_file...\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -30,6 +30,8 @@ void help() {
" -v PATH verify the torrent file, pass in the path of the files\n" " -v PATH verify the torrent file, pass in the path of the files\n"
" -s don't write any output\n" " -s don't write any output\n"
" -n Don't use torrent name as a folder when verifying\n" " -n Don't use torrent name as a folder when verifying\n"
" -f CHAR Show info from the .torrent file, as an input for a script\n"
" Valid CHARs are: i - Info hash\n"
"\n" "\n"
"EXIT CODE\n" "EXIT CODE\n"
" If no error, exit code is 0. In verify mode exit code is 0 if it's\n" " If no error, exit code is 0. In verify mode exit code is 0 if it's\n"
@ -56,12 +58,6 @@ int main(int argc, char** argv) {
fprintf(stderr, "Provide at least one torrent file\n"); fprintf(stderr, "Provide at least one torrent file\n");
usage(); usage();
} }
/*
if (data_path && optind != argc - 1) {
fprintf(stderr, "If used with -v, only input 1 bittorrent file\n");
usage();
}
*/
int exit_code = EXIT_SUCCESS; int exit_code = EXIT_SUCCESS;
for (int i = optind; i < argc; i++) { for (int i = optind; i < argc; i++) {
@ -74,6 +70,10 @@ int main(int argc, char** argv) {
showinfo(&m); showinfo(&m);
} }
if (opt_scriptformat_info != OPT_SCRIPTFORMAT_NONE) {
showinfo_script(&m);
}
if (opt_data_path) { /* Verify */ if (opt_data_path) { /* Verify */
int verify_result = verify(&m, opt_data_path, !opt_no_use_dir); int verify_result = verify(&m, opt_data_path, !opt_no_use_dir);
if (verify_result != 0) { if (verify_result != 0) {

View File

@ -1,17 +1,19 @@
#include "opts.h" #include "opts.h"
#include <unistd.h> #include <unistd.h>
#include <string.h>
int opt_silent = 0; int opt_silent = 0;
int opt_showinfo = 0; int opt_showinfo = 0;
int opt_help = 0; int opt_help = 0;
int opt_no_use_dir = 0; int opt_no_use_dir = 0;
int opt_pretty_progress = 0; int opt_pretty_progress = 0;
int opt_scriptformat_info = OPT_SCRIPTFORMAT_NONE;
char* opt_data_path = NULL; char* opt_data_path = NULL;
int opts_parse(int argc, char** argv) { int opts_parse(int argc, char** argv) {
int opt; int opt;
while ((opt = getopt(argc, argv, "pnihsv:")) != -1) { while ((opt = getopt(argc, argv, "pnihsv:f:")) != -1) {
switch (opt) { switch (opt) {
case 'i': case 'i':
opt_showinfo = 1; opt_showinfo = 1;
@ -31,6 +33,21 @@ int opts_parse(int argc, char** argv) {
case 'v': case 'v':
opt_data_path = optarg; opt_data_path = optarg;
break; break;
case 'f':
if (strlen(optarg) != 1)
return -1;
char info_char = optarg[0];
opt_scriptformat_info = OPT_SCRIPTFORMAT_INVALID;
for (int i = 0; i < OPT_SCRIPTFORMAT_MAPPING_LEN; i++) {
const opt_scriptformat_mapping_t* curr = &OPT_SCRIPTFORMAT_MAPPING[i];
if (info_char == curr->info_char) {
opt_scriptformat_info = curr->info;
break;
}
}
if (opt_scriptformat_info == OPT_SCRIPTFORMAT_INVALID)
return -1;
break;
default: default:
return -1; return -1;
} }

View File

@ -1,11 +1,29 @@
#ifndef OPTS_H #ifndef OPTS_H
#define OPTS_H #define OPTS_H
enum OPT_SCRIPTFORMAT {
OPT_SCRIPTFORMAT_INVALID = -1,
OPT_SCRIPTFORMAT_NONE,
OPT_SCRIPTFORMAT_INFOHASH,
};
typedef struct {
char info_char;
enum OPT_SCRIPTFORMAT info;
} opt_scriptformat_mapping_t;
const static opt_scriptformat_mapping_t OPT_SCRIPTFORMAT_MAPPING[] = {
{ .info_char = 'i', .info = OPT_SCRIPTFORMAT_INFOHASH },
};
#define OPT_SCRIPTFORMAT_MAPPING_LEN sizeof(OPT_SCRIPTFORMAT_MAPPING)/sizeof(OPT_SCRIPTFORMAT_MAPPING[0])
extern int opt_silent; extern int opt_silent;
extern int opt_showinfo; extern int opt_showinfo;
extern int opt_help; extern int opt_help;
extern int opt_no_use_dir; extern int opt_no_use_dir;
extern int opt_pretty_progress; extern int opt_pretty_progress;
extern int opt_scriptformat_info;
extern char* opt_data_path; extern char* opt_data_path;
/* Parse the given arguments. Return -1 if error */ /* Parse the given arguments. Return -1 if error */

View File

@ -3,6 +3,7 @@
#include <time.h> #include <time.h>
#include "showinfo.h" #include "showinfo.h"
#include "util.h" #include "util.h"
#include "opts.h"
void showinfo(metainfo_t* m) { void showinfo(metainfo_t* m) {
const char* s; const char* s;
@ -95,3 +96,20 @@ void showinfo(metainfo_t* m) {
strncpy(str_buff, "err", sizeof(str_buff)); strncpy(str_buff, "err", sizeof(str_buff));
printf("Total size: %s\n", str_buff); printf("Total size: %s\n", str_buff);
} }
void showinfo_script(metainfo_t* m) {
switch (opt_scriptformat_info) {
case OPT_SCRIPTFORMAT_INFOHASH:
{
char hex_str[sizeof(sha1sum_t)*2+1];
const sha1sum_t* infohash = metainfo_infohash(m);
util_byte2hex((const unsigned char*)infohash, sizeof(sha1sum_t), 0, hex_str);
printf("%s\n", hex_str);
break;
}
default:
printf("Unknown?? [>.<]\n");
break;
}
}

View File

@ -7,4 +7,10 @@
*/ */
void showinfo(metainfo_t* m); void showinfo(metainfo_t* m);
/*
* Print the selected info from the metainfo file
* to be readable by a script
*/
void showinfo_script(metainfo_t* m);
#endif #endif