comments: containers Fat
This commit is contained in:
1 parent
9c096a6331
commit
a0a11af953
1 file changed
+3
-24
@@ -1,15 +1,7 @@
|
|||||||
--!strict
|
--!strict
|
||||||
-- containers/Fat: the FAT file system (FAT12 / FAT16 / FAT32) over a byte range: the BPB, the FAT chains, directories
|
-- containers/Fat: FAT12 / FAT16 / FAT32 over a byte range (BPB, chains, VFAT long names checksum-validated against
|
||||||
-- with VFAT long names (UCS-2, checksum-validated against their 8.3 entry) and the NT lower-case flags of 8.3 names,
|
-- their 8.3 entry, NT lower-case flags; an MBR's first FAT partition) and a FAT12 floppy writer. New code, from
|
||||||
-- 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
|
-- Microsoft's fatgen103 and the VFAT long-name layout.
|
||||||
-- 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
|
|
||||||
--
|
|
||||||
-- DOS dates are local time without a zone: they are read and written as UTC. Never throws; every chain is bounded by
|
-- 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).
|
-- the cluster count (loops are errors).
|
||||||
local Archive = require("@rosetta/containers/Archive")
|
local Archive = require("@rosetta/containers/Archive")
|
||||||
@@ -48,7 +40,6 @@ export type Entry = {
|
|||||||
shortName: string,
|
shortName: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ open
|
|
||||||
local function bpbAt(b: buffer, o: number, len: number): (Volume?, string?)
|
local function bpbAt(b: buffer, o: number, len: number): (Volume?, string?)
|
||||||
if len < 512 then
|
if len < 512 then
|
||||||
return nil, "truncated: smaller than a boot sector"
|
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"
|
return nil, if o.partition then "no partition " .. o.partition else "no FAT partition"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ chains
|
|
||||||
local function nextCluster(v: Volume, c: number): number
|
local function nextCluster(v: Volume, c: number): number
|
||||||
local f = v.base + v.fatOffset
|
local f = v.base + v.fatOffset
|
||||||
if v.bits == 12 then
|
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)
|
return bit32.band(readu32(v.b, p), 0x0FFFFFFF)
|
||||||
end
|
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 function chainOf(v: Volume, first: number, maxClusters: number): ({ number }?, string?)
|
||||||
local out = {}
|
local out = {}
|
||||||
local c = first
|
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
|
return v.base + v.dataOffset + (c - 2) * v.spc * v.bps
|
||||||
end
|
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 function readChain(v: Volume, first: number, size: number?): (buffer?, string?)
|
||||||
local csize = v.spc * v.bps
|
local csize = v.spc * v.bps
|
||||||
local maxClusters = if size then math.ceil(size / csize) + 1 else v.clusters
|
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
|
return out, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ directories
|
|
||||||
local function latin1(s: string): string
|
local function latin1(s: string): string
|
||||||
if not string.find(s, "[\128-\255]") then
|
if not string.find(s, "[\128-\255]") then
|
||||||
return s
|
return s
|
||||||
@@ -259,7 +246,6 @@ local function ucs2(b: buffer, o: number, n: number, out: { number })
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- the entries of one directory's bytes
|
|
||||||
local function parseDir(d: buffer, prefix: string, out: { Entry }): { Entry }
|
local function parseDir(d: buffer, prefix: string, out: { Entry }): { Entry }
|
||||||
local lfn: { [number]: { number } } = {}
|
local lfn: { [number]: { number } } = {}
|
||||||
local lfnSum, lfnCount = -1, 0
|
local lfnSum, lfnCount = -1, 0
|
||||||
@@ -349,7 +335,6 @@ local function parseDir(d: buffer, prefix: string, out: { Entry }): { Entry }
|
|||||||
return subdirs
|
return subdirs
|
||||||
end
|
end
|
||||||
|
|
||||||
-- list(vol, maxEntries?) -> every file and directory (depth first, directories before their contents)
|
|
||||||
function Fat.list(v: Volume, maxEntries: number?): ({ Entry }?, string?)
|
function Fat.list(v: Volume, maxEntries: number?): ({ Entry }?, string?)
|
||||||
local out: { Entry } = {}
|
local out: { Entry } = {}
|
||||||
local limit = maxEntries or 65536
|
local limit = maxEntries or 65536
|
||||||
@@ -403,7 +388,6 @@ function Fat.list(v: Volume, maxEntries: number?): ({ Entry }?, string?)
|
|||||||
return out, nil
|
return out, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- read(vol, entry) -> the file's bytes
|
|
||||||
function Fat.read(v: Volume, e: Entry): (buffer?, string?)
|
function Fat.read(v: Volume, e: Entry): (buffer?, string?)
|
||||||
if e.dir or e.size == 0 then
|
if e.dir or e.size == 0 then
|
||||||
return buffer.create(0), nil
|
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)
|
return readChain(v, e.cluster, e.size)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ write
|
|
||||||
export type WriteFile = { name: string, data: buffer?, dir: boolean?, mtime: number? }
|
export type WriteFile = { name: string, data: buffer?, dir: boolean?, mtime: number? }
|
||||||
export type WriteOptions = {
|
export type WriteOptions = {
|
||||||
size: number?, -- 360 / 720 / 1200 / 1440 / 2880 KB (default 1440)
|
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 }
|
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 function shortName(name: string, used: { [string]: boolean }): (string, number, boolean)
|
||||||
local base, ext = string.match(name, "^(.*)%.([^%.]*)$")
|
local base, ext = string.match(name, "^(.*)%.([^%.]*)$")
|
||||||
if not base or base == "" then
|
if not base or base == "" then
|
||||||
@@ -491,7 +473,6 @@ local function shortName(name: string, used: { [string]: boolean }): (string, nu
|
|||||||
return " ___", 0, true
|
return " ___", 0, true
|
||||||
end
|
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 function dirEntries(n: Node, parentCluster: number, isRoot: boolean, label: string?): buffer
|
||||||
local w = {}
|
local w = {}
|
||||||
local function entry(raw: string, attr: number, nt: number, cluster: number, size: number, mtime: number?)
|
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
|
return out
|
||||||
end
|
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?)
|
function Fat.write(files: { WriteFile }, opts: WriteOptions?): (buffer?, string?)
|
||||||
local o: WriteOptions = opts or {}
|
local o: WriteOptions = opts or {}
|
||||||
local g = GEOMETRY[o.size or 1440]
|
local g = GEOMETRY[o.size or 1440]
|
||||||
@@ -681,7 +661,6 @@ function Fat.write(files: { WriteFile }, opts: WriteOptions?): (buffer?, string?
|
|||||||
setFat(1, 0xFFF)
|
setFat(1, 0xFFF)
|
||||||
buffer.copy(img, bps, fat)
|
buffer.copy(img, bps, fat)
|
||||||
buffer.copy(img, (1 + g.fat) * bps, fat)
|
buffer.copy(img, (1 + g.fat) * bps, fat)
|
||||||
-- boot sector
|
|
||||||
buffer.writestring(img, 0, "\235\60\144" .. "FMTSLUAU")
|
buffer.writestring(img, 0, "\235\60\144" .. "FMTSLUAU")
|
||||||
buffer.writeu16(img, 11, bps)
|
buffer.writeu16(img, 11, bps)
|
||||||
buffer.writeu8(img, 13, g.spc)
|
buffer.writeu8(img, 13, g.spc)
|
||||||
|
|||||||
Reference in new issue
Block a user