From 4e19a969f569aa9e9e7b6077d8a4eb63038ae765 Mon Sep 17 00:00:00 2001 From: b84330808 Date: Thu, 29 Oct 2026 00:17:45 +0900 Subject: [PATCH] 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 --- fs/operations/operations.go | 2 +- fs/operations/operations_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/operations/operations.go b/fs/operations/operations.go index 97f82068f..773fa68be 100644 --- a/fs/operations/operations.go +++ b/fs/operations/operations.go @@ -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 diff --git a/fs/operations/operations_test.go b/fs/operations/operations_test.go index b5057170f..cdff1fbc9 100644 --- a/fs/operations/operations_test.go +++ b/fs/operations/operations_test.go @@ -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