forked from pobrn/mktorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_pthreads.c
378 lines (320 loc) · 8.4 KB
/
hash_pthreads.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
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
*/
#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() */
#else
#include <inttypes.h>
#include "sha1.h"
#endif
#include <pthread.h> /* pthread functions and data structures */
#include "mktorrent.h"
#define EXPORT
#endif /* ALLINONE */
#ifndef PROGRESS_PERIOD
#define PROGRESS_PERIOD 200000
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#if defined _LARGEFILE_SOURCE && defined O_LARGEFILE
#define OPENFLAGS (O_RDONLY | O_BINARY | O_LARGEFILE)
#else
#define OPENFLAGS (O_RDONLY | O_BINARY)
#endif
struct piece_s;
typedef struct piece_s piece_t;
struct piece_s {
piece_t *next;
unsigned char *dest;
unsigned long len;
unsigned char data[1];
};
struct queue_s;
typedef struct queue_s queue_t;
struct queue_s {
piece_t *free;
piece_t *full;
unsigned int buffers_max;
unsigned int buffers;
pthread_mutex_t mutex_free;
pthread_mutex_t mutex_full;
pthread_cond_t cond_empty;
pthread_cond_t cond_full;
unsigned int done;
unsigned int pieces;
unsigned int pieces_hashed;
};
static piece_t *get_free(queue_t *q, size_t piece_length)
{
piece_t *r;
pthread_mutex_lock(&q->mutex_free);
if (q->free) {
r = q->free;
q->free = r->next;
} else if (q->buffers < q->buffers_max) {
r = malloc(sizeof(piece_t) - 1 + piece_length);
if (r == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
q->buffers++;
} else {
while (q->free == NULL) {
pthread_cond_wait(&q->cond_full, &q->mutex_free);
}
r = q->free;
q->free = r->next;
}
pthread_mutex_unlock(&q->mutex_free);
return r;
}
static piece_t *get_full(queue_t *q)
{
piece_t *r;
pthread_mutex_lock(&q->mutex_full);
again:
if (q->full) {
r = q->full;
q->full = r->next;
} else if (q->done) {
r = NULL;
} else {
pthread_cond_wait(&q->cond_empty, &q->mutex_full);
goto again;
}
pthread_mutex_unlock(&q->mutex_full);
return r;
}
static void put_free(queue_t *q, piece_t *p, unsigned int hashed)
{
pthread_mutex_lock(&q->mutex_free);
p->next = q->free;
q->free = p;
q->pieces_hashed += hashed;
pthread_mutex_unlock(&q->mutex_free);
pthread_cond_signal(&q->cond_full);
}
static void put_full(queue_t *q, piece_t *p)
{
pthread_mutex_lock(&q->mutex_full);
p->next = q->full;
q->full = p;
pthread_mutex_unlock(&q->mutex_full);
pthread_cond_signal(&q->cond_empty);
}
static void set_done(queue_t *q)
{
pthread_mutex_lock(&q->mutex_full);
q->done = 1;
pthread_mutex_unlock(&q->mutex_full);
pthread_cond_broadcast(&q->cond_empty);
}
static void free_buffers(queue_t *q)
{
piece_t *first = q->free;
while (first) {
piece_t *p = first;
first = p->next;
free(p);
}
q->free = NULL;
}
/*
* print the progress in a thread of its own
*/
static void *print_progress(void *data)
{
queue_t *q = data;
int err;
err = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
if (err) {
fprintf(stderr, "Error setting thread cancel type: %s\n",
strerror(err));
exit(EXIT_FAILURE);
}
while (1) {
/* print progress and flush the buffer immediately */
printf("\rHashed %u of %u pieces.", q->pieces_hashed, q->pieces);
fflush(stdout);
/* now sleep for PROGRESS_PERIOD microseconds */
usleep(PROGRESS_PERIOD);
}
return NULL;
}
static void *worker(void *data)
{
queue_t *q = data;
piece_t *p;
SHA_CTX c;
while ((p = get_full(q))) {
SHA1_Init(&c);
SHA1_Update(&c, p->data, p->len);
SHA1_Final(p->dest, &c);
put_free(q, p, 1);
}
return NULL;
}
static void read_files(metafile_t *m, queue_t *q, unsigned char *pos)
{
int fd; /* file descriptor */
flist_t *f; /* pointer to a place in the file list */
ssize_t r = 0; /* number of bytes read from file(s)
into the read buffer */
#ifndef NO_HASH_CHECK
off_t counter = 0; /* number of bytes hashed
should match size when done */
#endif
piece_t *p = get_free(q, m->piece_length);
/* go through all the files in the file list */
for (f = m->file_list; f; f = f->next) {
/* open the current file for reading */
if ((fd = open(f->path, OPENFLAGS)) == -1) {
fprintf(stderr, "Error opening '%s' for reading: %s\n",
f->path, strerror(errno));
exit(EXIT_FAILURE);
}
while (1) {
ssize_t d = read(fd, p->data + r, m->piece_length - r);
if (d < 0) {
fprintf(stderr, "Error reading from '%s': %s\n",
f->path, strerror(errno));
exit(EXIT_FAILURE);
}
if (d == 0) /* end of file */
break;
r += d;
if (r == m->piece_length) {
p->dest = pos;
p->len = m->piece_length;
put_full(q, p);
pos += SHA_DIGEST_LENGTH;
#ifndef NO_HASH_CHECK
counter += r;
#endif
r = 0;
p = get_free(q, m->piece_length);
}
}
/* now close the file */
if (close(fd)) {
fprintf(stderr, "Error closing '%s': %s\n",
f->path, strerror(errno));
exit(EXIT_FAILURE);
}
}
/* finally append the hash of the last irregular piece to the hash string */
if (r) {
p->dest = pos;
p->len = r;
put_full(q, p);
} else
put_free(q, p, 0);
#ifndef NO_HASH_CHECK
counter += r;
if (counter != m->size) {
fprintf(stderr, "Counted %" PRIoff " bytes, "
"but hashed %" PRIoff " bytes. "
"Something is wrong...\n", m->size, counter);
exit(EXIT_FAILURE);
}
#endif
}
EXPORT unsigned char *make_hash(metafile_t *m)
{
queue_t q = {
NULL, NULL, 0, 0,
PTHREAD_MUTEX_INITIALIZER,
PTHREAD_MUTEX_INITIALIZER,
PTHREAD_COND_INITIALIZER,
PTHREAD_COND_INITIALIZER,
0, 0, 0
};
pthread_t print_progress_thread; /* progress printer thread */
pthread_t *workers;
unsigned char *hash_string; /* the hash string */
unsigned int i;
int err;
workers = malloc(m->threads * sizeof(pthread_t));
hash_string = malloc(m->pieces * SHA_DIGEST_LENGTH);
if (workers == NULL || hash_string == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
q.pieces = m->pieces;
q.buffers_max = 3*m->threads;
/* create worker threads */
for (i = 0; i < m->threads; i++) {
err = pthread_create(&workers[i], NULL, worker, &q);
if (err) {
fprintf(stderr, "Error creating thread: %s\n",
strerror(err));
exit(EXIT_FAILURE);
}
}
/* now set off the progress printer */
err = pthread_create(&print_progress_thread, NULL, print_progress, &q);
if (err) {
fprintf(stderr, "Error creating thread: %s\n",
strerror(err));
exit(EXIT_FAILURE);
}
/* read files and feed pieces to the workers */
read_files(m, &q, hash_string);
/* we're done so stop printing our progress. */
err = pthread_cancel(print_progress_thread);
if (err) {
fprintf(stderr, "Error cancelling thread: %s\n",
strerror(err));
exit(EXIT_FAILURE);
}
/* inform workers we're done */
set_done(&q);
/* wait for workers to finish */
for (i = 0; i < m->threads; i++) {
err = pthread_join(workers[i], NULL);
if (err) {
fprintf(stderr, "Error joining thread: %s\n",
strerror(err));
exit(EXIT_FAILURE);
}
}
free(workers);
/* the progress printer should be done by now too */
err = pthread_join(print_progress_thread, NULL);
if (err) {
fprintf(stderr, "Error joining thread: %s\n",
strerror(err));
exit(EXIT_FAILURE);
}
/* destroy mutexes and condition variables */
pthread_mutex_destroy(&q.mutex_full);
pthread_mutex_destroy(&q.mutex_free);
pthread_cond_destroy(&q.cond_empty);
pthread_cond_destroy(&q.cond_full);
/* free buffers */
free_buffers(&q);
/* ok, let the user know we're done too */
printf("\rHashed %u of %u pieces.\n", q.pieces_hashed, q.pieces);
return hash_string;
}