Changeset: c1eb4709e8fb for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=c1eb4709e8fb
Modified Files:
        clients/examples/C/streamcat.c
        common/stream/bs.c
        common/stream/bs2.c
        common/stream/bz2_stream.c
        common/stream/callback.c
        common/stream/fwf.c
        common/stream/gz_stream.c
        common/stream/iconv_stream.c
        common/stream/lz4_stream.c
        common/stream/memio.c
        common/stream/socket_stream.c
        common/stream/stdio_stream.c
        common/stream/stream.c
        common/stream/stream_internal.h
        common/stream/text_stream.c
        common/stream/url_stream.c
        common/stream/xz_stream.c
Branch: makelibstreamgreatagain
Log Message:

Keep track of cause of failed attempts to open a stream (NOT FOR WINDOWS YET)


diffs (truncated from 674 to 300 lines):

diff --git a/clients/examples/C/streamcat.c b/clients/examples/C/streamcat.c
--- a/clients/examples/C/streamcat.c
+++ b/clients/examples/C/streamcat.c
@@ -80,6 +80,8 @@ croak(int status, const char *msg, ...)
 int
 main(int argc, char *argv[])
 {
+       mnstr_init();
+
        if (argc < 2)
                croak(1, NULL);
 
@@ -136,8 +138,10 @@ int cmd_read(char *argv[])
                croak(1, "Unknown opener '%s'", opener_name);
 
        s = opener(filename);
-       if (s == NULL)
-               croak(2, "Opener %s did not return a stream", opener_name);
+       if (s == NULL) {
+               char *msg = mnstr_error(NULL);
+               croak(2, "Opener %s failed: %s", opener_name, msg ? msg : "");
+       }
 
        for (; *arg != NULL; arg++) {
                if (arg[0][0] == '-')
@@ -231,8 +235,10 @@ int cmd_write(char *argv[])
                croak(1, "Unknown opener '%s'", opener_name);
 
        s = opener(filename);
-       if (s == NULL)
-               croak(2, "Opener %s did not return a stream", opener_name);
+       if (s == NULL) {
+               char *msg = mnstr_error(NULL);
+               croak(2, "Opener %s failed: %s", opener_name, msg ? msg : "");
+       }
 
        for (; *arg != NULL; arg++) {
                if (arg[0][0] == '-')
@@ -383,8 +389,6 @@ static stream *
 opener_wstream(char *filename)
 {
        stream *s = open_wstream(filename);
-       if (s == NULL)
-               croak(2, "Error opening file '%s': %s", filename, 
strerror(errno));
        return s;
 }
 
@@ -393,8 +397,6 @@ static stream *
 opener_wastream(char *filename)
 {
        stream *s = open_wastream(filename);
-       if (s == NULL)
-               croak(2, "Error opening file '%s': %s", filename, 
strerror(errno));
        return s;
 }
 
diff --git a/common/stream/bs.c b/common/stream/bs.c
--- a/common/stream/bs.c
+++ b/common/stream/bs.c
@@ -348,6 +348,7 @@ block_stream(stream *s)
                return NULL;
        if ((b = bs_create()) == NULL) {
                destroy_stream(ns);
+               mnstr_set_open_error(s->name, 0, "bs_create failed");
                return NULL;
        }
        /* blocksizes have a fixed little endian byteorder */
diff --git a/common/stream/bs2.c b/common/stream/bs2.c
--- a/common/stream/bs2.c
+++ b/common/stream/bs2.c
@@ -633,6 +633,7 @@ block_stream2(stream *s, size_t bufsiz, 
                return NULL;
        if ((b = bs2_create(s, bufsiz, comp)) == NULL) {
                destroy_stream(ns);
+               mnstr_set_open_error(s->name, 0, "bs2_create failed");
                return NULL;
        }
        /* blocksizes have a fixed little endian byteorder */
diff --git a/common/stream/bz2_stream.c b/common/stream/bz2_stream.c
--- a/common/stream/bz2_stream.c
+++ b/common/stream/bz2_stream.c
@@ -156,9 +156,16 @@ bz2_stream(stream *inner, int level)
                ret = BZ2_bzCompressInit(&bz->strm, level, 0, 0);
        }
 
+       if (ret != BZ_OK) {
+               free(bz);
+               free(state);
+               mnstr_set_open_error(inner->name, 0, "failed to initialize bz2: 
code %d", ret);
+               return NULL;
+       }
+
        stream *s = pump_stream(inner, state);
 
-       if (ret != BZ_OK || s == NULL) {
+       if (s == NULL) {
                bz->end(&bz->strm);
                free(bz);
                free(state);
@@ -236,25 +243,31 @@ bz2_stream(stream *inner, int preset)
 {
        (void) inner;
        (void) preset;
+       mnstr_set_open_error(url, 0, "BZIP2 support has been left out of this 
MonetDB");
        return NULL;
 }
+
 stream *open_bzrstream(const char *filename)
 {
+       mnstr_set_open_error(url, 0, "BZIP2 support has been left out of this 
MonetDB");
        return NULL;
 }
 
 stream *open_bzwstream(const char *filename, const char *mode)
 {
+       mnstr_set_open_error(url, 0, "BZIP2 support has been left out of this 
MonetDB");
        return NULL;
 }
 
 stream *open_bzrastream(const char *filename)
 {
+       mnstr_set_open_error(url, 0, "BZIP2 support has been left out of this 
MonetDB");
        return NULL;
 }
 
 stream *open_bzwastream(const char *filename, const char *mode)
 {
+       mnstr_set_open_error(url, 0, "BZIP2 support has been left out of this 
MonetDB");
        return NULL;
 }
 
diff --git a/common/stream/callback.c b/common/stream/callback.c
--- a/common/stream/callback.c
+++ b/common/stream/callback.c
@@ -70,6 +70,7 @@ callback_stream(void *restrict private,
        cb = malloc(sizeof(struct cbstream));
        if (cb == NULL) {
                destroy_stream(s);
+               mnstr_set_open_error(name, errno, NULL);
                return NULL;
        }
        *cb = (struct cbstream) {
diff --git a/common/stream/fwf.c b/common/stream/fwf.c
--- a/common/stream/fwf.c
+++ b/common/stream/fwf.c
@@ -132,6 +132,7 @@ stream_fwf_create(stream *restrict s, si
        stream_fwf_data *fsd = malloc(sizeof(stream_fwf_data));
 
        if (fsd == NULL) {
+               mnstr_set_open_error(STREAM_FWF_NAME, errno, NULL);
                return NULL;
        }
        *fsd = (stream_fwf_data) {
@@ -149,6 +150,7 @@ stream_fwf_create(stream *restrict s, si
        if (fsd->in_buf == NULL) {
                close_stream(fsd->s);
                free(fsd);
+               mnstr_set_open_error(STREAM_FWF_NAME, errno, NULL);
                return NULL;
        }
        fsd->out_buf = malloc(fsd->line_len * 3);
@@ -156,6 +158,7 @@ stream_fwf_create(stream *restrict s, si
                close_stream(fsd->s);
                free(fsd->in_buf);
                free(fsd);
+               mnstr_set_open_error(STREAM_FWF_NAME, errno, NULL);
                return NULL;
        }
        if ((ns = create_stream(STREAM_FWF_NAME)) == NULL) {
@@ -163,6 +166,7 @@ stream_fwf_create(stream *restrict s, si
                free(fsd->in_buf);
                free(fsd->out_buf);
                free(fsd);
+               mnstr_set_open_error(STREAM_FWF_NAME, errno, NULL);
                return NULL;
        }
        ns->read = stream_fwf_read;
diff --git a/common/stream/gz_stream.c b/common/stream/gz_stream.c
--- a/common/stream/gz_stream.c
+++ b/common/stream/gz_stream.c
@@ -143,9 +143,16 @@ gz_stream(stream *inner, int level)
                ret = deflateInit2(&gz->strm, level, Z_DEFLATED, 15 | 16, 8, 
Z_DEFAULT_STRATEGY);
        }
 
+       if (ret != LZMA_OK) {
+               free(gz);
+               free(state);
+               mnstr_set_open_error(inner->name, 0, "failed to initialize gz 
stream: code %d", (int)ret);
+               return NULL;
+       }
+
        stream *s = pump_stream(inner, state);
 
-       if (ret != LZMA_OK || s == NULL) {
+       if (s == NULL) {
                gz->indeflateEnd(&gz->strm);
                free(gz);
                free(state);
@@ -223,25 +230,30 @@ gz_stream(stream *inner, int preset)
 {
        (void) inner;
        (void) preset;
+       mnstr_set_open_error(url, 0, "GZ support has been left out of this 
MonetDB");
        return NULL;
 }
 stream *open_gzrstream(const char *filename)
 {
+       mnstr_set_open_error(url, 0, "GZ support has been left out of this 
MonetDB");
        return NULL;
 }
 
 stream *open_gzwstream(const char *filename, const char *mode)
 {
+       mnstr_set_open_error(url, 0, "GZ support has been left out of this 
MonetDB");
        return NULL;
 }
 
 stream *open_gzrastream(const char *filename)
 {
+       mnstr_set_open_error(url, 0, "GZ support has been left out of this 
MonetDB");
        return NULL;
 }
 
 stream *open_gzwastream(const char *filename, const char *mode)
 {
+       mnstr_set_open_error(url, 0, "GZ support has been left out of this 
MonetDB");
        return NULL;
 }
 
diff --git a/common/stream/iconv_stream.c b/common/stream/iconv_stream.c
--- a/common/stream/iconv_stream.c
+++ b/common/stream/iconv_stream.c
@@ -264,6 +264,7 @@ ic_open(iconv_t cd, stream *restrict ss,
        ic = malloc(sizeof(struct icstream));
        if (ic == NULL) {
                mnstr_destroy(s);
+               mnstr_set_open_error(s->name, errno, NULL);
                return NULL;
        }
        s->stream_data.p = ic;
@@ -338,6 +339,7 @@ iconv_rstream(stream *restrict ss, const
            strcmp(charset, "UTF8") == 0)
                return ss;
 
+       mnstr_set_open_error(url, 0, "ICONV support has been left out of this 
MonetDB");
        return NULL;
 }
 
@@ -352,6 +354,7 @@ iconv_wstream(stream *restrict ss, const
            strcmp(charset, "UTF8") == 0)
                return ss;
 
+       mnstr_set_open_error(url, 0, "ICONV support has been left out of this 
MonetDB");
        return NULL;
 }
 #endif /* HAVE_ICONV */
diff --git a/common/stream/lz4_stream.c b/common/stream/lz4_stream.c
--- a/common/stream/lz4_stream.c
+++ b/common/stream/lz4_stream.c
@@ -187,6 +187,7 @@ setup_decompression(stream *inner, pump_
                &inner_state->ctx.d, LZ4F_VERSION);
        if (LZ4F_isError(ret)) {
                free(buf);
+               mnstr_set_open_error(inner->name, 0, "failed to initialize lz4: 
%s", LZ4F_getErrorName(ret));
                return NULL;
        }
 
@@ -245,6 +246,7 @@ setup_compression(stream *inner, pump_st
        if (LZ4F_isError(nwritten)) {
                LZ4F_freeCompressionContext(inner_state->ctx.c);
                free(buffer);
+               mnstr_set_open_error(inner->name, 0, "failed to initialize lz4: 
%s", LZ4F_getErrorName(ret));
                return NULL;
        }
        inner_state->dst_win.start += nwritten;
@@ -363,11 +365,13 @@ lz4_stream(stream *inner, int preset)
 {
        (void) inner;
        (void) preset;
+       mnstr_set_open_error(url, 0, "LZ4 support has been left out of this 
MonetDB");
        return NULL;
 }
 stream *open_lz4rstream(const char *filename)
 {
        (void) filename;
+       mnstr_set_open_error(url, 0, "LZ4 support has been left out of this 
MonetDB");
        return NULL;
 }
 
@@ -375,12 +379,14 @@ stream *open_lz4wstream(const char *file
 {
        (void) filename;
        (void) mode;
+       mnstr_set_open_error(url, 0, "LZ4 support has been left out of this 
MonetDB");
        return NULL;
 }
 
 stream *open_lz4rastream(const char *filename)
 {
        (void) filename;
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to