Skip to content

Commit 343f634

Browse files
authored
Merge pull request #46 from uno20001/change_file_size_type
Change file size type
2 parents 84bcb42 + 5affc8e commit 343f634

10 files changed

+47
-115
lines changed

BSDmakefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ all: $(program)
6060

6161
.SUFFIXES: .o .c
6262
.c.o:
63-
$(CC) $(CFLAGS) $(DEFINES) -DPRIoff="\"`./prefix`d\"" -DVERSION="\"$(version)\"" -c $(.IMPSRC)
63+
$(CC) $(CFLAGS) $(DEFINES) -DVERSION="\"$(version)\"" -c $(.IMPSRC)
6464

65-
$(OBJS): $(HEADERS) prefix
65+
$(OBJS): $(HEADERS)
6666

6767
.include "rules.mk"

GNUmakefile

+2-4
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@ ifdef DEBUG
5454
DEFINES += -DDEBUG
5555
endif
5656

57-
OFFPRFX = $(shell ./prefix)
58-
5957
OBJS = $(SRCS:.c=.o)
6058

6159
all: $(program)
6260

63-
%.o: %.c $(HEADERS) prefix
64-
$(CC) $(CFLAGS) $(DEFINES) -DPRIoff="\"$(OFFPRFX)d\"" -DVERSION="\"$(version)\"" -c $<
61+
%.o: %.c $(HEADERS)
62+
$(CC) $(CFLAGS) $(DEFINES) -DVERSION="\"$(version)\"" -c $<
6563

6664
include rules.mk

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@
6565
program = mktorrent
6666
version = 1.1
6767

68-
HEADERS = mktorrent.h
68+
HEADERS = mktorrent.h ll.h
6969
SRCS = ftw.c init.c sha1.c hash.c output.c main.c msg.c ll.c

hash.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ EXPORT unsigned char *make_hash(struct metafile *m)
6565
the read buffer */
6666
SHA_CTX c; /* SHA1 hashing context */
6767
#ifndef NO_HASH_CHECK
68-
int64_t counter = 0; /* number of bytes hashed
68+
uintmax_t counter = 0; /* number of bytes hashed
6969
should match size when done */
7070
#endif
7171

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

9595
/* fill the read buffer with the contents of the file and append
@@ -133,7 +133,7 @@ EXPORT unsigned char *make_hash(struct metafile *m)
133133
#ifndef NO_HASH_CHECK
134134
counter += r;
135135
FATAL_IF(counter != m->size,
136-
"counted %" PRId64 " bytes, but hashed %" PRId64 " bytes; "
136+
"counted %" PRIuMAX " bytes, but hashed %" PRIuMAX " bytes; "
137137
"something is wrong...\n",
138138
m->size, counter);
139139
#endif

hash_pthreads.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ static void *worker(void *data)
202202

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

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

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

335335
return hash_string;
336336
}

init.c

+19-11
Original file line numberDiff line numberDiff line change
@@ -173,21 +173,24 @@ static int is_dir(struct metafile *m, char *target)
173173

174174
/* if it isn't a regular file either, something is wrong.. */
175175
FATAL_IF(!S_ISREG(s.st_mode),
176-
"'%s' is neither a directory nor regular file.\n", target);
177-
176+
"'%s' is neither a directory nor regular file\n", target);
177+
178+
/* if it has negative size, something it wrong */
179+
FATAL_IF(s.st_size < 0, "'%s' has negative size\n", target);
180+
178181
/* since we know the torrent is just a single file and we've
179182
already stat'ed it, we might as well set the file list */
180183
struct file_data fd = {
181184
strdup(target),
182-
s.st_size
185+
(uintmax_t) s.st_size
183186
};
184187

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

189192
/* ..and size variable */
190-
m->size = s.st_size;
193+
m->size = (uintmax_t) s.st_size;
191194

192195
/* now return 0 since it isn't a directory */
193196
return 0;
@@ -212,22 +215,27 @@ static int process_node(const char *path, const struct stat *sb, void *data)
212215
/* now path should be readable otherwise
213216
* display a warning and skip it */
214217
if (access(path, R_OK)) {
215-
fprintf(stderr, "Warning: Cannot read '%s', skipping.\n", path);
218+
fprintf(stderr, "warning: cannot read '%s', skipping\n", path);
219+
return 0;
220+
}
221+
222+
if (sb->st_size < 0) {
223+
fprintf(stderr, "warning: '%s' has negative size, skipping\n", path);
216224
return 0;
217225
}
218226

219227
if (m->verbose)
220-
printf("Adding %s\n", path);
228+
printf("adding %s\n", path);
221229

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

225233
/* create a new file list node for the file */
226234
struct file_data fd = {
227235
strdup(path),
228-
sb->st_size
236+
(uintmax_t) sb->st_size
229237
};
230-
238+
231239
if (fd.path == NULL || ll_append(m->file_list, &fd, sizeof(fd)) == NULL) {
232240
fprintf(stderr, "fatal error: out of memory\n");
233241
return -1;
@@ -546,8 +554,8 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
546554

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

mktorrent.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
#define DIRSEP_CHAR '/'
1010
#endif
1111

12+
13+
#include <stdint.h>
14+
1215
#include "ll.h"
1316

1417
struct file_data {
1518
char *path;
16-
int64_t size;
19+
uintmax_t size;
1720
};
1821

1922
struct metafile {
@@ -34,7 +37,7 @@ struct metafile {
3437
#endif
3538

3639
/* information calculated by read_dir() */
37-
int64_t size; /* combined size of all files */
40+
uintmax_t size; /* combined size of all files */
3841
struct ll *file_list; /* list of files and their sizes */
3942
unsigned int pieces; /* number of pieces */
4043
};

output.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
2222
#include <stdio.h> /* fprintf() etc. */
2323
#include <string.h> /* strlen() etc. */
2424
#include <time.h> /* time() */
25+
#include <inttypes.h> /* PRIuMAX */
2526

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

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

214215
/* let the user know we're done already */
215-
printf("done.\n");
216+
printf("done\n");
216217
fflush(stdout);
217218
}

prefix.c

-75
This file was deleted.

rules.mk

+3-6
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717

1818
.PHONY: strip indent clean install uninstall
1919

20-
prefix: prefix.c
21-
$(CC) $(CFLAGS) $(DEFINES) $(LDFLAGS) $< -o $@
22-
2320
$(program): $(OBJS)
2421
$(CC) $(CFLAGS) $(OBJS) -o $(program) $(LDFLAGS) $(LIBS)
2522

26-
allinone: $(SRCS) $(HEADERS) prefix
27-
$(CC) $(CFLAGS) $(DEFINES) -DPRIoff="\"`./prefix`d\"" -DVERSION="\"$(version)\"" -DALLINONE main.c -o $(program) $(LDFLAGS) $(LIBS)
23+
allinone: $(SRCS) $(HEADERS)
24+
$(CC) $(CFLAGS) $(DEFINES) -DVERSION="\"$(version)\"" -DALLINONE main.c -o $(program) $(LDFLAGS) $(LIBS)
2825

2926
strip:
3027
strip $(program)
@@ -33,7 +30,7 @@ indent:
3330
indent -kr -i8 *.c *.h
3431

3532
clean:
36-
rm -f $(program) prefix *.o *.c~ *.h~
33+
rm -f $(program) *.o *.c~ *.h~
3734

3835
install: $(program)
3936
$(INSTALL) -d $(DESTDIR)$(PREFIX)/bin

0 commit comments

Comments
 (0)