Skip to content

Commit

Permalink
added proper large file support
Browse files Browse the repository at this point in the history
  • Loading branch information
esmil committed May 31, 2009
1 parent e8df0ae commit 88ffe24
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.[oa]
*~
mktorrent
prefix
8 changes: 4 additions & 4 deletions BSDmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ LIBS += -lcrypto
DEFINES += -DUSE_LONG_OPTIONS
.endif

.ifdef USE_LONG_LONG
DEFINES += -DUSE_LONG_LONG
.ifdef USE_LARGE_FILES
DEFINES += -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
.endif

.ifdef NO_HASH_CHECK
Expand All @@ -60,8 +60,8 @@ all: $(program)

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

$(OBJS): $(HEADERS)
$(OBJS): $(HEADERS) prefix

.include "rules.mk"
10 changes: 6 additions & 4 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ ifdef USE_LONG_OPTIONS
DEFINES += -DUSE_LONG_OPTIONS
endif

ifdef USE_LONG_LONG
DEFINES += -DUSE_LONG_LONG
ifdef USE_LARGE_FILES
DEFINES += -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
endif

ifdef NO_HASH_CHECK
Expand All @@ -54,11 +54,13 @@ ifdef DEBUG
DEFINES += -DDEBUG
endif

OFFPRFX = $(shell ./prefix)

OBJS = $(SRCS:.c=.o)

all: $(program)

%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) $(DEFINES) -DVERSION="\"$(version)\"" -c $<
%.o: %.c $(HEADERS) prefix
$(CC) $(CFLAGS) $(DEFINES) -DPRIoff="\"$(OFFPRFX)d\"" -DVERSION="\"$(version)\"" -c $<

include rules.mk
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
# Enable long options, started with two dashes.
#USE_LONG_OPTIONS = 1

# Use the C type 'unsigned long long'. This is needed on 32bit machines
# to handle torrents > 4Gb properly. Some compilers might not like this.
#USE_LONG_LONG = 1
# This is needed on certain 32bit OSes (notably 32bit Linux) to support
# files and torrents > 2Gb.
#USE_LARGE_FILES = 1

# Disable a redundent check to see if the amount of bytes read from files while
# Disable a redundant check to see if the amount of bytes read from files while
# hashing matches the sum of reported file sizes. I've never seen this fail. It
# will fail if you change files yet to be hashed while mktorrent is running,
# but who'd want to do that?
Expand Down
26 changes: 15 additions & 11 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef ALLINONE

#include <stdlib.h> /* exit() */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* printf() etc. */
#include <fcntl.h> /* open() */
#include <unistd.h> /* read(), close() */
#include <stdlib.h> /* exit() */
#include <sys/types.h> /* off_t */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* printf() etc. */
#include <fcntl.h> /* open() */
#include <unistd.h> /* read(), close() */

#ifdef USE_OPENSSL
#include <openssl/sha.h> /* SHA1() - remember to compile with -lssl */
#include <openssl/sha.h> /* SHA1() */
#else
#include <inttypes.h>
#include "sha1.h"
Expand Down Expand Up @@ -58,7 +58,7 @@ EXPORT unsigned char *make_hash(metafile_t *m)
the read buffer */
SHA_CTX c; /* SHA1 hashing context */
#ifndef NO_HASH_CHECK
fsize_t counter = 0; /* number of bytes hashed
off_t counter = 0; /* number of bytes hashed
should match size when done */
#endif

Expand All @@ -82,7 +82,11 @@ EXPORT unsigned char *make_hash(metafile_t *m)
for (f = m->file_list; f; f = f->next) {

/* open the current file for reading */
#if defined _LARGEFILE_SOURCE && defined O_LARGEFILE
if ((fd = open(f->path, O_RDONLY | O_BINARY | O_LARGEFILE)) == -1) {
#else
if ((fd = open(f->path, O_RDONLY | O_BINARY)) == -1) {
#endif
fprintf(stderr, "Error opening '%s' for reading: %s\n",
f->path, strerror(errno));
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -138,8 +142,8 @@ EXPORT unsigned char *make_hash(metafile_t *m)
#ifndef NO_HASH_CHECK
counter += r;
if (counter != m->size) {
fprintf(stderr, "Counted " PRIfz " bytes, "
"but hashed " PRIfz " bytes. "
fprintf(stderr, "Counted %" PRIoff " bytes, "
"but hashed %" PRIoff " bytes. "
"Something is wrong...\n", m->size, counter);
exit(EXIT_FAILURE);
}
Expand Down
13 changes: 9 additions & 4 deletions hash_pthreads.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef ALLINONE
#include <stdlib.h> /* exit(), malloc() */
#include <sys/types.h> /* off_t */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* printf() etc. */
#include <fcntl.h> /* open() */
#include <unistd.h> /* access(), read(), close() */
#ifdef USE_OPENSSL
#include <openssl/sha.h> /* SHA1() - remember to compile with -lssl */
#include <openssl/sha.h> /* SHA1() */
#else
#include <inttypes.h>
#include "sha1.h"
Expand Down Expand Up @@ -209,7 +210,7 @@ static void read_files(metafile_t *m, queue_t *q, unsigned char *pos)
ssize_t r = 0; /* number of bytes read from
file(s) into the read buffer */
#ifndef NO_HASH_CHECK
fsize_t counter = 0; /* number of bytes hashed
off_t counter = 0; /* number of bytes hashed
should match size when done */
#endif
piece_t *p = get_free(q, m->piece_length);
Expand All @@ -218,7 +219,11 @@ static void read_files(metafile_t *m, queue_t *q, unsigned char *pos)
for (f = m->file_list; f; f = f->next) {

/* open the current file for reading */
#if defined _LARGEFILE_SOURCE && defined O_LARGEFILE
if ((fd = open(f->path, O_RDONLY | O_BINARY | O_LARGEFILE)) == -1) {
#else
if ((fd = open(f->path, O_RDONLY | O_BINARY)) == -1) {
#endif
fprintf(stderr, "Error opening '%s' for reading: %s\n",
f->path, strerror(errno));
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -270,8 +275,8 @@ static void read_files(metafile_t *m, queue_t *q, unsigned char *pos)
#ifndef NO_HASH_CHECK
counter += r;
if (counter != m->size) {
fprintf(stderr, "Counted " PRIfz " bytes, "
"but hashed " PRIfz " bytes. "
fprintf(stderr, "Counted %" PRIoff " bytes, "
"but hashed %" PRIoff " bytes. "
"Something is wrong...\n", m->size, counter);
exit(EXIT_FAILURE);
}
Expand Down
20 changes: 10 additions & 10 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef ALLINONE

#include <stdlib.h> /* exit() */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* printf() etc. */
#include <sys/stat.h> /* the stat structure */
#include <unistd.h> /* getopt(), getcwd() */
#include <string.h> /* strcmp(), strlen(), strncpy() */
#include <stdlib.h> /* exit() */
#include <sys/types.h> /* off_t */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* printf() etc. */
#include <sys/stat.h> /* the stat structure */
#include <unistd.h> /* getopt(), getcwd() */
#include <string.h> /* strcmp(), strlen(), strncpy() */
#ifdef USE_LONG_OPTIONS
#include <getopt.h> /* getopt_long() */
#include <getopt.h> /* getopt_long() */
#endif

#include "mktorrent.h"
Expand Down Expand Up @@ -563,7 +563,7 @@ EXPORT void init(metafile_t *m, int argc, char *argv[])

/* now print the size and piece count if we should be verbose */
if (m->verbose)
printf("\n" PRIfz " bytes in all.\n"
printf("\n%" PRIoff " bytes in all.\n"
"That's %u pieces of %u bytes each.\n\n",
m->size, m->pieces, m->piece_length);
}
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

#include <stdlib.h> /* exit() */
#include <sys/types.h> /* off_t */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* printf() etc. */
#include <sys/stat.h> /* S_IRUSR, S_IWUSR, S_IRGRP, S_IROTH */
#include <fcntl.h> /* open() */

#ifdef ALLINONE

#include <sys/stat.h>
#include <unistd.h> /* access(), read(), close(), getcwd() */
#ifdef USE_LONG_OPTIONS
Expand All @@ -44,7 +44,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#endif

#define EXPORT static
#else
#else /* ALLINONE */

#define EXPORT
#endif /* ALLINONE */
Expand Down
13 changes: 2 additions & 11 deletions mktorrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
#define DIRSEP_CHAR '/'
#endif

/* fsize_t */
#ifdef USE_LONG_LONG
typedef unsigned long long fsize_t;
#define PRIfz "%llu"
#else
typedef unsigned long fsize_t;
#define PRIfz "%lu"
#endif

/* string list */
struct slist_s;
typedef struct slist_s slist_t;
Expand All @@ -39,7 +30,7 @@ struct flist_s;
typedef struct flist_s flist_t;
struct flist_s {
char *path;
fsize_t size;
off_t size;
flist_t *next;
};

Expand All @@ -60,7 +51,7 @@ typedef struct {
#endif

/* information calculated by read_dir() */
fsize_t size; /* combined size of all files */
off_t size; /* combined size of all files */
flist_t *file_list; /* list of files and their sizes */
unsigned int pieces; /* number of pieces */
} metafile_t;
Expand Down
14 changes: 7 additions & 7 deletions output.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef ALLINONE

#include <stdio.h> /* printf() etc. */
#include <string.h> /* strlen() etc. */
#include <time.h> /* time() */
#include <sys/types.h> /* off_t */
#include <stdio.h> /* printf() etc. */
#include <string.h> /* strlen() etc. */
#include <time.h> /* time() */
#ifdef USE_OPENSSL
#include <openssl/sha.h> /* SHA_DIGEST_LENGTH */
#include <openssl/sha.h> /* SHA_DIGEST_LENGTH */
#else
#include <inttypes.h>
#include "sha1.h"
Expand Down Expand Up @@ -68,7 +68,7 @@ static void write_file_list(FILE *f, flist_t *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" PRIfz "e4:pathl", list->size);
fprintf(f, "d6:lengthi%" PRIoff "e4:pathl", list->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 @@ -146,7 +146,7 @@ EXPORT void write_metainfo(FILE *f, metafile_t *m, unsigned char *hash_string)
/* 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" PRIfz "e", m->file_list->size);
fprintf(f, "6:lengthi%" PRIoff "e", m->file_list->size);
else
write_file_list(f, m->file_list);

Expand Down
43 changes: 43 additions & 0 deletions prefix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>

#ifndef TYPE
#define TYPE off_t
#endif

int main(int argc, char *argv[])
{
unsigned int i;
char *prefix[9];

for (i = 0; i < 9; i++)
prefix[i] = NULL;

prefix[1] = "hh";
prefix[2] = "h";

prefix[8] = "ll";
prefix[sizeof(int)] = "";
prefix[sizeof(long int)] = "l";

#ifdef DEBUG
#define xstr(s) str(s)
#define str(s) #s

#ifdef _LARGEFILE_SOURCE
printf("_LARGEFILE_SOURCE is set\n");
#endif

#ifdef _FILE_OFFSET_BITS
printf("_FILE_OFFSET_BITS = %lu\n", _FILE_OFFSET_BITS);
#endif

printf("sizeof(" xstr(TYPE) ") = %lu, %lu bits\n",
sizeof(TYPE), 8*sizeof(TYPE));
#endif /* DEBUG */

printf("%s\n", prefix[sizeof(TYPE)]);

return EXIT_SUCCESS;
}
9 changes: 6 additions & 3 deletions rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

.PHONY: strip indent clean install uninstall

prefix: prefix.c
$(CC) $(CFLAGS) $(DEFINES) $(LDFLAGS) $< -o $@

$(program): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(program) $(LDFLAGS) $(LIBS)

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

strip:
strip $(program)
Expand All @@ -30,7 +33,7 @@ indent:
indent -kr -i8 *.c *.h

clean:
rm -f $(program) *.o *.c~ *.h~
rm -f $(program) prefix *.o *.c~ *.h~

install: $(program)
$(INSTALL) -d $(DESTDIR)$(PREFIX)/bin
Expand Down

0 comments on commit 88ffe24

Please sign in to comment.