Changeset: fdb40ac745a4 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/fdb40ac745a4
Modified Files:
        sql/server/rel_select.c
Branch: literal_features
Log Message:

merge with default


diffs (truncated from 1275 to 300 lines):

diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -1728,7 +1728,6 @@ int mnstr_writeShtArray(stream *restrict
 int mnstr_writeStr(stream *restrict s, const char *restrict val);
 stream *open_rastream(const char *filename);
 stream *open_rstream(const char *filename);
-stream *open_urlstream(const char *url);
 stream *open_wastream(const char *filename);
 stream *open_wstream(const char *filename);
 stream *openssl_rstream(const char *hostname, BIO *bio);
diff --git a/clients/examples/C/CMakeLists.txt 
b/clients/examples/C/CMakeLists.txt
--- a/clients/examples/C/CMakeLists.txt
+++ b/clients/examples/C/CMakeLists.txt
@@ -46,7 +46,8 @@ add_executable(streamcat
 target_link_libraries(streamcat
   PRIVATE
   monetdb_config_header
-  stream)
+  stream
+  $<$<BOOL:${CURL_FOUND}>:CURL::libcurl>)
 
 add_executable(testcondvar
   testcondvar.c)
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
@@ -436,6 +436,75 @@ opener_rastream(char *filename)
        return s;
 }
 
+#ifdef HAVE_CURL
+#include <curl/curl.h>
+
+#ifndef CURL_WRITEFUNC_ERROR
+#define CURL_WRITEFUNC_ERROR 0
+#endif
+
+static size_t
+write_callback(char *buffer, size_t size, size_t nitems, void *userp)
+{
+       stream *s = userp;
+
+       /* size is expected to always be 1 */
+
+       ssize_t sz = mnstr_write(s, buffer, size, nitems);
+       if (sz < 0)
+               return CURL_WRITEFUNC_ERROR; /* indicate failure to library */
+       return (size_t) sz * size;
+}
+
+static stream *
+open_urlstream(const char *url)
+{
+       CURL *handle;
+       stream *s;
+       CURLcode ret;
+       char errbuf[CURL_ERROR_SIZE];
+
+       s = buffer_wastream(NULL, url);
+       if (s == NULL) {
+               return NULL;
+       }
+
+       if ((handle = curl_easy_init()) == NULL) {
+               mnstr_destroy(s);
+               return NULL;
+       }
+
+       errbuf[0] = 0;
+
+       if ((ret = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errbuf)) != 
CURLE_OK ||
+           (ret = curl_easy_setopt(handle, CURLOPT_URL, url)) != CURLE_OK ||
+           (ret = curl_easy_setopt(handle, CURLOPT_WRITEDATA, s)) != CURLE_OK 
||
+           (ret = curl_easy_setopt(handle, CURLOPT_VERBOSE, 0)) != CURLE_OK ||
+           (ret = curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1)) != CURLE_OK ||
+           (ret = curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1)) != 
CURLE_OK ||
+           (ret = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, 
write_callback)) != CURLE_OK ||
+           (ret = curl_easy_perform(handle)) != CURLE_OK) {
+               curl_easy_cleanup(handle);
+               mnstr_destroy(s);
+               if (errbuf[0])
+                       fprintf(stderr, "%s\n", errbuf);
+               else
+                       fprintf(stderr, "%s\n", curl_easy_strerror(ret));
+               return NULL;
+       }
+       curl_easy_cleanup(handle);
+       (void) mnstr_get_buffer(s);     /* switch to read-only */
+       return s;
+}
+#else
+static stream *
+open_urlstream(const char *url)
+{
+       (void) url;
+       return NULL;
+}
+#endif
+
 static stream *
 opener_urlstream(char *url)
 {
diff --git a/clients/mapiclient/CMakeLists.txt 
b/clients/mapiclient/CMakeLists.txt
--- a/clients/mapiclient/CMakeLists.txt
+++ b/clients/mapiclient/CMakeLists.txt
@@ -54,6 +54,7 @@ target_link_libraries(mclient
   mapi
   stream
   $<$<BOOL:${READLINE_FOUND}>:Readline::Readline>
+  $<$<BOOL:${CURL_FOUND}>:CURL::libcurl>
   $<$<BOOL:${Iconv_FOUND}>:Iconv::Iconv>
   $<$<PLATFORM_ID:Windows>:${GETOPT_LIB}>)
 
diff --git a/clients/mapiclient/mclient.c b/clients/mapiclient/mclient.c
--- a/clients/mapiclient/mclient.c
+++ b/clients/mapiclient/mclient.c
@@ -3072,10 +3072,74 @@ doFile(Mapi mid, stream *fp, bool useins
        return errseen;
 }
 
+#ifdef HAVE_CURL
+#include <curl/curl.h>
+
+#ifndef CURL_WRITEFUNC_ERROR
+#define CURL_WRITEFUNC_ERROR 0
+#endif
+
+static size_t
+write_callback(char *buffer, size_t size, size_t nitems, void *userp)
+{
+       stream *s = userp;
+
+       /* size is expected to always be 1 */
+
+       ssize_t sz = mnstr_write(s, buffer, size, nitems);
+       if (sz < 0)
+               return CURL_WRITEFUNC_ERROR; /* indicate failure to library */
+       return (size_t) sz * size;
+}
+
+static stream *
+open_urlstream(const char *url, char *errbuf)
+{
+       CURL *handle;
+       stream *s;
+       CURLcode ret;
+
+       s = buffer_wastream(NULL, url);
+       if (s == NULL) {
+               snprintf(errbuf, CURL_ERROR_SIZE, "could not allocate memory");
+               return NULL;
+       }
+
+       if ((handle = curl_easy_init()) == NULL) {
+               mnstr_destroy(s);
+               snprintf(errbuf, CURL_ERROR_SIZE, "could not create CURL 
handle");
+               return NULL;
+       }
+
+       errbuf[0] = 0;
+
+       if ((ret = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errbuf)) != 
CURLE_OK ||
+           (ret = curl_easy_setopt(handle, CURLOPT_URL, url)) != CURLE_OK ||
+           (ret = curl_easy_setopt(handle, CURLOPT_WRITEDATA, s)) != CURLE_OK 
||
+           (ret = curl_easy_setopt(handle, CURLOPT_VERBOSE, 0)) != CURLE_OK ||
+           (ret = curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1)) != CURLE_OK ||
+           (ret = curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1)) != 
CURLE_OK ||
+           (ret = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, 
write_callback)) != CURLE_OK ||
+           (ret = curl_easy_perform(handle)) != CURLE_OK) {
+               curl_easy_cleanup(handle);
+               mnstr_destroy(s);
+               if (errbuf[0] == 0)
+                       snprintf(errbuf, CURL_ERROR_SIZE, "%s", 
curl_easy_strerror(ret));
+               return NULL;
+       }
+       curl_easy_cleanup(handle);
+       (void) mnstr_get_buffer(s);     /* switch to read-only */
+       return s;
+}
+#endif
+
 struct privdata {
        stream *f;
        char *buf;
        Mapi mid;
+#ifdef HAVE_CURL
+       char errbuf[CURL_ERROR_SIZE];
+#endif
 };
 
 #define READSIZE       (1 << 16)
@@ -3086,7 +3150,7 @@ static const char alpha[] = "ABCDEFGHIJK
 
 static char *
 getfile(void *data, const char *filename, bool binary,
-       uint64_t offset, size_t *size)
+               uint64_t offset, size_t *size)
 {
        stream *f;
        char *buf;
@@ -3116,9 +3180,13 @@ getfile(void *data, const char *filename
                                    && filename[x] == ':'
                                    && filename[x+1] == '/'
                                    && filename[x+2] == '/') {
-                                       if (allow_remote)
-                                               f = open_urlstream(filename);
-                                       else
+#ifdef HAVE_CURL
+                                       if (allow_remote) {
+                                               f = open_urlstream(filename, 
priv->errbuf);
+                                               if (f == NULL && 
priv->errbuf[0])
+                                                       return priv->errbuf;
+                                       } else
+#endif
                                                return "client refuses to 
retrieve remote content";
                                }
                        }
@@ -3161,7 +3229,7 @@ getfile(void *data, const char *filename
                                return "error reading file";
                        }
                        if (s == 0) {
-                               /* reached EOF withing offset lines */
+                               /* reached EOF within offset lines */
                                close_stream(f);
                                return NULL;
                        }
diff --git a/common/stream/CMakeLists.txt b/common/stream/CMakeLists.txt
--- a/common/stream/CMakeLists.txt
+++ b/common/stream/CMakeLists.txt
@@ -32,7 +32,6 @@ target_sources(stream
   bz2_stream.c
   xz_stream.c
   lz4_stream.c
-  url_stream.c
   socket_stream.c
   mapi_stream.c
   memio.c
@@ -67,7 +66,6 @@ target_link_libraries(stream
   PRIVATE
   $<$<BOOL:${ZLIB_FOUND}>:ZLIB::ZLIB>
   $<$<BOOL:${BZIP2_FOUND}>:BZip2::BZip2>
-  $<$<BOOL:${CURL_FOUND}>:CURL::libcurl>
   $<$<BOOL:${LIBLZMA_FOUND}>:LibLZMA::LibLZMA>
   $<$<BOOL:${LZ4_FOUND}>:LZ4::LZ4>
   $<$<BOOL:${Iconv_FOUND}>:Iconv::Iconv>
@@ -92,10 +90,6 @@ if (NOT WIN32)
     set(PKG_BZIP2 "bzip2")
   endif()
 
-  if(CURL_FOUND)
-    set(PKG_CURL "libcurl")
-  endif()
-
   if(LZ4_FOUND)
     set(PKG_LZ4 "liblz4")
   endif()
diff --git a/common/stream/ChangeLog b/common/stream/ChangeLog
--- a/common/stream/ChangeLog
+++ b/common/stream/ChangeLog
@@ -1,3 +1,7 @@
 # ChangeLog file for stream
 # This file is updated with Maddlog
 
+* Wed Feb 21 2024 Sjoerd Mullender <sjo...@acm.org>
+- CURL support has been removed from the stream library.  If support is
+  needed, look at the source code in either streamcat.c or mclient.c.
+
diff --git a/common/stream/memio.c b/common/stream/memio.c
--- a/common/stream/memio.c
+++ b/common/stream/memio.c
@@ -84,7 +84,16 @@ mnstr_get_buffer(stream *s)
 {
        if (s == NULL)
                return NULL;
-       return (buffer *) s->stream_data.p;
+       buffer *b = (buffer *) s->stream_data.p;
+       if (b == NULL)
+               return NULL;
+       if (!s->readonly) {
+               /* switching from write mode to read mode */
+               s->readonly = true;
+               b->len = b->pos;
+       }
+       b->pos = 0;
+       return b;
 }
 
 static ssize_t
@@ -95,7 +104,11 @@ buffer_read(stream *restrict s, void *re
 
        b = (buffer *) s->stream_data.p;
        assert(b);
-       if (size && b && b->pos + size <= b->len) {
+       if (b == NULL)
+               return -1;
+       if (size != 0) {
+               if (size + b->pos > b->len)
+                       size = b->len - b->pos;
                memcpy(buf, b->buf + b->pos, size);
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to