From 9ae97f8b8b07c63d354ac41cd37771d87c679df7 Mon Sep 17 00:00:00 2001
From: Ian Lance Taylor <iant@golang.org>
Date: Fri, 10 Mar 2023 20:41:14 -0800
Subject: [PATCH] os: don't hide all methods in recursive call to io.Copy

In order to avoid a recursive call to ReadFrom, we were converting
a *File to an io.Writer. But all we really need to do is hide
the ReadFrom method. In particular, this gives us the option of
adding a WriteTo method.

For #58808

Change-Id: I20d3a45749d528c93c23267c467e607fc17dc83f
Reviewed-on: https://go-review.googlesource.com/c/go/+/475535
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
---
 src/os/file.go | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/os/file.go b/src/os/file.go
index 10aed03b965..ea81a8ba634 100644
--- a/src/os/file.go
+++ b/src/os/file.go
@@ -158,11 +158,19 @@ func (f *File) ReadFrom(r io.Reader) (n int64, err error) {
 }
 
 func genericReadFrom(f *File, r io.Reader) (int64, error) {
-	return io.Copy(onlyWriter{f}, r)
+	return io.Copy(fileWithoutReadFrom{f}, r)
 }
 
-type onlyWriter struct {
-	io.Writer
+// fileWithoutReadFrom implements all the methods of *File other
+// than ReadFrom. This is used to permit ReadFrom to call io.Copy
+// without leading to a recursive call to ReadFrom.
+type fileWithoutReadFrom struct {
+	*File
+}
+
+// This ReadFrom method hides the *File ReadFrom method.
+func (fileWithoutReadFrom) ReadFrom(fileWithoutReadFrom) {
+	panic("unreachable")
 }
 
 // Write writes len(b) bytes from b to the File.
-- 
GitLab