Introduce ALPM_BUFFER_SIZE constant

This takes the place of three previously used constants:
ARCHIVE_DEFAULT_BYTES_PER_BLOCK, BUFFER_SIZE, and CPBUFSIZE.

In libarchive 3.0, the first constant will be no more, so we can ensure
we are forward-compatible by removing our usage of it now. The rest are
unified for consistency.

By default, we will use the value of BUFSIZ provided by <stdio.h>, which
is 8192 on Linux. If that is undefined, a default value is provided.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2011-10-27 14:59:24 -05:00
parent 1052709921
commit 3343185473
5 changed files with 25 additions and 19 deletions

View file

@ -456,6 +456,7 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
alpm_pkg_t *oldpkg = NULL; alpm_pkg_t *oldpkg = NULL;
alpm_db_t *db = handle->db_local; alpm_db_t *db = handle->db_local;
alpm_trans_t *trans = handle->trans; alpm_trans_t *trans = handle->trans;
const char *pkgfile;
ASSERT(trans != NULL, return -1); ASSERT(trans != NULL, return -1);
@ -479,13 +480,15 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
EVENT(handle, ALPM_EVENT_ADD_START, newpkg, NULL); EVENT(handle, ALPM_EVENT_ADD_START, newpkg, NULL);
} }
pkgfile = newpkg->origin_data.file;
_alpm_log(handle, ALPM_LOG_DEBUG, "%s package %s-%s\n", _alpm_log(handle, ALPM_LOG_DEBUG, "%s package %s-%s\n",
is_upgrade ? "upgrading" : "adding", newpkg->name, newpkg->version); is_upgrade ? "upgrading" : "adding", newpkg->name, newpkg->version);
/* pre_install/pre_upgrade scriptlet */ /* pre_install/pre_upgrade scriptlet */
if(alpm_pkg_has_scriptlet(newpkg) && if(alpm_pkg_has_scriptlet(newpkg) &&
!(trans->flags & ALPM_TRANS_FLAG_NOSCRIPTLET)) { !(trans->flags & ALPM_TRANS_FLAG_NOSCRIPTLET)) {
const char *scriptlet_name = is_upgrade ? "pre_upgrade" : "pre_install"; const char *scriptlet_name = is_upgrade ? "pre_upgrade" : "pre_install";
_alpm_runscriptlet(handle, newpkg->origin_data.file, _alpm_runscriptlet(handle, pkgfile,
scriptlet_name, newpkg->version, NULL, 1); scriptlet_name, newpkg->version, NULL, 1);
} }
@ -531,9 +534,9 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
archive_read_support_compression_all(archive); archive_read_support_compression_all(archive);
archive_read_support_format_all(archive); archive_read_support_format_all(archive);
_alpm_log(handle, ALPM_LOG_DEBUG, "archive: %s\n", newpkg->origin_data.file); _alpm_log(handle, ALPM_LOG_DEBUG, "archive: %s\n", pkgfile);
if(archive_read_open_filename(archive, newpkg->origin_data.file, if(archive_read_open_filename(archive, pkgfile,
ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) { ALPM_BUFFER_SIZE) != ARCHIVE_OK) {
handle->pm_errno = ALPM_ERR_PKG_OPEN; handle->pm_errno = ALPM_ERR_PKG_OPEN;
ret = -1; ret = -1;
goto cleanup; goto cleanup;

View file

@ -59,7 +59,7 @@ static void *_package_changelog_open(alpm_pkg_t *pkg)
archive_read_support_format_all(archive); archive_read_support_format_all(archive);
if(archive_read_open_filename(archive, pkgfile, if(archive_read_open_filename(archive, pkgfile,
ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) { ALPM_BUFFER_SIZE) != ARCHIVE_OK) {
RET_ERR(pkg->handle, ALPM_ERR_PKG_OPEN, NULL); RET_ERR(pkg->handle, ALPM_ERR_PKG_OPEN, NULL);
} }
@ -390,7 +390,7 @@ alpm_pkg_t *_alpm_pkg_load_internal(alpm_handle_t *handle,
archive_read_support_format_all(archive); archive_read_support_format_all(archive);
if(archive_read_open_filename(archive, pkgfile, if(archive_read_open_filename(archive, pkgfile,
ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) { ALPM_BUFFER_SIZE) != ARCHIVE_OK) {
alpm_pkg_free(newpkg); alpm_pkg_free(newpkg);
RET_ERR(handle, ALPM_ERR_PKG_OPEN, NULL); RET_ERR(handle, ALPM_ERR_PKG_OPEN, NULL);
} }

View file

@ -440,7 +440,7 @@ static int sync_db_populate(alpm_db_t *db)
_alpm_log(db->handle, ALPM_LOG_DEBUG, "opening database archive %s\n", dbpath); _alpm_log(db->handle, ALPM_LOG_DEBUG, "opening database archive %s\n", dbpath);
if(archive_read_open_filename(archive, dbpath, if(archive_read_open_filename(archive, dbpath,
ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) { ALPM_BUFFER_SIZE) != ARCHIVE_OK) {
_alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), dbpath, _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), dbpath,
archive_error_string(archive)); archive_error_string(archive));
archive_read_finish(archive); archive_read_finish(archive);

View file

@ -129,8 +129,6 @@ int _alpm_makepath_mode(const char *path, mode_t mode)
return ret; return ret;
} }
#define CPBUFSIZE 8 * 1024
int _alpm_copyfile(const char *src, const char *dest) int _alpm_copyfile(const char *src, const char *dest)
{ {
FILE *in, *out; FILE *in, *out;
@ -148,10 +146,10 @@ int _alpm_copyfile(const char *src, const char *dest)
return 1; return 1;
} }
CALLOC(buf, (size_t)CPBUFSIZE, (size_t)1, ret = 1; goto cleanup;); MALLOC(buf, (size_t)ALPM_BUFFER_SIZE, ret = 1; goto cleanup);
/* do the actual file copy */ /* do the actual file copy */
while((len = fread(buf, 1, CPBUFSIZE, in))) { while((len = fread(buf, 1, ALPM_BUFFER_SIZE, in))) {
size_t nwritten = 0; size_t nwritten = 0;
nwritten = fwrite(buf, 1, len, out); nwritten = fwrite(buf, 1, len, out);
if((nwritten != len) || ferror(out)) { if((nwritten != len) || ferror(out)) {
@ -174,7 +172,7 @@ int _alpm_copyfile(const char *src, const char *dest)
cleanup: cleanup:
fclose(in); fclose(in);
fclose(out); fclose(out);
FREE(buf); free(buf);
return ret; return ret;
} }
@ -288,7 +286,7 @@ int _alpm_unpack(alpm_handle_t *handle, const char *archive, const char *prefix,
archive_read_support_format_all(_archive); archive_read_support_format_all(_archive);
if(archive_read_open_filename(_archive, archive, if(archive_read_open_filename(_archive, archive,
ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) { ALPM_BUFFER_SIZE) != ARCHIVE_OK) {
_alpm_log(handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), archive, _alpm_log(handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), archive,
archive_error_string(_archive)); archive_error_string(_archive));
RET_ERR(handle, ALPM_ERR_PKG_OPEN, 1); RET_ERR(handle, ALPM_ERR_PKG_OPEN, 1);
@ -741,8 +739,6 @@ int _alpm_lstat(const char *path, struct stat *buf)
} }
#ifdef HAVE_LIBSSL #ifdef HAVE_LIBSSL
#define BUFFER_SIZE 8192
static int md5_file(const char *path, unsigned char output[16]) static int md5_file(const char *path, unsigned char output[16])
{ {
FILE *f; FILE *f;
@ -750,7 +746,7 @@ static int md5_file(const char *path, unsigned char output[16])
MD5_CTX ctx; MD5_CTX ctx;
unsigned char *buf; unsigned char *buf;
CALLOC(buf, BUFFER_SIZE, sizeof(unsigned char), return 1); CALLOC(buf, ALPM_BUFFER_SIZE, sizeof(unsigned char), return 1);
if((f = fopen(path, "rb")) == NULL) { if((f = fopen(path, "rb")) == NULL) {
free(buf); free(buf);
@ -759,7 +755,7 @@ static int md5_file(const char *path, unsigned char output[16])
MD5_Init(&ctx); MD5_Init(&ctx);
while((n = fread(buf, 1, BUFFER_SIZE, f)) > 0) { while((n = fread(buf, 1, ALPM_BUFFER_SIZE, f)) > 0) {
MD5_Update(&ctx, buf, n); MD5_Update(&ctx, buf, n);
} }
@ -785,7 +781,7 @@ static int sha2_file(const char *path, unsigned char output[32], int is224)
SHA256_CTX ctx; SHA256_CTX ctx;
unsigned char *buf; unsigned char *buf;
CALLOC(buf, BUFFER_SIZE, sizeof(unsigned char), return 1); CALLOC(buf, ALPM_BUFFER_SIZE, sizeof(unsigned char), return 1);
if((f = fopen(path, "rb")) == NULL) { if((f = fopen(path, "rb")) == NULL) {
free(buf); free(buf);
@ -798,7 +794,7 @@ static int sha2_file(const char *path, unsigned char output[32], int is224)
SHA256_Init(&ctx); SHA256_Init(&ctx);
} }
while((n = fread(buf, 1, BUFFER_SIZE, f)) > 0) { while((n = fread(buf, 1, ALPM_BUFFER_SIZE, f)) > 0) {
if(is224) { if(is224) {
SHA224_Update(&ctx, buf, n); SHA224_Update(&ctx, buf, n);
} else { } else {

View file

@ -75,6 +75,13 @@
#define CHECK_HANDLE(handle, action) do { if(!(handle)) { action; } (handle)->pm_errno = 0; } while(0) #define CHECK_HANDLE(handle, action) do { if(!(handle)) { action; } (handle)->pm_errno = 0; } while(0)
/** Standard buffer size used throughout the library. */
#ifdef BUFSIZ
#define ALPM_BUFFER_SIZE BUFSIZ
#else
#define ALPM_BUFFER_SIZE 8192
#endif
/** /**
* Used as a buffer/state holder for _alpm_archive_fgets(). * Used as a buffer/state holder for _alpm_archive_fgets().
*/ */