From 06cd2769f8d5c07e4813c3ec17597b4a2552fe2d Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Tue, 21 Jun 2016 16:10:09 +0200 Subject: [PATCH 1/5] Add support for Lua 5.1 From http://lua-users.org/wiki/CompatibilityWithLuaFive: luaL_reg (Lua 5.0) changed its name to luaL_Reg since Lua 5.1. Apply the recommended fix to support lua 5.1 and prior versions. --- CMakeLists.txt | 2 +- ar.c | 3 ++- ar.h | 4 ++++ ar_entry.c | 5 +++-- ar_read.c | 5 +++-- ar_write.c | 5 +++-- 6 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 ar.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b191370..3200dfb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ CMAKE_MINIMUM_REQUIRED (VERSION 2.6) # Define how to build archive.so: INCLUDE_DIRECTORIES(${LIBARCHIVE_INCLUDE_DIR} ${LUA_INCLUDE_DIR}) ADD_LIBRARY(cmod_archive MODULE - ar.c ar_write.c ar_registry.c ar_read.c ar_entry.c archive.def) + ar.c ar_write.c ar_registry.c ar_read.c ar_entry.c ar.h archive.def) SET_TARGET_PROPERTIES(cmod_archive PROPERTIES PREFIX "") SET_TARGET_PROPERTIES(cmod_archive PROPERTIES OUTPUT_NAME archive) TARGET_LINK_LIBRARIES(cmod_archive ${LUA_LIBRARIES} ${LIBARCHIVE_LIBRARY}) diff --git a/ar.c b/ar.c index bc7739c..24f3b8f 100644 --- a/ar.c +++ b/ar.c @@ -6,6 +6,7 @@ #include #include +#include "ar.h" #include "ar_registry.h" #include "ar_read.h" #include "ar_write.h" @@ -39,7 +40,7 @@ static int ar_version(lua_State *L) { ////////////////////////////////////////////////////////////////////// LUALIB_API int luaopen_archive(lua_State *L) { - static luaL_reg fns[] = { + static luaL_Reg fns[] = { { "version", ar_version }, { NULL, NULL } }; diff --git a/ar.h b/ar.h new file mode 100644 index 0000000..d137a55 --- /dev/null +++ b/ar.h @@ -0,0 +1,4 @@ +#if !defined LUA_VERSION_NUM +/* Lua 5.0 */ +#define luaL_Reg luaL_reg +#endif diff --git a/ar_entry.c b/ar_entry.c index 72056ac..19265dd 100644 --- a/ar_entry.c +++ b/ar_entry.c @@ -7,6 +7,7 @@ #include #include +#include "ar.h" #include "ar_entry.h" #define err(...) (luaL_error(L, __VA_ARGS__)) @@ -430,13 +431,13 @@ static int ar_entry_pathname(lua_State *L) { ////////////////////////////////////////////////////////////////////// int ar_entry_init(lua_State *L) { - static luaL_reg fns[] = { + static luaL_Reg fns[] = { { "entry", ar_entry }, { "_entry_ref_count", ar_ref_count }, { NULL, NULL } }; // So far there are no methods on the entry objects. - static luaL_reg m_fns[] = { + static luaL_Reg m_fns[] = { { "fflags", ar_entry_fflags }, { "dev", ar_entry_dev }, { "ino", ar_entry_ino }, diff --git a/ar_read.c b/ar_read.c index 39e0812..751ff29 100644 --- a/ar_read.c +++ b/ar_read.c @@ -10,6 +10,7 @@ #include #include +#include "ar.h" #include "ar_read.h" #include "ar_entry.h" #include "ar_registry.h" @@ -317,12 +318,12 @@ static int ar_read_data(lua_State *L) { // of the stack, and the archive{read} metatable is registered. ////////////////////////////////////////////////////////////////////// int ar_read_init(lua_State *L) { - static luaL_reg fns[] = { + static luaL_Reg fns[] = { { "read", ar_read }, { "_read_ref_count", ar_ref_count }, { NULL, NULL } }; - static luaL_reg m_fns[] = { + static luaL_Reg m_fns[] = { { "next_header", ar_read_next_header }, { "headers", ar_read_headers }, { "data", ar_read_data }, diff --git a/ar_write.c b/ar_write.c index 95ed17e..c820bb4 100644 --- a/ar_write.c +++ b/ar_write.c @@ -10,6 +10,7 @@ #include #include +#include "ar.h" #include "ar_write.h" #include "ar_entry.h" #include "ar_registry.h" @@ -319,12 +320,12 @@ static int ar_write_data(lua_State *L) { // of the stack, and the archive{write} metatable is registered. ////////////////////////////////////////////////////////////////////// int ar_write_init(lua_State *L) { - static luaL_reg fns[] = { + static luaL_Reg fns[] = { { "write", ar_write }, { "_write_ref_count", ar_ref_count }, { NULL, NULL } }; - static luaL_reg m_fns[] = { + static luaL_Reg m_fns[] = { { "header", ar_write_header }, { "data", ar_write_data }, { "close", ar_write_destroy }, From b68669ee30a99a8f66462e7ce54840606c871ade Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Mon, 20 Jun 2016 20:39:28 +0200 Subject: [PATCH 2/5] Add support for Lua 5.2 Use lua_getuservalue()/lua_setuservalue() instead of the removed lua_getfenv()/lua_setfenv(). --- ar.h | 6 ++++++ ar_read.c | 6 +++--- ar_write.c | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ar.h b/ar.h index d137a55..f3a5144 100644 --- a/ar.h +++ b/ar.h @@ -2,3 +2,9 @@ /* Lua 5.0 */ #define luaL_Reg luaL_reg #endif + +#if LUA_VERSION_NUM <= 501 +/* Lua 5.1 or older */ +#define lua_setuservalue lua_setfenv +#define lua_getuservalue lua_getfenv +#endif diff --git a/ar_read.c b/ar_read.c index 751ff29..bf92077 100644 --- a/ar_read.c +++ b/ar_read.c @@ -114,7 +114,7 @@ static int ar_read(lua_State *L) { lua_getfield(L, 1, "reader"); // {ud}, {fenv}, fn if ( ! lua_isfunction(L, -1) ) err("MissingArgument: required parameter 'reader' must be a function"); lua_setfield(L, -2, "reader"); // {ud}, {fenv} - lua_setfenv(L, -2); // {ud} + lua_setuservalue(L, -2); // {ud} // Do it the easy way for now... perhaps in the future we will // have a parameter to support toggling which algorithms are @@ -179,7 +179,7 @@ static int ar_read(lua_State *L) { // the index to the argument for which to pass to reader exists. If // idx is zero, nil is passed into reader. static void ar_read_get_reader(lua_State *L, int self_idx) { - lua_getfenv(L, self_idx); // {env} + lua_getuservalue(L, self_idx); // {env} lua_pushliteral(L, "reader"); // {env}, "reader" lua_rawget(L, -2); // {env}, reader lua_insert(L, -2); // reader, {env} @@ -249,7 +249,7 @@ static __LA_SSIZE_T ar_read_cb(struct archive * self, // We directly return the raw internal buffer, so we need to keep // a reference around: - lua_getfenv(L, -2); // {ud}, result, {fenv} + lua_getuservalue(L, -2); // {ud}, result, {fenv} lua_insert(L, -2); // {ud}, {fenv}, result lua_pushliteral(L, "read_buffer"); // {ud}, {fenv}, result, "read_buffer" lua_insert(L, -2); // {ud}, {fenv}, "read_buffer", result diff --git a/ar_write.c b/ar_write.c index c820bb4..3a9908c 100644 --- a/ar_write.c +++ b/ar_write.c @@ -84,7 +84,7 @@ static int ar_write(lua_State *L) { err("MissingArgument: required parameter 'writer' must be a function"); } lua_setfield(L, -2, "writer"); - lua_setfenv(L, -2); // {ud} + lua_setuservalue(L, -2); // {ud} // Extract various fields and prepare the archive: lua_getfield(L, 1, "bytes_per_block"); @@ -197,7 +197,7 @@ static int ar_write(lua_State *L) { // the index to the argument for which to pass to writer exists. If // idx is zero, nil is passed into writer. static void ar_write_get_writer(lua_State *L, int self_idx) { - lua_getfenv(L, self_idx); // {env} + lua_getuservalue(L, self_idx); // {env} lua_pushliteral(L, "writer"); // {env}, "writer" lua_rawget(L, -2); // {env}, writer lua_insert(L, -2); // writer, {env} From a357e192bee6e78a90474329913a61242627abaf Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Mon, 20 Jun 2016 20:46:50 +0200 Subject: [PATCH 3/5] Port to libarchive 3.0 Replace the deprecated functions with their libarchive 3.0 replacements as listed at: https://github.com/libarchive/libarchive/wiki/ManPageLibarchiveChanges3 --- ar_read.c | 22 +++++++++++----------- ar_write.c | 16 ++++++++-------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/ar_read.c b/ar_read.c index bf92077..4c0ee4c 100644 --- a/ar_read.c +++ b/ar_read.c @@ -86,13 +86,13 @@ static int ar_read(lua_State *L) { { NULL, NULL } }; static named_setter compression_names[] = { - { "all", archive_read_support_compression_all }, - { "bzip2", archive_read_support_compression_bzip2 }, - { "compress", archive_read_support_compression_compress }, - { "gzip", archive_read_support_compression_gzip }, - { "lzma", archive_read_support_compression_lzma }, - { "none", archive_read_support_compression_none }, - { "xz", archive_read_support_compression_xz }, + { "all", archive_read_support_filter_all }, + { "bzip2", archive_read_support_filter_bzip2 }, + { "compress", archive_read_support_filter_compress }, + { "gzip", archive_read_support_filter_gzip }, + { "lzma", archive_read_support_filter_lzma }, + { "none", archive_read_support_filter_none }, + { "xz", archive_read_support_filter_xz }, { NULL, NULL } }; @@ -119,7 +119,7 @@ static int ar_read(lua_State *L) { // Do it the easy way for now... perhaps in the future we will // have a parameter to support toggling which algorithms are // supported: - if ( ARCHIVE_OK != archive_read_support_compression_all(*self_ref) ) { + if ( ARCHIVE_OK != archive_read_support_filter_all(*self_ref) ) { err("archive_read_support_compression_all: %s", archive_error_string(*self_ref)); } if ( ARCHIVE_OK != archive_read_support_format_all(*self_ref) ) { @@ -198,7 +198,7 @@ static int ar_read_destroy(lua_State *L) { if ( ARCHIVE_OK != archive_read_close(*self_ref) ) { lua_pushfstring(L, "archive_read_close: %s", archive_error_string(*self_ref)); - archive_read_finish(*self_ref); + archive_read_free(*self_ref); __ref_count--; *self_ref = NULL; lua_error(L); @@ -211,8 +211,8 @@ static int ar_read_destroy(lua_State *L) { lua_call(L, 2, 1); // {self}, result } - if ( ARCHIVE_OK != archive_read_finish(*self_ref) ) { - luaL_error(L, "archive_read_finish: %s", archive_error_string(*self_ref)); + if ( ARCHIVE_OK != archive_read_free(*self_ref) ) { + luaL_error(L, "archive_read_free: %s", archive_error_string(*self_ref)); } __ref_count--; *self_ref = NULL; diff --git a/ar_write.c b/ar_write.c index 3a9908c..af07e8e 100644 --- a/ar_write.c +++ b/ar_write.c @@ -155,11 +155,11 @@ static int ar_write(lua_State *L) { const char *name; int (*setter)(struct archive *); } names[] = { - { "bzip2", archive_write_set_compression_bzip2 }, - { "compress", archive_write_set_compression_compress }, - { "gzip", archive_write_set_compression_gzip }, - { "lzma", archive_write_set_compression_lzma }, - { "xz", archive_write_set_compression_xz }, + { "bzip2", archive_write_add_filter_bzip2 }, + { "compress", archive_write_add_filter_compress }, + { "gzip", archive_write_add_filter_gzip }, + { "lzma", archive_write_add_filter_lzma }, + { "xz", archive_write_add_filter_xz }, { NULL, NULL } }; int idx = 0; @@ -216,7 +216,7 @@ static int ar_write_destroy(lua_State *L) { if ( ARCHIVE_OK != archive_write_close(*self_ref) ) { lua_pushfstring(L, "archive_write_close: %s", archive_error_string(*self_ref)); - archive_write_finish(*self_ref); + archive_write_free(*self_ref); __ref_count--; *self_ref = NULL; lua_error(L); @@ -229,8 +229,8 @@ static int ar_write_destroy(lua_State *L) { lua_call(L, 2, 1); // {self}, result } - if ( ARCHIVE_OK != archive_write_finish(*self_ref) ) { - luaL_error(L, "archive_write_finish: %s", archive_error_string(*self_ref)); + if ( ARCHIVE_OK != archive_write_free(*self_ref) ) { + luaL_error(L, "archive_write_free: %s", archive_error_string(*self_ref)); } __ref_count--; *self_ref = NULL; From ceecc4b604f91dc2a76164f90acd7a4952bb6463 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Tue, 21 Jun 2016 12:36:30 +0200 Subject: [PATCH 4/5] Fix use of unset variable "wrote" was checked against -1 but was never actually set. --- ar_write.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ar_write.c b/ar_write.c index af07e8e..129a7bc 100644 --- a/ar_write.c +++ b/ar_write.c @@ -304,7 +304,7 @@ static int ar_write_data(lua_State *L) { data = lua_tolstring(L, 2, &len); - archive_write_data(self, data, len); + wrote = archive_write_data(self, data, len); if ( -1 == wrote ) { err("archive_write_data: %s", archive_error_string(self)); } From bc0929b61fe1884f9a4c42b1fc8df65e9bcf32ce Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Tue, 21 Jun 2016 15:52:45 +0200 Subject: [PATCH 5/5] Fix invalid FFlag in tests When running the tests, libarchive says: InvalidFFlag: 'archive' is not a known fflag So remove that one flag from our tests. --- test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.lua b/test.lua index 93b147d..3c1f4bf 100644 --- a/test.lua +++ b/test.lua @@ -74,7 +74,7 @@ function test_basic() -- Test creating our own "normal" entry: local normal_entry = { - fflags="nosappnd,dump,archive", + fflags="nosappnd,dump", dev=200, ino=1000, mode=0x80FF,