From ae5792cfd6026d4ff0c543002d733a6da0de7039 Mon Sep 17 00:00:00 2001 From: closer-claudio Date: Mon, 23 Nov 2026 09:37:26 -0800 Subject: [PATCH] With a data file, a copied file is the size its source declared With a data file, CopyFrom read a regular file to the end of its reader and recorded what it had read as the file's size. Without one, Close stops at the size the source declared and fails on a short read. The two stored different files from one source: a file declared 10 bytes that read as a megabyte was 10 bytes in the image and 1048576 with a data file, all of them written to it, and a reader that never ended filled the data file. A file that read shorter than it declared was stored short, without an error. CopyFrom now reads the declared size and no more into the data file, and fails with io.ErrUnexpectedEOF on a file that ends before it, as Close does without a data file. mkfs.erofs copies the size it found the file with and fails on fewer (lib/inode.c, erofs_write_unencoded_file: i_size is st_size, and a tail read short of it is -EIO), read and not run. The other reader a copy stores in a data file, a holed file's data laid out by chunks, is this package's own, over an image's ranges, and bounded by them: no source hands one. TestContractCopiedFileIsTheSizeItsSourceDeclared, with a data file and without: a file that reads a megabyte where it declared ten bytes is ten bytes, and the data file holds no more than a block of it; one that reads none, four or nine of its ten bytes fails with io.ErrUnexpectedEOF, in CopyFrom with a data file and at Close without. With a data file the first, the second and the size of the data file fail before the change. With the read not bounded, the short read not refused, or one byte short allowed, the test fails. For https://github.com/forkcloser/erofs/issues/272 Signed-off-by: closer-claudio Co-Authored-By: Claude Fable 5.2 --- contract_copy_declared_size_test.go | 142 ++++++++++++++++++++++++++++ mkfs.go | 14 ++- 2 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 contract_copy_declared_size_test.go diff --git a/contract_copy_declared_size_test.go b/contract_copy_declared_size_test.go new file mode 100644 index 0000000..b7ce317 --- /dev/null +++ b/contract_copy_declared_size_test.go @@ -0,0 +1,142 @@ +package erofs_test + +import ( + "errors" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "testing" + "testing/fstest" + + "github.com/forkcloser/erofs" +) + +// misreadFile is a one-file source whose file "f" reads as reads bytes, +// whatever size it declares: a file that grows or shrinks while it is +// packed. +type misreadFile struct { + fstest.MapFS + + reads int +} + +type misreadingFile struct { + fs.File + + left int +} + +func (m misreadFile) Open(name string) (fs.File, error) { + f, err := m.MapFS.Open(name) + if err != nil || name != "f" { + return f, err + } + + return &misreadingFile{File: f, left: m.reads}, nil +} + +func (f *misreadingFile) Read(p []byte) (int, error) { + if f.left == 0 { + return 0, io.EOF + } + + n := min(len(p), f.left) + clear(p[:n]) + f.left -= n + + return n, nil +} + +// TestContractCopiedFileIsTheSizeItsSourceDeclared: a regular file copied +// from a source is as long as the source declared it, with a data file as +// without one: a file that reads longer is stored to its declared size, and +// one that reads shorter fails the copy with io.ErrUnexpectedEOF, during +// CopyFrom where the data is read into a data file, at Close where it is +// read into the image. +func TestContractCopiedFileIsTheSizeItsSourceDeclared(t *testing.T) { + t.Parallel() + + const declared = 10 + + source := func(reads int) fs.FS { + return misreadFile{ + MapFS: fstest.MapFS{"f": &fstest.MapFile{Data: make([]byte, declared), Mode: 0o644}}, + reads: reads, + } + } + + for _, withDataFile := range []bool{false, true} { + t.Run(fmt.Sprintf("data file %v", withDataFile), func(t *testing.T) { + t.Parallel() + + create := func() (*erofs.Writer, *os.File) { + if !withDataFile { + return erofs.Create(&testBuffer{}), nil + } + + df, err := os.Create(filepath.Join(t.TempDir(), "data")) + if err != nil { + t.Fatal(err) + } + + t.Cleanup(func() { _ = df.Close() }) + + return erofs.Create(&testBuffer{}, erofs.WithDataFile(df)), df + } + + // A megabyte where ten bytes were declared. + w, df := create() + if err := w.CopyFrom(source(1 << 20)); err != nil { + t.Fatalf("CopyFrom of a file that reads longer than it declared: %v", err) + } + + info, err := w.Stat("/f") + if err != nil { + t.Fatal(err) + } + + if info.Size() != declared { + t.Errorf("the copied file is %d bytes; the source declared %d", info.Size(), declared) + } + + if df != nil { + st, serr := df.Stat() + if serr != nil { + t.Fatal(serr) + } + + // One block of the data file, at most. + if st.Size() > 4096 { + t.Errorf("the data file holds %d bytes of a file declared %d", st.Size(), declared) + } + } + + if cerr := w.Close(); cerr != nil { + t.Errorf("Close: %v", cerr) + } + + // Fewer than the ten declared: none, four, and all but one. + for _, reads := range []int{0, 4, declared - 1} { + w, _ = create() + + err = w.CopyFrom(source(reads)) + if err == nil { + err = w.Close() + } else if !withDataFile { + t.Errorf("CopyFrom read the file's data without a data file: %v", err) + } + + if !errors.Is(err, io.ErrUnexpectedEOF) { + t.Errorf( + "a file that reads %d bytes where it declared %d: %v, want io.ErrUnexpectedEOF", + reads, + declared, + err, + ) + } + } + }) + } +} diff --git a/mkfs.go b/mkfs.go index 03a2ddb..a6791e2 100644 --- a/mkfs.go +++ b/mkfs.go @@ -756,7 +756,9 @@ func (l listedOnce) ReadDir(name string) ([]fs.DirEntry, error) { // files are first opened then, by name: one that cannot be opened, or has // become shorter, fails Close; one that has grown is copied as it was // found, its first bytes; and a name that holds another file by then is -// read as that file, under the metadata of the first. +// read as that file, under the metadata of the first. With a data file the +// same is so at once: a file shorter than it was found fails CopyFrom, and +// one that has grown is copied to the size it was found with. // Reads symlink targets via readLinker interface when Entry.LinkTarget is empty. // If src implements blockSizer, the image block size is set accordingly. // A directory an EROFS image names twice, a hard link to it or a cycle @@ -2069,7 +2071,10 @@ func (fsys *Writer) add(p string, info fs.FileInfo) error { f.dataStartOff = fsys.dataOff fe.dataStartOff = fsys.dataOff - _, err := f.ReadFrom(be.Data) + // As Close reads it without a data file: the size the source + // declared and no more, a reader that goes on being one that + // never ends, and an error for less. + written, err := f.ReadFrom(io.LimitReader(be.Data, info.Size())) // The source's file is done with here, read or not. if c, ok := be.Data.(io.Closer); ok { _ = c.Close() @@ -2079,6 +2084,11 @@ func (fsys *Writer) add(p string, info fs.FileInfo) error { return err } + if written != info.Size() { + return fmt.Errorf("mkfs: %s: short read: got %d bytes, expected %d: %w", + p, written, info.Size(), io.ErrUnexpectedEOF) + } + if err := f.Close(); err != nil { return err }