Skip to content

Commit 89cefe2

Browse files
authored
cmd: use package filepath over path for file system operations (ethereum#29227)
Package filepath implements utility routines for manipulating filename paths in a way compatible with the target operating system-defined file paths. Package path implements utility routines for manipulating slash-separated paths. The path package should only be used for paths separated by forward slashes, such as the paths in URLs
1 parent 4e1116f commit 89cefe2

File tree

10 files changed

+32
-31
lines changed

10 files changed

+32
-31
lines changed

cmd/devp2p/internal/ethtest/chain.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"io"
2727
"math/big"
2828
"os"
29-
"path"
29+
"path/filepath"
3030
"sort"
3131
"strings"
3232

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

65-
blocks, err := blocksFromFile(path.Join(dir, "chain.rlp"), gblock)
65+
blocks, err := blocksFromFile(filepath.Join(dir, "chain.rlp"), gblock)
6666
if err != nil {
6767
return nil, err
6868
}
69-
state, err := readState(path.Join(dir, "headstate.json"))
69+
state, err := readState(filepath.Join(dir, "headstate.json"))
7070
if err != nil {
7171
return nil, err
7272
}
73-
accounts, err := readAccounts(path.Join(dir, "accounts.json"))
73+
accounts, err := readAccounts(filepath.Join(dir, "accounts.json"))
7474
if err != nil {
7575
return nil, err
7676
}

cmd/devp2p/internal/ethtest/engine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"io"
2323
"net/http"
2424
"os"
25-
"path"
25+
"path/filepath"
2626
"time"
2727

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

3939
// NewEngineClient creates a new engine client.
4040
func NewEngineClient(dir, url, jwt string) (*EngineClient, error) {
41-
headfcu, err := os.ReadFile(path.Join(dir, "headfcu.json"))
41+
headfcu, err := os.ReadFile(filepath.Join(dir, "headfcu.json"))
4242
if err != nil {
4343
return nil, fmt.Errorf("failed to read headfcu: %w", err)
4444
}

cmd/devp2p/internal/ethtest/suite_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
crand "crypto/rand"
2121
"fmt"
2222
"os"
23-
"path"
23+
"path/filepath"
2424
"testing"
2525
"time"
2626

@@ -39,7 +39,7 @@ func makeJWTSecret() (string, [32]byte, error) {
3939
if _, err := crand.Read(secret[:]); err != nil {
4040
return "", secret, fmt.Errorf("failed to create jwt secret: %v", err)
4141
}
42-
jwtPath := path.Join(os.TempDir(), "jwt_secret")
42+
jwtPath := filepath.Join(os.TempDir(), "jwt_secret")
4343
if err := os.WriteFile(jwtPath, []byte(hexutil.Encode(secret[:])), 0600); err != nil {
4444
return "", secret, fmt.Errorf("failed to prepare jwt secret file: %v", err)
4545
}

cmd/era/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"fmt"
2323
"math/big"
2424
"os"
25-
"path"
25+
"path/filepath"
2626
"strconv"
2727
"strings"
2828
"time"
@@ -176,7 +176,7 @@ func open(ctx *cli.Context, epoch uint64) (*era.Era, error) {
176176
if epoch >= uint64(len(entries)) {
177177
return nil, fmt.Errorf("epoch out-of-bounds: last %d, want %d", len(entries)-1, epoch)
178178
}
179-
return era.Open(path.Join(dir, entries[epoch]))
179+
return era.Open(filepath.Join(dir, entries[epoch]))
180180
}
181181

182182
// verify checks each era1 file in a directory to ensure it is well-formed and
@@ -212,7 +212,7 @@ func verify(ctx *cli.Context) error {
212212
// Wrap in function so defers don't stack.
213213
err := func() error {
214214
name := entries[i]
215-
e, err := era.Open(path.Join(dir, name))
215+
e, err := era.Open(filepath.Join(dir, name))
216216
if err != nil {
217217
return fmt.Errorf("error opening era1 file %s: %w", name, err)
218218
}

cmd/evm/internal/t8ntool/transition.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"fmt"
2323
"math/big"
2424
"os"
25-
"path"
25+
"path/filepath"
2626

2727
"github.com/ethereum/go-ethereum/common"
2828
"github.com/ethereum/go-ethereum/common/hexutil"
@@ -96,7 +96,7 @@ func Transition(ctx *cli.Context) error {
9696
Debug: true,
9797
}
9898
getTracer = func(txIndex int, txHash common.Hash) (vm.EVMLogger, error) {
99-
traceFile, err := os.Create(path.Join(baseDir, fmt.Sprintf("trace-%d-%v.jsonl", txIndex, txHash.String())))
99+
traceFile, err := os.Create(filepath.Join(baseDir, fmt.Sprintf("trace-%d-%v.jsonl", txIndex, txHash.String())))
100100
if err != nil {
101101
return nil, NewError(ErrorIO, fmt.Errorf("failed creating trace-file: %v", err))
102102
}
@@ -108,7 +108,7 @@ func Transition(ctx *cli.Context) error {
108108
config = []byte(ctx.String(TraceTracerConfigFlag.Name))
109109
}
110110
getTracer = func(txIndex int, txHash common.Hash) (vm.EVMLogger, error) {
111-
traceFile, err := os.Create(path.Join(baseDir, fmt.Sprintf("trace-%d-%v.json", txIndex, txHash.String())))
111+
traceFile, err := os.Create(filepath.Join(baseDir, fmt.Sprintf("trace-%d-%v.json", txIndex, txHash.String())))
112112
if err != nil {
113113
return nil, NewError(ErrorIO, fmt.Errorf("failed creating trace-file: %v", err))
114114
}
@@ -302,7 +302,7 @@ func saveFile(baseDir, filename string, data interface{}) error {
302302
if err != nil {
303303
return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err))
304304
}
305-
location := path.Join(baseDir, filename)
305+
location := filepath.Join(baseDir, filename)
306306
if err = os.WriteFile(location, b, 0644); err != nil {
307307
return NewError(ErrorIO, fmt.Errorf("failed writing output: %v", err))
308308
}

cmd/utils/cmd.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"io"
2828
"os"
2929
"os/signal"
30-
"path"
30+
"path/filepath"
3131
"runtime"
3232
"strings"
3333
"syscall"
@@ -251,7 +251,7 @@ func ImportHistory(chain *core.BlockChain, db ethdb.Database, dir string, networ
251251
if err != nil {
252252
return fmt.Errorf("error reading %s: %w", dir, err)
253253
}
254-
checksums, err := readList(path.Join(dir, "checksums.txt"))
254+
checksums, err := readList(filepath.Join(dir, "checksums.txt"))
255255
if err != nil {
256256
return fmt.Errorf("unable to read checksums.txt: %w", err)
257257
}
@@ -268,7 +268,7 @@ func ImportHistory(chain *core.BlockChain, db ethdb.Database, dir string, networ
268268
)
269269
for i, filename := range entries {
270270
err := func() error {
271-
f, err := os.Open(path.Join(dir, filename))
271+
f, err := os.Open(filepath.Join(dir, filename))
272272
if err != nil {
273273
return fmt.Errorf("unable to open era: %w", err)
274274
}
@@ -425,7 +425,7 @@ func ExportHistory(bc *core.BlockChain, dir string, first, last, step uint64) er
425425
)
426426
for i := first; i <= last; i += step {
427427
err := func() error {
428-
filename := path.Join(dir, era.Filename(network, int(i/step), common.Hash{}))
428+
filename := filepath.Join(dir, era.Filename(network, int(i/step), common.Hash{}))
429429
f, err := os.Create(filename)
430430
if err != nil {
431431
return fmt.Errorf("could not create era file: %w", err)
@@ -458,7 +458,7 @@ func ExportHistory(bc *core.BlockChain, dir string, first, last, step uint64) er
458458
return fmt.Errorf("export failed to finalize %d: %w", step/i, err)
459459
}
460460
// Set correct filename with root.
461-
os.Rename(filename, path.Join(dir, era.Filename(network, int(i/step), root)))
461+
os.Rename(filename, filepath.Join(dir, era.Filename(network, int(i/step), root)))
462462

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

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

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

cmd/utils/history_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"io"
2323
"math/big"
2424
"os"
25-
"path"
25+
"path/filepath"
2626
"strings"
2727
"testing"
2828

@@ -99,7 +99,7 @@ func TestHistoryImportAndExport(t *testing.T) {
9999
}
100100

101101
// Read checksums.
102-
b, err := os.ReadFile(path.Join(dir, "checksums.txt"))
102+
b, err := os.ReadFile(filepath.Join(dir, "checksums.txt"))
103103
if err != nil {
104104
t.Fatalf("failed to read checksums: %v", err)
105105
}
@@ -109,7 +109,7 @@ func TestHistoryImportAndExport(t *testing.T) {
109109
entries, _ := era.ReadDir(dir, "mainnet")
110110
for i, filename := range entries {
111111
func() {
112-
f, err := os.Open(path.Join(dir, filename))
112+
f, err := os.Open(filepath.Join(dir, filename))
113113
if err != nil {
114114
t.Fatalf("error opening era file: %v", err)
115115
}

core/blockchain_repair_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ package core
2222

2323
import (
2424
"math/big"
25-
"path"
25+
"path/filepath"
2626
"testing"
2727
"time"
2828

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

17631763
// Create a temporary persistent database
17641764
datadir := t.TempDir()
1765-
ancient := path.Join(datadir, "ancient")
1765+
ancient := filepath.Join(datadir, "ancient")
17661766

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

19131913
// Create a temporary persistent database
19141914
datadir := t.TempDir()
1915-
ancient := path.Join(datadir, "ancient")
1915+
ancient := filepath.Join(datadir, "ancient")
19161916

19171917
db, err := rawdb.Open(rawdb.OpenOptions{
19181918
Directory: datadir,

core/blockchain_snapshot_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"fmt"
2525
"math/big"
2626
"os"
27-
"path"
27+
"path/filepath"
2828
"strings"
2929
"testing"
3030
"time"
@@ -63,7 +63,7 @@ type snapshotTestBasic struct {
6363
func (basic *snapshotTestBasic) prepare(t *testing.T) (*BlockChain, []*types.Block) {
6464
// Create a temporary persistent database
6565
datadir := t.TempDir()
66-
ancient := path.Join(datadir, "ancient")
66+
ancient := filepath.Join(datadir, "ancient")
6767

6868
db, err := rawdb.Open(rawdb.OpenOptions{
6969
Directory: datadir,

core/rawdb/freezer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"math/rand"
2525
"os"
2626
"path"
27+
"path/filepath"
2728
"sync"
2829
"testing"
2930

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

395396
// Create file in dir1 and fill with data
396-
f, err := os.Create(path.Join(dir1, fname))
397+
f, err := os.Create(filepath.Join(dir1, fname))
397398
if err != nil {
398399
t.Fatal(err)
399400
}

0 commit comments

Comments
 (0)