diff mbox series

[07/11] Lua: introduce image:copy2file() method

Message ID 20171103123009.18705-7-christian.storm@siemens.com
State Accepted
Headers show
Series [01/11] Lua: expose get_tmpdir() to Lua | expand

Commit Message

Storm, Christian Nov. 3, 2017, 12:30 p.m. UTC
Introduce the image:copy2file() method implementing the common
use case of storing the artifact's data into a file.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 corelib/lua_interface.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

Comments

mailander Nov. 7, 2017, 4:19 p.m. UTC | #1
On 03/11/2017 13:30, Christian Storm wrote:
> Introduce the image:copy2file() method implementing the common
> use case of storing the artifact's data into a file.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  corelib/lua_interface.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 64 insertions(+)
> 
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index 9694480..a551cde 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -20,6 +20,7 @@
>   */
>  
>  #include <stdlib.h>
> +#include <unistd.h>
>  #include <stdbool.h>
>  #include <errno.h>
>  #include "lua.h"
> @@ -55,6 +56,9 @@ extern const char EMBEDDED_LUA_SRC[];
>  static int l_register_handler( lua_State *L );
>  static int l_call_handler(lua_State *L);
>  #endif
> +static void image2table(lua_State* L, struct img_type *img);
> +static void table2image(lua_State* L, struct img_type *img);
> +static void update_table(lua_State* L, struct img_type *img);
>  
>  void LUAstackDump(lua_State *L)
>  {
> @@ -265,6 +269,60 @@ static void lua_number_to_img(struct img_type *img, const char *key,
>  
>  }
>  
> +#ifdef CONFIG_HANDLER_IN_LUA
> +static int l_copy2file(lua_State *L)
> +{
> +	luaL_checktype(L, 1, LUA_TTABLE);
> +	luaL_checktype(L, 2, LUA_TSTRING);
> +
> +	int fdout = openfileoutput(lua_tostring(L, 2));
> +	lua_pop(L, 1);
> +	if (fdout < 0) {
> +		lua_pop(L, 1);
> +		lua_pushinteger(L, -1);
> +		lua_pushstring(L, strerror(errno));
> +		return 2;
> +	}
> +
> +	struct img_type img = {};
> +	uint32_t checksum = 0;
> +
> +	table2image(L, &img);
> +	int ret = copyfile(img.fdin,
> +				 &fdout,
> +				 img.size,
> +				 (unsigned long *)&img.offset,
> +				 img.seek,
> +				 0, /* no skip */
> +				 img.compressed,
> +				 &checksum,
> +				 img.sha256,
> +				 img.is_encrypted,
> +				 NULL);
> +	update_table(L, &img);
> +	lua_pop(L, 1);
> +
> +	if (ret < 0) {
> +		lua_pushinteger(L, -1);
> +		lua_pushstring(L, strerror(errno));
> +		goto copyfile_exit;
> +	}
> +	if ((img.checksum != 0) && (checksum != img.checksum)) {
> +		lua_pushinteger(L, -1);
> +		lua_pushfstring(L, "Checksums WRONG! Computed 0x%d, should be 0x%d\n",
> +						checksum, img.checksum);
> +		goto copyfile_exit;
> +	}
> +
> +	lua_pushinteger(L, 0);
> +	lua_pushnil(L);
> +
> +copyfile_exit:
> +	close(fdout);
> +	return 2;
> +}
> +#endif
> +
>  static void update_table(lua_State* L, struct img_type *img)
>  {
>  	if (L && img) {
> @@ -292,6 +350,12 @@ static void update_table(lua_State* L, struct img_type *img)
>  		LUA_PUSH_IMG_NUMBER(img, "size", size);
>  		LUA_PUSH_IMG_NUMBER(img, "checksum", checksum);
>  
> +#ifdef CONFIG_HANDLER_IN_LUA
> +		lua_pushstring(L, "copy2file");
> +		lua_pushcfunction(L, &l_copy2file);
> +		lua_settable(L, -3);
> +#endif
> +
>  		lua_getfield(L, -1, "_private");
>  		LUA_PUSH_IMG_NUMBER(img, "offset", offset);
>  		lua_pop(L, 1);
> 

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 9694480..a551cde 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -20,6 +20,7 @@ 
  */
 
 #include <stdlib.h>
+#include <unistd.h>
 #include <stdbool.h>
 #include <errno.h>
 #include "lua.h"
@@ -55,6 +56,9 @@  extern const char EMBEDDED_LUA_SRC[];
 static int l_register_handler( lua_State *L );
 static int l_call_handler(lua_State *L);
 #endif
+static void image2table(lua_State* L, struct img_type *img);
+static void table2image(lua_State* L, struct img_type *img);
+static void update_table(lua_State* L, struct img_type *img);
 
 void LUAstackDump(lua_State *L)
 {
@@ -265,6 +269,60 @@  static void lua_number_to_img(struct img_type *img, const char *key,
 
 }
 
+#ifdef CONFIG_HANDLER_IN_LUA
+static int l_copy2file(lua_State *L)
+{
+	luaL_checktype(L, 1, LUA_TTABLE);
+	luaL_checktype(L, 2, LUA_TSTRING);
+
+	int fdout = openfileoutput(lua_tostring(L, 2));
+	lua_pop(L, 1);
+	if (fdout < 0) {
+		lua_pop(L, 1);
+		lua_pushinteger(L, -1);
+		lua_pushstring(L, strerror(errno));
+		return 2;
+	}
+
+	struct img_type img = {};
+	uint32_t checksum = 0;
+
+	table2image(L, &img);
+	int ret = copyfile(img.fdin,
+				 &fdout,
+				 img.size,
+				 (unsigned long *)&img.offset,
+				 img.seek,
+				 0, /* no skip */
+				 img.compressed,
+				 &checksum,
+				 img.sha256,
+				 img.is_encrypted,
+				 NULL);
+	update_table(L, &img);
+	lua_pop(L, 1);
+
+	if (ret < 0) {
+		lua_pushinteger(L, -1);
+		lua_pushstring(L, strerror(errno));
+		goto copyfile_exit;
+	}
+	if ((img.checksum != 0) && (checksum != img.checksum)) {
+		lua_pushinteger(L, -1);
+		lua_pushfstring(L, "Checksums WRONG! Computed 0x%d, should be 0x%d\n",
+						checksum, img.checksum);
+		goto copyfile_exit;
+	}
+
+	lua_pushinteger(L, 0);
+	lua_pushnil(L);
+
+copyfile_exit:
+	close(fdout);
+	return 2;
+}
+#endif
+
 static void update_table(lua_State* L, struct img_type *img)
 {
 	if (L && img) {
@@ -292,6 +350,12 @@  static void update_table(lua_State* L, struct img_type *img)
 		LUA_PUSH_IMG_NUMBER(img, "size", size);
 		LUA_PUSH_IMG_NUMBER(img, "checksum", checksum);
 
+#ifdef CONFIG_HANDLER_IN_LUA
+		lua_pushstring(L, "copy2file");
+		lua_pushcfunction(L, &l_copy2file);
+		lua_settable(L, -3);
+#endif
+
 		lua_getfield(L, -1, "_private");
 		LUA_PUSH_IMG_NUMBER(img, "offset", offset);
 		lua_pop(L, 1);