-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starts to implement exerciser application
- Loading branch information
Felix "xq" Queißner
committed
Oct 11, 2024
1 parent
6c9fcec
commit 0445749
Showing
5 changed files
with
213 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) void { | ||
const target = b.standardTargetOptions(.{}); | ||
const optimize = b.standardOptimizeOption(.{}); | ||
|
||
const zfat_dep = b.dependency("zfat", .{ | ||
.code_page = .us, | ||
.@"sector-size" = @as(u32, 512), | ||
.@"volume-count" = @as(u32, 5), | ||
// .@"volume-names" = @as([]const u8, "a,b,c,h,z"), // TODO(fqu): Requires VolToPart to be defined | ||
|
||
// Enable features: | ||
.find = true, | ||
.mkfs = true, | ||
.fastseek = true, | ||
.expand = true, | ||
.chmod = true, | ||
.label = true, | ||
.forward = true, | ||
.relative_path_api = .enabled_with_getcwd, | ||
// .multi_partition = true, // TODO(fqu): Requires VolToPart to be defined | ||
.lba64 = true, | ||
.use_trim = true, | ||
.exfat = true, | ||
}); | ||
const zfat_mod = zfat_dep.module("zfat"); | ||
|
||
const op_tester = b.addExecutable(.{ | ||
.name = "zfat-op-tester", | ||
.target = target, | ||
.optimize = optimize, | ||
.root_source_file = b.path("op_tester.zig"), | ||
}); | ||
op_tester.root_module.addImport("zfat", zfat_mod); | ||
op_tester.linkLibC(); | ||
|
||
b.installArtifact(op_tester); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.{ | ||
.name = "zfat-tests", | ||
.version = "0.1.0", | ||
.paths = .{ | ||
"op_tester.zig", | ||
"build.zig", | ||
"build.zig.zon", | ||
}, | ||
.dependencies = .{ | ||
.zfat = .{ | ||
.path = "..", | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
//! | ||
//! This file implements several tests that try to exercise | ||
//! and perform all possible operations of the zfat library. | ||
//! | ||
const std = @import("std"); | ||
const zfat = @import("zfat"); | ||
|
||
// requires pointer stability | ||
var global_fs: zfat.FileSystem = undefined; | ||
|
||
// requires pointer stability | ||
var ramdisks: [5]RamDisk = .{.{}} ** 5; | ||
|
||
pub const std_options = std.Options{ | ||
.log_level = .info, | ||
}; | ||
|
||
pub fn main() !void { | ||
try ramdisks[0].init(100_000); | ||
|
||
zfat.disks[0] = &ramdisks[0].interface; | ||
|
||
// try mkfs with all formats: | ||
{ | ||
const formats = [_]zfat.DiskFormat{ .any, .fat, .exfat, .fat32 }; | ||
var workspace: [4096]u8 = undefined; | ||
|
||
for (formats) |target_fmt| { | ||
std.log.info("format disk with {s}...", .{@tagName(target_fmt)}); | ||
try zfat.mkfs("0:", .{ | ||
.filesystem = target_fmt, | ||
.sector_align = 1, | ||
.use_partitions = true, | ||
}, &workspace); | ||
} | ||
} | ||
|
||
std.log.info("mount disk...", .{}); | ||
try global_fs.mount("0:", true); | ||
defer zfat.FileSystem.unmount("0:") catch |e| std.log.err("failed to unmount filesystem: {s}", .{@errorName(e)}); | ||
|
||
// mkdir | ||
// unlink | ||
// rename | ||
// stat | ||
// chmod | ||
// utime | ||
// chdir | ||
// chdrive | ||
// getcwd | ||
|
||
// Dir.open | ||
// Dir.close | ||
// Dir.next | ||
// Dir.rewind | ||
|
||
// File.open | ||
// File.create | ||
// File.openRead | ||
// File.openWrite | ||
// File.close | ||
// File.sync | ||
// File.truncate | ||
// File.seekTo | ||
// File.expand | ||
// File.endOfFile | ||
// File.hasError | ||
// File.tell | ||
// File.size | ||
// File.rewind | ||
// File.write | ||
// File.read | ||
|
||
// api.fdisk | ||
// api.getfree | ||
// api.getlabel | ||
// api.setlabel | ||
// api.setcp | ||
} | ||
|
||
pub const RamDisk = struct { | ||
const sector_size = 512; | ||
|
||
interface: zfat.Disk = .{ | ||
.getStatusFn = getStatus, | ||
.initializeFn = initialize, | ||
.readFn = read, | ||
.writeFn = write, | ||
.ioctlFn = ioctl, | ||
}, | ||
sectors: [][sector_size]u8 = &.{}, | ||
|
||
pub fn init(rd: *RamDisk, sector_count: usize) !void { | ||
rd.* = .{}; | ||
rd.sectors = try std.heap.page_allocator.alloc([sector_size]u8, sector_count); | ||
} | ||
|
||
pub fn deinit(rd: *RamDisk) void { | ||
if (rd.sectors.len > 0) { | ||
std.heap.page_allocator.free(rd.sectors); | ||
} | ||
rd.sectors = &.{}; | ||
} | ||
|
||
pub fn getStatus(interface: *zfat.Disk) zfat.Disk.Status { | ||
const self: *RamDisk = @fieldParentPtr("interface", interface); | ||
return zfat.Disk.Status{ | ||
.initialized = (self.sectors.len > 0), | ||
.disk_present = true, | ||
.write_protected = false, | ||
}; | ||
} | ||
|
||
pub fn initialize(interface: *zfat.Disk) zfat.Disk.Error!zfat.Disk.Status { | ||
const self: *RamDisk = @fieldParentPtr("interface", interface); | ||
return getStatus(&self.interface); | ||
} | ||
|
||
pub fn read(interface: *zfat.Disk, buff: [*]u8, sector: zfat.LBA, count: c_uint) zfat.Disk.Error!void { | ||
const self: *RamDisk = @fieldParentPtr("interface", interface); | ||
|
||
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; | ||
} | ||
|
||
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; | ||
} | ||
|
||
pub fn ioctl(interface: *zfat.Disk, cmd: zfat.IoCtl, buff: [*]u8) zfat.Disk.Error!void { | ||
const self: *RamDisk = @fieldParentPtr("interface", interface); | ||
|
||
switch (cmd) { | ||
.sync => {}, | ||
.get_sector_count => { | ||
@as(*align(1) zfat.LBA, @ptrCast(buff)).* = @intCast(self.sectors.len); | ||
}, | ||
.trim => {}, | ||
else => { | ||
std.log.err("invalid ioctl: {}", .{cmd}); | ||
return error.InvalidParameter; | ||
}, | ||
} | ||
} | ||
}; |