Skip to content

A file whose copy fails part of the way is no longer left in the image: Close wrote it at its declared size over bytes it did not have - #311

Open
closer-claudio wants to merge 2 commits into
mainfrom
claudio/20261123-failed-copy
Open

closer-claudio wants to merge 2 commits into
mainfrom
claudio/20261123-failed-copy

Conversation

@closer-claudio

Copy link
Copy Markdown

This is the second layer of a stack: it sits on #310, whose commit shows in the diff until that one merges. Its own commit is "A file whose copy fails once it is in the tree is taken out of it".

The defect

CopyFrom places a file's entry in the tree and then finishes it: its chunks put on the copy's devices when metadata-only, its data read into the data file when there is one. When that failed, CopyFrom returned the error and the entry stayed, with the size the source declared and less data than that, or none.

A Writer closed after it wrote the entry as it was. With a data file:

  • a file of 10,000 bytes whose read failed at once gave an image this package's own reader refuses: "data range [0, +10000) lies past the end of the 8192 byte image";
  • a smaller one gave a file that reads the next file's bytes.

The change

The entry goes with the failure, as Remove takes it. What follows the placement is a function of its own (settle, and toDataFile for the read into the data file), so that one place sees every way it fails. No other line of logic changes.

What a consumer sees

After a CopyFrom that failed on a file, the Writer no longer finds that file, and an image written afterwards holds whole files only. A caller that stops at CopyFrom's error sees no difference.

What is not changed

A CopyFrom that failed has still left in the tree what it copied before the failure, and Close still writes that and returns nil. Whether such a Writer should refuse to go on is the open half of #309.

Evidence

TestContractAFileWhoseCopyFailedIsNotInTheImage, with a data file: between two files that copy, one of 10, 4097 or 10000 bytes whose read fails or ends, at once or half way. CopyFrom fails; the Writer no longer finds the file; after a later copy and Close the image opens and holds the other files, each with its own bytes. All twelve cases fail before the change, three of them on an image that does not read. With the entry left in place the twelve fail.

just lint and just test pass.

For #309

closer-claudio and others added 2 commits November 23, 2026 09:37
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>
CopyFrom places a file's entry in the tree and then finishes it: its
chunks put on the copy's devices when metadata-only, its data read
into the data file when there is one. When that failed, CopyFrom
returned the error and the entry stayed, with the size the source
declared and less data than that, or none. A Writer closed after it
wrote the entry as it was: with a data file, a file of 10,000 bytes
whose read failed at once gave an image this package's own reader
refuses ("data range [0, +10000) lies past the end of the 8192 byte
image"), and a smaller one a file that reads the next file's bytes.

The entry now goes with the failure, as Remove takes it. What follows
the placement is a function of its own (settle, and toDataFile for the
read into the data file), so that one place sees every way it fails.

What is not changed: a CopyFrom that failed has still left in the tree
what it copied before the failure, and Close still writes that and
returns nil. Whether such a Writer should refuse to go on is
#309; what it writes is at
least an image of whole files.

TestContractAFileWhoseCopyFailedIsNotInTheImage, with a data file:
between two files that copy, one of 10, 4097 or 10000 bytes whose read
fails or ends at once or half way. CopyFrom fails; the Writer no longer
finds the file; after a later copy and Close the image opens and holds
the two other files and the later one's, each with its own bytes. All
twelve cases fail before the change, three of them on an image that
does not read.

For #309

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 c98e8d4

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