Skip to content

Commit 155f4bf

Browse files
author
Felix "xq" Queißner
committed
Adds label support.
1 parent 0501c0f commit 155f4bf

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

build.zig

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub fn build(b: *std.Build) void {
1616
// .rtc = .dynamic,
1717
.mkfs = true,
1818
.exfat = true,
19+
.label = true,
1920
});
2021

2122
const zfat_mod = zfat_dep.module("zfat");

src/components/fs/FatFileSystem.zig

+26-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const fatfs = @import("zfat");
66

77
const block_size = 512;
88
const max_path_len = 8192; // this should be enough
9+
const max_label_len = 11; // see http://elm-chan.org/fsw/ff/doc/setlabel.html
910

1011
const FAT = @This();
1112

@@ -128,21 +129,35 @@ fn render(self: *FAT, stream: *dim.BinaryStream) dim.Content.RenderError!void {
128129
};
129130

130131
const ops = self.ops.items;
131-
if (ops.len > 0) {
132-
filesystem.mount("0:", true) catch |err| switch (err) {
133-
error.NotEnabled => @panic("bug in zfat"),
134-
error.DiskErr => return error.IoError,
135-
error.NotReady => @panic("bug in zfat disk wrapper"),
136-
error.InvalidDrive => @panic("bug in AtomicOps"),
137-
error.NoFilesystem => @panic("bug in zfat"),
138-
};
139132

140-
const wrapper = AtomicOps{};
133+
filesystem.mount("0:", true) catch |err| switch (err) {
134+
error.NotEnabled => @panic("bug in zfat"),
135+
error.DiskErr => return error.IoError,
136+
error.NotReady => @panic("bug in zfat disk wrapper"),
137+
error.InvalidDrive => @panic("bug in AtomicOps"),
138+
error.NoFilesystem => @panic("bug in zfat"),
139+
};
141140

142-
for (ops) |op| {
143-
try op.execute(wrapper);
141+
if (self.label) |label| {
142+
if (label.len <= max_label_len) {
143+
var label_buffer: [max_label_len + 3:0]u8 = undefined;
144+
const buf = std.fmt.bufPrintZ(&label_buffer, "0:{s}", .{label}) catch @panic("buffer too small");
145+
146+
_ = fatfs.api.setlabel(buf.ptr);
147+
} else {
148+
std.log.err("label \"{}\" is {} characters long, but only up to {} are permitted.", .{
149+
std.zig.fmtEscapes(label),
150+
label.len,
151+
max_label_len,
152+
});
144153
}
145154
}
155+
156+
const wrapper = AtomicOps{};
157+
158+
for (ops) |op| {
159+
try op.execute(wrapper);
160+
}
146161
}
147162

148163
const FatType = enum {

0 commit comments

Comments
 (0)