From 824af49c11ab76e11cfc9a8f712beea96150af58 Mon Sep 17 00:00:00 2001 From: Welp Wazowski Date: Sun, 7 Oct 2018 18:55:45 -0700 Subject: [PATCH] Orpheus option now works. Made a janky but functional system to manage the memory of transforms. --- make-debug.sh | 3 +++ src/cli.c | 27 +++++++++++++++++----- src/libannouncebulk.c | 52 +++++++++++++++++++++++++++++++++++++++---- src/libannouncebulk.h | 15 ++++++++----- 4 files changed, 82 insertions(+), 15 deletions(-) create mode 100755 make-debug.sh diff --git a/make-debug.sh b/make-debug.sh new file mode 100755 index 0000000..f5208b9 --- /dev/null +++ b/make-debug.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +CFLAGS='-g -DGRN_LOG_LEVEL=4' make "$@" diff --git a/src/cli.c b/src/cli.c index 412e1a0..3be52a7 100644 --- a/src/cli.c +++ b/src/cli.c @@ -34,6 +34,7 @@ char help_text[] = "USAGE:\n" int main( int argc, char **argv ) { int in_err; + int use_orpheus_transforms; // basically bools, but getopt_long wants an int #define X_CLIENT(x_machine, x_enum, x_human) int x_machine = 0; @@ -48,6 +49,12 @@ int main( int argc, char **argv ) { .flag = NULL, .val = 'h', }, + { + .name = "orpheus", + .has_arg = 1, + .flag = &use_orpheus_transforms, + .val = 1, + }, #define X_CLIENT(x_machine, x_enum, x_human) { \ .name = #x_machine, \ .has_arg = 0, \ @@ -75,6 +82,16 @@ int main( int argc, char **argv ) { int opt_c = 0; while ( ( opt_c = getopt_long( argc, argv, shortopts, longopts, NULL ) ) != -1 ) { switch ( ( char )opt_c ) { + // other long option + case 0: + ; + if ( use_orpheus_transforms ) { + grn_cat_transforms_orpheus( transforms, optarg, &in_err ); + if ( in_err ) { + goto cleanup_err; + } + } + break; // unknown option case '?': ; @@ -129,10 +146,8 @@ int main( int argc, char **argv ) { printf( "About to process %d files.\n", files_n ); if ( vector_length( transforms ) == 0 ) { - grn_cat_transforms_orpheus( transforms, &in_err ); - if ( in_err ) { - goto cleanup_err; - } + puts( "No transformations to apply. Try using --orpheus yourpasscode to convert from Apollo to Orpheus." ); + goto cleanup_ok; } grn_ctx_set_files_v( ctx, files ); @@ -159,13 +174,13 @@ int main( int argc, char **argv ) { cleanup_err: puts( grn_err_to_string( in_err ) ); vector_free_all( files ); - vector_free_all( transforms ); + grn_free_transforms_v( transforms );;; grn_ctx_free( ctx, &in_err ); return 1; cleanup_ok: vector_free_all( files ); - vector_free_all( transforms ); + grn_free_transforms_v( transforms );;; grn_ctx_free( ctx, &in_err ); return 0; } diff --git a/src/libannouncebulk.c b/src/libannouncebulk.c index 3de1a6d..cc5be7b 100644 --- a/src/libannouncebulk.c +++ b/src/libannouncebulk.c @@ -113,6 +113,7 @@ char *grn_err_to_string( int err ) { "Could not determine the path for the given bittorrent client.", "Could not access the path for the given bittorrent client.", "Invalid regular expression.", + "Orpheus passphrase/announce URL was invalid.", }; return err_strings[err]; } @@ -195,11 +196,11 @@ bool normalize_announce_url( char *user_announce, char *our_announce ) { void grn_cat_transforms_orpheus( struct vector *vec, char *user_announce, int *out_err ) { *out_err = GRN_OK; - char *normalized_url = malloc(OPS_URL_LENGTH); - ERR(normalized_url == NULL, GRN_ERR_OOM); + char *normalized_url = malloc( OPS_URL_LENGTH ); + ERR( normalized_url == NULL, GRN_ERR_OOM ); ERR( !normalize_announce_url( user_announce, normalized_url ), GRN_ERR_ORPHEUS_ANNOUNCE_SYNTAX ); - GRN_LOG_DEBUG("Normalized announce URL: %s", normalized_url); + GRN_LOG_DEBUG( "Normalized announce URL: %s", normalized_url ); // there's no fucking way this should be dynamically allocated, but it is. struct grn_transform *key_subst = malloc( sizeof( struct grn_transform ) ); @@ -209,7 +210,9 @@ void grn_cat_transforms_orpheus( struct vector *vec, char *user_announce, int *o *key_subst = grn_mktransform_substitute_regex( "^https?:\\/\\/(mars\\.)?(apollo|xanax)\\.rip(:2095)?\\/[a-f0-9]{32}\\/announce\\/?$", normalized_url, out_err ); ERR_FW(); + key_subst->dynamalloc = GRN_DYNAMIC_TRANSFORM_SELF | GRN_DYNAMIC_TRANSFORM_FIRST | GRN_DYNAMIC_TRANSFORM_SECOND; *list_subst = *key_subst; + list_subst->dynamalloc = GRN_DYNAMIC_TRANSFORM_SELF | GRN_DYNAMIC_TRANSFORM_SECOND; key_subst->key = announce_str_key; list_subst->key = announce_list_key; @@ -229,6 +232,7 @@ struct grn_transform grn_mktransform_set_string( char *key, char *val ) { .val = val, }, }, + .dynamalloc = 0, }; } @@ -240,6 +244,7 @@ struct grn_transform grn_mktransform_delete( char *key ) { .key = key, }, }, + .dynamalloc = 0, }; } @@ -252,6 +257,7 @@ struct grn_transform grn_mktransform_substitute( char *find, char *replace ) { .replace = replace, }, }, + .dynamalloc = 0, }; } @@ -264,7 +270,8 @@ struct grn_transform grn_mktransform_substitute_regex( char *find_regstr, char * .substitute_regex = { .replace = replace, }, - } + }, + .dynamalloc = GRN_DYNAMIC_TRANSFORM_FIRST, }; int regcomp_res = regcomp( &to_return.payload.substitute_regex.find, find_regstr, REG_EXTENDED ); @@ -280,6 +287,42 @@ struct grn_transform grn_mktransform_substitute_regex( char *find_regstr, char * return to_return; } +// this feels like such overkill for such a simple struct, but oh well +void grn_free_transform( struct grn_transform *transform ) { + const int bits = transform->dynamalloc; + if ( bits & GRN_DYNAMIC_TRANSFORM_KEY_ELEMENTS ) { + for ( int i = 0; transform->key[i] != NULL; i++ ) { + free( transform->key[i] ); + } + } + if ( bits & GRN_DYNAMIC_TRANSFORM_KEY ) { + free( transform->key ); + } + if ( bits & GRN_DYNAMIC_TRANSFORM_FIRST ) { + if ( transform->operation == GRN_TRANSFORM_SUBSTITUTE_REGEX ) { + regfree( &transform->payload.substitute_regex.find ); + } else { + free( transform->payload.delete_.key ); + } + } + if ( bits & GRN_DYNAMIC_TRANSFORM_SECOND ) { + if ( transform->operation == GRN_TRANSFORM_SUBSTITUTE_REGEX ) { + free( transform->payload.substitute_regex.replace ); + } else { + free( transform->payload.substitute.replace ); + } + } + if ( bits & GRN_DYNAMIC_TRANSFORM_SELF ) { + free( transform ); + } +} + +void grn_free_transforms_v( struct vector *vec ) { + for ( int i = 0; i < vec->used_n; i++ ) { + grn_free_transform( vec->buffer[i] ); + } +} + // END preset and semi-presets /** @@ -724,3 +767,4 @@ void grn_cat_client( struct vector *vec, int client, int *out_err ) { // TODO: better errors for catting ERR_FW(); } + diff --git a/src/libannouncebulk.h b/src/libannouncebulk.h index 564d537..745bdf9 100644 --- a/src/libannouncebulk.h +++ b/src/libannouncebulk.h @@ -38,11 +38,6 @@ struct grn_transform { char *val; } set_string; - struct grn_op_set_bool { - char *key; - bool val; - } set_bool; - struct grn_op_substitute { char *find; char *replace; @@ -54,6 +49,13 @@ struct grn_transform { char *replace; } substitute_regex; } payload; + enum grn_dynamic_transform { + GRN_DYNAMIC_TRANSFORM_SELF = 1, + GRN_DYNAMIC_TRANSFORM_FIRST = 2, // first element of payload, whether it's key or find + GRN_DYNAMIC_TRANSFORM_SECOND = 4, // second element of payload, whether it's val or replace + GRN_DYNAMIC_TRANSFORM_KEY = 8, + GRN_DYNAMIC_TRANSFORM_KEY_ELEMENTS = 16, + } dynamalloc; }; struct grn_transform grn_mktransform_set_string( char *key, char *val ); @@ -62,6 +64,9 @@ struct grn_transform grn_mktransform_substitute( char *find, char *replace ); // can fail because of regex compilation struct grn_transform grn_mktransform_substitute_regex( char *find_regstr, char *replace, int *out_err ); +void grn_free_transform( struct grn_transform *transform ); +void grn_free_transforms_v( struct vector *vec ); + struct grn_callback_arg { // progress bar info int numerator;