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

cmd: use package filepath over path for file system operations #29227

Merged
merged 2 commits into from
Mar 12, 2024
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
10 changes: 5 additions & 5 deletions cmd/devp2p/internal/ethtest/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"io"
"math/big"
"os"
"path"
"path/filepath"
"sort"
"strings"

Expand Down Expand Up @@ -56,21 +56,21 @@ type Chain struct {
// NewChain takes the given chain.rlp file, and decodes and returns
// the blocks from the file.
func NewChain(dir string) (*Chain, error) {
gen, err := loadGenesis(path.Join(dir, "genesis.json"))
gen, err := loadGenesis(filepath.Join(dir, "genesis.json"))
if err != nil {
return nil, err
}
gblock := gen.ToBlock()

blocks, err := blocksFromFile(path.Join(dir, "chain.rlp"), gblock)
blocks, err := blocksFromFile(filepath.Join(dir, "chain.rlp"), gblock)
if err != nil {
return nil, err
}
state, err := readState(path.Join(dir, "headstate.json"))
state, err := readState(filepath.Join(dir, "headstate.json"))
if err != nil {
return nil, err
}
accounts, err := readAccounts(path.Join(dir, "accounts.json"))
accounts, err := readAccounts(filepath.Join(dir, "accounts.json"))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/devp2p/internal/ethtest/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"io"
"net/http"
"os"
"path"
"path/filepath"
"time"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -38,7 +38,7 @@ type EngineClient struct {

// NewEngineClient creates a new engine client.
func NewEngineClient(dir, url, jwt string) (*EngineClient, error) {
headfcu, err := os.ReadFile(path.Join(dir, "headfcu.json"))
headfcu, err := os.ReadFile(filepath.Join(dir, "headfcu.json"))
if err != nil {
return nil, fmt.Errorf("failed to read headfcu: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/devp2p/internal/ethtest/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
crand "crypto/rand"
"fmt"
"os"
"path"
"path/filepath"
"testing"
"time"

Expand All @@ -39,7 +39,7 @@ func makeJWTSecret() (string, [32]byte, error) {
if _, err := crand.Read(secret[:]); err != nil {
return "", secret, fmt.Errorf("failed to create jwt secret: %v", err)
}
jwtPath := path.Join(os.TempDir(), "jwt_secret")
jwtPath := filepath.Join(os.TempDir(), "jwt_secret")
if err := os.WriteFile(jwtPath, []byte(hexutil.Encode(secret[:])), 0600); err != nil {
return "", secret, fmt.Errorf("failed to prepare jwt secret file: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/era/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"
"math/big"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -176,7 +176,7 @@ func open(ctx *cli.Context, epoch uint64) (*era.Era, error) {
if epoch >= uint64(len(entries)) {
return nil, fmt.Errorf("epoch out-of-bounds: last %d, want %d", len(entries)-1, epoch)
}
return era.Open(path.Join(dir, entries[epoch]))
return era.Open(filepath.Join(dir, entries[epoch]))
}

// verify checks each era1 file in a directory to ensure it is well-formed and
Expand Down Expand Up @@ -212,7 +212,7 @@ func verify(ctx *cli.Context) error {
// Wrap in function so defers don't stack.
err := func() error {
name := entries[i]
e, err := era.Open(path.Join(dir, name))
e, err := era.Open(filepath.Join(dir, name))
if err != nil {
return fmt.Errorf("error opening era1 file %s: %w", name, err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"
"math/big"
"os"
"path"
"path/filepath"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -96,7 +96,7 @@ func Transition(ctx *cli.Context) error {
Debug: true,
}
getTracer = func(txIndex int, txHash common.Hash) (vm.EVMLogger, error) {
traceFile, err := os.Create(path.Join(baseDir, fmt.Sprintf("trace-%d-%v.jsonl", txIndex, txHash.String())))
traceFile, err := os.Create(filepath.Join(baseDir, fmt.Sprintf("trace-%d-%v.jsonl", txIndex, txHash.String())))
if err != nil {
return nil, NewError(ErrorIO, fmt.Errorf("failed creating trace-file: %v", err))
}
Expand All @@ -108,7 +108,7 @@ func Transition(ctx *cli.Context) error {
config = []byte(ctx.String(TraceTracerConfigFlag.Name))
}
getTracer = func(txIndex int, txHash common.Hash) (vm.EVMLogger, error) {
traceFile, err := os.Create(path.Join(baseDir, fmt.Sprintf("trace-%d-%v.json", txIndex, txHash.String())))
traceFile, err := os.Create(filepath.Join(baseDir, fmt.Sprintf("trace-%d-%v.json", txIndex, txHash.String())))
if err != nil {
return nil, NewError(ErrorIO, fmt.Errorf("failed creating trace-file: %v", err))
}
Expand Down Expand Up @@ -302,7 +302,7 @@ func saveFile(baseDir, filename string, data interface{}) error {
if err != nil {
return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err))
}
location := path.Join(baseDir, filename)
location := filepath.Join(baseDir, filename)
if err = os.WriteFile(location, b, 0644); err != nil {
return NewError(ErrorIO, fmt.Errorf("failed writing output: %v", err))
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"io"
"os"
"os/signal"
"path"
"path/filepath"
"runtime"
"strings"
"syscall"
Expand Down Expand Up @@ -251,7 +251,7 @@ func ImportHistory(chain *core.BlockChain, db ethdb.Database, dir string, networ
if err != nil {
return fmt.Errorf("error reading %s: %w", dir, err)
}
checksums, err := readList(path.Join(dir, "checksums.txt"))
checksums, err := readList(filepath.Join(dir, "checksums.txt"))
if err != nil {
return fmt.Errorf("unable to read checksums.txt: %w", err)
}
Expand All @@ -268,7 +268,7 @@ func ImportHistory(chain *core.BlockChain, db ethdb.Database, dir string, networ
)
for i, filename := range entries {
err := func() error {
f, err := os.Open(path.Join(dir, filename))
f, err := os.Open(filepath.Join(dir, filename))
if err != nil {
return fmt.Errorf("unable to open era: %w", err)
}
Expand Down Expand Up @@ -425,7 +425,7 @@ func ExportHistory(bc *core.BlockChain, dir string, first, last, step uint64) er
)
for i := first; i <= last; i += step {
err := func() error {
filename := path.Join(dir, era.Filename(network, int(i/step), common.Hash{}))
filename := filepath.Join(dir, era.Filename(network, int(i/step), common.Hash{}))
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("could not create era file: %w", err)
Expand Down Expand Up @@ -458,7 +458,7 @@ func ExportHistory(bc *core.BlockChain, dir string, first, last, step uint64) er
return fmt.Errorf("export failed to finalize %d: %w", step/i, err)
}
// Set correct filename with root.
os.Rename(filename, path.Join(dir, era.Filename(network, int(i/step), root)))
os.Rename(filename, filepath.Join(dir, era.Filename(network, int(i/step), root)))

// Compute checksum of entire Era1.
if _, err := f.Seek(0, io.SeekStart); err != nil {
Expand All @@ -481,7 +481,7 @@ func ExportHistory(bc *core.BlockChain, dir string, first, last, step uint64) er
}
}

os.WriteFile(path.Join(dir, "checksums.txt"), []byte(strings.Join(checksums, "\n")), os.ModePerm)
os.WriteFile(filepath.Join(dir, "checksums.txt"), []byte(strings.Join(checksums, "\n")), os.ModePerm)

log.Info("Exported blockchain to", "dir", dir)

Expand Down
6 changes: 3 additions & 3 deletions cmd/utils/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"io"
"math/big"
"os"
"path"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -99,7 +99,7 @@ func TestHistoryImportAndExport(t *testing.T) {
}

// Read checksums.
b, err := os.ReadFile(path.Join(dir, "checksums.txt"))
b, err := os.ReadFile(filepath.Join(dir, "checksums.txt"))
if err != nil {
t.Fatalf("failed to read checksums: %v", err)
}
Expand All @@ -109,7 +109,7 @@ func TestHistoryImportAndExport(t *testing.T) {
entries, _ := era.ReadDir(dir, "mainnet")
for i, filename := range entries {
func() {
f, err := os.Open(path.Join(dir, filename))
f, err := os.Open(filepath.Join(dir, filename))
if err != nil {
t.Fatalf("error opening era file: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions core/blockchain_repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package core

import (
"math/big"
"path"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -1762,7 +1762,7 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s

// Create a temporary persistent database
datadir := t.TempDir()
ancient := path.Join(datadir, "ancient")
ancient := filepath.Join(datadir, "ancient")

db, err := rawdb.Open(rawdb.OpenOptions{
Directory: datadir,
Expand Down Expand Up @@ -1912,7 +1912,7 @@ func testIssue23496(t *testing.T, scheme string) {

// Create a temporary persistent database
datadir := t.TempDir()
ancient := path.Join(datadir, "ancient")
ancient := filepath.Join(datadir, "ancient")

db, err := rawdb.Open(rawdb.OpenOptions{
Directory: datadir,
Expand Down
4 changes: 2 additions & 2 deletions core/blockchain_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"fmt"
"math/big"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -63,7 +63,7 @@ type snapshotTestBasic struct {
func (basic *snapshotTestBasic) prepare(t *testing.T) (*BlockChain, []*types.Block) {
// Create a temporary persistent database
datadir := t.TempDir()
ancient := path.Join(datadir, "ancient")
ancient := filepath.Join(datadir, "ancient")

db, err := rawdb.Open(rawdb.OpenOptions{
Directory: datadir,
Expand Down
3 changes: 2 additions & 1 deletion core/rawdb/freezer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"math/rand"
"os"
"path"
"path/filepath"
"sync"
"testing"

Expand Down Expand Up @@ -393,7 +394,7 @@ func TestRenameWindows(t *testing.T) {
dir2 := t.TempDir()

// Create file in dir1 and fill with data
f, err := os.Create(path.Join(dir1, fname))
f, err := os.Create(filepath.Join(dir1, fname))
if err != nil {
t.Fatal(err)
}
Expand Down