Changeset: a98408e8ca23 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=a98408e8ca23 Modified Files: common/stream/stream.c common/stream/stream.h common/stream/stream_internal.h sql/backends/monet5/sql_result.c Branch: makelibstreamgreatagain Log Message:
Replace error method by error buffer diffs (191 lines): diff --git a/common/stream/stream.c b/common/stream/stream.c --- a/common/stream/stream.c +++ b/common/stream/stream.c @@ -203,12 +203,42 @@ mnstr_destroy(stream *s) char * mnstr_error(const stream *s) { + char buf[128]; + if (s == NULL) return "Connection terminated"; - return s->error(s); + + if (s->errnr == MNSTR_NO__ERROR) + return strdup("no error"); + + if (s->errmsg != NULL) + return strdup(s->errmsg); + + switch (s->errnr) { + case MNSTR_NO__ERROR: + /* unreachable */ + assert(0); + return NULL; + case MNSTR_OPEN_ERROR: + snprintf(buf, sizeof(buf), "error could not open file %.100s\n", + s->name); + return strdup(buf); + case MNSTR_READ_ERROR: + snprintf(buf, sizeof(buf), "error reading file %.100s\n", + s->name); + return strdup(buf); + case MNSTR_WRITE_ERROR: + snprintf(buf, sizeof(buf), "error writing file %.100s\n", + s->name); + return strdup(buf); + case MNSTR_TIMEOUT: + snprintf(buf, sizeof(buf), "timeout on %.100s\n", s->name); + return strdup(buf); + } + snprintf(buf, sizeof(buf), "Unknown error %d\n", (int)s->errnr); + return strdup(buf); } - /* flush buffer, return 0 on success, non-zero on failure */ int mnstr_flush(stream *s) @@ -308,12 +338,31 @@ mnstr_errnr(const stream *s) return s->errnr; } +const char * +mnstr_error_kind_name(mnstr_error_kind k) +{ + switch (k) { + case MNSTR_NO__ERROR: + return "MNSTR_NO__ERROR"; + case MNSTR_OPEN_ERROR: + return "MNSTR_OPEN_ERROR"; + case MNSTR_READ_ERROR: + return "MNSTR_READ_ERROR"; + case MNSTR_WRITE_ERROR: + return "MNSTR_WRITE_ERROR"; + case MNSTR_TIMEOUT: + return "MNSTR_TIMEOUT"; + default: + return "<UNKNOWN_ERROR>"; + } +} void mnstr_clearerr(stream *s) { if (s != NULL) { s->errnr = MNSTR_NO__ERROR; + s->errmsg[0] = '\0'; if (s->clrerr) s->clrerr(s); } @@ -381,34 +430,6 @@ destroy_stream(stream *s) } -static char * -error(const stream *s) -{ - char buf[128]; - - switch (s->errnr) { - case MNSTR_NO__ERROR: - return strdup("no error"); - case MNSTR_OPEN_ERROR: - snprintf(buf, sizeof(buf), "error could not open file %.100s\n", - s->name); - return strdup(buf); - case MNSTR_READ_ERROR: - snprintf(buf, sizeof(buf), "error reading file %.100s\n", - s->name); - return strdup(buf); - case MNSTR_WRITE_ERROR: - snprintf(buf, sizeof(buf), "error writing file %.100s\n", - s->name); - return strdup(buf); - case MNSTR_TIMEOUT: - snprintf(buf, sizeof(buf), "timeout on %.100s\n", s->name); - return strdup(buf); - } - return strdup("Unknown error"); -} - - stream * create_stream(const char *name) { @@ -425,7 +446,7 @@ create_stream(const char *name) .binary = false, .name = strdup(name), .errnr = MNSTR_NO__ERROR, - .error = error, + .errmsg = {0}, .destroy = destroy_stream, }; if(s->name == NULL) { @@ -468,13 +489,6 @@ wrapper_clrerr(stream *s) } -static char * -wrapper_error(const stream *s) -{ - return s->inner->error(s->inner); -} - - static void wrapper_destroy(stream *s) { @@ -550,7 +564,6 @@ create_wrapper_stream(const char *name, s->write = inner->write == NULL ? NULL : wrapper_write; s->close = inner->close == NULL ? NULL : wrapper_close; s->clrerr = inner->clrerr == NULL ? NULL : wrapper_clrerr; - s->error = inner->error == NULL ? NULL : wrapper_error; s->destroy = inner->destroy == NULL ? NULL : wrapper_destroy; s->flush = inner->flush == NULL ? NULL : wrapper_flush; s->fsync = inner->fsync == NULL ? NULL : wrapper_fsync; diff --git a/common/stream/stream.h b/common/stream/stream.h --- a/common/stream/stream.h +++ b/common/stream/stream.h @@ -96,6 +96,7 @@ typedef enum mnstr_error_kind { stream_export char *mnstr_error(const stream *s); stream_export mnstr_error_kind mnstr_errnr(const stream *s); +stream_export const char *mnstr_error_kind_name(mnstr_error_kind k); stream_export void mnstr_clearerr(stream *s); /* all mnstr_readX/mnstr_writeX return diff --git a/common/stream/stream_internal.h b/common/stream/stream_internal.h --- a/common/stream/stream_internal.h +++ b/common/stream/stream_internal.h @@ -158,7 +158,6 @@ struct stream { ssize_t (*write)(stream *restrict s, const void *restrict buf, size_t elmsize, size_t cnt); void (*close)(stream *s); void (*clrerr)(stream *s); - char *(*error)(const stream *s); void (*destroy)(stream *s); int (*flush)(stream *s); int (*fsync)(stream *s); @@ -166,6 +165,7 @@ struct stream { int (*fsetpos)(stream *restrict s, fpos_t *restrict p); void (*update_timeout)(stream *s); int (*isalive)(const stream *s); + char errmsg[1024]; // avoid allocation on error. We don't have THAT many streams.. }; /* This is used in the file opening functions, in a code sequence that is diff --git a/sql/backends/monet5/sql_result.c b/sql/backends/monet5/sql_result.c --- a/sql/backends/monet5/sql_result.c +++ b/sql/backends/monet5/sql_result.c @@ -860,7 +860,10 @@ mvc_import_table(Client cntxt, BAT ***ba return NULL; } if (mnstr_errnr(bs->s)) { - sql_error(m, 500, "stream not open %d", mnstr_errnr(bs->s)); + mnstr_error_kind errnr = mnstr_errnr(bs->s); + char *msg = mnstr_error(bs->s); + sql_error(m, 500, "stream not open %s: %s", mnstr_error_kind_name(errnr), msg ? msg : "unknown error"); + free(msg); return NULL; } if (offset < 0 || offset > (lng) BUN_MAX) { _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list