Skip to content

With a data file, CopyFrom stores a file at the size its source declared, and refuses one that reads short - #310

Open
closer-claudio wants to merge 1 commit into
mainfrom
claudio/20261123-datafile-size
Open

closer-claudio wants to merge 1 commit into
mainfrom
claudio/20261123-datafile-size

Conversation

@closer-claudio

Copy link
Copy Markdown

The defect

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 1,048,576 bytes with a data file, all of them written to it;
  • a reader that never ended filled the data file;
  • a file that read shorter than it declared was stored short, without an error.

The change

CopyFrom 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 bytes (lib/inode.c, erofs_write_unencoded_file: i_size is st_size, and a tail read short of it is -EIO). Read from the source, 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.

What a consumer sees

With a data file:

  • a source whose file reads longer than the size it declared is stored at the declared size, its first bytes, where every byte read was stored;
  • a source whose file reads shorter fails CopyFrom with io.ErrUnexpectedEOF, where it was stored short.

Nothing changes without a data file, nor for a source whose files read the size they declare.

Evidence

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 three of these fail before the change.

With the read not bounded, the short read not refused, or one byte short allowed, the test fails.

just lint and just test pass.

For #272

Not changed here: after a CopyFrom that failed part of the way, Close writes what was copied until then and returns nil, and with a data file that image can be corrupt. A file that reads short now takes that path, where before it was stored short with no error: #309.

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 #272

Signed-off-by: closer-claudio <claudio@farcloser.world>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@closer-claudio

Copy link
Copy Markdown
Author

Reviewed at ae5792c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant