diff --git a/include/villas/advio.h b/include/villas/advio.h index 3f8b06e9d..765c843e8 100644 --- a/include/villas/advio.h +++ b/include/villas/advio.h @@ -21,6 +21,9 @@ struct advio { FILE *file; const char *url; + /* For use with ADVIO_MODE_MEM */ + char *buf; + size_t size; }; diff --git a/lib/advio.c b/lib/advio.c index 648524e42..f47e9156d 100644 --- a/lib/advio.c +++ b/lib/advio.c @@ -38,7 +38,11 @@ AFILE *afopen(const char *uri, const char *mode, int flags) AFILE *af = alloc(sizeof(AFILE)); - af->file = tmpfile(); + if (flags & ADVIO_MEM) + af->file = open_memstream(&af->buf, &af->size); + else + af->file = tmpfile(); + if (!af->file) goto out2; @@ -113,10 +117,6 @@ int afflush(AFILE *af) { int ret; - ret = fflush(af->file); - if (ret) - return ret; - /* Only upload file if it was changed */ if (af->flags & ADVIO_DIRTY) { CURLcode res; @@ -125,6 +125,12 @@ int afflush(AFILE *af) /* Remember old stream pointer */ pos = ftell(af->file); fseek(af->file, 0, SEEK_SET); + /* Flushing a memory backed stream is sensless */ + if (!(af->flags & ADVIO_MEM)) { + ret = fflush(af->file); + if (ret) + return ret; + } curl_easy_setopt(af->curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(af->curl, CURLOPT_READDATA, af->file);