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/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),