From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <viktorin@rehivetech.com>
Received: from wes1-so1.wedos.net (wes1-so1.wedos.net [46.28.106.15])
 by dpdk.org (Postfix) with ESMTP id 2770A5597
 for <dev@dpdk.org>; Fri, 29 Apr 2016 15:13:43 +0200 (CEST)
Received: from pcviktorin.fit.vutbr.cz (pcviktorin.fit.vutbr.cz
 [147.229.13.147])
 by wes1-so1.wedos.net (Postfix) with ESMTPSA id 3qxDfZ6pCZzB1m;
 Fri, 29 Apr 2016 15:13:42 +0200 (CEST)
From: Jan Viktorin <viktorin@rehivetech.com>
To: dev@dpdk.org
Cc: Jan Viktorin <viktorin@rehivetech.com>,
 Thomas Monjalon <thomas.monjalon@6wind.com>,
 David Marchand <david.marchand@6wind.com>
Date: Fri, 29 Apr 2016 15:11:35 +0200
Message-Id: <1461935496-20367-4-git-send-email-viktorin@rehivetech.com>
X-Mailer: git-send-email 2.8.0
In-Reply-To: <1461935496-20367-1-git-send-email-viktorin@rehivetech.com>
References: <1461935496-20367-1-git-send-email-viktorin@rehivetech.com>
Subject: [dpdk-dev] [RFC 3/4] app/test: add functions to create files from
	resources
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: patches and discussions about DPDK <dev.dpdk.org>
List-Unsubscribe: <http://dpdk.org/ml/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://dpdk.org/ml/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <http://dpdk.org/ml/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Fri, 29 Apr 2016 13:13:43 -0000

A resource can be written into the target filesystem by calling resource_fwrite
or resource_fwrite_file. Such file can be created before a test is started and
removed after the test finishes.

Signed-off-by: Jan Viktorin <viktorin@rehivetech.com>
---
 app/test/resource.c      | 35 +++++++++++++++++++++++++++++++++++
 app/test/resource.h      |  4 ++++
 app/test/test_resource.c | 10 ++++++++++
 3 files changed, 49 insertions(+)

diff --git a/app/test/resource.c b/app/test/resource.c
index 50a4510..a11c86e 100644
--- a/app/test/resource.c
+++ b/app/test/resource.c
@@ -31,6 +31,7 @@
  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <stdio.h>
 #include <string.h>
 #include <errno.h>
 #include <sys/queue.h>
@@ -55,6 +56,40 @@ const struct resource *resource_find(const char *name)
 	return NULL;
 }
 
+int resource_fwrite(const struct resource *r, FILE *f)
+{
+	const size_t goal = resource_size(r);
+	size_t total = 0;
+
+	while (total < goal) {
+		size_t wlen = fwrite(r->beg + total, 1, goal - total, f);
+		if (wlen == 0) {
+			perror(__func__);
+			return -1;
+		}
+
+		total += wlen;
+	}
+
+	return 0;
+}
+
+int resource_fwrite_file(const struct resource *r, const char *fname)
+{
+	FILE *f;
+	int ret;
+
+	f = fopen(fname, "w");
+	if (f == NULL) {
+		perror(__func__);
+		return -1;
+	}
+
+	ret = resource_fwrite(r, f);
+	fclose(f);
+	return ret;
+}
+
 void __resource_register(struct resource *r)
 {
 	TAILQ_INSERT_TAIL(&resource_list, r, next);
diff --git a/app/test/resource.h b/app/test/resource.h
index 7978e77..096633c 100644
--- a/app/test/resource.h
+++ b/app/test/resource.h
@@ -35,6 +35,7 @@
 #define _RESOURCE_H_
 
 #include <sys/queue.h>
+#include <stdio.h>
 #include <stddef.h>
 
 #include <rte_eal.h>
@@ -57,6 +58,9 @@ static inline size_t resource_size(const struct resource *r)
 
 const struct resource *resource_find(const char *name);
 
+int resource_fwrite(const struct resource *r, FILE *f);
+int resource_fwrite_file(const struct resource *r, const char *fname);
+
 void __resource_register(struct resource *r);
 
 #define REGISTER_LINKED_RESOURCE(_n) \
diff --git a/app/test/test_resource.c b/app/test/test_resource.c
index 7752522..148964e 100644
--- a/app/test/test_resource.c
+++ b/app/test/test_resource.c
@@ -65,6 +65,7 @@ REGISTER_LINKED_RESOURCE(test_resource_c);
 static int test_resource_c(void)
 {
 	const struct resource *r;
+	FILE *f;
 
 	r = resource_find("test_resource_c");
 	TEST_ASSERT_NOT_NULL(r, "No test_resource_c found");
@@ -72,6 +73,15 @@ static int test_resource_c(void)
 			"Found resource %s, expected test_resource_c",
 			r->name);
 
+	TEST_ASSERT_SUCCESS(resource_fwrite_file(r, "test_resource.c"),
+			"Failed to to write file %s", r->name);
+
+	f = fopen("test_resource.c", "r");
+	TEST_ASSERT_NOT_NULL(f,
+			"Missing extracted file resource.c");
+	fclose(f);
+	remove("test_resource.c");
+
 	return 0;
 }
 
-- 
2.8.0