Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add core copyfile func #1291

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions core/os.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package core

import (
"fmt"
"github.com/mitchellh/go-homedir"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -67,3 +69,39 @@

return strings.HasSuffix(os.Args[0], ".test")
}

// CopyFile copies a file from src to dest and preserves its permissions.
func CopyFile(src, dest string) error {
// Open source file for reading
//nolint: gosec
srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to open source file: %w", err)
}

Check warning on line 80 in core/os.go

View check run for this annotation

Codecov / codecov/patch

core/os.go#L79-L80

Added lines #L79 - L80 were not covered by tests
defer func() {
_ = srcFile.Close()
}()

// Obtain stat information from source
srcInfo, err := srcFile.Stat()
if err != nil {
return fmt.Errorf("failed to stat source file: %w", err)
}

Check warning on line 89 in core/os.go

View check run for this annotation

Codecov / codecov/patch

core/os.go#L88-L89

Added lines #L88 - L89 were not covered by tests

// Create destination file with the source file permissions
//nolint: gosec
destFile, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcInfo.Mode())
if err != nil {
return fmt.Errorf("failed to open destination file: %w", err)
}

Check warning on line 96 in core/os.go

View check run for this annotation

Codecov / codecov/patch

core/os.go#L95-L96

Added lines #L95 - L96 were not covered by tests
defer func() {
_ = destFile.Close()
}()

// Copy data from source to destination
if _, err := io.Copy(destFile, srcFile); err != nil {
return fmt.Errorf("failed to copy data from source to destination: %w", err)
}

Check warning on line 104 in core/os.go

View check run for this annotation

Codecov / codecov/patch

core/os.go#L103-L104

Added lines #L103 - L104 were not covered by tests

return nil
}
41 changes: 41 additions & 0 deletions core/os_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package core_test

import (
"bytes"
"github.com/Flaque/filet"
"github.com/brianvoe/gofakeit/v6"
. "github.com/stretchr/testify/assert"
common "github.com/synapsecns/sanguine/core"
Expand Down Expand Up @@ -110,3 +112,42 @@ func TestGetEnvBool(t *testing.T) {
})
}
}

func TestCopyFile(t *testing.T) {
// Prepare test files
content := []byte("This is a test content.")
src := filet.TmpFile(t, "", string(content))

dest := filet.TmpFile(t, "", string(content))
_ = dest.Close()

// Copy file
if err := common.CopyFile(src.Name(), dest.Name()); err != nil {
t.Errorf("CopyFile() error: %v", err)
}

// Check if file is copied correctly
destContent, err := os.ReadFile(dest.Name())
if err != nil {
t.Fatalf("Failed to read dest file: %v", err)
}

if !bytes.Equal(content, destContent) {
t.Errorf("Content mismatch: expected %s, got %s", content, destContent)
}

// Check if permissions are the same
srcInfo, err := os.Stat(src.Name())
if err != nil {
t.Fatalf("Failed to stat src file: %v", err)
}

destInfo, err := os.Stat(dest.Name())
if err != nil {
t.Fatalf("Failed to stat dest file: %v", err)
}

if srcInfo.Mode() != destInfo.Mode() {
t.Errorf("Permission mismatch: expected %v, got %v", srcInfo.Mode(), destInfo.Mode())
}
}
Loading