changeset 335:68287c7bcdb5

core: implement vfs-zip (read-only)
author David Demelier <markand@malikania.fr>
date Wed, 13 Oct 2021 15:58:02 +0200
parents 1d3e6108cb99
children f5a5bbb77122
files CMakeLists.txt cmake/FindLibzip.cmake src/libmlk-core/CMakeLists.txt src/libmlk-core/core/vfs-directory.c src/libmlk-core/core/vfs-zip.c src/libmlk-core/core/vfs-zip.h src/libmlk-core/core/vfs.c tests/CMakeLists.txt tests/assets/vfs/data.zip tests/test-vfs-zip.c
diffstat 10 files changed, 272 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Wed Oct 13 15:26:08 2021 +0200
+++ b/CMakeLists.txt	Wed Oct 13 15:58:02 2021 +0200
@@ -37,6 +37,7 @@
 endif ()
 
 option(MLK_WITH_NLS "Enable NLS support" On)
+option(MLK_WITH_ZIP "Enable zip file support" On)
 option(MLK_WITH_ZSTD "Enable zstd compression" On)
 option(MLK_WITH_TESTS "Enable unit tests" Off)
 option(MLK_WITH_EXAMPLES "Enable examples" Off)
@@ -67,6 +68,10 @@
 	endif ()
 endif ()
 
+if (MLK_WITH_ZIP)
+	find_package(Libzip REQUIRED)
+endif ()
+
 add_subdirectory(extern/libsqlite)
 add_subdirectory(extern/libgreatest)
 add_subdirectory(extern/librexo)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake/FindLibzip.cmake	Wed Oct 13 15:58:02 2021 +0200
@@ -0,0 +1,42 @@
+# FindLibzip
+# -----------
+#
+# Find Libzip library, this modules defines:
+#
+# Libzip_INCLUDE_DIRS, where to find libzip.h
+# Libzip_LIBRARIES, where to find library
+# Libzip_FOUND, if it is found
+#
+# The following imported targets will be available:
+#
+# Libzip::Libzip, if found.
+#
+
+find_path(Libzip_INCLUDE_DIR NAMES zip.h)
+find_library(Libzip_LIBRARY NAMES libzip zip)
+
+include(FindPackageHandleStandardArgs)
+
+find_package_handle_standard_args(
+	Libzip
+	FOUND_VAR Libzip_FOUND
+	REQUIRED_VARS Libzip_LIBRARY Libzip_INCLUDE_DIR
+)
+
+if (Libzip_FOUND)
+	set(Libzip_LIBRARIES ${Libzip_LIBRARY})
+	set(Libzip_INCLUDE_DIRS ${Libzip_INCLUDE_DIR})
+
+	if (NOT TARGET Libzip::Libzip)
+		add_library(Libzip::Libzip UNKNOWN IMPORTED)
+		set_target_properties(
+			Libzip::Libzip
+			PROPERTIES
+				IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+				IMPORTED_LOCATION "${Libzip_LIBRARY}"
+				INTERFACE_INCLUDE_DIRECTORIES "${Libzip_INCLUDE_DIRS}"
+		)
+	endif ()
+endif ()
+
+mark_as_advanced(Libzip_INCLUDE_DIR Libzip_LIBRARY)
--- a/src/libmlk-core/CMakeLists.txt	Wed Oct 13 15:26:08 2021 +0200
+++ b/src/libmlk-core/CMakeLists.txt	Wed Oct 13 15:58:02 2021 +0200
@@ -76,6 +76,8 @@
 	${libmlk-core_SOURCE_DIR}/core/util.h
 	${libmlk-core_SOURCE_DIR}/core/vfs-directory.c
 	${libmlk-core_SOURCE_DIR}/core/vfs-directory.h
+	${libmlk-core_SOURCE_DIR}/core/vfs-zip.c
+	${libmlk-core_SOURCE_DIR}/core/vfs-zip.h
 	${libmlk-core_SOURCE_DIR}/core/vfs.c
 	${libmlk-core_SOURCE_DIR}/core/vfs.h
 	${libmlk-core_SOURCE_DIR}/core/window.c
@@ -98,6 +100,10 @@
 	list(APPEND LIBRARIES ZSTD::ZSTD)
 endif ()
 
+if (MLK_WITH_ZIP)
+	list(APPEND LIBRARIES Libzip::Libzip)
+endif ()
+
 if (CMAKE_SYSTEM_NAME MATCHES "Windows")
 	list(APPEND LIBRARIES shlwapi)
 endif ()
--- a/src/libmlk-core/core/vfs-directory.c	Wed Oct 13 15:26:08 2021 +0200
+++ b/src/libmlk-core/core/vfs-directory.c	Wed Oct 13 15:58:02 2021 +0200
@@ -77,7 +77,6 @@
 	char path[PATH_MAX];
 
 	snprintf(path, sizeof (path), "%s/%s", self->base, entry);
-	memset(file, 0, sizeof (*file));
 
 	if (!(file->data = fopen(path, mode)))
 		return errorf("%s: %s", path, strerror(errno));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmlk-core/core/vfs-zip.c	Wed Oct 13 15:58:02 2021 +0200
@@ -0,0 +1,123 @@
+/*
+ * vfs-zip.c -- VFS subsystem for zip archives
+ *
+ * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <string.h>
+
+#include <zip.h>
+
+#include "error.h"
+#include "vfs-zip.h"
+#include "vfs.h"
+
+static inline int
+mkflags(const char *mode)
+{
+	/* TODO: this should check for mutual exclusions. */
+	int flags = 0;
+
+	for (; *mode; ++mode) {
+		switch (*mode) {
+		case 'w':
+			flags |= ZIP_CREATE;
+			break;
+		case 'x':
+			flags |= ZIP_EXCL;
+			break;
+		case '+':
+			flags |= ZIP_TRUNCATE;
+			break;
+		case 'r':
+			flags |= ZIP_RDONLY;
+			break;
+		default:
+			break;
+		}
+	}
+
+	return flags;
+}
+
+static size_t
+file_read(struct vfs_file *file, void *buf, size_t bufsz)
+{
+	return zip_fread(file->data, buf, bufsz);
+}
+
+static size_t
+file_write(struct vfs_file *file, const void *buf, size_t bufsz)
+{
+	(void)file;
+	(void)buf;
+	(void)bufsz;
+
+	return errorf("operation not supported");
+}
+
+static int
+file_flush(struct vfs_file *file)
+{
+	(void)file;
+
+	return 0;
+}
+
+static void
+file_finish(struct vfs_file *file)
+{
+	zip_fclose(file->data);
+}
+
+static int
+impl_open(struct vfs *vfs, struct vfs_file *file, const char *entry, const char *mode)
+{
+	(void)mode;
+
+	if (!(file->data = zip_fopen(vfs->data, entry, 0)))
+		return errorf("unable to open file in archive");
+
+	file->read = file_read;
+	file->write = file_write;
+	file->flush = file_flush;
+	file->finish = file_finish;
+
+	return 0;
+}
+
+static void
+impl_finish(struct vfs *vfs)
+{
+	zip_close(vfs->data);
+}
+
+int
+vfs_zip(struct vfs *vfs, const char *file, const char *mode)
+{
+	assert(vfs);
+	assert(file);
+
+	memset(vfs, 0, sizeof (*vfs));
+
+	if (!(vfs->data = zip_open(file, mkflags(mode), NULL)))
+		return errorf("%s: unable to open zip file", file);
+
+	vfs->open = impl_open;
+	vfs->finish = impl_finish;
+
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmlk-core/core/vfs-zip.h	Wed Oct 13 15:58:02 2021 +0200
@@ -0,0 +1,33 @@
+/*
+ * vfs-zip.h -- VFS subsystem for zip archives
+ *
+ * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef MLK_CORE_VFS_ZIP_H
+#define MLK_CORE_VFS_ZIP_H
+
+#include "core.h"
+
+struct vfs;
+
+CORE_BEGIN_DECLS
+
+int
+vfs_zip(struct vfs *, const char *, const char *);
+
+CORE_END_DECLS
+
+#endif /* !MLK_CORE_VFS_ZIP_H */
--- a/src/libmlk-core/core/vfs.c	Wed Oct 13 15:26:08 2021 +0200
+++ b/src/libmlk-core/core/vfs.c	Wed Oct 13 15:58:02 2021 +0200
@@ -27,6 +27,8 @@
 	assert(vfs);
 	assert(entry);
 
+	memset(file, 0, sizeof (*file));
+
 	return vfs->open(vfs, file, entry, mode);
 }
 
--- a/tests/CMakeLists.txt	Wed Oct 13 15:26:08 2021 +0200
+++ b/tests/CMakeLists.txt	Wed Oct 13 15:58:02 2021 +0200
@@ -34,6 +34,7 @@
 	tileset
 	util
 	vfs-directory
+	vfs-zip
 )
 
 foreach (t ${TESTS})
Binary file tests/assets/vfs/data.zip has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-vfs-zip.c	Wed Oct 13 15:58:02 2021 +0200
@@ -0,0 +1,60 @@
+/*
+ * test-zip-directory.c -- test VFS zip
+ *
+ * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <rexo.h>
+
+#include <core/vfs-zip.h>
+#include <core/vfs.h>
+
+RX_SET_UP(setup)
+{
+	if (vfs_zip(RX_DATA, DIRECTORY "/vfs/data.zip", "r") < 0)
+		return RX_ERROR;
+
+	return RX_SUCCESS;
+}
+
+RX_TEAR_DOWN(teardown)
+{
+	vfs_finish(RX_DATA);
+}
+
+RX_FIXTURE(basics_fixture, struct vfs, .set_up = setup, .tear_down = teardown);
+
+RX_TEST_CASE(basics, read, .fixture = basics_fixture)
+{
+	struct vfs_file file;
+	char data[256] = {0};
+
+	RX_INT_REQUIRE_EQUAL(vfs_open(RX_DATA, &file, "texts/hello.txt", "r"), 0);
+	RX_UINT_REQUIRE_EQUAL(vfs_file_read(&file, data, sizeof (data)), 21U);
+	RX_STR_REQUIRE_EQUAL(data, "Hello from zip file!\n");
+}
+
+RX_TEST_CASE(basics, notfound, .fixture = basics_fixture)
+{
+	struct vfs_file file;
+
+	RX_INT_REQUIRE_EQUAL(vfs_open(RX_DATA, &file, "notfound.txt", "r"), -1);
+}
+
+int
+main(int argc, char **argv)
+{
+	return rx_main(0, NULL, argc, (const char **)argv) == RX_SUCCESS ? 0 : 1;
+}