forked from pobrn/mktorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
176 lines (149 loc) · 4.31 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
This file is part of mktorrent
Copyright (C) 2007, 2009 Emil Renner Berthing
mktorrent is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
mktorrent is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
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(), sysconf() */
#ifdef USE_LONG_OPTIONS
#include <getopt.h> /* getopt_long() */
#endif
#include <time.h> /* time() */
#include <dirent.h> /* opendir(), closedir(), readdir() etc. */
#ifdef USE_OPENSSL
#include <openssl/sha.h> /* SHA1(), SHA_DIGEST_LENGTH */
#else
#include <inttypes.h>
#endif
#ifdef USE_PTHREADS
#include <pthread.h> /* pthread functions and data structures */
#endif
#define EXPORT static
#else /* ALLINONE */
#define EXPORT
#endif /* ALLINONE */
#include "mktorrent.h"
#ifdef ALLINONE
#include "ftw.c"
#include "init.c"
#ifndef USE_OPENSSL
#include "sha1.c"
#endif
#ifdef USE_PTHREADS
#include "hash_pthreads.c"
#else
#include "hash.c"
#endif
#include "output.c"
#else /* ALLINONE */
/* init.c */
extern void init(metafile_t *m, int argc, char *argv[]);
/* hash.c */
extern unsigned char *make_hash(metafile_t *m);
/* output.c */
extern void write_metainfo(FILE *f, metafile_t *m, unsigned char *hash_string);
#endif /* ALLINONE */
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef S_IRGRP
#define S_IRGRP 0
#endif
#ifndef S_IROTH
#define S_IROTH 0
#endif
/*
* create and open the metainfo file for writing and create a stream for it
* we don't want to overwrite anything, so abort if the file is already there
*/
static FILE *open_file(const char *path)
{
int fd; /* file descriptor */
FILE *f; /* file stream */
/* open and create the file if it doesn't exist already */
fd = open(path, O_WRONLY | O_BINARY | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0) {
fprintf(stderr, "Error creating '%s': %s\n",
path, strerror(errno));
exit(EXIT_FAILURE);
}
/* create the stream from this filedescriptor */
f = fdopen(fd, "wb");
if (f == NULL) {
fprintf(stderr, "Error creating stream for '%s': %s\n",
path, strerror(errno));
exit(EXIT_FAILURE);
}
return f;
}
/*
* close the metainfo file
*/
static void close_file(FILE *f)
{
/* close the metainfo file */
if (fclose(f)) {
fprintf(stderr, "Error closing stream: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
}
/*
* main().. it starts
*/
int main(int argc, char *argv[])
{
FILE *file; /* stream for writing to the metainfo file */
metafile_t m = {
/* options */
18, /* piece_length, 2^18 = 256kb by default */
NULL, /* announce_list */
NULL, /* torrent_name */
NULL, /* metainfo_file_path */
NULL, /* web_seed_url */
NULL, /* comment */
0, /* target_is_directory */
0, /* no_creation_date */
0, /* private */
0, /* verbose */
#ifdef USE_PTHREADS
0, /* threads, initialised by init() */
#endif
/* information calculated by read_dir() */
0, /* size */
NULL, /* file_list */
0 /* pieces */
};
/* print who we are */
printf("mktorrent " VERSION " (c) 2007, 2009 Emil Renner Berthing\n\n");
/* process options */
init(&m, argc, argv);
/* open the file stream now, so we don't have to abort
_after_ we did all the hashing in case we fail */
file = open_file(m.metainfo_file_path);
/* calculate hash string and write the metainfo to file */
write_metainfo(file, &m, make_hash(&m));
/* close the file stream */
close_file(file);
/* yeih! everything seemed to go as planned */
return EXIT_SUCCESS;
}