Skip to content

Commit

Permalink
Vastly improve mounting in aisap-zig
Browse files Browse the repository at this point in the history
 * Go -> C bindings have been removed. Most parts of it have been ported to Zig and maintaining it was just slowing me down
 * build.zig `linkVendored` has been renamed to `link`
 * libfuse-dev (assuming Debian-based system) is no longer required to build
 * Mounting in aisap-zig still not ideal, the current solution is to daemonize
   libfuse in a thread and wait until the mount appears in `/dev/mounts`, but
   it actually works now
 * unmounting AppImages added for aisap-zig
 * Added `aisap_appimage_mount_dir` to C bindings
 * test.c file is no longer incredibly cluttered
  • Loading branch information
mgord9518 committed Aug 8, 2023
1 parent bab0c9f commit 94d807b
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 350 deletions.
2 changes: 1 addition & 1 deletion appimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type AppImage struct {

// Current version of aisap
const (
Version = "0.9.2-alpha"
Version = "0.9.3-alpha"
)

// Create a new AppImage object from a path
Expand Down
131 changes: 0 additions & 131 deletions cbindings/appimage.go

This file was deleted.

13 changes: 5 additions & 8 deletions include/aisap.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ typedef struct aisap_appimage {
const char* path;
size_t path_len;

// For Go implementation (Go does not allow C structs to store Go pointers,
// so an index into an array is used)
size_t _go_index;

// For Zig implemenation, points to Zig AppImage
void* _zig_parent;
} aisap_appimage ;
Expand Down Expand Up @@ -82,10 +78,10 @@ extern "C" {
// work in Zig.
// TODO: Make char get passed correctly. This may just be easiest to just
// make another AppImage run function that accepts char** instead of Go strings
extern void aisap_appimage_init_go(aisap_appimage* ai, const char* path, aisap_error* err);
extern void aisap_appimage_destroy_go(aisap_appimage* ai);
extern int aisap_appimage_run(aisap_appimage* ai, char** args);
extern int aisap_appimage_ismounted(aisap_appimage* ai);
//extern void aisap_appimage_init_go(aisap_appimage* ai, const char* path, aisap_error* err);
//extern void aisap_appimage_destroy_go(aisap_appimage* ai);
//extern int aisap_appimage_run(aisap_appimage* ai, char** args);
//extern int aisap_appimage_ismounted(aisap_appimage* ai);

// Zig-implemented C functions
// `aisap_appimage_new` initializes both the Zig and Go AppImage structs, so
Expand All @@ -103,6 +99,7 @@ extern aisap_bundle_type aisap_appimage_type(aisap_appimage* ai, aisap_error* er
extern size_t aisap_appimage_offset(aisap_appimage* ai, aisap_error* err);
extern const char* aisap_appimage_md5(aisap_appimage* ai, char* buf, size_t buf_len, aisap_error* err);
extern void aisap_appimage_mount(aisap_appimage* ai, char* path, aisap_error* err);
extern const char* aisap_appimage_mount_dir(aisap_appimage* ai);

// THESE FUNCTIONS NOT YET IMPLEMENTED
//extern uint8_t aisap_appimage_sandbox(aisap_appimage* ai, int argc, char** args);
Expand Down
19 changes: 2 additions & 17 deletions resources/build_libaisap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,7 @@ set -e

cd "$(dirname $0)"

echo "Building Go functions for libaisap"
cd ../cbindings
go mod tidy
CC="zig cc" go build -buildmode c-archive -o ../libaisap-x86_64.a

# Don't need this auto-generated header file
rm ../libaisap-x86_64.h

echo "Building Zig functions for libaisap"
cd ../zig
zig build -Doptimize=ReleaseSafe

# Extract both, then combine them into a single lib
zig ar -x ../libaisap-x86_64.a
zig ar -x zig-out/lib/libaisap.a
zig ar -qc ../libaisap-x86_64.a *.o
zig build #-Doptimize=ReleaseSafe

# Clean up
rm *.o
mv zig-out/lib/libaisap.a ../libaisap-x86_64.a
27 changes: 10 additions & 17 deletions zig/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ pub const LinkOptions = struct {
use_libdeflate: bool = true,
};

// TODO: maybe this should just be named `link`?
pub fn linkVendored(exe: *std.Build.Step.Compile, opts: LinkOptions) void {
pub fn link(exe: *std.Build.Step.Compile, opts: LinkOptions) void {
const prefix = thisDir();

exe.addIncludePath(.{ .path = prefix ++ "/../include" });

squashfuse.linkVendored(exe, .{
squashfuse.link(exe, .{
.enable_lz4 = opts.enable_lz4,
.enable_lzo = opts.enable_lzo,
.enable_zlib = opts.enable_zlib,
.enable_zstd = opts.enable_zstd,
.enable_xz = opts.enable_xz,

.enable_fuse = true,

.use_libdeflate = opts.use_libdeflate,
});
}
Expand Down Expand Up @@ -59,7 +60,7 @@ pub fn module(b: *std.Build) *std.Build.Module {
.source_file = .{ .path = prefix ++ "/known-folders/known-folders.zig" },
});

return b.addModule("aisap", .{
return b.createModule(.{
.source_file = .{ .path = prefix ++ "/lib.zig" },
.dependencies = &.{
.{
Expand Down Expand Up @@ -115,21 +116,13 @@ pub fn build(b: *std.Build) void {

lib.addIncludePath(.{ .path = prefix ++ "/../include" });

squashfuse.linkVendored(lib, .{
.enable_lz4 = true,
.enable_lzo = true,
.enable_zlib = true,
.enable_zstd = true,
.enable_xz = true,

.use_libdeflate = true,
});
link(lib, .{});

const known_folders_mod = b.addModule("known-folders", .{
const known_folders_module = b.addModule("known-folders", .{
.source_file = .{ .path = "known-folders/known-folders.zig" },
});

const squashfuse_mod = b.addModule("squashfuse", .{
const squashfuse_module = b.addModule("squashfuse", .{
.source_file = .{ .path = "squashfuse-zig/lib.zig" },
.dependencies = &.{
.{
Expand All @@ -139,8 +132,8 @@ pub fn build(b: *std.Build) void {
},
});

lib.addModule("squashfuse", squashfuse_mod);
lib.addModule("known-folders", known_folders_mod);
lib.addModule("squashfuse", squashfuse_module);
lib.addModule("known-folders", known_folders_module);

const pie = b.option(bool, "pie", "build as a PIE (position independent executable)") orelse true;
lib.pie = pie;
Expand Down
2 changes: 1 addition & 1 deletion zig/build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.{
.name = "aisap",
.version = "0.9.2",
.version = "0.9.3",
}
48 changes: 9 additions & 39 deletions zig/build_test.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
#!/bin/sh
# In order to build the test C program,
# `../resources/build_libaisap.sh` must first be run. Then, run this script,
# which will generate the `test` executable, which prints debug information
# about an AppImage

zig cc -static \
-o test test.c \
../libaisap-x86_64.a \
-lfuse3 \
-I../include \
# -I ../ \
# -I squashfuse-zig/zstd/lib \
# -DENABLE_ZLIB \
# -DUSE_LIBDEFLATE \
# -DENABLE_ZSTD
# squashfuse-zig/zstd/lib/decompress/zstd_decompress.c \
# squashfuse-zig/zstd/lib/decompress/zstd_decompress_block.c \
# squashfuse-zig/zstd/lib/decompress/zstd_ddict.c \
# squashfuse-zig/zstd/lib/decompress/huf_decompress.c \
# squashfuse-zig/zstd/lib/common/zstd_common.c \
# squashfuse-zig/zstd/lib/common/error_private.c \
# squashfuse-zig/zstd/lib/common/entropy_common.c \
# squashfuse-zig/zstd/lib/common/fse_decompress.c \
# squashfuse-zig/zstd/lib/common/xxhash.c \
# squashfuse-zig/zstd/lib/decompress/huf_decompress_amd64.S \
# \
# squashfuse-zig/libdeflate/lib/adler32.c \
# squashfuse-zig/libdeflate/lib/crc32.c \
# squashfuse-zig/libdeflate/lib/deflate_decompress.c \
# squashfuse-zig/libdeflate/lib/utils.c \
# squashfuse-zig/libdeflate/lib/zlib_decompress.c \
# squashfuse-zig/libdeflate/lib/x86/cpu_features.c \
# \
# squashfuse-zig/lz4/lib/lz4.c \
# \
# squashfuse-zig/squashfuse/fs.c \
# squashfuse-zig/squashfuse/table.c \
# squashfuse-zig/squashfuse/xattr.c \
# squashfuse-zig/squashfuse/cache.c \
# squashfuse-zig/squashfuse/dir.c \
# squashfuse-zig/squashfuse/file.c \
# squashfuse-zig/squashfuse/nonstd-makedev.c \
# squashfuse-zig/squashfuse/nonstd-pread.c \
# squashfuse-zig/squashfuse/nonstd-stat.c \
# squashfuse-zig/squashfuse/stat.c \
# squashfuse-zig/squashfuse/stack.c \
# squashfuse-zig/squashfuse/swap.c \
-I../include

# -lfuse3 \
2 changes: 1 addition & 1 deletion zig/examples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn build(b: *std.build.Builder) void {
const aisap_module = aisap.module(b);
exe.addModule("aisap", aisap_module);

aisap.linkVendored(exe, .{});
aisap.link(exe, .{});

b.installArtifact(exe);

Expand Down
Loading

0 comments on commit 94d807b

Please sign in to comment.