Skip to content

Commit

Permalink
Fix bug that caused data dir to not be created by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mgord9518 committed Jun 18, 2024
1 parent 783b7e5 commit cfbeee8
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 57 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v2
- uses: actions/setup-go@v4
- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.12.0

- name: Install deps
run: |
Expand Down
13 changes: 4 additions & 9 deletions permissions/perms.go → permissions/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,12 @@ func FromIni(e *ini.File) (*AppImagePerms, error) {
devicePerms := e.Section("X-App Permissions").Key("Devices").Value()
socketPerms := e.Section("X-App Permissions").Key("Sockets").Value()

// Enable saving to a data dir by default. If NoDataDir is true, the AppImage
// HOME dir will be in RAM and non-persistent.
if e.Section("X-App Permissions").Key("NoDataDir").Value() == "true" {
p.DataDir = false
}

// Phasing out negative bools, I will eventually replace `NoDataDir`
// in favor of `DataDir`
// Enable saving to a data dir by default
if e.Section("X-App Permissions").Key("DataDir").Value() == "false" {
p.DataDir = false
} else {
p.DataDir = true
}
}

l, err := strconv.Atoi(level)
if err != nil || l < 0 || l > 3 {
Expand Down
18 changes: 10 additions & 8 deletions zig/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,15 @@ pub fn build(b: *std.Build) void {
// These options will be renamed in the future
.@"enable-fuse" = true,
.@"enable-zlib" = true,
.@"use-zig-zlib" = true,
//.@"use-libdeflate" = true,
.@"enable-xz" = false,
.@"enable-lzma" = false,
//.@"use-zig-zlib" = true,
.@"use-libdeflate" = true,
.@"enable-xz" = true,
.@"enable-lzma" = true,
.@"enable-lzo" = false,
.@"enable-lz4" = false,
.@"enable-zstd" = false,
.@"enable-lz4" = true,
.@"enable-zstd" = true,
});

//lib.linkLibrary(squashfuse_dep.artifact("deflate"));

const fuse_dep = b.dependency("fuse", .{
.target = target,
.optimize = optimize,
Expand All @@ -104,6 +102,10 @@ pub fn build(b: *std.Build) void {
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);
b.installArtifact(squashfuse_dep.artifact("deflate"));
b.installArtifact(squashfuse_dep.artifact("zstd"));
b.installArtifact(squashfuse_dep.artifact("lz4"));
b.installArtifact(fuse_dep.artifact("fuse"));

// Creates a step for unit testing. This only builds the test executable
// but does not run it.
Expand Down
4 changes: 2 additions & 2 deletions zig/build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = "aisap",
.version = "0.9.9-alpha",
.version = "0.9.10-alpha",
.paths = [][]const u8 {""},
.dependencies = .{
.squashfuse = .{
Expand All @@ -9,7 +9,7 @@
},
.fuse = .{
.url = "https://github.com/mgord9518/libfuse-zig/archive/refs/tags/continuous.tar.gz",
.hash = "12200be40def11e7699ec3db554818fd00e19fa331d45f762c2a89a46981ded91079",
.hash = "1220c5f5640bdf55d07c1cd67986a39df1c41fff641f58e975a7cf6a470ae5b2461b",
},
},
}
12 changes: 8 additions & 4 deletions zig/examples/build.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const aisap = @import("aisap/zig/build.zig");

pub fn build(b: *std.build.Builder) void {
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
Expand All @@ -20,10 +20,14 @@ pub fn build(b: *std.build.Builder) void {
});

// TODO: find if there's some kind of convention here and follow it if so
const aisap_module = aisap.module(b);
exe.addModule("aisap", aisap_module);
//const aisap_module = aisap.module(b);
//exe.addModule("aisap", aisap_module);

aisap.link(exe, .{});
exe.root_module.addAnonymousImport("aisap", .{
.root_source_file = .{ .path = "../lib.zig" },
});

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

b.installArtifact(exe);

Expand Down
7 changes: 5 additions & 2 deletions zig/examples/build_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
zig cc -static \
-o test test.c \
../zig-out/lib/libaisap.a \
-I../../include \
-lfuse3
../zig-out/lib/libzstd.a \
../zig-out/lib/libdeflate.a \
../zig-out/lib/liblz4.a \
../zig-out/lib/libfuse.a \
-I../../include
2 changes: 1 addition & 1 deletion zig/examples/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn printSockets(slice: []AppImage.SocketPermissions) void {
}

pub fn main() !void {
var allocator = std.heap.page_allocator;
const allocator = std.heap.page_allocator;
var args = std.process.args();

// Skip arg[0]
Expand Down
36 changes: 20 additions & 16 deletions zig/examples/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main(int argc, char** argv) {
const char* md5_string = aisap_appimage_md5(&ai, buf, sizeof(buf), &err);

if (err) {
printf("%d\n", err);
printf("md5 err%d\n", err);
return err;
}

Expand All @@ -47,24 +47,28 @@ int main(int argc, char** argv) {

printf(" mount_dir: %s\n", aisap_appimage_mount_dir(&ai));

char** wrap_args = aisap_appimage_wrapargs(&ai, &err);
if (err) {
printf("%d\n", err);
return err;
if (false) {
char** wrap_args = aisap_appimage_wrapargs(&ai, &err);
if (err) {
printf("wrapargs err %d\n", err);
return err;
}

printf(" wrapargs:\n");
char** i = wrap_args;
for (char* str = *i; str; str = *++i) {
printf("%s ", str);
}
printf("\n");
}

printf(" wrapargs:\n");
char** i = wrap_args;
for (char* str = *i; str; str = *++i) {
printf("%s ", str);
}
printf("\n");
//getchar();

// aisap_appimage_sandbox(&ai, argc - 2, argv + 2, &err);
// if (err) {
// printf("%d\n", err);
// return err;
// }
aisap_appimage_sandbox(&ai, argc - 2, argv + 2, &err);
if (err) {
printf("sandbox error %d\n", err);
return err;
}

// Test libappimage API:
printf("libappimage API:\n");
Expand Down
51 changes: 44 additions & 7 deletions zig/lib/appimage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const mem = std.mem;
const span = std.mem.span;
const expect = std.testing.expect;
const os = std.os;
const posix = std.posix;
const ChildProcess = std.ChildProcess;

const Md5 = std.crypto.hash.Md5;
Expand Down Expand Up @@ -699,7 +700,7 @@ pub const AppImage = struct {
};
defer perms.deinit();

if (perms.level == 0) return WrapArgsError.SandboxLevelTooLow;
//if (perms.level == 0) return WrapArgsError.SandboxLevelTooLow;

const home = try known_folders.getPath(allocator, .home) orelse {
return WrapArgsError.HomeNotFound;
Expand Down Expand Up @@ -1138,16 +1139,47 @@ pub const AppImage = struct {
if (opts.foreground) {
try fuse_helper.mountImage(ai.path, ai.mount_dir.?, off);
} else {
_ = try std.Thread.spawn(
.{},
fuse_helper.mountImage,
.{ ai.path, ai.mount_dir.?, off },
);
const pid = try posix.fork();
if (pid == 0) {
// _ = try std.Thread.spawn(
// .{},
// fuse_helper.mountImage,
// .{ ai.path, ai.mount_dir.?, off },
// );

if (true) {
try fuse_helper.mountImage(
ai.path,
ai.mount_dir.?,
off,
);
} else {
const offset_string = try std.fmt.allocPrint(
ai.allocator,
"-ooffset={d}",
.{off},
);
defer ai.allocator.free(offset_string);

var proc = ChildProcess.init(&[_][]const u8{
"squashfuse",
offset_string,
ai.path,
ai.mount_dir.?,
}, ai.allocator);

_ = try proc.spawnAndWait();
}

std.debug.print("UNMOUNT\n\n", .{});

posix.exit(0);
}

// Values in nanoseconds
var waited: usize = 0;
const wait_step = 1000;
const wait_max = 500_000;
const wait_max = 500_0000;

var buf: [4096]u8 = undefined;

Expand Down Expand Up @@ -1214,6 +1246,11 @@ pub const AppImage = struct {

const wrap_args = try ai.wrapArgs(arena_allocator);

std.debug.print("DEBUG {s} {s}", .{
@src().fn_name,
wrap_args,
});

if (opts.args) |args| {
var list = std.ArrayList([]const u8).init(arena_allocator);
defer list.deinit();
Expand Down
29 changes: 22 additions & 7 deletions zig/lib/c_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,12 @@ export fn aisap_appimage_md5(
// `aisap_appimage_sandbox` should be preferred
// Returned memory must be freed
export fn aisap_appimage_wrapargs(
ai: *c.aisap_appimage,
c_ai: *c.aisap_appimage,
err: *CAppImageError,
) [*:null]?[*:0]const u8 {
return getParent(ai).wrapArgsZ(std.heap.c_allocator) catch {
const ai = getParent(c_ai);

return ai.wrapArgsZ(ai.allocator) catch {
err.* = .err;
return undefined;
};
Expand All @@ -155,7 +157,9 @@ export fn aisap_appimage_sandbox(
var zig_ai = getParent(ai);

if (argc > 0) {
var args = zig_ai.allocator.alloc([]const u8, argc) catch {
var args = zig_ai.allocator.alloc([]const u8, argc) catch |e| {
std.debug.print("{s} ERR: {!}\n", .{ @src().fn_name, e });

err.* = .err;
return;
};
Expand All @@ -177,14 +181,16 @@ export fn aisap_appimage_sandbox(

zig_ai.sandbox(.{
.args = args,
}) catch {
}) catch |e| {
std.debug.print("{s} ERR: {!}\n", .{ @src().fn_name, e });
err.* = .err;
};

return;
}

zig_ai.sandbox(.{}) catch {
zig_ai.sandbox(.{}) catch |e| {
std.debug.print("{s} ERR: {!}\n", .{ @src().fn_name, e });
err.* = .err;
};
}
Expand Down Expand Up @@ -214,8 +220,17 @@ export fn aisap_appimage_offset(ai: *c.aisap_appimage, errno: *CAppImageError) u
/// This function allocates memory on the heap, the caller is responsible
/// for freeing it
export fn appimage_get_md5(path: [*:0]const u8) [*:0]const u8 {
const buf = std.heap.page_allocator.alloc(u8, Md5.digest_length * 2 + 1) catch unreachable;
return (aisap.md5FromPath(std.mem.span(path), buf) catch unreachable).ptr;
const allocator = std.heap.c_allocator;

const buf = allocator.alloc(
u8,
Md5.digest_length * 2 + 1,
) catch unreachable;

return (aisap.md5FromPath(
std.mem.span(path),
buf,
) catch unreachable).ptr;
}

export fn appimage_get_payload_offset(path: [*:0]const u8) std.posix.off_t {
Expand Down

0 comments on commit cfbeee8

Please sign in to comment.