input_stream: return errors with GError

This commit is contained in:
Max Kellermann
2009-11-14 23:53:04 +01:00
parent d000d31355
commit 228b03edf8
36 changed files with 422 additions and 175 deletions

View File

@@ -51,11 +51,19 @@ typedef struct {
static const struct input_plugin bz2_inputplugin;
static inline GQuark
bz2_quark(void)
{
return g_quark_from_static_string("bz2");
}
/* single archive handling allocation helpers */
static bool
bz2_alloc(bz2_context *data)
bz2_alloc(bz2_context *data, GError **error_r)
{
int ret;
data->bzstream.bzalloc = NULL;
data->bzstream.bzfree = NULL;
data->bzstream.opaque = NULL;
@@ -64,9 +72,13 @@ bz2_alloc(bz2_context *data)
data->bzstream.next_in = (void *) data->buffer;
data->bzstream.avail_in = 0;
if (BZ2_bzDecompressInit(&data->bzstream, 0, 0) != BZ_OK) {
ret = BZ2_bzDecompressInit(&data->bzstream, 0, 0);
if (ret != BZ_OK) {
g_free(data->buffer);
g_free(data);
g_set_error(error_r, bz2_quark(), ret,
"BZ2_bzDecompressInit() has failed");
return false;
}
@@ -92,7 +104,7 @@ bz2_open(char *pathname)
context = g_malloc(sizeof(*context));
//open archive
if (!input_stream_open(&context->istream, pathname)) {
if (!input_stream_open(&context->istream, pathname, NULL)) {
g_warning("failed to open an bzip2 archive %s\n",pathname);
g_free(context);
return NULL;
@@ -153,7 +165,7 @@ bz2_close(struct archive_file *file)
static bool
bz2_open_stream(struct archive_file *file, struct input_stream *is,
G_GNUC_UNUSED const char *path)
G_GNUC_UNUSED const char *path, GError **error_r)
{
bz2_context *context = (bz2_context *) file;
@@ -163,10 +175,8 @@ bz2_open_stream(struct archive_file *file, struct input_stream *is,
is->data = context;
is->seekable = false;
if (!bz2_alloc(context)) {
g_warning("alloc bz2 failed\n");
if (!bz2_alloc(context, error_r))
return false;
}
context->eof = false;
@@ -184,7 +194,7 @@ bz2_is_close(struct input_stream *is)
}
static bool
bz2_fillbuffer(bz2_context *context)
bz2_fillbuffer(bz2_context *context, GError **error_r)
{
size_t count;
bz_stream *bzstream;
@@ -195,7 +205,8 @@ bz2_fillbuffer(bz2_context *context)
return true;
count = input_stream_read(&context->istream,
context->buffer, BZ_BUFSIZE);
context->buffer, BZ_BUFSIZE,
error_r);
if (count == 0)
return false;
@@ -205,7 +216,8 @@ bz2_fillbuffer(bz2_context *context)
}
static size_t
bz2_is_read(struct input_stream *is, void *ptr, size_t length)
bz2_is_read(struct input_stream *is, void *ptr, size_t length,
GError **error_r)
{
bz2_context *context = (bz2_context *) is->data;
bz_stream *bzstream;
@@ -220,10 +232,8 @@ bz2_is_read(struct input_stream *is, void *ptr, size_t length)
bzstream->avail_out = length;
do {
if (!bz2_fillbuffer(context)) {
is->error = -1;
if (!bz2_fillbuffer(context, error_r))
return 0;
}
bz_result = BZ2_bzDecompress(bzstream);
@@ -233,7 +243,8 @@ bz2_is_read(struct input_stream *is, void *ptr, size_t length)
}
if (bz_result != BZ_OK) {
is->error = bz_result;
g_set_error(error_r, bz2_quark(), bz_result,
"BZ2_bzDecompress() has failed");
return 0;
}
} while (bzstream->avail_out == length);

View File

@@ -44,6 +44,12 @@ typedef struct {
static const struct input_plugin iso_inputplugin;
static inline GQuark
iso9660_quark(void)
{
return g_quark_from_static_string("iso9660");
}
/* archive open && listing routine */
static void
@@ -141,7 +147,7 @@ iso_close(struct archive_file *file)
static bool
iso_open_stream(struct archive_file *file, struct input_stream *is,
const char *pathname)
const char *pathname, GError **error_r)
{
iso_context *context = (iso_context *) file;
//setup file ops
@@ -154,7 +160,8 @@ iso_open_stream(struct archive_file *file, struct input_stream *is,
context->statbuf = iso9660_ifs_stat_translate (context->iso, pathname);
if (context->statbuf == NULL) {
g_warning("file %s not found in iso\n", pathname);
g_set_error(error_r, iso9660_quark(), 0,
"not found in the ISO file: %s", pathname);
return false;
}
context->cur_ofs = 0;
@@ -173,7 +180,7 @@ iso_is_close(struct input_stream *is)
static size_t
iso_is_read(struct input_stream *is, void *ptr, size_t size)
iso_is_read(struct input_stream *is, void *ptr, size_t size, GError **error_r)
{
iso_context *context = (iso_context *) is->data;
int toread, readed = 0;
@@ -197,9 +204,10 @@ iso_is_read(struct input_stream *is, void *ptr, size_t size)
context->statbuf->lsn + cur_block, no_blocks);
if (readed != no_blocks * ISO_BLOCKSIZE) {
g_warning("error reading ISO file at lsn %lu\n",
(long unsigned int) cur_block );
return -1;
g_set_error(error_r, iso9660_quark(), 0,
"error reading ISO file at lsn %lu",
(long unsigned int) cur_block);
return 0;
}
if (left_bytes < size) {
readed = left_bytes;

View File

@@ -40,6 +40,12 @@ struct zzip_archive {
static const struct input_plugin zzip_input_plugin;
static inline GQuark
zzip_quark(void)
{
return g_quark_from_static_string("zzip");
}
/* archive open && listing routine */
static struct archive_file *
@@ -108,7 +114,7 @@ zzip_archive_close(struct archive_file *file)
static bool
zzip_archive_open_stream(struct archive_file *file, struct input_stream *is,
const char *pathname)
const char *pathname, GError **error_r)
{
struct zzip_archive *context = (struct zzip_archive *) file;
ZZIP_STAT z_stat;
@@ -122,7 +128,8 @@ zzip_archive_open_stream(struct archive_file *file, struct input_stream *is,
context->file = zzip_file_open(context->dir, pathname, 0);
if (!context->file) {
g_warning("file %s not found in the zipfile\n", pathname);
g_set_error(error_r, zzip_quark(), 0,
"not found in the ZIP file: %s", pathname);
return false;
}
zzip_file_stat(context->file, &z_stat);
@@ -140,13 +147,15 @@ zzip_input_close(struct input_stream *is)
}
static size_t
zzip_input_read(struct input_stream *is, void *ptr, size_t size)
zzip_input_read(struct input_stream *is, void *ptr, size_t size,
GError **error_r)
{
struct zzip_archive *context = (struct zzip_archive *) is->data;
int ret;
ret = zzip_file_read(context->file, ptr, size);
if (ret < 0) {
g_warning("error %d reading zipfile\n", ret);
g_set_error(error_r, zzip_quark(), ret,
"zzip_file_read() has failed");
return 0;
}
return ret;
@@ -160,12 +169,14 @@ zzip_input_eof(struct input_stream *is)
}
static bool
zzip_input_seek(G_GNUC_UNUSED struct input_stream *is,
G_GNUC_UNUSED goffset offset, G_GNUC_UNUSED int whence)
zzip_input_seek(struct input_stream *is,
goffset offset, int whence, GError **error_r)
{
struct zzip_archive *context = (struct zzip_archive *) is->data;
zzip_off_t ofs = zzip_seek(context->file, offset, whence);
if (ofs != -1) {
g_set_error(error_r, zzip_quark(), ofs,
"zzip_seek() has failed");
is->offset = ofs;
return true;
}