Skip to content

Commit

Permalink
add IO streams support for torrent info file
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Tsai committed Jul 20, 2023
1 parent aa04b96 commit 59875da
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AC_INIT([Partclone],[0.3.23],[[email protected]])
AC_INIT([Partclone],[0.3.24],[[email protected]])
AM_INIT_AUTOMAKE([-Wall foreign])
AM_GNU_GETTEXT_VERSION([0.16.1])
AM_GNU_GETTEXT([external])
Expand Down
24 changes: 12 additions & 12 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions src/torrent_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions src/torrent_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef struct {
unsigned long long PIECE_SIZE;
unsigned char hash[20]; /* SHA_DIGEST_LENGTH, only present in <openssl/sha.h> */
/* 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;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 59875da

Please sign in to comment.