Skip to content

Commit

Permalink
compatible with zig 0.10.0
Browse files Browse the repository at this point in the history
Signed-off-by: Tw <tw19881113@gmail.com>
  • Loading branch information
kassane authored and tw4452852 committed Nov 16, 2022
1 parent a4991ae commit daab69c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 62 deletions.
12 changes: 2 additions & 10 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,10 @@ jobs:
submodules: recursive
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.9.1
version: 0.10.0
- run: zig build -Dtarget=x86_64-linux-musl
- run: sudo `which zig` build test -Dtarget=x86_64-linux-musl
- uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: ./zig-out/bin/barn

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.9.1
files: ./zig-out/bin/barn
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ You could either download the [prebuilt binary](https://github.com/tw4452852/bar

### How to build

**Zig version:** 0.10.0

```
git clone https://github.com/tw4452852/barn
cd barn
Expand Down
28 changes: 22 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");

pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
Expand All @@ -11,16 +12,19 @@ pub fn build(b: *std.build.Builder) void {
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();

if (comptime !checkVersion())
@compileError("Old compiler!");

const exe = b.addExecutable("barn", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.linkLibC();
exe.addIncludeDir("vendor/include");
exe.addIncludeDir("vendor/libfuse/include");
exe.addIncludePath("vendor/include");
exe.addIncludePath("vendor/libfuse/include");

const libfuse = b.addStaticLibrary("libfuse", null);
libfuse.addIncludeDir("vendor/include");
libfuse.addIncludeDir("vendor/libfuse/include");
libfuse.addIncludePath("vendor/include");
libfuse.addIncludePath("vendor/libfuse/include");
libfuse.setTarget(target);
libfuse.setBuildMode(mode);
libfuse.linkLibC();
Expand Down Expand Up @@ -62,10 +66,22 @@ pub fn build(b: *std.build.Builder) void {
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
exe_tests.linkLibC();
exe_tests.addIncludeDir("vendor/include");
exe_tests.addIncludeDir("vendor/libfuse/include");
exe_tests.addIncludePath("vendor/include");
exe_tests.addIncludePath("vendor/libfuse/include");
exe_tests.linkLibrary(libfuse);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
}

// ziglings reference
fn checkVersion() bool {
if (!@hasDecl(builtin, "zig_version")) {
return false;
}

const needed_version = std.SemanticVersion.parse("0.10.0-dev.3685") catch unreachable;
const version = builtin.zig_version;
const order = version.order(needed_version);
return order != .lt;
}
43 changes: 22 additions & 21 deletions src/client.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");
const net = std.net;
const linux = os.linux;
const os = std.os;
Expand Down Expand Up @@ -63,9 +64,9 @@ fn usage(args: *c.fuse_args) void {
c.fuse_lib_help(args);
}

pub fn main(argv: [][*:0]u8, is_test: bool) !void {
pub fn main(argv: [][*:0]u8) !void {
defer log(.info, "client exit\n", .{});
const allocator = if (is_test) std.testing.allocator else std.heap.c_allocator;
const allocator = if (builtin.is_test) std.testing.allocator else std.heap.c_allocator;

c.fuse_set_log_func(fuse.fuse_log);

Expand Down Expand Up @@ -129,7 +130,7 @@ pub fn main(argv: [][*:0]u8, is_test: bool) !void {
var buf = try allocator.alloc(u8, fuse.bufsize);
defer allocator.free(buf);
const str = try std.fmt.bufPrintZ(buf, "/dev/fd/{}", .{try os.dup(socket.handle)});
var ret = c.fuse_mount(s, str);
var ret = c.fuse_mount(s, @ptrCast([*c]const u8, str));
if (ret != 0) {
log(.err, "client mount failed: {}\n", .{ret});
return;
Expand Down Expand Up @@ -163,7 +164,7 @@ pub fn main(argv: [][*:0]u8, is_test: bool) !void {

var off: usize = 0;
while (left_in_buff > 0) {
const header = @ptrCast(*fuse.ReqHeader, buf.ptr + off);
const header = @ptrCast(*fuse.ReqHeader, @alignCast(@alignOf(*fuse.ReqHeader), buf.ptr + off));
if (header.len > left_in_buff) {
std.mem.copy(u8, buf[0..left_in_buff], buf[off .. off + left_in_buff]);
break;
Expand Down Expand Up @@ -196,7 +197,7 @@ const FileInfo = extern struct {

fn pass_init(ci: [*c]c.fuse_conn_info, cfg: [*c]c.fuse_config) callconv(.C) ?*anyopaque {
cfg.*.kernel_cache = 1;
ci.*.want &= @bitReverse(c_uint, c.FUSE_CAP_SPLICE_READ);
ci.*.want &= @bitReverse(c.FUSE_CAP_SPLICE_READ);

return c.fuse_get_context().*.private_data;
}
Expand All @@ -206,18 +207,18 @@ fn pass_getattr(path: [*c]const u8, stbuf: [*c]c.struct_stat, _: ?*c.fuse_file_i
const real_path = std.fmt.allocPrintZ(ctx.allocator, "{s}{s}", .{ ctx.root, path }) catch unreachable;
defer ctx.allocator.free(real_path);

return @intCast(c_int, linux.lstat(real_path, @ptrCast(*os.Stat, stbuf)));
return @intCast(c_int, @bitCast(isize, linux.lstat(real_path, @ptrCast(*os.Stat, stbuf))));
}

fn pass_access(path: [*c]const u8, mask: c_int) callconv(.C) c_int {
const ctx = @ptrCast(*align(1) Ctx, c.fuse_get_context().*.private_data);
const real_path = std.fmt.allocPrintZ(ctx.allocator, "{s}/{s}", .{ ctx.root, path }) catch unreachable;
defer ctx.allocator.free(real_path);

return @intCast(c_int, linux.access(real_path, @intCast(c_uint, mask)));
return @intCast(c_int, @bitCast(isize, linux.access(real_path, @intCast(c_uint, mask))));
}

fn pass_readdir(path: [*c]const u8, buffer: ?*anyopaque, filler: ?fn (?*anyopaque, [*c]const u8, [*c]const c.struct_stat, c_long, c_uint) callconv(.C) c_int, offset: c_long, fi: ?*c.fuse_file_info, flags: c_uint) callconv(.C) c_int {
fn pass_readdir(path: [*c]const u8, buffer: ?*anyopaque, filler: ?*const fn (?*anyopaque, [*c]const u8, [*c]const c.struct_stat, c_long, c_uint) callconv(.C) c_int, offset: c_long, fi: ?*c.fuse_file_info, flags: c_uint) callconv(.C) c_int {
_ = offset;
_ = fi;
_ = flags;
Expand All @@ -226,7 +227,7 @@ fn pass_readdir(path: [*c]const u8, buffer: ?*anyopaque, filler: ?fn (?*anyopaqu
const real_path = std.fmt.allocPrintZ(ctx.allocator, "{s}/{s}", .{ ctx.root, path }) catch unreachable;
defer ctx.allocator.free(real_path);

const p = c.opendir(real_path);
const p = c.opendir(@ptrCast([*c]const u8, real_path));
if (p) |dp| {
defer _ = c.closedir(dp);

Expand All @@ -249,7 +250,7 @@ fn pass_open(path: [*c]const u8, fi: ?*c.fuse_file_info) callconv(.C) c_int {
const p = @ptrCast(*align(1) FileInfo, fi.?);
const ret = linux.open(real_path, @intCast(u32, p.flags), 0);
if (linux.getErrno(ret) != .SUCCESS) {
return @intCast(c_int, ret);
return @intCast(c_int, @bitCast(isize, ret));
}

p.fh = @intCast(u64, ret);
Expand All @@ -269,7 +270,7 @@ fn pass_read(_: [*c]const u8, buf: [*c]u8, size: usize, offset: c_long, fi: ?*c.

const read = linux.pread(fd, buf, want_size, offset);

return @intCast(c_int, read);
return @intCast(c_int, @bitCast(isize, read));
}

fn pass_release(_: [*c]const u8, fi: ?*c.fuse_file_info) callconv(.C) c_int {
Expand All @@ -286,7 +287,7 @@ fn pass_create(path: [*c]const u8, mode: c_uint, fi: ?*c.fuse_file_info) callcon
const p = @ptrCast(*align(1) FileInfo, fi.?);
const ret = linux.open(real_path, @intCast(u32, p.flags), mode);
if (linux.getErrno(ret) != .SUCCESS) {
return @intCast(c_int, ret);
return @intCast(c_int, @bitCast(isize, ret));
}

p.fh = @intCast(u64, ret);
Expand All @@ -299,38 +300,38 @@ fn pass_write(_: [*c]const u8, buf: [*c]const u8, size: usize, offset: c_long, f

const read = linux.pwrite(fd, buf, size, offset);

return @intCast(c_int, read);
return @intCast(c_int, @bitCast(isize, read));
}

fn pass_mkdir(path: [*c]const u8, mode: c_uint) callconv(.C) c_int {
const ctx = @ptrCast(*align(1) Ctx, c.fuse_get_context().*.private_data);
const real_path = std.fmt.allocPrintZ(ctx.allocator, "{s}/{s}", .{ ctx.root, path }) catch unreachable;
defer ctx.allocator.free(real_path);

return @intCast(c_int, linux.mkdir(real_path, mode));
return @intCast(c_int, @bitCast(isize, linux.mkdir(real_path, mode)));
}

fn pass_rmdir(path: [*c]const u8) callconv(.C) c_int {
const ctx = @ptrCast(*align(1) Ctx, c.fuse_get_context().*.private_data);
const real_path = std.fmt.allocPrintZ(ctx.allocator, "{s}/{s}", .{ ctx.root, path }) catch unreachable;
defer ctx.allocator.free(real_path);

return @intCast(c_int, linux.rmdir(real_path));
return @intCast(c_int, @bitCast(isize, linux.rmdir(real_path)));
}

fn pass_unlink(path: [*c]const u8) callconv(.C) c_int {
const ctx = @ptrCast(*align(1) Ctx, c.fuse_get_context().*.private_data);
const real_path = std.fmt.allocPrintZ(ctx.allocator, "{s}/{s}", .{ ctx.root, path }) catch unreachable;
defer ctx.allocator.free(real_path);

return @intCast(c_int, linux.unlink(real_path));
return @intCast(c_int, @bitCast(isize, linux.unlink(real_path)));
}

fn pass_lseek(_: [*c]const u8, offset: c_long, whence: c_int, fi: ?*c.fuse_file_info) callconv(.C) c_long {
const p = @ptrCast(*align(1) FileInfo, fi.?);
const fd = @intCast(os.fd_t, p.fh);

return @intCast(c_long, linux.lseek(fd, offset, @intCast(usize, whence)));
return @intCast(c_long, @bitCast(isize, linux.lseek(fd, offset, @intCast(usize, whence))));
}

fn pass_readlink(path: [*c]const u8, buf: [*c]u8, size: usize) callconv(.C) c_int {
Expand All @@ -340,7 +341,7 @@ fn pass_readlink(path: [*c]const u8, buf: [*c]u8, size: usize) callconv(.C) c_in

const ret = linux.readlink(real_path, buf, size - 1);
if (linux.getErrno(ret) != .SUCCESS) {
return @intCast(c_int, ret);
return @intCast(c_int, @bitCast(isize, ret));
}

buf[ret] = 0;
Expand All @@ -356,14 +357,14 @@ fn pass_truncate(path: [*c]const u8, size: c_long, fip: ?*c.fuse_file_info) call
const p = @ptrCast(*align(1) FileInfo, fi);
const fd = @intCast(os.fd_t, p.fh);

return @intCast(c_int, linux.ftruncate(fd, size));
return @intCast(c_int, @bitCast(isize, linux.ftruncate(fd, size)));
} else {
const ctx = @ptrCast(*align(1) Ctx, c.fuse_get_context().*.private_data);
const real_path = std.fmt.allocPrintZ(ctx.allocator, "{s}/{s}", .{ ctx.root, path }) catch unreachable;
defer ctx.allocator.free(real_path);

const f = fs.cwd().openFile(real_path, .{ .write = true }) catch unreachable;
const f = fs.cwd().openFile(real_path, .{ .mode = .write_only }) catch unreachable;
defer f.close();
return @intCast(c_int, linux.ftruncate(f.handle, size));
return @intCast(c_int, @bitCast(isize, linux.ftruncate(f.handle, size)));
}
}
Loading

0 comments on commit daab69c

Please sign in to comment.