henriquejsza opened a new pull request, #4916:
URL: https://github.com/apache/bookkeeper/pull/4916
Fix #4736
### Motivation
`FileInfo.moveToNewLocation` copies the ledger index file to a new location
with
`FileChannel.transferTo`:
```java
long written = 0;
while (written < size) {
long count = fc.transferTo(written, size, newFc);
...
written += count;
}
if (written <= 0 && size > 0) {
throw new IOException("Copying to new location " + rlocFile + " failed");
}
```
`transferTo` may transfer fewer bytes than requested. Because the count
passed to
every call is `size` (not the number of bytes still to copy), a call after a
partial transfer reads from position `written` for up to `size` more bytes.
When
the source file holds more than `size` bytes, that copies past the requested
range, so the new location can end up with more than `size` bytes. This
matters
in practice: `moveToNewLocation` is called with `getSizeSinceLastWrite()`,
which
can be smaller than the actual file size.
The post-loop `written <= 0 && size > 0` check reported in the issue is
unreachable: when `size > 0` the loop runs at least once and either throws or
increments `written` to a positive value.
### Changes
- Pass the remaining byte count (`size - written`) to `transferTo` on each
iteration so exactly `size` bytes are copied.
- Extract the copy loop into a small package-private `transferFully` helper
and
remove the unreachable post-loop check.
- Add `FileInfoTest` covering a partial transfer, asserting that exactly
`size`
bytes are copied.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]