From bae46854c5adb3ca6faa62c64576dc40bf46b997 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 29 Jul 2025 10:47:35 +0200 Subject: [PATCH] pkg/fileutils: remove unused CopyFile utility It's not used, and has no external consumers; we don't have to carry it in the new module. Signed-off-by: Sebastiaan van Stijn --- pkg/fileutils/fileutils.go | 26 -------- pkg/fileutils/fileutils_test.go | 101 -------------------------------- 2 files changed, 127 deletions(-) diff --git a/pkg/fileutils/fileutils.go b/pkg/fileutils/fileutils.go index ec83b832c1..2528d0013e 100644 --- a/pkg/fileutils/fileutils.go +++ b/pkg/fileutils/fileutils.go @@ -2,36 +2,10 @@ package fileutils import ( "fmt" - "io" "os" "path/filepath" ) -// CopyFile copies from src to dst until either EOF is reached -// on src or an error occurs. It verifies src exists and removes -// the dst if it exists. -func CopyFile(src, dst string) (int64, error) { - cleanSrc := filepath.Clean(src) - cleanDst := filepath.Clean(dst) - if cleanSrc == cleanDst { - return 0, nil - } - sf, err := os.Open(cleanSrc) - if err != nil { - return 0, err - } - defer sf.Close() - if err := os.Remove(cleanDst); err != nil && !os.IsNotExist(err) { - return 0, err - } - df, err := os.Create(cleanDst) - if err != nil { - return 0, err - } - defer df.Close() - return io.Copy(df, sf) -} - // ReadSymlinkedDirectory returns the target directory of a symlink. // The target of the symbolic link may not be a file. func ReadSymlinkedDirectory(path string) (realPath string, _ error) { diff --git a/pkg/fileutils/fileutils_test.go b/pkg/fileutils/fileutils_test.go index cdb93053d5..ec370187b2 100644 --- a/pkg/fileutils/fileutils_test.go +++ b/pkg/fileutils/fileutils_test.go @@ -10,107 +10,6 @@ import ( "testing" ) -// CopyFile with invalid src -func TestCopyFileWithInvalidSrc(t *testing.T) { - tempDir := t.TempDir() - bytes, err := CopyFile(filepath.Join(tempDir, "/invalid/file/path"), path.Join(t.TempDir(), "dest")) - if err == nil { - t.Error("Should have fail to copy an invalid src file") - } - if !errors.Is(err, os.ErrNotExist) { - t.Errorf("Expected an os.ErrNotExist, got: %v", err) - } - if bytes != 0 { - t.Errorf("Should have written 0 bytes, got: %d", bytes) - } -} - -// CopyFile with invalid dest -func TestCopyFileWithInvalidDest(t *testing.T) { - tempFolder := t.TempDir() - src := path.Join(tempFolder, "file") - err := os.WriteFile(src, []byte("content"), 0o740) - if err != nil { - t.Fatal(err) - } - bytes, err := CopyFile(src, path.Join(tempFolder, "/invalid/dest/path")) - if err == nil { - t.Error("Should have fail to copy an invalid src file") - } - if !errors.Is(err, os.ErrNotExist) { - t.Errorf("Expected an os.ErrNotExist, got: %v", err) - } - if bytes != 0 { - t.Errorf("Should have written 0 bytes, got: %d", bytes) - } -} - -// CopyFile with same src and dest -func TestCopyFileWithSameSrcAndDest(t *testing.T) { - file := path.Join(t.TempDir(), "file") - err := os.WriteFile(file, []byte("content"), 0o740) - if err != nil { - t.Fatal(err) - } - bytes, err := CopyFile(file, file) - if err != nil { - t.Fatal(err) - } - if bytes != 0 { - t.Fatal("Should have written 0 bytes as it is the same file.") - } -} - -// CopyFile with same src and dest but path is different and not clean -func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) { - testFolder := path.Join(t.TempDir(), "test") - err := os.Mkdir(testFolder, 0o740) - if err != nil { - t.Fatal(err) - } - file := path.Join(testFolder, "file") - sameFile := testFolder + "/../test/file" - err = os.WriteFile(file, []byte("content"), 0o740) - if err != nil { - t.Fatal(err) - } - bytes, err := CopyFile(file, sameFile) - if err != nil { - t.Fatal(err) - } - if bytes != 0 { - t.Fatal("Should have written 0 bytes as it is the same file.") - } -} - -func TestCopyFile(t *testing.T) { - tempFolder := t.TempDir() - src := path.Join(tempFolder, "src") - dest := path.Join(tempFolder, "dest") - err := os.WriteFile(src, []byte("content"), 0o777) - if err != nil { - t.Error(err) - } - err = os.WriteFile(dest, []byte("destContent"), 0o777) - if err != nil { - t.Error(err) - } - bytes, err := CopyFile(src, dest) - if err != nil { - t.Fatal(err) - } - if bytes != 7 { - t.Fatalf("Should have written %d bytes but wrote %d", 7, bytes) - } - actual, err := os.ReadFile(dest) - if err != nil { - t.Fatal(err) - } - if string(actual) != "content" { - t.Fatalf("Dest content was '%s', expected '%s'", string(actual), "content") - } -} - // Reading a symlink to a directory must return the directory func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) { // TODO Windows: Port this test