libfetch: fix races in the cache connection code.

Tested by @Gottox.
This commit is contained in:
Juan RP
2014-12-23 10:52:54 +01:00
parent 6a985190aa
commit 4ee6f943dd
3 changed files with 11 additions and 16 deletions

View File

@@ -30,7 +30,6 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "xbps_api_impl.h"
@@ -44,7 +43,6 @@ struct fetch_archive {
struct url *url;
struct fetchIO *fetch;
char buffer[32768];
pthread_mutex_t mtx;
};
static int
@@ -52,9 +50,7 @@ fetch_archive_open(struct archive *a _unused, void *client_data)
{
struct fetch_archive *f = client_data;
pthread_mutex_lock(&f->mtx);
f->fetch = fetchGet(f->url, NULL);
pthread_mutex_unlock(&f->mtx);
if (f->fetch == NULL)
return ENOENT;
@@ -66,13 +62,9 @@ static ssize_t
fetch_archive_read(struct archive *a _unused, void *client_data, const void **buf)
{
struct fetch_archive *f = client_data;
ssize_t res;
*buf = f->buffer;
pthread_mutex_lock(&f->mtx);
res = fetchIO_read(f->fetch, f->buffer, sizeof(f->buffer));
pthread_mutex_unlock(&f->mtx);
return res;
return fetchIO_read(f->fetch, f->buffer, sizeof(f->buffer));
}
static int
@@ -80,11 +72,8 @@ fetch_archive_close(struct archive *a _unused, void *client_data)
{
struct fetch_archive *f = client_data;
pthread_mutex_lock(&f->mtx);
if (f->fetch != NULL)
fetchIO_close(f->fetch);
pthread_mutex_unlock(&f->mtx);
pthread_mutex_destroy(&f->mtx);
free(f);
return 0;
@@ -101,7 +90,6 @@ open_archive_by_url(struct url *url)
return NULL;
f->url = url;
pthread_mutex_init(&f->mtx, NULL);
if ((a = archive_read_new()) == NULL) {
free(f);