From 59875da94e31617e538692d97ef2a8dda765d8b6 Mon Sep 17 00:00:00 2001 From: Thomas Tsai Date: Thu, 20 Jul 2023 13:50:10 +0800 Subject: [PATCH] add IO streams support for torrent info file --- configure.ac | 2 +- src/main.c | 24 ++++++++++++------------ src/torrent_helper.c | 20 ++++++++++---------- src/torrent_helper.h | 4 ++-- src/version.h | 2 +- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/configure.ac b/configure.ac index aed9d46e..d4bb18c3 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT([Partclone],[0.3.23],[thomas@nchc.org.tw]) +AC_INIT([Partclone],[0.3.24],[thomas@nchc.org.tw]) AM_INIT_AUTOMAKE([-Wall foreign]) AM_GNU_GETTEXT_VERSION([0.16.1]) AM_GNU_GETTEXT([external]) diff --git a/src/main.c b/src/main.c index 2618d43d..bb219eab 100644 --- a/src/main.c +++ b/src/main.c @@ -439,7 +439,7 @@ int main(int argc, char **argv) { char *read_buffer, *write_buffer; // SHA1 for torrent info - int tinfo = -1; + FILE* tinfo = NULL; torrent_generator torrent; blocks_per_cs = img_opt.blocks_per_checksum; @@ -470,11 +470,11 @@ int main(int argc, char **argv) { if (opt.blockfile == 1) { char torrent_name[PATH_MAX + 1] = {'\0'}; sprintf(torrent_name,"%s/torrent.info", target); - tinfo = open(torrent_name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + tinfo = fopen(torrent_name, "w"); torrent_init(&torrent, tinfo); - dprintf(tinfo, "block_size: %u\n", block_size); - dprintf(tinfo, "blocks_total: %llu\n", blocks_total); + fprintf(tinfo, "block_size: %u\n", block_size); + fprintf(tinfo, "blocks_total: %llu\n", blocks_total); } block_id = 0; @@ -640,7 +640,7 @@ int main(int argc, char **argv) { unsigned long long blocks_used_fix = 0, test_block = 0; // SHA1 for torrent info - int tinfo = -1; + FILE *tinfo = NULL; torrent_generator torrent; log_mesg(1, 0, 0, debug, "#\nBuffer capacity = %u, Blocks per cs = %u\n#\n", buffer_capacity, blocks_per_cs); @@ -697,11 +697,11 @@ int main(int argc, char **argv) { if (opt.blockfile == 1) { char torrent_name[PATH_MAX + 1] = {'\0'}; sprintf(torrent_name,"%s/torrent.info", target); - tinfo = open(torrent_name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + tinfo = fopen(torrent_name, "w"); torrent_init(&torrent, tinfo); - dprintf(tinfo, "block_size: %u\n", block_size); - dprintf(tinfo, "blocks_total: %llu\n", blocks_total); + fprintf(tinfo, "block_size: %u\n", block_size); + fprintf(tinfo, "blocks_total: %llu\n", blocks_total); } block_id = 0; @@ -1053,7 +1053,7 @@ int main(int argc, char **argv) { int blocks_in_buffer = block_size < opt.buffer_size ? opt.buffer_size / block_size : 1; // SHA1 for torrent info - int tinfo = -1; + FILE *tinfo = NULL; torrent_generator torrent; buffer = (char*)malloc(blocks_in_buffer * block_size); @@ -1067,10 +1067,10 @@ int main(int argc, char **argv) { if (opt.blockfile == 1) { char torrent_name[PATH_MAX + 1] = {'\0'}; sprintf(torrent_name,"%s/torrent.info", target); - tinfo = open(torrent_name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + tinfo = fopen(torrent_name, "w"); torrent_init(&torrent, tinfo); - dprintf(tinfo, "block_size: %u\n", block_size); - dprintf(tinfo, "blocks_total: %llu\n", blocks_total); + fprintf(tinfo, "block_size: %u\n", block_size); + fprintf(tinfo, "blocks_total: %llu\n", blocks_total); } log_mesg(0, 0, 0, debug, "Total block %llu\n", blocks_total); diff --git a/src/torrent_helper.c b/src/torrent_helper.c index e0b02f4b..6488f4a6 100644 --- a/src/torrent_helper.c +++ b/src/torrent_helper.c @@ -13,7 +13,7 @@ #include "torrent_helper.h" -void torrent_init(torrent_generator *torrent, int tinfo) +void torrent_init(torrent_generator *torrent, FILE *tinfo) { torrent->PIECE_SIZE = DEFAULT_PIECE_SIZE; torrent->length = 0; @@ -37,7 +37,7 @@ void torrent_update(torrent_generator *torrent, void *buffer, size_t length) unsigned long long buffer_remain_length = length; unsigned long long buffer_offset = 0; - int tinfo = torrent->tinfo; + FILE *tinfo = torrent->tinfo; int x = 0; while (buffer_remain_length > 0) { @@ -49,11 +49,11 @@ void torrent_update(torrent_generator *torrent, void *buffer, size_t length) #else SHA1_Final(torrent->hash, &torrent->ctx); #endif - dprintf(tinfo, "sha1: "); + fprintf(tinfo, "sha1: "); for (x = 0; x < 20 /* SHA_DIGEST_LENGTH */; x++) { - dprintf(tinfo, "%02x", torrent->hash[x]); + fprintf(tinfo, "%02x", torrent->hash[x]); } - dprintf(tinfo, "\n"); + fprintf(tinfo, "\n"); // start for next piece; #if defined(HAVE_EVP_MD_CTX_methods) EVP_MD_CTX_reset(torrent->ctx); @@ -105,20 +105,20 @@ void torrent_final(torrent_generator *torrent) EVP_DigestFinal(torrent->ctx, torrent->hash, NULL); EVP_MD_CTX_destroy(torrent->ctx); #endif - dprintf(torrent->tinfo, "sha1: "); + fprintf(torrent->tinfo, "sha1: "); for (x = 0; x < 20 /* SHA_DIGEST_LENGTH */; x++) { - dprintf(torrent->tinfo, "%02x", torrent->hash[x]); + fprintf(torrent->tinfo, "%02x", torrent->hash[x]); } - dprintf(torrent->tinfo, "\n"); + fprintf(torrent->tinfo, "\n"); } } void torrent_start_offset(torrent_generator *torrent, unsigned long long offset) { - dprintf(torrent->tinfo, "offset: %032llx\n", offset); + fprintf(torrent->tinfo, "offset: %032llx\n", offset); } void torrent_end_length(torrent_generator *torrent, unsigned long long length) { - dprintf(torrent->tinfo, "length: %032llx\n", length); + fprintf(torrent->tinfo, "length: %032llx\n", length); } diff --git a/src/torrent_helper.h b/src/torrent_helper.h index 61459a44..9896af29 100644 --- a/src/torrent_helper.h +++ b/src/torrent_helper.h @@ -38,7 +38,7 @@ typedef struct { unsigned long long PIECE_SIZE; unsigned char hash[20]; /* SHA_DIGEST_LENGTH, only present in */ /* fd for torrent.info. You should close fd yourself */ - int tinfo; + FILE *tinfo; /* remember the length for a piece size */ #if defined(HAVE_EVP_MD_CTX_methods) EVP_MD_CTX *ctx; @@ -49,7 +49,7 @@ typedef struct { } torrent_generator; // init -void torrent_init(torrent_generator *torrent, int tinfo); +void torrent_init(torrent_generator *torrent, FILE *tinfo); // put or write data void torrent_update(torrent_generator *torrent, void *buffer, size_t length); // flush all sha1 hash for end diff --git a/src/version.h b/src/version.h index 13d791f7..391d5cfe 100644 --- a/src/version.h +++ b/src/version.h @@ -3,5 +3,5 @@ * WHETHER THEY ARE BUILT BY OTHERS OR DURING DEVELOPMENT OR FOR THE * OFFICIAL PARTCLONE RELEASES. */ -#define git_version "d8fabb236cacace6c683e582a66e56c89ab2c4f1" +#define git_version "aa04b963aab8b8c856a5f9a2862ad2ffd767288b"