From e7ba883b60e9ea02fd2f48d486432cb7f3ab9453 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sun, 12 Feb 2017 14:39:55 -0300 Subject: [PATCH] added flags parameter to afopen() --- include/villas/advio.h | 10 ++++++++-- lib/advio.c | 10 +++++----- tests/advio.c | 6 +++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/include/villas/advio.h b/include/villas/advio.h index 33435887f..3f8b06e9d 100644 --- a/include/villas/advio.h +++ b/include/villas/advio.h @@ -9,18 +9,24 @@ #include +enum advio_flags { + ADVIO_MEM = (1 << 0), /**< Instead of a real file, a memory backed stdio stream is used. */ + ADVIO_DIRTY = (1 << 1) /**< The file contents have been modified. We need to upload. */ +}; + struct advio { + int flags; + CURL *curl; FILE *file; const char *url; - bool dirty; /**< The file contents have been modified. We need to upload */ }; typedef struct advio AFILE; -AFILE *afopen(const char *url, const char *mode); +AFILE *afopen(const char *url, const char *mode, int flags); int afclose(AFILE *file); diff --git a/lib/advio.c b/lib/advio.c index 9111db9e1..648524e42 100644 --- a/lib/advio.c +++ b/lib/advio.c @@ -32,7 +32,7 @@ #include "config.h" #include "advio.h" -AFILE *afopen(const char *url, const char *mode) +AFILE *afopen(const char *uri, const char *mode, int flags) { CURLcode res; @@ -80,8 +80,8 @@ AFILE *afopen(const char *url, const char *mode) } af->url = strdup(url); - af->dirty = false; + af->flags = flags & ~ADVIO_DIRTY; return af; out0: curl_easy_cleanup(af->curl); @@ -118,7 +118,7 @@ int afflush(AFILE *af) return ret; /* Only upload file if it was changed */ - if (af->dirty) { + if (af->flags & ADVIO_DIRTY) { CURLcode res; long pos; @@ -137,7 +137,7 @@ int afflush(AFILE *af) if (res != CURLE_OK) return -1; - af->dirty = false; + af->flags &= ADVIO_DIRTY; } return 0; @@ -155,7 +155,7 @@ size_t afwrite(const void *restrict ptr, size_t size, size_t nitems, AFILE *rest ret = fwrite(ptr, size, nitems, stream->file); if (ret > 0) - stream->dirty = true; + af->flags |= ADVIO_DIRTY; return ret; } diff --git a/tests/advio.c b/tests/advio.c index 5622020b4..3f3900a79 100644 --- a/tests/advio.c +++ b/tests/advio.c @@ -16,7 +16,7 @@ Test(advio, download) const char *url = "http://www.textfiles.com/100/angela.art"; - af = afopen(url, "r"); + af = afopen(url, "r", 0); cr_assert(af, "Failed to download file"); cr_log_info("Opened file %s", url); @@ -46,7 +46,7 @@ Test(advio, upload) len1 = read_random(rnd, sizeof(rnd)); /* Open file for writing */ - af = afopen(uri, "w+"); + af = afopen(uri, "w+", 0); cr_assert(af, "Failed to download file"); len2 = afwrite(rnd, 1, len1, af); @@ -56,7 +56,7 @@ Test(advio, upload) cr_assert_eq(ret, 0, "Failed to close/upload file"); /* Open for reading and comparison */ - af = afopen(uri, "r"); + af = afopen(uri, "r", 0); cr_assert(af, "Failed to download file"); len2 = afread(buffer, 1, len1, af);