Skip to content

Commit

Permalink
file only ends when read() returns 0
Browse files Browse the repository at this point in the history
  • Loading branch information
esmil committed May 31, 2009
1 parent 4e8815b commit e8df0ae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
28 changes: 16 additions & 12 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,21 @@ EXPORT unsigned char *make_hash(metafile_t *m)
exit(EXIT_FAILURE);
}

r += d;

if (r < m->piece_length)
if (d == 0) /* end of file */
break;

SHA1_Init(&c);
SHA1_Update(&c, read_buf, m->piece_length);
SHA1_Final(pos, &c);
pos += SHA_DIGEST_LENGTH;
r += d;

if (r == m->piece_length) {
SHA1_Init(&c);
SHA1_Update(&c, read_buf, m->piece_length);
SHA1_Final(pos, &c);
pos += SHA_DIGEST_LENGTH;
#ifndef NO_HASH_CHECK
counter += r; /* r == piece_length */
counter += r; /* r == piece_length */
#endif
r = 0;
r = 0;
}
}

/* now close the file */
Expand All @@ -127,9 +129,11 @@ EXPORT unsigned char *make_hash(metafile_t *m)
}

/* finally append the hash of the last irregular piece to the hash string */
SHA1_Init(&c);
SHA1_Update(&c, read_buf, r);
SHA1_Final(pos, &c);
if (r) {
SHA1_Init(&c);
SHA1_Update(&c, read_buf, r);
SHA1_Final(pos, &c);
}

#ifndef NO_HASH_CHECK
counter += r;
Expand Down
37 changes: 21 additions & 16 deletions hash_pthreads.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ static piece_t *get_full(queue_t *q)
return r;
}

static void put_free(queue_t *q, piece_t *p)
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++;
q->pieces_hashed += hashed;
pthread_mutex_unlock(&q->mutex_free);
pthread_cond_signal(&q->cond_full);
}
Expand Down Expand Up @@ -195,7 +195,7 @@ static void *worker(void *data)
SHA1_Init(&c);
SHA1_Update(&c, p->data, p->len);
SHA1_Final(p->dest, &c);
put_free(q, p);
put_free(q, p, 1);
}

return NULL;
Expand Down Expand Up @@ -233,20 +233,22 @@ static void read_files(metafile_t *m, queue_t *q, unsigned char *pos)
exit(EXIT_FAILURE);
}

r += d;

if (r < m->piece_length)
if (d == 0) /* end of file */
break;

p->dest = pos;
p->len = m->piece_length;
put_full(q, p);
pos += SHA_DIGEST_LENGTH;
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;
counter += r;
#endif
r = 0;
p = get_free(q, m->piece_length);
r = 0;
p = get_free(q, m->piece_length);
}
}

/* now close the file */
Expand All @@ -258,9 +260,12 @@ static void read_files(metafile_t *m, queue_t *q, unsigned char *pos)
}

/* finally append the hash of the last irregular piece to the hash string */
p->dest = pos;
p->len = r;
put_full(q, p);
if (r) {
p->dest = pos;
p->len = r;
put_full(q, p);
} else
put_free(q, p, 0);

#ifndef NO_HASH_CHECK
counter += r;
Expand Down

0 comments on commit e8df0ae

Please sign in to comment.