From ecadad5a5ba468e7c31b289ec174db6f1fc50244 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Tue, 29 Nov 2022 21:32:16 -0500 Subject: Potentially got things working. --- .gitignore | 2 +- .gitmodules | 12 +++--- README.md | 6 +-- build.sh | 24 +++++------ lib/libarchive | 1 - lib/liblzma | 1 - lib/libtar | 1 + lib/libzip | 1 + src/lpm.c | 130 +++++++++++++++++++++++++++++---------------------------- src/lpm.lua | 6 ++- 10 files changed, 96 insertions(+), 88 deletions(-) delete mode 160000 lib/libarchive delete mode 160000 lib/liblzma create mode 160000 lib/libtar create mode 160000 lib/libzip diff --git a/.gitignore b/.gitignore index ceb376b..5ed035e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ lpm -lua +lua.exe lpm-* *.o lpm.lua.c diff --git a/.gitmodules b/.gitmodules index 601f401..ef53f82 100644 --- a/.gitmodules +++ b/.gitmodules @@ -8,9 +8,9 @@ [submodule "lib/zlib"] path = lib/zlib url = https://github.com/madler/zlib.git -[submodule "lib/libarchive"] - path = lib/libarchive - url = https://github.com/libarchive/libarchive.git -[submodule "lib/liblzma"] - path = lib/liblzma - url = https://github.com/kobolabs/liblzma.git +[submodule "lib/libtar"] + path = lib/libtar + url = https://github.com/tklauser/libtar.git +[submodule "lib/libzip"] + path = lib/libzip + url = https://github.com/nih-at/libzip.git diff --git a/README.md b/README.md index c4577c8..bfbb626 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,8 @@ build `lpm`: * `mbedtls` (https/SSL support) * `libgit2` (accessing git repositories directly) * `libz` (supporting library for everything) -* `liblzma` (supporting library for libarchive) -* `libarchive` (for unpacking .tar.gz and .zip files) +* `libzip` (for unpacking .zip files) +* `libtar` (for unpacking .tar.gz files) ## Use in CI @@ -86,7 +86,7 @@ lpm --help ### Linux to Windows ``` -CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-gcc-ar WINDRES=x86_64-w64-mingw32-windres CMAKE_DEFAULT_FLAGS="-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=NEVER -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_INCLUDE_PATH=/usr/share/mingw-w64/include" LZMA_CONFIGURE="--host=x86_64-w64-mingw32" GIT2_CONFIGURE="-DDLLTOOL=x86_64-w64-mingw32-dlltool" ./build.sh -DLPM_VERSION='"'$VERSION-x86_64-windows-`git rev-parse --short HEAD`'"' +CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-gcc-ar WINDRES=x86_64-w64-mingw32-windres CMAKE_DEFAULT_FLAGS="-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=NEVER -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_INCLUDE_PATH=/usr/share/mingw-w64/include" GIT2_CONFIGURE="-DDLLTOOL=x86_64-w64-mingw32-dlltool" ./build.sh -DLPM_VERSION='"'$VERSION-x86_64-windows-`git rev-parse --short HEAD`'"' ``` diff --git a/build.sh b/build.sh index cb44af5..b1cf48f 100755 --- a/build.sh +++ b/build.sh @@ -6,9 +6,10 @@ : ${JOBS=4} SRCS="src/*.c" -LDFLAGS="$LDFLAGS -lm -pthread -static-libgcc" +CFLAGS="$CFLAGS -Ilib/prefix/include" +LDFLAGS="$LDFLAGS -lm -pthread -static-libgcc -Llib/prefix/lib" -[[ "$@" == "clean" ]] && rm -rf lib/libgit2/build lib/zlib/build lib/mbedtls-2.27.0/build lib/curl/build lib/libarchive/build-tmp lib/liblzma/build lib/prefix lua $BIN *.exe src/lpm.luac src/lpm.lua.c && exit 0 +[[ "$@" == "clean" ]] && rm -rf lib/libgit2/build lib/zlib/build lib/libtar/build lib/mbedtls-2.27.0/build lib/libarchive/build-tmp lib/libzip/build lib/prefix lua $BIN *.exe src/lpm.luac src/lpm.lua.c && exit 0 # Build supporting libraries, libz, libmbedtls, libmbedcrypto, libgit2, liblzma, libarchive, liblua CMAKE_DEFAULT_FLAGS=" $CMAKE_DEFAULT_FLAGS -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=`pwd`/lib/prefix -DCMAKE_INSTALL_PREFIX=`pwd`/lib/prefix -DBUILD_SHARED_LIBS=OFF" @@ -17,24 +18,23 @@ if [[ "$@" != *"-lz"* ]]; then [ ! -e "lib/zlib" ] && echo "Make sure you've cloned submodules. (git submodule update --init --depth=1)" && exit -1 [[ ! -e "lib/zlib/build" && $OSTYPE != 'msys'* ]] && cd lib/zlib && mkdir build && cd build && ../configure --prefix=`pwd`/../../prefix && $MAKE -j $JOBS && $MAKE install && cd ../../../ [[ ! -e "lib/zlib/build" && $OSTYPE == 'msys'* ]] && cd lib/zlib && mkdir build && $MAKE -f ../win32/Makefile.gcc -j $JOBS && cp *.a ../prefix/lib && cp *.h ../prefix/include && cd ../../ - LDFLAGS="$LDFLAGS -Llib/libz/build -l:libz.a" && CFLAGS="$CFLAGS -Ilib/prefix/include" && LDFLAGS="$LDFLAGS -Llib/prefix/lib -Llib/prefix/lib64" + LDFLAGS="$LDFLAGS -l:libz.a" fi if [[ "$@" != *"-lmbedtls"* && "$@" != *"-lmbedcrypto"* ]]; then [ ! -e "lib/mbedtls-2.27.0/build" ] && cd lib/mbedtls-2.27.0 && mkdir build && cd build && CFLAGS="-DMBEDTLS_MD4_C=1" cmake .. $CMAKE_DEFAULT_FLAGS -G "Unix Makefiles" -DENABLE_TESTING=OFF -DENABLE_PROGRAMS=OFF $SSL_CONFIGURE && CFLAGS="-DMBEDTLS_MD4_C=1" $MAKE -j $JOBS && $MAKE install && cd ../../../ - CFLAGS="$CFLAGS -Ilib/prefix/include" && LDFLAGS="$LDFLAGS -Llib/prefix/lib -Llib/prefix/lib64 -lmbedtls -lmbedx509 -lmbedcrypto" + LDFLAGS="$LDFLAGS -l:libmbedtls.a -l:libmbedx509.a -l:libmbedcrypto.a" fi if [[ "$@" != *"-lgit2"* ]]; then [ ! -e "lib/libgit2/build" ] && cd lib/libgit2 && mkdir build && cd build && cmake .. -G "Unix Makefiles" $GIT2_CONFIGURE $CMAKE_DEFAULT_FLAGS -DBUILD_TESTS=OFF -DBUILD_CLI=OFF -DREGEX_BACKEND=builtin -DUSE_SSH=OFF -DUSE_HTTPS=mbedTLS && $MAKE -j $JOBS && $MAKE install && cd ../../../ - LDFLAGS="-Llib/libgit2/build -l:libgit2.a $LDFLAGS -Llib/prefix/lib -Llib/prefix/lib64" && CFLAGS="$CFLAGS -Ilib/prefix/include" + LDFLAGS="-l:libgit2.a $LDFLAGS" fi -if [[ "$@" != *"-llzma"* ]]; then - [ ! -e "lib/liblzma/build" ] && cd lib/liblzma && mkdir build && cd build && ../configure $LZMA_CONFIGURE --prefix=`pwd`/../../prefix && $MAKE -j $JOBS && $MAKE install && cd ../../../ - LDFLAGS="-Llib/liblzma/build -Llib/prefix/lib -l:liblzma.a $LDFLAGS" && CFLAGS="$CFLAGS -Ilib/prefix/include" +if [[ "$@" != *"-lzip"* ]]; then + [ ! -e "lib/libzip/build" ] && cd lib/libzip && mkdir build && cd build && cmake .. -G "Unix Makefiles" $CMAKE_DEFAULT_FLAGS -DBUILD_TOOLS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_DOC=OFF -DENABLE_COMMONCRYPTO=OFF -DENABLE_GNUTLS=OFF -DENABLE_OPENSSL=OFF -DENABLE_BZIP2=OFF -DENABLE_LZMA=OFF -DENABLE_ZSTD=OFF && $MAKE -j $JOBS && $MAKE install && cd ../../../ + LDFLAGS="$LDFLAGS -l:libzip.a" fi -if [[ "$@" != *"-larchive"* ]]; then - [ ! -e "lib/libarchive/build-tmp" ] && cd lib/libarchive && mkdir build-tmp && cd build-tmp && cmake .. -G "Unix Makefiles" $ARCHIVE_CONFIGURE $CMAKE_DEFAULT_FLAGS -DENABLE_ICONV=OFF -DZLIB_WINAPI_EXITCODE=0 -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DENABLE_TEST=OFF -DZLIB_WINAPI_EXITCODE__TRYRUN_OUTPUT="test" -DENABLE_EXPAT=OFF -DENABLE_ACL=OFF -DENABLE_XATTR=OFF -DENABLE_CAT=OFF -DENABLE_CPIO=OFF -DENABLE_ZSTD=OFF -DENABLE_BZip2=OFF -DENABLE_OPENSSL=OFF -DENABLE_LIBXML2=OFF -DENABLE_PCREPOSIX=OFF && $MAKE -j $JOBS && $MAKE install && cd ../../../ - [ ! -e "lib/prefix/lib/libarchive.a" ] && cp -f lib/prefix/lib/libarchive_static.a lib/prefix/lib/libarchive.a - LDFLAGS="-Llib/libarchive/build-tmp -Llib/prefix/lib -l:libarchive.a $LDFLAGS" && CFLAGS="$CFLAGS -Ilib/prefix/include" +if [[ "$@" != *"-ltar"* ]]; then + [ ! -e "lib/libtar/build" ] && cd lib/libtar && mkdir build && autoreconf --force --install && cd build && ../configure --prefix=`pwd`/../../prefix && $MAKE -j $JOBS && $MAKE install && cd ../../../ + LDFLAGS="$LDFLAGS -l:libtar.a" fi [[ "$@" != *"-llua"* ]] && CFLAGS="$CFLAGS -Ilib/lua -DMAKE_LIB=1" && SRCS="$SRCS lib/lua/onelua.c" diff --git a/lib/libarchive b/lib/libarchive deleted file mode 160000 index 552547e..0000000 --- a/lib/libarchive +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 552547eacf6bfbdf5052f33cad57f181b6f92a61 diff --git a/lib/liblzma b/lib/liblzma deleted file mode 160000 index 87b7682..0000000 --- a/lib/liblzma +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 87b7682ce4b1c849504e2b3641cebaad62aaef87 diff --git a/lib/libtar b/lib/libtar new file mode 160000 index 0000000..6379b5d --- /dev/null +++ b/lib/libtar @@ -0,0 +1 @@ +Subproject commit 6379b5d2ae777dad576aeae70566740670057821 diff --git a/lib/libzip b/lib/libzip new file mode 160000 index 0000000..3286a93 --- /dev/null +++ b/lib/libzip @@ -0,0 +1 @@ +Subproject commit 3286a930a11fd046ee268671df2d9d0a3e0ca7e4 diff --git a/src/lpm.c b/src/lpm.c index ade952f..26c3773 100644 --- a/src/lpm.c +++ b/src/lpm.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -33,7 +34,9 @@ #include #include - +#include +#include +#include static char hex_digits[] = "0123456789abcdef"; static int lpm_hash(lua_State* L) { @@ -433,72 +436,73 @@ static int lpm_certs(lua_State* L) { } +static int gzopen_frontend(char *pathname, int oflags, int mode) { + gzFile gzf; + int fd; + fd = open(pathname, oflags, mode); + if (fd == -1) + return -1; + gzf = gzdopen(fd, "rb"); + if (!gzf) { + errno = ENOMEM; + return -1; + } + return (int)(long long)gzf; +} + +tartype_t gztype = { (openfunc_t) gzopen_frontend, (closefunc_t) gzclose, + (readfunc_t) gzread, (writefunc_t) gzwrite +}; + static int lpm_extract(lua_State* L) { const char* src = luaL_checkstring(L, 1); - const char* dst = luaL_optstring(L, 2, "."); - - char error_buffer[1024] = {0}; - struct archive_entry *entry; - const void *buff; - int flags = 0; - int r; - size_t size; -#if ARCHIVE_VERSION_NUMBER >= 3000000 - int64_t offset; -#else - off_t offset; -#endif - struct archive *ar = archive_read_new(); - struct archive *aw = archive_write_disk_new(); - archive_write_disk_set_options(aw, flags); - archive_read_support_format_tar(ar); - archive_read_support_format_zip(ar); - archive_read_support_filter_gzip(ar); - if ((r = archive_read_open_filename(ar, src, 10240))) { - snprintf(error_buffer, sizeof(error_buffer), "error extracting archive %s: %s", src, archive_error_string(ar)); - goto cleanup; - } - for (;;) { - int r = archive_read_next_header(ar, &entry); - if (r == ARCHIVE_EOF) - break; - if (r != ARCHIVE_OK) { - snprintf(error_buffer, sizeof(error_buffer), "error extracting archive %s: %s", src, archive_error_string(ar)); - goto cleanup; - } - char path[MAX_PATH]; - strcpy(path, dst); strcat(path, "/"); - strncat(path, archive_entry_pathname(entry), sizeof(path) - 3); path[MAX_PATH-1] = 0; - archive_entry_set_pathname(entry, path); - if (archive_write_header(aw, entry) != ARCHIVE_OK) { - snprintf(error_buffer, sizeof(error_buffer), "error extracting archive %s: %s", src, archive_error_string(aw)); - goto cleanup; - } - for (;;) { - int r = archive_read_data_block(ar, &buff, &size, &offset); - if (r == ARCHIVE_EOF) - break; - if (r != ARCHIVE_OK) { - snprintf(error_buffer, sizeof(error_buffer), "error extracting archive %s: %s", src, archive_error_string(ar)); - goto cleanup; - } - if (archive_write_data_block(aw, buff, size, offset) != ARCHIVE_OK) { - snprintf(error_buffer, sizeof(error_buffer), "error extracting archive %s: %s", src, archive_error_string(aw)); - goto cleanup; + const char* dst = luaL_checkstring(L, 2); + + if (strstr(src, ".zip")) { + int zip_error; + zip_t* archive = zip_open(src, ZIP_RDONLY, &zip_error); + if (!archive) + return luaL_error(L, "can't open zip archive %s: %s", src, strerror(zip_error)); + zip_int64_t entries = zip_get_num_entries(archive, 0); + for (zip_int64_t i = 0; i < entries; ++i) { + zip_file_t* zip_file = zip_fopen_index(archive, i, 0); + const char* zip_name = zip_get_name(archive, i, ZIP_FL_ENC_GUESS); + if (!zip_file) + return luaL_error(L, "can't read zip archive file %s: %s", zip_name, zip_strerror(archive)); + char target[MAX_PATH]; + int target_length = snprintf(target, sizeof(target), "%s/%s", dst, zip_name); + for (int i = 0; i < target_length; ++i) { + if (target[i] == '/') { + target[i] = 0; + int status = mkdir(target, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); + if (status && status != EEXIST) + return luaL_error(L, "can't extract zip archive file %s, can't create directory %s: %s", src, target, strerror(errno)); + target[i] = '/'; + } } + FILE* file = fopen(target, "wb"); + if (!file) + return luaL_error(L, "can't write file %s: %s", target, strerror(errno)); + while (1) { + char buffer[8192]; + zip_int64_t length = zip_fread(zip_file, buffer, sizeof(buffer)); + if (length == -1) + return luaL_error(L, "can't read zip archive file %s: %s", zip_name, zip_file_strerror(zip_file)); + if (length == 0) break; + fwrite(buffer, sizeof(char), length, file); + } + fclose(file); } - if (archive_write_finish_entry(aw) != ARCHIVE_OK) { - snprintf(error_buffer, sizeof(error_buffer), "error extracting archive %s: %s", src, archive_error_string(aw)); - goto cleanup; - } - } - cleanup: - archive_read_close(ar); - archive_read_free(ar); - archive_write_close(aw); - archive_write_free(aw); - if (error_buffer[0]) - return luaL_error(L, "error extracting archive %s: %s", src, archive_error_string(ar)); + } else if (strstr(src, ".tar.gz")) { + TAR* archive; + if (tar_open(&archive, src, &gztype, O_RDONLY, 0, TAR_GNU)) + return luaL_error(L, "can't open tar archive %s: %s", src, strerror(errno)); + if (tar_extract_all(archive, (char*)dst)) + return luaL_error(L, "can't extract tar archive %s to %s: %s", src, dst, strerror(errno)); + if (tar_close(archive)) + return luaL_error(L, "can't close tar archive %s: %s", src, strerror(errno)); + } else + return luaL_error(L, "unrecognized archive format %s", src); return 0; } diff --git a/src/lpm.lua b/src/lpm.lua index 02738a8..c1c9ab9 100644 --- a/src/lpm.lua +++ b/src/lpm.lua @@ -1628,10 +1628,14 @@ Flags have the following effects: end if ARGS[2] == "download" then - local file = common.get("https://raw.githubusercontent.com/adamharrison/lite-xl-simplified/master/manifest.json"); + local file = common.get(ARGS[3]); print(file) os.exit(0) end + if ARGS[2] == "extract" then + system.extract(ARGS[3], ARGS[4] or ".") + os.exit(0) + end -- Base setup; initialize default repos if applicable, read them in. Determine Lite XL system binary if not specified, and pull in a list of all local lite-xl's. -- cgit v1.2.3