osbase/fat32: adopt structfs
Change the external interface of the FAT32 writer to take a
structfs.Tree instead of a FAT32-specific data structure. Producers of
file system data are no longer specific to FAT32.
With these changes, the blkio package becomes obsolete. The
LazyFileReader did not actually work as intended when used with
osbase/fat32, because fat32 copies data with io.CopyN and thus stops
reading before reaching EOF, so the LazyFileReader is never closed. The
new Blob interface requires the consumer to explicitly Open and Close.
Change-Id: I9a71a5f0bddf36ac38c656659e6dcfe520b88fb0
Reviewed-on: https://review.monogon.dev/c/monogon/+/4037
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
Tested-by: Jenkins CI
diff --git a/osbase/blkio/BUILD.bazel b/osbase/blkio/BUILD.bazel
deleted file mode 100644
index 8b23a34..0000000
--- a/osbase/blkio/BUILD.bazel
+++ /dev/null
@@ -1,8 +0,0 @@
-load("@io_bazel_rules_go//go:def.bzl", "go_library")
-
-go_library(
- name = "blkio",
- srcs = ["blkio.go"],
- importpath = "source.monogon.dev/osbase/blkio",
- visibility = ["//visibility:public"],
-)
diff --git a/osbase/blkio/blkio.go b/osbase/blkio/blkio.go
deleted file mode 100644
index a4669ab..0000000
--- a/osbase/blkio/blkio.go
+++ /dev/null
@@ -1,86 +0,0 @@
-// Copyright The Monogon Project Authors.
-// SPDX-License-Identifier: Apache-2.0
-
-package blkio
-
-import (
- "fmt"
- "io"
- "os"
-)
-
-type ReaderWithSize struct {
- io.Reader
- size int64
-}
-
-// SizedReader is an io.Reader with a known size
-type SizedReader interface {
- io.Reader
- Size() int64
-}
-
-// NewSizedReader returns a SizedReader given a reader and a size.
-// The returned SizedReader is a ReaderWithSize.
-func NewSizedReader(r io.Reader, size int64) SizedReader {
- return &ReaderWithSize{r, size}
-}
-
-func (r *ReaderWithSize) Size() int64 {
- return r.size
-}
-
-// LazyFileReader implements a SizedReader which opens a file on first read
-// and closes it again after the reader has reached EOF.
-type LazyFileReader struct {
- name string
- size int64
- f *os.File
- done bool
-}
-
-func (r *LazyFileReader) init() error {
- f, err := os.Open(r.name)
- if err != nil {
- return fmt.Errorf("failed to open file for reading: %w", err)
- }
- r.f = f
- return nil
-}
-
-func (r *LazyFileReader) Size() int64 {
- return r.size
-}
-
-func (r *LazyFileReader) Read(b []byte) (n int, err error) {
- if r.done {
- return 0, io.EOF
- }
- if r.f == nil {
- if err = r.init(); err != nil {
- return
- }
- }
- n, err = r.f.Read(b)
- if err == io.EOF {
- r.done = true
- r.f.Close()
- }
- return
-}
-
-func (r *LazyFileReader) Close() {
- r.done = true
- r.f.Close()
-}
-
-func NewFileReader(name string) (*LazyFileReader, error) {
- info, err := os.Stat(name)
- if err != nil {
- return nil, fmt.Errorf("failed to stat: %w", err)
- }
- return &LazyFileReader{
- size: info.Size(),
- name: name,
- }, nil
-}
diff --git a/osbase/build/mkimage/BUILD.bazel b/osbase/build/mkimage/BUILD.bazel
index a822c62..aba561e 100644
--- a/osbase/build/mkimage/BUILD.bazel
+++ b/osbase/build/mkimage/BUILD.bazel
@@ -6,9 +6,9 @@
importpath = "source.monogon.dev/osbase/build/mkimage",
visibility = ["//visibility:private"],
deps = [
- "//osbase/blkio",
"//osbase/blockdev",
"//osbase/build/mkimage/osimage",
+ "//osbase/structfs",
],
)
diff --git a/osbase/build/mkimage/main.go b/osbase/build/mkimage/main.go
index 79e2920..bef7517 100644
--- a/osbase/build/mkimage/main.go
+++ b/osbase/build/mkimage/main.go
@@ -19,9 +19,9 @@
"log"
"os"
- "source.monogon.dev/osbase/blkio"
"source.monogon.dev/osbase/blockdev"
"source.monogon.dev/osbase/build/mkimage/osimage"
+ "source.monogon.dev/osbase/structfs"
)
func main() {
@@ -51,33 +51,30 @@
// Open the input files for osimage.Create, fill in reader objects and
// metadata in osimage.Params.
// Start with the EFI Payload the OS will boot from.
- p, err := blkio.NewFileReader(efiPayload)
+ var err error
+ cfg.EFIPayload, err = structfs.OSPathBlob(efiPayload)
if err != nil {
log.Fatalf("while opening the EFI payload at %q: %v", efiPayload, err)
}
- cfg.EFIPayload = p
- ab, err := blkio.NewFileReader(abLoaderPayload)
+ cfg.ABLoader, err = structfs.OSPathBlob(abLoaderPayload)
if err != nil {
log.Fatalf("while opening the abloader payload at %q: %v", abLoaderPayload, err)
}
- cfg.ABLoader = ab
// Attempt to open the system image if its path is set. In case the path
// isn't set, the system partition will still be created, but no
// contents will be written into it.
if systemImage != "" {
- img, err := os.Open(systemImage)
+ cfg.SystemImage, err = structfs.OSPathBlob(systemImage)
if err != nil {
log.Fatalf("while opening the system image at %q: %v", systemImage, err)
}
- defer img.Close()
- cfg.SystemImage = img
}
// Attempt to open the node parameters file if its path is set.
if nodeParams != "" {
- np, err := blkio.NewFileReader(nodeParams)
+ np, err := structfs.OSPathBlob(nodeParams)
if err != nil {
log.Fatalf("while opening node parameters at %q: %v", nodeParams, err)
}
diff --git a/osbase/build/mkimage/osimage/BUILD.bazel b/osbase/build/mkimage/osimage/BUILD.bazel
index cfcf096..f85fa20 100644
--- a/osbase/build/mkimage/osimage/BUILD.bazel
+++ b/osbase/build/mkimage/osimage/BUILD.bazel
@@ -10,6 +10,7 @@
"//osbase/efivarfs",
"//osbase/fat32",
"//osbase/gpt",
+ "//osbase/structfs",
"@com_github_google_uuid//:uuid",
],
)
diff --git a/osbase/build/mkimage/osimage/osimage.go b/osbase/build/mkimage/osimage/osimage.go
index 71b5499..4cd3952 100644
--- a/osbase/build/mkimage/osimage/osimage.go
+++ b/osbase/build/mkimage/osimage/osimage.go
@@ -16,6 +16,7 @@
"source.monogon.dev/osbase/efivarfs"
"source.monogon.dev/osbase/fat32"
"source.monogon.dev/osbase/gpt"
+ "source.monogon.dev/osbase/structfs"
)
var (
@@ -60,17 +61,17 @@
Output blockdev.BlockDev
// ABLoader provides the A/B loader which then loads the EFI loader for the
// correct slot.
- ABLoader fat32.SizedReader
+ ABLoader structfs.Blob
// EFIPayload provides contents of the EFI payload file. It must not be
// nil. This gets put into boot slot A.
- EFIPayload fat32.SizedReader
+ EFIPayload structfs.Blob
// SystemImage provides contents of the Metropolis system partition.
// If nil, no contents will be copied into the partition.
- SystemImage io.Reader
+ SystemImage structfs.Blob
// NodeParameters provides contents of the node parameters file. If nil,
// the node parameters file won't be created in the target ESP
// filesystem.
- NodeParameters fat32.SizedReader
+ NodeParameters structfs.Blob
// DiskGUID is a unique identifier of the image and a part of Table
// header. It's optional and can be left blank if the identifier is
// to be randomly generated. Setting it to a predetermined value can
@@ -86,7 +87,7 @@
type plan struct {
*Params
- rootInode fat32.Inode
+ efiRoot structfs.Tree
tbl *gpt.Table
efiPartition *gpt.Partition
systemPartitionA *gpt.Partition
@@ -100,7 +101,7 @@
// Ignore errors, this is only advisory.
i.Output.Discard(0, i.Output.BlockCount()*i.Output.BlockSize())
- if err := fat32.WriteFS(blockdev.NewRWS(i.efiPartition), i.rootInode, fat32.Options{
+ if err := fat32.WriteFS(blockdev.NewRWS(i.efiPartition), i.efiRoot, fat32.Options{
BlockSize: uint16(i.efiPartition.BlockSize()),
BlockCount: uint32(i.efiPartition.BlockCount()),
Label: "MNGN_BOOT",
@@ -108,9 +109,15 @@
return nil, fmt.Errorf("failed to write FAT32: %w", err)
}
- if _, err := io.Copy(blockdev.NewRWS(i.systemPartitionA), i.SystemImage); err != nil {
+ systemImage, err := i.SystemImage.Open()
+ if err != nil {
+ return nil, fmt.Errorf("failed to open system image: %w", err)
+ }
+ if _, err := io.Copy(blockdev.NewRWS(i.systemPartitionA), systemImage); err != nil {
+ systemImage.Close()
return nil, fmt.Errorf("failed to write system partition A: %w", err)
}
+ systemImage.Close()
if err := i.tbl.Write(); err != nil {
return nil, fmt.Errorf("failed to write Table: %w", err)
@@ -155,25 +162,22 @@
return nil, fmt.Errorf("failed to allocate ESP: %w", err)
}
- params.rootInode = fat32.Inode{
- Attrs: fat32.AttrDirectory,
- }
- if err := params.rootInode.PlaceFile(strings.TrimPrefix(EFIBootAPath, "/"), params.EFIPayload); err != nil {
+ if err := params.efiRoot.PlaceFile(strings.TrimPrefix(EFIBootAPath, "/"), params.EFIPayload); err != nil {
return nil, err
}
// Place the A/B loader at the EFI bootloader autodiscovery path.
- if err := params.rootInode.PlaceFile(strings.TrimPrefix(EFIPayloadPath, "/"), params.ABLoader); err != nil {
+ if err := params.efiRoot.PlaceFile(strings.TrimPrefix(EFIPayloadPath, "/"), params.ABLoader); err != nil {
return nil, err
}
if params.NodeParameters != nil {
- if err := params.rootInode.PlaceFile(nodeParamsPath, params.NodeParameters); err != nil {
+ if err := params.efiRoot.PlaceFile(nodeParamsPath, params.NodeParameters); err != nil {
return nil, err
}
}
// Try to layout the fat32 partition. If it detects that the disk is too
// small, an error will be returned.
- if _, err := fat32.SizeFS(params.rootInode, fat32.Options{
+ if _, err := fat32.SizeFS(params.efiRoot, fat32.Options{
BlockSize: uint16(params.efiPartition.BlockSize()),
BlockCount: uint32(params.efiPartition.BlockCount()),
Label: "MNGN_BOOT",
diff --git a/osbase/fat32/BUILD.bazel b/osbase/fat32/BUILD.bazel
index 1e0e909..ac3eb96 100644
--- a/osbase/fat32/BUILD.bazel
+++ b/osbase/fat32/BUILD.bazel
@@ -11,6 +11,7 @@
],
importpath = "source.monogon.dev/osbase/fat32",
visibility = ["//visibility:public"],
+ deps = ["//osbase/structfs"],
)
go_test(
@@ -28,6 +29,7 @@
"xFsckPath": "$(rlocationpath @com_github_dosfstools_dosfstools//:fsck )",
},
deps = [
+ "//osbase/structfs",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
"@io_bazel_rules_go//go/runfiles",
diff --git a/osbase/fat32/dos83.go b/osbase/fat32/dos83.go
index 1d988be..b8d219d 100644
--- a/osbase/fat32/dos83.go
+++ b/osbase/fat32/dos83.go
@@ -28,12 +28,12 @@
// well as valid ASCII
var validDOSName = regexp.MustCompile(`^^([A-Z0-9!#$%&'()@^_\x60{}~-]{0,8})(\.[A-Z0-9!#$%&'()-@^_\x60{}~-]{1,3})?$`)
-func makeUniqueDOSNames(inodes []*Inode) error {
+func makeUniqueDOSNames(nodes []*node) error {
taken := make(map[[11]byte]bool)
- var lossyNameInodes []*Inode
+ var lossyNameNodes []*node
// Make two passes to ensure that names can always be passed through even
// if they would conflict with a generated name.
- for _, i := range inodes {
+ for _, i := range nodes {
for j := range i.dosName {
i.dosName[j] = ' '
}
@@ -54,7 +54,7 @@
taken[i.dosName] = true
continue
}
- lossyNameInodes = append(lossyNameInodes, i)
+ lossyNameNodes = append(lossyNameNodes, i)
}
// Willfully ignore the recommended short name generation algorithm as it
// requires tons of bookkeeping and doesn't result in stable names so
@@ -63,7 +63,7 @@
// of that because of long file name entries), so 4 hex characters
// guarantee uniqueness, regardless of the rest of name.
var nameIdx int
- for _, i := range lossyNameInodes {
+ for _, i := range lossyNameNodes {
nameUpper := strings.ToUpper(i.Name)
dotParts := strings.Split(nameUpper, ".")
for j := range dotParts {
diff --git a/osbase/fat32/fat32.go b/osbase/fat32/fat32.go
index e5c2e58..340bb45 100644
--- a/osbase/fat32/fat32.go
+++ b/osbase/fat32/fat32.go
@@ -13,9 +13,10 @@
"io/fs"
"math"
"math/bits"
- "strings"
"time"
"unicode/utf16"
+
+ "source.monogon.dev/osbase/structfs"
)
// This package contains multiple references to the FAT32 specification, called
@@ -33,7 +34,7 @@
// as large as it needs to be.
BlockCount uint32
- // Human-readable filesystem label. Maximum 10 bytes (gets cut off), should
+ // Human-readable filesystem label. Maximum 11 bytes (gets cut off), should
// be uppercase alphanumeric.
Label string
@@ -42,13 +43,7 @@
ID uint32
}
-// SizedReader is an io.Reader with a known size
-type SizedReader interface {
- io.Reader
- Size() int64
-}
-
-// Attribute is a bitset of flags set on an inode.
+// Attribute is a bitset of flags set on a directory entry.
// See also the spec page 24
type Attribute uint8
@@ -59,39 +54,51 @@
AttrHidden Attribute = 0x02
// AttrSystem indicates that this is an operating system file.
AttrSystem Attribute = 0x04
- // AttrDirectory indicates that this is a directory and not a file.
- AttrDirectory Attribute = 0x10
+ // attrVolumeID indicates that this is a special directory entry which
+ // contains the volume label.
+ attrVolumeID Attribute = 0x08
+ // attrDirectory indicates that this is a directory and not a file.
+ attrDirectory Attribute = 0x10
// AttrArchive canonically indicates that a file has been created/modified
// since the last backup. Its use in practice is inconsistent.
AttrArchive Attribute = 0x20
)
-// Inode is file or directory on the FAT32 filesystem. Note that the concept
-// of an inode doesn't really exist on FAT32, its directories are just special
-// files.
-type Inode struct {
- // Name of the file or directory (not including its path)
- Name string
- // Time the file or directory was last modified
- ModTime time.Time
+// DirEntrySys contains additional directory entry fields which are specific to
+// FAT32. To set these fields, the Sys field of a [structfs.Node] can be set to
+// a pointer to this or to a struct which embeds it.
+type DirEntrySys struct {
// Time the file or directory was created
CreateTime time.Time
// Attributes
Attrs Attribute
- // Children of this directory (only valid when Attrs has AttrDirectory set)
- Children []*Inode
- // Content of this file
- // Only valid when Attrs doesn't have AttrDirectory set.
- Content SizedReader
+}
- // Filled out on placement and write-out
- startCluster int
- parent *Inode
+func (d *DirEntrySys) FAT32() *DirEntrySys {
+ return d
+}
+
+// DirEntrySysAccessor is used to access [DirEntrySys] instead of directly type
+// asserting the struct, to allow for embedding.
+type DirEntrySysAccessor interface {
+ FAT32() *DirEntrySys
+}
+
+// node is a file or directory on the FAT32 filesystem. It wraps a
+// [structfs.Node] and holds additional fields which are filled during planning.
+type node struct {
+ *structfs.Node
dosName [11]byte
+ createTime time.Time
+ attrs Attribute
+ parent *node
+ children []*node
+ size uint32
+ startCluster int
}
// Number of LFN entries + normal entry (all 32 bytes)
-func (i Inode) metaSize() (int64, error) {
+func (i node) metaSize() (int64, error) {
fileNameUTF16 := utf16.Encode([]rune(i.Name))
// VFAT file names are null-terminated
fileNameUTF16 = append(fileNameUTF16, 0x00)
@@ -112,9 +119,9 @@
return sum
}
-// writeMeta writes information about this inode into the contents of the parent
-// inode.
-func (i Inode) writeMeta(w io.Writer) error {
+// writeMeta writes information about this node into the contents of the parent
+// node.
+func (i node) writeMeta(w io.Writer) error {
fileNameUTF16 := utf16.Encode([]rune(i.Name))
// VFAT file names are null-terminated
fileNameUTF16 = append(fileNameUTF16, 0x00)
@@ -153,21 +160,15 @@
return err
}
}
- selfSize, err := i.dataSize()
- if err != nil {
- return err
- }
- if selfSize >= 4*1024*1024*1024 {
- return errors.New("single file size exceeds 4GiB which is prohibited in FAT32")
- }
- if i.Attrs&AttrDirectory != 0 {
+ selfSize := i.size
+ if i.attrs&attrDirectory != 0 {
selfSize = 0 // Directories don't have an explicit size
}
date, t, _ := timeToMsDosTime(i.ModTime)
- cdate, ctime, ctens := timeToMsDosTime(i.CreateTime)
+ cdate, ctime, ctens := timeToMsDosTime(i.createTime)
if err := binary.Write(w, binary.LittleEndian, &dirEntry{
DOSName: i.dosName,
- Attributes: uint8(i.Attrs),
+ Attributes: uint8(i.attrs),
CreationTenMilli: ctens,
CreationTime: ctime,
CreationDate: cdate,
@@ -175,27 +176,27 @@
LastWrittenToTime: t,
LastWrittenToDate: date,
FirstClusterLow: uint16(i.startCluster & 0xffff),
- FileSize: uint32(selfSize),
+ FileSize: selfSize,
}); err != nil {
return err
}
return nil
}
-// writeData writes the contents of this inode (including possible metadata
+// writeData writes the contents of this node (including possible metadata
// of its children, but not its children's data)
-func (i Inode) writeData(w io.Writer, volumeLabel [11]byte) error {
- if i.Attrs&AttrDirectory != 0 {
+func (i node) writeData(w io.Writer, volumeLabel [11]byte) error {
+ if i.attrs&attrDirectory != 0 {
if i.parent == nil {
if err := binary.Write(w, binary.LittleEndian, &dirEntry{
DOSName: volumeLabel,
- Attributes: 0x08, // Volume ID, internal use only
+ Attributes: uint8(attrVolumeID),
}); err != nil {
return err
}
} else {
date, t, _ := timeToMsDosTime(i.ModTime)
- cdate, ctime, ctens := timeToMsDosTime(i.CreateTime)
+ cdate, ctime, ctens := timeToMsDosTime(i.createTime)
if err := binary.Write(w, binary.LittleEndian, &dirEntry{
DOSName: [11]byte{'.', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
CreationDate: cdate,
@@ -203,7 +204,7 @@
CreationTenMilli: ctens,
LastWrittenToTime: t,
LastWrittenToDate: date,
- Attributes: uint8(i.Attrs),
+ Attributes: uint8(i.attrs),
FirstClusterHigh: uint16(i.startCluster >> 16),
FirstClusterLow: uint16(i.startCluster & 0xffff),
}); err != nil {
@@ -224,93 +225,56 @@
CreationTenMilli: ctens,
LastWrittenToTime: t,
LastWrittenToDate: date,
- Attributes: uint8(AttrDirectory),
+ Attributes: uint8(attrDirectory),
FirstClusterHigh: uint16(startCluster >> 16),
FirstClusterLow: uint16(startCluster & 0xffff),
}); err != nil {
return err
}
}
- err := makeUniqueDOSNames(i.Children)
- if err != nil {
- return err
- }
- for _, c := range i.Children {
+ for _, c := range i.children {
if err := c.writeMeta(w); err != nil {
return err
}
}
} else {
- if _, err := io.CopyN(w, i.Content, i.Content.Size()); err != nil {
+ content, err := i.Content.Open()
+ if err != nil {
+ return err
+ }
+ defer content.Close()
+ if _, err := io.CopyN(w, content, int64(i.size)); err != nil {
return err
}
}
return nil
}
-func (i Inode) dataSize() (int64, error) {
- if i.Attrs&AttrDirectory != 0 {
- var size int64
- if i.parent != nil {
- // Dot and dotdot directories
- size += 2 * 32
- } else {
- // Volume ID
- size += 1 * 32
- }
- for _, c := range i.Children {
- cs, err := c.metaSize()
- if err != nil {
- return 0, err
- }
- size += cs
- }
- if size > 2*1024*1024 {
- return 0, errors.New("directory contains > 2MiB of metadata which is prohibited in FAT32")
- }
- return size, nil
+func (i node) dirSize() (uint32, error) {
+ var size int64
+ if i.parent != nil {
+ // Dot and dotdot directories
+ size += 2 * 32
} else {
- return i.Content.Size(), nil
+ // Volume ID
+ size += 1 * 32
}
-}
-
-func (i *Inode) PlaceFile(path string, reader SizedReader) error {
- pathParts := strings.Split(path, "/")
- inodeRef := i
- for j, part := range pathParts {
- var childExists bool
- for _, child := range inodeRef.Children {
- if strings.EqualFold(child.Name, part) {
- inodeRef = child
- childExists = true
- break
- }
+ for _, c := range i.children {
+ cs, err := c.metaSize()
+ if err != nil {
+ return 0, err
}
- if j == len(pathParts)-1 { // Is last path part (i.e. file name)
- if childExists {
- return &fs.PathError{Path: path, Err: fs.ErrExist, Op: "create"}
- }
- newInode := &Inode{
- Name: part,
- Content: reader,
- }
- inodeRef.Children = append(inodeRef.Children, newInode)
- return nil
- } else if !childExists {
- newInode := &Inode{
- Name: part,
- Attrs: AttrDirectory,
- }
- inodeRef.Children = append(inodeRef.Children, newInode)
- inodeRef = newInode
- }
+ size += cs
}
- panic("unreachable")
+ if size > 2*1024*1024 {
+ return 0, errors.New("directory contains > 2MiB of metadata which is prohibited in FAT32")
+ }
+ return uint32(size), nil
}
type planningState struct {
- // List of inodes in filesystem layout order
- orderedInodes []*Inode
+ // List of nodes in filesystem layout order
+ orderedNodes []*node
// File Allocation Table
fat []uint32
// Size of a single cluster in the FAT in bytes
@@ -335,16 +299,54 @@
return allocStartCluster
}
-func (i *Inode) placeRecursively(p *planningState) error {
- selfDataSize, err := i.dataSize()
- if err != nil {
- return fmt.Errorf("%s: %w", i.Name, err)
+func (i *node) placeRecursively(p *planningState) error {
+ if i.Mode.IsDir() {
+ for _, c := range i.Node.Children {
+ node := &node{
+ Node: c,
+ createTime: c.ModTime,
+ parent: i,
+ }
+ if sys, ok := c.Sys.(DirEntrySysAccessor); ok {
+ sys := sys.FAT32()
+ node.attrs = sys.Attrs & (AttrReadOnly | AttrHidden | AttrSystem | AttrArchive)
+ if !sys.CreateTime.IsZero() {
+ node.createTime = sys.CreateTime
+ }
+ }
+ switch {
+ case c.Mode.IsRegular():
+ size := c.Content.Size()
+ if size < 0 {
+ return fmt.Errorf("%s: negative file size", c.Name)
+ }
+ if size >= 4*1024*1024*1024 {
+ return fmt.Errorf("%s: single file size exceeds 4GiB which is prohibited in FAT32", c.Name)
+ }
+ node.size = uint32(size)
+ if len(c.Children) != 0 {
+ return fmt.Errorf("%s: file cannot have children", c.Name)
+ }
+ case c.Mode.IsDir():
+ node.attrs |= attrDirectory
+ default:
+ return fmt.Errorf("%s: unsupported file type %s", c.Name, c.Mode.Type().String())
+ }
+ i.children = append(i.children, node)
+ }
+ err := makeUniqueDOSNames(i.children)
+ if err != nil {
+ return err
+ }
+ i.size, err = i.dirSize()
+ if err != nil {
+ return fmt.Errorf("%s: %w", i.Name, err)
+ }
}
- i.startCluster = p.allocBytes(selfDataSize)
- p.orderedInodes = append(p.orderedInodes, i)
- for _, c := range i.Children {
- c.parent = i
- err = c.placeRecursively(p)
+ i.startCluster = p.allocBytes(int64(i.size))
+ p.orderedNodes = append(p.orderedNodes, i)
+ for _, c := range i.children {
+ err := c.placeRecursively(p)
if err != nil {
return fmt.Errorf("%s/%w", i.Name, err)
}
@@ -352,10 +354,9 @@
return nil
}
-// WriteFS writes a filesystem described by a root inode and its children to a
-// given io.Writer.
-func WriteFS(w io.Writer, rootInode Inode, opts Options) error {
- bs, fsi, p, err := prepareFS(&opts, rootInode)
+// WriteFS writes a filesystem described by a tree to a given io.Writer.
+func WriteFS(w io.Writer, root structfs.Tree, opts Options) error {
+ bs, fsi, p, err := prepareFS(&opts, root)
if err != nil {
return err
}
@@ -405,9 +406,9 @@
}
}
- for _, i := range p.orderedInodes {
+ for _, i := range p.orderedNodes {
if err := i.writeData(wb, bs.Label); err != nil {
- return fmt.Errorf("failed to write inode %q: %w", i.Name, err)
+ return fmt.Errorf("failed to write contents of %q: %w", i.Name, err)
}
if err := wb.FinishBlock(int64(opts.BlockSize)*int64(bs.BlocksPerCluster), true); err != nil {
return err
@@ -420,7 +421,7 @@
return nil
}
-func prepareFS(opts *Options, rootInode Inode) (*bootSector, *fsinfo, *planningState, error) {
+func prepareFS(opts *Options, root structfs.Tree) (*bootSector, *fsinfo, *planningState, error) {
if opts.BlockSize == 0 {
opts.BlockSize = 512
}
@@ -437,9 +438,6 @@
}
opts.ID = binary.BigEndian.Uint32(buf[:])
}
- if rootInode.Attrs&AttrDirectory == 0 {
- return nil, nil, nil, errors.New("root inode must be a directory (i.e. have AttrDirectory set)")
- }
bs := bootSector{
// Assembled x86_32 machine code corresponding to
// jmp $
@@ -499,7 +497,14 @@
}
// First two clusters are special
p.fat = append(p.fat, 0x0fffff00|uint32(bs.MediaCode), 0x0fffffff)
- err := rootInode.placeRecursively(&p)
+ rootNode := &node{
+ Node: &structfs.Node{
+ Mode: fs.ModeDir,
+ Children: root,
+ },
+ attrs: attrDirectory,
+ }
+ err := rootNode.placeRecursively(&p)
if err != nil {
return nil, nil, nil, err
}
@@ -514,7 +519,7 @@
p.fat = append(p.fat, fatFree)
}
- bs.RootClusterNumber = uint32(rootInode.startCluster)
+ bs.RootClusterNumber = uint32(rootNode.startCluster)
bs.BlocksPerFAT = uint32(binary.Size(p.fat)+int(opts.BlockSize)-1) / uint32(opts.BlockSize)
occupiedBlocks := uint32(bs.ReservedBlocks) + (uint32(len(p.fat)-2) * uint32(bs.BlocksPerCluster)) + bs.BlocksPerFAT*uint32(bs.NumFATs)
@@ -552,10 +557,10 @@
}
// SizeFS returns the number of blocks required to hold the filesystem defined
-// by rootInode and opts. This can be used for sizing calculations before
-// calling WriteFS.
-func SizeFS(rootInode Inode, opts Options) (int64, error) {
- bs, _, _, err := prepareFS(&opts, rootInode)
+// by root and opts. This can be used for sizing calculations before calling
+// WriteFS.
+func SizeFS(root structfs.Tree, opts Options) (int64, error) {
+ bs, _, _, err := prepareFS(&opts, root)
if err != nil {
return 0, err
}
diff --git a/osbase/fat32/fsck_test.go b/osbase/fat32/fsck_test.go
index d4908be..5043003 100644
--- a/osbase/fat32/fsck_test.go
+++ b/osbase/fat32/fsck_test.go
@@ -14,6 +14,8 @@
"time"
"github.com/bazelbuild/rules_go/go/runfiles"
+
+ "source.monogon.dev/osbase/structfs"
)
var (
@@ -39,14 +41,14 @@
}
}
-func testWithFsck(t *testing.T, rootInode Inode, opts Options) {
+func testWithFsck(t *testing.T, root structfs.Tree, opts Options) {
t.Helper()
testFile, err := os.CreateTemp("", "fat32-fsck-test")
if err != nil {
t.Fatal(err)
}
defer os.Remove(testFile.Name())
- sizeBlocks, err := SizeFS(rootInode, opts)
+ sizeBlocks, err := SizeFS(root, opts)
if err != nil {
t.Fatalf("failed to calculate size: %v", err)
}
@@ -62,7 +64,7 @@
t.Fatalf("seek failed: %v", err)
}
- if err := WriteFS(testFile, rootInode, opts); err != nil {
+ if err := WriteFS(testFile, root, opts); err != nil {
t.Fatalf("failed to write test FS: %v", err)
}
// Run fsck non-interactively (-n), disallow spaces in short file names (-S)
@@ -89,11 +91,7 @@
for _, blockSize := range []uint16{512, 4096, 32768} {
for _, fixed := range []string{"", "Fixed"} {
t.Run(fmt.Sprintf("BlockSize%d%v", blockSize, fixed), func(t *testing.T) {
- rootInode := Inode{
- Attrs: AttrDirectory,
- ModTime: time.Date(2022, 03, 04, 5, 6, 7, 8, time.UTC),
- CreateTime: time.Date(2022, 03, 04, 5, 6, 7, 8, time.UTC),
- }
+ var root structfs.Tree
files := []struct {
name string
path string
@@ -105,7 +103,7 @@
{"LargeFile", "test1/largefile.txt", largeString.String()},
}
for _, c := range files {
- err := rootInode.PlaceFile(c.path, strings.NewReader(c.content))
+ err := root.PlaceFile(c.path, structfs.Bytes(c.content))
if err != nil {
t.Errorf("failed to place file: %v", err)
}
@@ -115,7 +113,7 @@
// Use a block count that is slightly higher than the minimum
opts.BlockCount = 67000
}
- testWithFsck(t, rootInode, opts)
+ testWithFsck(t, root, opts)
})
}
}
@@ -125,19 +123,18 @@
if os.Getenv("IN_KTEST") == "true" {
t.Skip("In ktest")
}
- rootInode := Inode{
- Attrs: AttrDirectory,
- ModTime: time.Date(2022, 03, 04, 5, 6, 7, 8, time.UTC),
- }
+ var root structfs.Tree
for i := 0; i < (32*1024)-2; i++ {
- rootInode.Children = append(rootInode.Children, &Inode{
+ root = append(root, &structfs.Node{
Name: fmt.Sprintf("test%d", i),
- Content: strings.NewReader("random test content"),
- // Add some random attributes
- Attrs: AttrHidden | AttrSystem,
- // And a random ModTime
+ Content: structfs.Bytes("random test content"),
+ // Add a random ModTime
ModTime: time.Date(2022, 03, 04, 5, 6, 7, 8, time.UTC),
+ Sys: &DirEntrySys{
+ // Add some random attributes
+ Attrs: AttrHidden | AttrSystem,
+ },
})
}
- testWithFsck(t, rootInode, Options{ID: 1234, Label: "TEST"})
+ testWithFsck(t, root, Options{ID: 1234, Label: "TEST"})
}
diff --git a/osbase/fat32/linux_test.go b/osbase/fat32/linux_test.go
index ffe7672..002ac2c 100644
--- a/osbase/fat32/linux_test.go
+++ b/osbase/fat32/linux_test.go
@@ -4,9 +4,9 @@
package fat32
import (
- "bytes"
"fmt"
"io"
+ "io/fs"
"math/rand"
"os"
"strings"
@@ -16,6 +16,8 @@
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
+
+ "source.monogon.dev/osbase/structfs"
)
func TestKernelInterop(t *testing.T) {
@@ -25,7 +27,7 @@
type testCase struct {
name string
- setup func(root *Inode) error
+ setup func() structfs.Tree
validate func(t *testing.T) error
}
@@ -43,14 +45,15 @@
tests := []testCase{
{
name: "SimpleFolder",
- setup: func(root *Inode) error {
- root.Children = []*Inode{{
- Name: "testdir",
- Attrs: AttrDirectory,
- CreateTime: testTimestamp1,
- ModTime: testTimestamp2,
+ setup: func() structfs.Tree {
+ return structfs.Tree{{
+ Name: "testdir",
+ Mode: fs.ModeDir,
+ ModTime: testTimestamp2,
+ Sys: &DirEntrySys{
+ CreateTime: testTimestamp1,
+ },
}}
- return nil
},
validate: func(t *testing.T) error {
var stat unix.Statx_t
@@ -81,14 +84,15 @@
},
{
name: "SimpleFile",
- setup: func(root *Inode) error {
- root.Children = []*Inode{{
- Name: "testfile",
- CreateTime: testTimestamp3,
- ModTime: testTimestamp4,
- Content: strings.NewReader(testContent1),
+ setup: func() structfs.Tree {
+ return structfs.Tree{{
+ Name: "testfile",
+ ModTime: testTimestamp4,
+ Sys: &DirEntrySys{
+ CreateTime: testTimestamp3,
+ },
+ Content: structfs.Bytes(testContent1),
}}
- return nil
},
validate: func(t *testing.T) error {
var stat unix.Statx_t
@@ -118,20 +122,23 @@
},
{
name: "FolderHierarchy",
- setup: func(i *Inode) error {
- i.Children = []*Inode{{
- Name: "l1",
- Attrs: AttrDirectory,
- CreateTime: testTimestamp1,
- ModTime: testTimestamp2,
- Children: []*Inode{{
- Name: "l2",
- Attrs: AttrDirectory,
+ setup: func() structfs.Tree {
+ return structfs.Tree{{
+ Name: "l1",
+ Mode: fs.ModeDir,
+ ModTime: testTimestamp2,
+ Sys: &DirEntrySys{
CreateTime: testTimestamp1,
- ModTime: testTimestamp2,
+ },
+ Children: structfs.Tree{{
+ Name: "l2",
+ Mode: fs.ModeDir,
+ ModTime: testTimestamp2,
+ Sys: &DirEntrySys{
+ CreateTime: testTimestamp1,
+ },
}},
}}
- return nil
},
validate: func(t *testing.T) error {
dirInfo, err := os.ReadDir("/dut/l1")
@@ -149,14 +156,13 @@
},
{
name: "LargeFile",
- setup: func(i *Inode) error {
+ setup: func() structfs.Tree {
content := make([]byte, 6500)
io.ReadFull(rand.New(rand.NewSource(1)), content)
- i.Children = []*Inode{{
+ return structfs.Tree{{
Name: "test.bin",
- Content: bytes.NewReader(content),
+ Content: structfs.Bytes(content),
}}
- return nil
},
validate: func(t *testing.T) error {
var stat unix.Stat_t
@@ -176,12 +182,11 @@
},
{
name: "Unicode",
- setup: func(i *Inode) error {
- i.Children = []*Inode{{
+ setup: func() structfs.Tree {
+ return structfs.Tree{{
Name: "β¨π", // Really exercise that UTF-16 conversion
- Content: strings.NewReader("π"),
+ Content: structfs.Bytes("π"),
}}
- return nil
},
validate: func(t *testing.T) error {
file, err := os.Open("/dut/β¨π")
@@ -197,25 +202,26 @@
t.Fatalf("Failed to open unicode file: %v (available files: %v)", err, strings.Join(availableFileNames, ", "))
}
defer file.Close()
- contents, err := io.ReadAll(file)
- if err != nil {
- t.Errorf("Wrong content: expected %x, got %x", []byte("π"), contents)
- }
+ expected := []byte("π")
+ actual, err := io.ReadAll(file)
+ assert.NoError(t, err, "failed to read test file")
+ assert.Equal(t, expected, actual, "content not identical")
return nil
},
},
{
name: "MultipleMetaClusters",
- setup: func(root *Inode) error {
+ setup: func() structfs.Tree {
// Only test up to 2048 files as Linux gets VERY slow if going
// up to the maximum of approximately 32K
+ var root structfs.Tree
for i := 0; i < 2048; i++ {
- root.Children = append(root.Children, &Inode{
+ root = append(root, &structfs.Node{
Name: fmt.Sprintf("verylongtestfilename%d", i),
- Content: strings.NewReader("random test content"),
+ Content: structfs.Bytes("random test content"),
})
}
- return nil
+ return root
},
validate: func(t *testing.T) error {
files, err := os.ReadDir("/dut")
@@ -245,13 +251,8 @@
t.Fatalf("failed to get ramdisk block size: %v", err)
}
defer file.Close()
- rootInode := Inode{
- Attrs: AttrDirectory,
- }
- if err := test.setup(&rootInode); err != nil {
- t.Fatalf("setup failed: %v", err)
- }
- if err := WriteFS(file, rootInode, Options{
+ root := test.setup()
+ if err := WriteFS(file, root, Options{
ID: 1234,
Label: "KTEST",
BlockSize: uint16(blockSize),