Skip to content

Commit

Permalink
change file size type and change some status messages
Browse files Browse the repository at this point in the history
  • Loading branch information
uno20001 committed May 24, 2020
1 parent 84bcb42 commit 2d55f76
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
6 changes: 3 additions & 3 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ EXPORT unsigned char *make_hash(struct metafile *m)
the read buffer */
SHA_CTX c; /* SHA1 hashing context */
#ifndef NO_HASH_CHECK
int64_t counter = 0; /* number of bytes hashed
uintmax_t counter = 0; /* number of bytes hashed
should match size when done */
#endif

Expand All @@ -89,7 +89,7 @@ EXPORT unsigned char *make_hash(struct metafile *m)
/* open the current file for reading */
FATAL_IF((fd = open(f->path, OPENFLAGS)) == -1,
"cannot open '%s' for reading: %s\n", f->path, strerror(errno));
printf("Hashing %s.\n", f->path);
printf("hashing %s\n", f->path);
fflush(stdout);

/* fill the read buffer with the contents of the file and append
Expand Down Expand Up @@ -133,7 +133,7 @@ EXPORT unsigned char *make_hash(struct metafile *m)
#ifndef NO_HASH_CHECK
counter += r;
FATAL_IF(counter != m->size,
"counted %" PRId64 " bytes, but hashed %" PRId64 " bytes; "
"counted %" PRIuMAX " bytes, but hashed %" PRIuMAX " bytes; "
"something is wrong...\n",
m->size, counter);
#endif
Expand Down
14 changes: 7 additions & 7 deletions hash_pthreads.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ static void *worker(void *data)

static void read_files(struct metafile *m, struct queue *q, unsigned char *pos)
{
int fd; /* file descriptor */
size_t r = 0; /* number of bytes read from file(s)
into the read buffer */
int fd; /* file descriptor */
size_t r = 0; /* number of bytes read from file(s)
into the read buffer */
#ifndef NO_HASH_CHECK
int64_t counter = 0; /* number of bytes hashed
should match size when done */
uintmax_t counter = 0; /* number of bytes hashed
should match size when done */
#endif
struct piece *p = get_free(q, m->piece_length);

Expand Down Expand Up @@ -259,7 +259,7 @@ static void read_files(struct metafile *m, struct queue *q, unsigned char *pos)
#ifndef NO_HASH_CHECK
counter += r;
FATAL_IF(counter != m->size,
"counted %" PRId64 " bytes, but hashed %" PRId64 " bytes; "
"counted %" PRIuMAX " bytes, but hashed %" PRIuMAX " bytes; "
"something is wrong...\n",
m->size, counter);
#endif
Expand Down Expand Up @@ -330,7 +330,7 @@ EXPORT unsigned char *make_hash(struct metafile *m)
free_buffers(&q);

/* ok, let the user know we're done too */
printf("\rHashed %u of %u pieces.\n", q.pieces_hashed, q.pieces);
printf("\rhashed %u of %u pieces\n", q.pieces_hashed, q.pieces);

return hash_string;
}
30 changes: 19 additions & 11 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,24 @@ static int is_dir(struct metafile *m, char *target)

/* if it isn't a regular file either, something is wrong.. */
FATAL_IF(!S_ISREG(s.st_mode),
"'%s' is neither a directory nor regular file.\n", target);

"'%s' is neither a directory nor regular file\n", target);

/* if it has negative size, something it wrong */
FATAL_IF(s.st_size < 0, "'%s' has negative size\n", target);

/* since we know the torrent is just a single file and we've
already stat'ed it, we might as well set the file list */
struct file_data fd = {
strdup(target),
s.st_size
(uintmax_t) s.st_size
};

FATAL_IF0(
fd.path == NULL || ll_append(m->file_list, &fd, sizeof(fd)) == NULL,
"out of memory\n");

/* ..and size variable */
m->size = s.st_size;
m->size = (uintmax_t) s.st_size;

/* now return 0 since it isn't a directory */
return 0;
Expand All @@ -212,22 +215,27 @@ static int process_node(const char *path, const struct stat *sb, void *data)
/* now path should be readable otherwise
* display a warning and skip it */
if (access(path, R_OK)) {
fprintf(stderr, "Warning: Cannot read '%s', skipping.\n", path);
fprintf(stderr, "warning: cannot read '%s', skipping\n", path);
return 0;
}

if (sb->st_size < 0) {
fprintf(stderr, "warning: '%s' has negative size, skipping\n", path);
return 0;
}

if (m->verbose)
printf("Adding %s\n", path);
printf("adding %s\n", path);

/* count the total size of the files */
m->size += sb->st_size;
m->size += (uintmax_t) sb->st_size;

/* create a new file list node for the file */
struct file_data fd = {
strdup(path),
sb->st_size
(uintmax_t) sb->st_size
};

if (fd.path == NULL || ll_append(m->file_list, &fd, sizeof(fd)) == NULL) {
fprintf(stderr, "fatal error: out of memory\n");
return -1;
Expand Down Expand Up @@ -546,8 +554,8 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])

/* now print the size and piece count if we should be verbose */
if (m->verbose)
printf("\n%" PRIoff " bytes in all.\n"
"That's %u pieces of %u bytes each.\n\n",
printf("\n%" PRIuMAX " bytes in all\n"
"that's %u pieces of %u bytes each\n\n",
m->size, m->pieces, m->piece_length);
}

Expand Down
7 changes: 5 additions & 2 deletions mktorrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
#define DIRSEP_CHAR '/'
#endif


#include <stdint.h>

#include "ll.h"

struct file_data {
char *path;
int64_t size;
uintmax_t size;
};

struct metafile {
Expand All @@ -34,7 +37,7 @@ struct metafile {
#endif

/* information calculated by read_dir() */
int64_t size; /* combined size of all files */
uintmax_t size; /* combined size of all files */
struct ll *file_list; /* list of files and their sizes */
unsigned int pieces; /* number of pieces */
};
Expand Down
9 changes: 5 additions & 4 deletions output.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include <stdio.h> /* fprintf() etc. */
#include <string.h> /* strlen() etc. */
#include <time.h> /* time() */
#include <inttypes.h> /* PRIuMAX */

#ifdef USE_OPENSSL
#include <openssl/sha.h> /* SHA_DIGEST_LENGTH */
Expand Down Expand Up @@ -77,7 +78,7 @@ static void write_file_list(FILE *f, struct ll *list)
/* the file list contains a dictionary for every file
with entries for the length and path
write the length first */
fprintf(f, "d6:lengthi%" PRIoff "e4:pathl", fd->size);
fprintf(f, "d6:lengthi%" PRIuMAX "e4:pathl", fd->size);
/* the file path is written as a list of subdirectories
and the last entry is the filename
sorry this code is even uglier than the rest */
Expand Down Expand Up @@ -128,7 +129,7 @@ static void write_web_seed_list(FILE *f, struct ll *list)
EXPORT void write_metainfo(FILE *f, struct metafile *m, unsigned char *hash_string)
{
/* let the user know we've started writing the metainfo file */
printf("Writing metainfo file... ");
printf("writing metainfo file... ");
fflush(stdout);

/* every metainfo file is one big dictonary */
Expand Down Expand Up @@ -173,7 +174,7 @@ EXPORT void write_metainfo(FILE *f, struct metafile *m, unsigned char *hash_stri
/* first entry is either 'length', which specifies the length of a
single file torrent, or a list of files and their respective sizes */
if (!m->target_is_directory)
fprintf(f, "6:lengthi%" PRIoff "e",
fprintf(f, "6:lengthi%" PRIuMAX "e",
LL_DATA_AS(LL_HEAD(m->file_list), struct file_data*)->size);
else
write_file_list(f, m->file_list);
Expand Down Expand Up @@ -212,6 +213,6 @@ EXPORT void write_metainfo(FILE *f, struct metafile *m, unsigned char *hash_stri
fprintf(f, "e");

/* let the user know we're done already */
printf("done.\n");
printf("done\n");
fflush(stdout);
}

0 comments on commit 2d55f76

Please sign in to comment.