gzip: always autoclose the inner stream

cute-signatures
Timo Teras 2009-07-16 13:47:26 +03:00
parent 6715a0ba12
commit 0f6d96a4f5
9 changed files with 22 additions and 41 deletions

View File

@ -21,7 +21,6 @@ typedef int (*apk_archive_entry_parser)(void *ctx,
struct apk_istream *istream);
int apk_parse_tar(struct apk_istream *, apk_archive_entry_parser parser, void *ctx);
int apk_parse_tar_gz(struct apk_bstream *, apk_archive_entry_parser parser, void *ctx);
int apk_write_tar_entry(struct apk_ostream *, const struct apk_file_info *ae, char *data);
int apk_archive_entry_extract(const struct apk_file_info *ae,

View File

@ -51,12 +51,11 @@ struct apk_ostream {
typedef int (*apk_multipart_cb)(void *ctx, EVP_MD_CTX *mdctx, int part);
struct apk_istream *apk_bstream_gunzip_mpart(struct apk_bstream *, int,
struct apk_istream *apk_bstream_gunzip_mpart(struct apk_bstream *,
apk_multipart_cb cb, void *ctx);
static inline struct apk_istream *apk_bstream_gunzip(struct apk_bstream *bs,
int autoclose)
static inline struct apk_istream *apk_bstream_gunzip(struct apk_bstream *bs)
{
return apk_bstream_gunzip_mpart(bs, autoclose, NULL, NULL);
return apk_bstream_gunzip_mpart(bs, NULL, NULL);
}
struct apk_ostream *apk_ostream_gzip(struct apk_ostream *);

View File

@ -255,19 +255,6 @@ int apk_write_tar_entry(struct apk_ostream *os, const struct apk_file_info *ae,
return 0;
}
int apk_parse_tar_gz(struct apk_bstream *bs, apk_archive_entry_parser parser,
void *ctx)
{
struct apk_istream *is;
int rc;
is = apk_bstream_gunzip(bs, FALSE);
rc = apk_parse_tar(is, parser, ctx);
is->close(is);
return rc;
}
int apk_archive_entry_extract(const struct apk_file_info *ae,
struct apk_istream *is,
const char *fn, apk_progress_cb cb,

View File

@ -1135,7 +1135,7 @@ int apk_db_add_repository(apk_database_t _db, apk_blob_t repository)
} else {
bs = apk_repository_file_open(repo, apk_index_gz);
}
bs = apk_bstream_from_istream(apk_bstream_gunzip(bs, TRUE));
bs = apk_bstream_from_istream(apk_bstream_gunzip(bs));
if (bs == NULL) {
apk_warning("Failed to open index for %s", repo->url);
return -1;
@ -1377,7 +1377,6 @@ static int apk_db_unpack_pkg(struct apk_database *db,
struct apk_istream *tar;
char pkgname[256], file[256];
int i, need_copy = FALSE;
size_t length;
snprintf(pkgname, sizeof(pkgname), "%s-%s.apk",
newpkg->name->name, newpkg->version);
@ -1433,10 +1432,10 @@ static int apk_db_unpack_pkg(struct apk_database *db,
.cb_ctx = cb_ctx,
};
tar = apk_bstream_gunzip_mpart(bs, FALSE, apk_db_gzip_part, &ctx);
tar = apk_bstream_gunzip_mpart(bs, apk_db_gzip_part, &ctx);
if (apk_parse_tar(tar, apk_db_install_archive_entry, &ctx) != 0)
goto err_close;
bs->close(bs, &length);
tar->close(tar);
/* Check the package checksum */
if (apk_checksum_compare(&ctx.data_csum, &newpkg->csum) != 0)
@ -1444,14 +1443,10 @@ static int apk_db_unpack_pkg(struct apk_database *db,
newpkg->name->name, newpkg->version);
if (need_copy) {
if (length == newpkg->size) {
char file2[256];
apk_db_cache_get_name(file2, sizeof(file2), db,
&newpkg->csum, pkgname, FALSE);
rename(file, file2);
} else {
unlink(file);
}
char file2[256];
apk_db_cache_get_name(file2, sizeof(file2), db,
&newpkg->csum, pkgname, FALSE);
rename(file, file2);
}
return 0;

View File

@ -22,7 +22,6 @@ struct apk_gzip_istream {
struct apk_bstream *bs;
z_stream zs;
int z_err;
int autoclose;
EVP_MD_CTX mdctx;
void *mdblock;
@ -104,12 +103,11 @@ static void gz_close(void *stream)
if (gis->cb != NULL)
EVP_MD_CTX_cleanup(&gis->mdctx);
inflateEnd(&gis->zs);
if (gis->autoclose)
gis->bs->close(gis->bs, NULL);
gis->bs->close(gis->bs, NULL);
free(gis);
}
struct apk_istream *apk_bstream_gunzip_mpart(struct apk_bstream *bs, int autoclose,
struct apk_istream *apk_bstream_gunzip_mpart(struct apk_bstream *bs,
apk_multipart_cb cb, void *ctx)
{
struct apk_gzip_istream *gis;
@ -126,7 +124,6 @@ struct apk_istream *apk_bstream_gunzip_mpart(struct apk_bstream *bs, int autoclo
.is.close = gz_close,
.bs = bs,
.z_err = 0,
.autoclose = autoclose,
.cb = cb,
.cbctx = ctx,
};

View File

@ -48,7 +48,7 @@ static int index_read_file(struct apk_database *db, struct index_ctx *ictx)
if (apk_file_get_info(ictx->index, APK_CHECKSUM_NONE, &fi) < 0)
return -1;
ictx->index_mtime = fi.mtime;
bs = apk_bstream_from_istream(apk_bstream_gunzip(apk_bstream_from_url(ictx->index), 1));
bs = apk_bstream_from_istream(apk_bstream_gunzip(apk_bstream_from_url(ictx->index)));
if (bs == NULL)
return -1;
r = apk_db_index_read(db, bs, 0);

View File

@ -479,7 +479,7 @@ int apk_file_get_info(const char *filename, int checksum, struct apk_file_info *
struct apk_istream *apk_istream_from_file_gz(const char *file)
{
return apk_bstream_gunzip(apk_bstream_from_file(file), TRUE);
return apk_bstream_gunzip(apk_bstream_from_file(file));
}
struct apk_fd_ostream {

View File

@ -430,12 +430,15 @@ static int apk_pkg_gzip_part(void *ctx, EVP_MD_CTX *mdctx, int part)
struct apk_package *apk_pkg_read(struct apk_database *db, const char *file)
{
struct read_info_ctx ctx;
struct apk_file_info fi;
struct apk_bstream *bs;
struct apk_istream *tar;
char realfile[PATH_MAX];
if (realpath(file, realfile) < 0)
return NULL;
if (apk_file_get_info(realfile, APK_CHECKSUM_NONE, &fi) < 0)
return NULL;
ctx.pkg = apk_pkg_new();
if (ctx.pkg == NULL)
@ -447,14 +450,15 @@ struct apk_package *apk_pkg_read(struct apk_database *db, const char *file)
ctx.db = db;
ctx.has_install = 0;
ctx.pkg->size = fi.size;
tar = apk_bstream_gunzip_mpart(bs, FALSE, apk_pkg_gzip_part, &ctx);
tar = apk_bstream_gunzip_mpart(bs, apk_pkg_gzip_part, &ctx);
if (apk_parse_tar(tar, read_info_entry, &ctx) < 0) {
apk_error("File %s is not an APK archive", file);
bs->close(bs, NULL);
goto err;
}
bs->close(bs, &ctx.pkg->size);
tar->close(tar);
if (ctx.pkg->name == NULL) {
apk_error("File %s is corrupted", file);

View File

@ -4,7 +4,7 @@
* Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation. See http://www.gnu.org/ for details.
*/
@ -68,7 +68,7 @@ struct apk_istream *apk_istream_from_url(const char *url)
struct apk_istream *apk_istream_from_url_gz(const char *file)
{
return apk_bstream_gunzip(apk_bstream_from_url(file), TRUE);
return apk_bstream_gunzip(apk_bstream_from_url(file));
}
struct apk_bstream *apk_bstream_from_url(const char *url)