operations: fix cat --offset before the start of a file printing its end

When a negative --offset reached back past the start of a file and
--count was set, rclone cat printed bytes from the end of the file
instead of from its start. For example on a 10 byte file

    rclone cat --offset -12 --count 5 file

printed the last 2 bytes rather than the first 5. With several files
of different sizes, each file shorter than -offset was affected.

The start of the range stayed negative after the file size was added,
and a negative Start with an End set is a suffix range (the last End
bytes). Clamp the start to 0 instead, which matches what
`tail -c 12 file | head -c 5` prints. --tail and --offset without
--count were not affected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
b84330808andClaude Opus 5.5 authored and Nick Craig-Wood committed 2026-10-31 15:29:07 +00:00
1 parent a4a2a78b90
commit 4e19a969f5
2 files changed
+3 -1

No files matched your search

+1 -1
View File
@@ -1367,7 +1367,7 @@ func catObject(ctx context.Context, o fs.Object, w io.Writer, offset, count int6
opt := fs.RangeOption{Start: offset, End: -1}
size := o.Size()
if opt.Start < 0 && size >= 0 {
opt.Start += size
opt.Start = max(opt.Start+size, 0)
}
if count >= 0 {
opt.End = opt.Start + count - 1
+2
View File
@@ -636,6 +636,8 @@ func TestCat(t *testing.T) {
{0, 5, "", "ABCDE", "01234"},
{-3, -1, "", "HIJ", "678"},
{1, 3, "", "BCD", "123"},
{-10, 3, "", "ABC", "012"},
{-12, 5, "", "ABCDE", "01234"},
{0, -1, "\n", "ABCDEFGHIJ", "012345678"},
} {
var buf bytes.Buffer