From a0a11af9531158b8e450d3a974022ecbe1120739 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sun, 1 Nov 2026 01:52:49 -0500 Subject: [PATCH] comments: containers Fat --- src/Rosetta/containers/Fat.luau | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/src/Rosetta/containers/Fat.luau b/src/Rosetta/containers/Fat.luau index 3c2ab21..792ed44 100644 --- a/src/Rosetta/containers/Fat.luau +++ b/src/Rosetta/containers/Fat.luau @@ -1,15 +1,7 @@ --!strict --- containers/Fat: the FAT file system (FAT12 / FAT16 / FAT32) over a byte range: the BPB, the FAT chains, directories --- with VFAT long names (UCS-2, checksum-validated against their 8.3 entry) and the NT lower-case flags of 8.3 names, --- and a FAT12 floppy writer (1.44 MB / 720 KB / 2.88 MB, long names when a name is not a plain 8.3 name). Disk images --- with an MBR partition table open their first FAT partition (or the one asked for). New code, written from --- Microsoft's "FAT: General Overview of On-Disk Format" (fatgen103) and the VFAT long-name layout. --- --- local vol, err = Fat.open(buf, offset, length, { partition = 1 }) -- Volume | nil, message --- local list, err = Fat.list(vol, maxEntries) -- { Entry } (paths with "/", dirs end in "/") --- local data, err = Fat.read(vol, entry) -- buffer | nil, message --- local img, err = Fat.write(files, { size = 1440, label = "DISK" }) -- FAT12 floppy image --- +-- containers/Fat: FAT12 / FAT16 / FAT32 over a byte range (BPB, chains, VFAT long names checksum-validated against +-- their 8.3 entry, NT lower-case flags; an MBR's first FAT partition) and a FAT12 floppy writer. New code, from +-- Microsoft's fatgen103 and the VFAT long-name layout. -- DOS dates are local time without a zone: they are read and written as UTC. Never throws; every chain is bounded by -- the cluster count (loops are errors). local Archive = require("@rosetta/containers/Archive") @@ -48,7 +40,6 @@ export type Entry = { shortName: string, } --- ------------------------------------------------------------------------------------------------ open local function bpbAt(b: buffer, o: number, len: number): (Volume?, string?) if len < 512 then return nil, "truncated: smaller than a boot sector" @@ -154,7 +145,6 @@ function Fat.open(b: buffer, off: number, len: number, opts: OpenOptions?): (Vol return nil, if o.partition then "no partition " .. o.partition else "no FAT partition" end --- ------------------------------------------------------------------------------------------------ chains local function nextCluster(v: Volume, c: number): number local f = v.base + v.fatOffset if v.bits == 12 then @@ -180,7 +170,6 @@ local function nextCluster(v: Volume, c: number): number return bit32.band(readu32(v.b, p), 0x0FFFFFFF) end --- the clusters of a chain (nil, message when it leaves the volume or loops) local function chainOf(v: Volume, first: number, maxClusters: number): ({ number }?, string?) local out = {} local c = first @@ -204,7 +193,6 @@ local function clusterOffset(v: Volume, c: number): number return v.base + v.dataOffset + (c - 2) * v.spc * v.bps end --- the bytes of a chain, up to size bytes (nil = the whole chain) local function readChain(v: Volume, first: number, size: number?): (buffer?, string?) local csize = v.spc * v.bps local maxClusters = if size then math.ceil(size / csize) + 1 else v.clusters @@ -233,7 +221,6 @@ local function readChain(v: Volume, first: number, size: number?): (buffer?, str return out, nil end --- ------------------------------------------------------------------------------------------------ directories local function latin1(s: string): string if not string.find(s, "[\128-\255]") then return s @@ -259,7 +246,6 @@ local function ucs2(b: buffer, o: number, n: number, out: { number }) end end --- the entries of one directory's bytes local function parseDir(d: buffer, prefix: string, out: { Entry }): { Entry } local lfn: { [number]: { number } } = {} local lfnSum, lfnCount = -1, 0 @@ -349,7 +335,6 @@ local function parseDir(d: buffer, prefix: string, out: { Entry }): { Entry } return subdirs end --- list(vol, maxEntries?) -> every file and directory (depth first, directories before their contents) function Fat.list(v: Volume, maxEntries: number?): ({ Entry }?, string?) local out: { Entry } = {} local limit = maxEntries or 65536 @@ -403,7 +388,6 @@ function Fat.list(v: Volume, maxEntries: number?): ({ Entry }?, string?) return out, nil end --- read(vol, entry) -> the file's bytes function Fat.read(v: Volume, e: Entry): (buffer?, string?) if e.dir or e.size == 0 then return buffer.create(0), nil @@ -414,7 +398,6 @@ function Fat.read(v: Volume, e: Entry): (buffer?, string?) return readChain(v, e.cluster, e.size) end --- ------------------------------------------------------------------------------------------------ write export type WriteFile = { name: string, data: buffer?, dir: boolean?, mtime: number? } export type WriteOptions = { size: number?, -- 360 / 720 / 1200 / 1440 / 2880 KB (default 1440) @@ -439,7 +422,6 @@ end type Node = { name: string, data: buffer?, dir: boolean, mtime: number?, children: { Node }, byName: { [string]: Node }, cluster: number } --- the 11-byte short name and NT flags for a long name, unique within used; and whether a long name entry is needed local function shortName(name: string, used: { [string]: boolean }): (string, number, boolean) local base, ext = string.match(name, "^(.*)%.([^%.]*)$") if not base or base == "" then @@ -491,7 +473,6 @@ local function shortName(name: string, used: { [string]: boolean }): (string, nu return " ___", 0, true end --- the directory entries (32 bytes each) of a node's children local function dirEntries(n: Node, parentCluster: number, isRoot: boolean, label: string?): buffer local w = {} local function entry(raw: string, attr: number, nt: number, cluster: number, size: number, mtime: number?) @@ -556,7 +537,6 @@ local function dirEntries(n: Node, parentCluster: number, isRoot: boolean, label return out end --- write(files, opts?) -> a FAT12 floppy image holding the files (directories created from the paths) function Fat.write(files: { WriteFile }, opts: WriteOptions?): (buffer?, string?) local o: WriteOptions = opts or {} local g = GEOMETRY[o.size or 1440] @@ -681,7 +661,6 @@ function Fat.write(files: { WriteFile }, opts: WriteOptions?): (buffer?, string? setFat(1, 0xFFF) buffer.copy(img, bps, fat) buffer.copy(img, (1 + g.fat) * bps, fat) - -- boot sector buffer.writestring(img, 0, "\235\60\144" .. "FMTSLUAU") buffer.writeu16(img, 11, bps) buffer.writeu8(img, 13, g.spc)