Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
3 changes: 2 additions & 1 deletion ar.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdlib.h>
#include <string.h>

#include "ar.h"
#include "ar_registry.h"
#include "ar_read.h"
#include "ar_write.h"
Expand Down Expand Up @@ -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 }
};
Expand Down
10 changes: 10 additions & 0 deletions ar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#if !defined LUA_VERSION_NUM
/* 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
5 changes: 3 additions & 2 deletions ar_entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <assert.h>
#include <sys/stat.h>

#include "ar.h"
#include "ar_entry.h"

#define err(...) (luaL_error(L, __VA_ARGS__))
Expand Down Expand Up @@ -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 },
Expand Down
33 changes: 17 additions & 16 deletions ar_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdlib.h>
#include <string.h>

#include "ar.h"
#include "ar_read.h"
#include "ar_entry.h"
#include "ar_registry.h"
Expand Down Expand Up @@ -85,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 }
};

Expand All @@ -113,12 +114,12 @@ 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
// 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) ) {
Expand Down Expand Up @@ -178,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}
Expand All @@ -197,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);
Expand All @@ -210,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;
Expand Down Expand Up @@ -248,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
Expand Down Expand Up @@ -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 },
Expand Down
27 changes: 14 additions & 13 deletions ar_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdlib.h>
#include <string.h>

#include "ar.h"
#include "ar_write.h"
#include "ar_entry.h"
#include "ar_registry.h"
Expand Down Expand Up @@ -83,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");
Expand Down Expand Up @@ -154,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;
Expand Down Expand Up @@ -196,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}
Expand All @@ -215,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);
Expand All @@ -228,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;
Expand Down Expand Up @@ -303,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));
}
Expand All @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down