forked from pobrn/mktorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmktorrent.c
283 lines (241 loc) · 7.18 KB
/
libmktorrent.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
This file is part of mktorrent
copy from main by pertershaw
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 "libmktorrent.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);
}
}
metafile_t *getDefaultMetaStruc(){
static 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 */
};
return &m;
}
/*
* a reimplementation of init.
* this version does not pare the command line,
* it takes &m as given input.
*/
int initLib(metafile_t *m, char *annouceUrl, char *fileOrDirName)
{
llist_t *announce_last = NULL;
slist_t *web_seed_last = NULL;
if (announce_last == NULL) {
m->announce_list = announce_last = malloc(sizeof(llist_t));
} else {
announce_last->next = malloc(sizeof(llist_t));
announce_last = announce_last->next;
}
if (announce_last == NULL) {
fprintf(stderr, "Out of memory.\n");
return EXIT_FAILURE;
}
announce_last->l = get_slist(annouceUrl);
/* set the correct piece length.
default is 2^18 = 256kb. */
if (m->piece_length < 15 || m->piece_length > 28) {
fprintf(stderr,
"The piece length must be a number between "
"15 and 28.\n");
return EXIT_FAILURE;
}
m->piece_length = 1 << m->piece_length;
/* user must specify at least one announce URL as it wouldn't make
* any sense to have a default for this.
* it is ok not to have any unless torrent is private. */
if (m->announce_list == NULL && m->private == 1) {
fprintf(stderr, "Must specify an announce URL.\n");
return EXIT_FAILURE;
}
if (announce_last != NULL){
announce_last->next = NULL;
}
#ifdef USE_PTHREADS
/* check the number of threads */
if (m->threads) {
if (m->threads > 20) {
fprintf(stderr, "The number of threads is limited to "
"at most 20\n");
return EXIT_FAILURE;
}
} else {
#ifdef _SC_NPROCESSORS_ONLN
m->threads = sysconf(_SC_NPROCESSORS_ONLN);
if (m->threads == -1)
#endif
m->threads = 2; /* some sane default */
}
#endif
/* strip ending DIRSEP's from target */
strip_ending_dirseps(fileOrDirName);
/* if the torrent name isn't set use the basename of the target */
if (m->torrent_name == NULL){
m->torrent_name = basename(fileOrDirName);
}
/* make sure m->metainfo_file_path is the absolute path to the file */
set_absolute_file_path(m);
/* if we should be verbose print out all the options
as we have set them */
if (m->verbose){
dump_options(m);
}
/* check if target is a directory or just a single file */
m->target_is_directory = is_dir(m, fileOrDirName);
if (m->target_is_directory) {
/* change to the specified directory */
if (chdir(fileOrDirName)) {
fprintf(stderr, "Error changing directory to '%s': %s\n",
fileOrDirName, strerror(errno));
return EXIT_FAILURE;
}
if (file_tree_walk("." DIRSEP, MAX_OPENFD, process_node, m)){
return EXIT_FAILURE;
}
}
/* calculate the number of pieces
pieces = ceil( size / piece_length ) */
m->pieces = (m->size + m->piece_length - 1) / m->piece_length;
/* 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",
m->size, m->pieces, m->piece_length);
}
return EXIT_SUCCESS;
}
int mktorrent(char *fileOrDirName, char* annouceUrl, metafile_t *m){
FILE *file; /* stream for writing to the metainfo file */
/* process options */
int status = initLib(m, annouceUrl, fileOrDirName);
if(status == EXIT_FAILURE){
return status;
}
/* 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);
return EXIT_SUCCESS;
}