Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
uses: actions/checkout@v2

- name: Setup Zig
uses: mlugg/setup-zig@v1
uses: mlugg/setup-zig@v2
with:
version: 0.15.2
version: 0.17.0

- name: Build Demo
run: |
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.zig-cache/
zig-out/
disk.img
disk.img
zig-pkg
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ pub fn build(b: *std.Build) void {
.c_source_file = b.path("fatfs.h"),
.target = target,
.optimize = optimize,
.default_init = true,
});
t.mod.addCSourceFiles(.{
.root = upstream_copy_dir,
Expand Down
7 changes: 4 additions & 3 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
.name = .zfat,
.fingerprint = 0x4212c7b5f54ad348,
.version = "0.0.1",
.minimum_zig_version = "0.17.0-dev.1471+ff10b90bc",
.minimum_zig_version = "0.17.0",
.dependencies = .{
.translate_c = .{
.url = "git+https://codeberg.org/ziglang/translate-c#2a6613bcc09c74296d785e660fe98d2eb310d760",
.hash = "translate_c-0.0.0-Q_BUWjQ7BwAhlMt3Zf4l3Jw-y0_TOpJWgcK8rk4d0aWv",
.url = "git+https://codeberg.org/ziglang/translate-c?ref=zig-0.17.x#02ff0c523fb92a38939b04d0145c4a9602a74d56",
.hash = "translate_c-2.0.0-Q_BUWltOBwBYABF84EUL208pIFeUIwbBs29FmVBaafN5",
},
},
.paths = .{
Expand All @@ -15,5 +15,6 @@
"src",
"test",
"vendor",
"fatfs.h",
},
}
10 changes: 6 additions & 4 deletions test/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ pub fn build(b: *std.Build) void {

const op_tester = b.addExecutable(.{
.name = "zfat-op-tester",
.target = target,
.optimize = optimize,
.root_source_file = b.path("op_tester.zig"),
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.root_source_file = b.path("op_tester.zig"),
.link_libc = true,
}),
});
op_tester.root_module.addImport("zfat", zfat_mod);
op_tester.linkLibC();

b.installArtifact(op_tester);
}
36 changes: 26 additions & 10 deletions test/op_tester.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const zfat = @import("zfat");
var global_fs: [5]zfat.FileSystem = undefined;

// requires pointer stability
var ramdisks = [1]RamDisk{.{}} ** 5;
var ramdisks: [5]RamDisk = @splat(.{});

pub const std_options = std.Options{
pub const std_options: std.Options = .{
.log_level = .info,
};

Expand Down Expand Up @@ -242,7 +242,11 @@ fn writeFile(path: zfat.Path, contents: []const u8) !void {
var file = try zfat.File.create(path);
defer file.close();

try file.writer().writeAll(contents);
var buff: [1024]u8 = undefined;
var file_writer = file.writer(&buff);

try file_writer.writer.writeAll(contents);
try file_writer.writer.flush();
}

pub const RamDisk = struct {
Expand All @@ -257,6 +261,10 @@ pub const RamDisk = struct {
},
sectors: [][sector_size]u8 = &.{},

fn sectorsBytes(self: *RamDisk) []u8 {
return std.mem.sliceAsBytes(self.sectors);
}

pub fn init(rd: *RamDisk, sector_count: usize) !void {
rd.* = .{};
rd.sectors = try std.heap.page_allocator.alloc([sector_size]u8, sector_count);
Expand All @@ -271,7 +279,7 @@ pub const RamDisk = struct {

pub fn getStatus(interface: *zfat.Disk) zfat.Disk.Status {
const self: *RamDisk = @fieldParentPtr("interface", interface);
return zfat.Disk.Status{
return .{
.initialized = (self.sectors.len > 0),
.disk_present = true,
.write_protected = false,
Expand All @@ -288,19 +296,27 @@ pub const RamDisk = struct {

std.log.debug("read({*}, {}, {})", .{ buff, sector, count });

var sectors = std.io.fixedBufferStream(std.mem.sliceAsBytes(self.sectors));
sectors.seekTo(sector * sector_size) catch return error.IoError;
sectors.reader().readNoEof(buff[0 .. sector_size * count]) catch return error.IoError;
const bytes = sector_size * count;
const start = sector * sector_size;
const disk = self.sectorsBytes();
if (start > disk.len) return error.IoError;

var sectors = std.Io.Reader.fixed(disk[start..]);
sectors.readSliceAll(buff[0..bytes]) catch return error.IoError;
}

pub fn write(interface: *zfat.Disk, buff: [*]const u8, sector: zfat.LBA, count: c_uint) zfat.Disk.Error!void {
const self: *RamDisk = @fieldParentPtr("interface", interface);

std.log.debug("write({*}, {}, {})", .{ buff, sector, count });

var sectors = std.io.fixedBufferStream(std.mem.sliceAsBytes(self.sectors));
sectors.seekTo(sector * sector_size) catch return error.IoError;
sectors.writer().writeAll(buff[0 .. sector_size * count]) catch return error.IoError;
const bytes = sector_size * count;
const start = sector * sector_size;
const disk = self.sectorsBytes();
if (start > disk.len) return error.IoError;

var sectors = std.Io.Writer.fixed(disk[start..]);
sectors.writeAll(buff[0..bytes]) catch return error.IoError;
}

pub fn ioctl(interface: *zfat.Disk, cmd: zfat.IoCtl, buff: [*]u8) zfat.Disk.Error!void {
Expand Down
Loading