Skip to content

Commit c53f3c5

Browse files
gammazerolidel
authored andcommitted
chore: stop using go-homedir (#10568)
* chore: stop using go-homedir The `github.com/mitchellh/go-homedir` repo is archived, no longer needed, and no longer maintained. - `homedir.Dir` is replaced by the stdlib `os.UserHomeDir` - `homedir.Expand` is replaced by fsutil.ExpandHome` in the `github.com/ipfs/kubo/misc/fsutil` package. Additional functionality, such as `DirWritable` and `FileExists` was moved into or included in the `github.com/ipfs/kubo/misc/fsutil` package. (cherry picked from commit 4009ad3)
1 parent 999aea8 commit c53f3c5

File tree

14 files changed

+200
-94
lines changed

14 files changed

+200
-94
lines changed

client/rpc/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
ipfs "github.com/ipfs/kubo"
2121
iface "github.com/ipfs/kubo/core/coreiface"
2222
caopts "github.com/ipfs/kubo/core/coreiface/options"
23+
"github.com/ipfs/kubo/misc/fsutil"
2324
dagpb "github.com/ipld/go-codec-dagpb"
2425
_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
2526
"github.com/ipld/go-ipld-prime/node/basicnode"
26-
"github.com/mitchellh/go-homedir"
2727
ma "github.com/multiformats/go-multiaddr"
2828
manet "github.com/multiformats/go-multiaddr/net"
2929
)
@@ -82,7 +82,7 @@ func NewPathApi(ipfspath string) (*HttpApi, error) {
8282

8383
// ApiAddr reads api file in specified ipfs path.
8484
func ApiAddr(ipfspath string) (ma.Multiaddr, error) {
85-
baseDir, err := homedir.Expand(ipfspath)
85+
baseDir, err := fsutil.ExpandHome(ipfspath)
8686
if err != nil {
8787
return nil, err
8888
}

cmd/ipfswatch/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import (
1616
core "github.com/ipfs/kubo/core"
1717
coreapi "github.com/ipfs/kubo/core/coreapi"
1818
corehttp "github.com/ipfs/kubo/core/corehttp"
19+
"github.com/ipfs/kubo/misc/fsutil"
1920
fsrepo "github.com/ipfs/kubo/repo/fsrepo"
2021

2122
fsnotify "github.com/fsnotify/fsnotify"
2223
"github.com/ipfs/boxo/files"
2324
process "github.com/jbenet/goprocess"
24-
homedir "github.com/mitchellh/go-homedir"
2525
)
2626

2727
var (
@@ -57,7 +57,7 @@ func run(ipfsPath, watchPath string) error {
5757
proc := process.WithParent(process.Background())
5858
log.Printf("running IPFSWatch on '%s' using repo at '%s'...", watchPath, ipfsPath)
5959

60-
ipfsPath, err := homedir.Expand(ipfsPath)
60+
ipfsPath, err := fsutil.ExpandHome(ipfsPath)
6161
if err != nil {
6262
return err
6363
}

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"path/filepath"
1010
"strings"
1111

12-
"github.com/mitchellh/go-homedir"
12+
"github.com/ipfs/kubo/misc/fsutil"
1313
)
1414

1515
// Config is used to load ipfs config files.
@@ -59,7 +59,7 @@ func PathRoot() (string, error) {
5959
dir := os.Getenv(EnvDir)
6060
var err error
6161
if len(dir) == 0 {
62-
dir, err = homedir.Expand(DefaultPathRoot)
62+
dir, err = fsutil.ExpandHome(DefaultPathRoot)
6363
}
6464
return dir, err
6565
}

docs/changelogs/v0.32.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [🔦 Highlights](#-highlights)
99
- [🎯 AutoTLS: Automatic Certificates for libp2p WebSockets via `libp2p.direct`](#-autotls-automatic-certificates-for-libp2p-websockets-via-libp2pdirect)
1010
- [📦️ Boxo and go-libp2p updates](#-boxo-and-go-libp2p-updates)
11+
- [Replaced dependency on archived `github.com/mitchellh/go-homedir`](replaced-go-homedir)
1112
- [📝 Changelog](#-changelog)
1213
- [👨‍👩‍👧‍👦 Contributors](#-contributors)
1314

@@ -31,6 +32,14 @@ See [`AutoTLS`](https://github.com/ipfs/kubo/blob/master/docs/config.md#autotls)
3132
- update `go-libp2p-kad-dht` to [v0.27.0](https://github.com/libp2p/go-libp2p-kad-dht/releases/tag/v0.27.0)
3233
- update `go-libp2p-pubsub` to [v0.12.0](https://github.com/libp2p/go-libp2p-pubsub/releases/tag/v0.12.0)
3334

35+
### Replaced go-homedir
36+
37+
The `github.com/mitchellh/go-homedir` repo is archived, no longer needed, and no longer maintained.
38+
39+
- `homedir.Dir` is replaced by the stdlib `os.UserHomeDir`
40+
- `homedir.Expand` is replaced by `fsutil.ExpandHome` in the `github.com/ipfs/kubo/misc/fsutil` package.
41+
- The new `github.com/ipfs/kubo/misc/fsutil` package contains file utility code previously located elsewhere in kubo.
42+
3443
### 📝 Changelog
3544

3645
### 👨‍👩‍👧‍👦 Contributors

docs/examples/kubo-as-a-library/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ require (
144144
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
145145
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
146146
github.com/minio/sha256-simd v1.0.1 // indirect
147-
github.com/mitchellh/go-homedir v1.1.0 // indirect
148147
github.com/mr-tron/base58 v1.2.0 // indirect
149148
github.com/multiformats/go-base32 v0.1.0 // indirect
150149
github.com/multiformats/go-base36 v0.2.0 // indirect

docs/examples/kubo-as-a-library/go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,6 @@ github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl
535535
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
536536
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
537537
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
538-
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
539538
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
540539
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
541540
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ require (
6464
github.com/libp2p/go-libp2p-routing-helpers v0.7.4
6565
github.com/libp2p/go-libp2p-testing v0.12.0
6666
github.com/libp2p/go-socket-activation v0.1.0
67-
github.com/mitchellh/go-homedir v1.1.0
6867
github.com/multiformats/go-multiaddr v0.13.0
6968
github.com/multiformats/go-multiaddr-dns v0.4.0
7069
github.com/multiformats/go-multibase v0.2.0

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl
629629
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
630630
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
631631
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
632-
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
633632
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
634633
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
635634
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

misc/fsutil/fsutil.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package fsutil
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io/fs"
7+
"os"
8+
"path/filepath"
9+
)
10+
11+
// DirWritable checks if a directory is writable. If the directory does
12+
// not exist it is created with writable permission.
13+
func DirWritable(dir string) error {
14+
if dir == "" {
15+
return errors.New("directory not specified")
16+
}
17+
18+
var err error
19+
dir, err = ExpandHome(dir)
20+
if err != nil {
21+
return err
22+
}
23+
24+
fi, err := os.Stat(dir)
25+
if err != nil {
26+
if errors.Is(err, fs.ErrNotExist) {
27+
// Directory does not exist, so create it.
28+
err = os.Mkdir(dir, 0775)
29+
if err == nil {
30+
return nil
31+
}
32+
}
33+
if errors.Is(err, fs.ErrPermission) {
34+
err = fs.ErrPermission
35+
}
36+
return fmt.Errorf("directory not writable: %s: %w", dir, err)
37+
}
38+
if !fi.IsDir() {
39+
return fmt.Errorf("not a directory: %s", dir)
40+
}
41+
42+
// Directory exists, check that a file can be written.
43+
file, err := os.CreateTemp(dir, "writetest")
44+
if err != nil {
45+
if errors.Is(err, fs.ErrPermission) {
46+
err = fs.ErrPermission
47+
}
48+
return fmt.Errorf("directory not writable: %s: %w", dir, err)
49+
}
50+
file.Close()
51+
return os.Remove(file.Name())
52+
}
53+
54+
// ExpandHome expands the path to include the home directory if the path is
55+
// prefixed with `~`. If it isn't prefixed with `~`, the path is returned
56+
// as-is.
57+
func ExpandHome(path string) (string, error) {
58+
if path == "" {
59+
return path, nil
60+
}
61+
62+
if path[0] != '~' {
63+
return path, nil
64+
}
65+
66+
if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
67+
return "", errors.New("cannot expand user-specific home dir")
68+
}
69+
70+
dir, err := os.UserHomeDir()
71+
if err != nil {
72+
return "", err
73+
}
74+
75+
return filepath.Join(dir, path[1:]), nil
76+
}
77+
78+
// FileExists return true if the file exists
79+
func FileExists(filename string) bool {
80+
_, err := os.Lstat(filename)
81+
return !errors.Is(err, os.ErrNotExist)
82+
}

misc/fsutil/fsutil_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package fsutil_test
2+
3+
import (
4+
"io/fs"
5+
"os"
6+
"path/filepath"
7+
"runtime"
8+
"testing"
9+
10+
"github.com/ipfs/kubo/misc/fsutil"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestDirWritable(t *testing.T) {
15+
err := fsutil.DirWritable("")
16+
require.Error(t, err)
17+
18+
err = fsutil.DirWritable("~nosuchuser/tmp")
19+
require.Error(t, err)
20+
21+
tmpDir := t.TempDir()
22+
23+
wrDir := filepath.Join(tmpDir, "readwrite")
24+
err = fsutil.DirWritable(wrDir)
25+
require.NoError(t, err)
26+
27+
// Check that DirWritable created directory.
28+
fi, err := os.Stat(wrDir)
29+
require.NoError(t, err)
30+
require.True(t, fi.IsDir())
31+
32+
err = fsutil.DirWritable(wrDir)
33+
require.NoError(t, err)
34+
35+
// If running on Windows, skip read-only directory tests.
36+
if runtime.GOOS == "windows" {
37+
t.SkipNow()
38+
}
39+
40+
roDir := filepath.Join(tmpDir, "readonly")
41+
require.NoError(t, os.Mkdir(roDir, 0500))
42+
err = fsutil.DirWritable(roDir)
43+
require.ErrorIs(t, err, fs.ErrPermission)
44+
45+
roChild := filepath.Join(roDir, "child")
46+
err = fsutil.DirWritable(roChild)
47+
require.ErrorIs(t, err, fs.ErrPermission)
48+
}
49+
50+
func TestFileExists(t *testing.T) {
51+
fileName := filepath.Join(t.TempDir(), "somefile")
52+
require.False(t, fsutil.FileExists(fileName))
53+
54+
file, err := os.Create(fileName)
55+
require.NoError(t, err)
56+
file.Close()
57+
58+
require.True(t, fsutil.FileExists(fileName))
59+
}
60+
61+
func TestExpandHome(t *testing.T) {
62+
dir, err := fsutil.ExpandHome("")
63+
require.NoError(t, err)
64+
require.Equal(t, "", dir)
65+
66+
origDir := filepath.Join("somedir", "somesub")
67+
dir, err = fsutil.ExpandHome(origDir)
68+
require.NoError(t, err)
69+
require.Equal(t, origDir, dir)
70+
71+
_, err = fsutil.ExpandHome(filepath.FromSlash("~nosuchuser/somedir"))
72+
require.Error(t, err)
73+
74+
homeEnv := "HOME"
75+
if runtime.GOOS == "windows" {
76+
homeEnv = "USERPROFILE"
77+
}
78+
origHome := os.Getenv(homeEnv)
79+
defer os.Setenv(homeEnv, origHome)
80+
homeDir := filepath.Join(t.TempDir(), "testhome")
81+
os.Setenv(homeEnv, homeDir)
82+
83+
const subDir = "mytmp"
84+
origDir = filepath.Join("~", subDir)
85+
dir, err = fsutil.ExpandHome(origDir)
86+
require.NoError(t, err)
87+
require.Equal(t, filepath.Join(homeDir, subDir), dir)
88+
89+
os.Unsetenv(homeEnv)
90+
_, err = fsutil.ExpandHome(origDir)
91+
require.Error(t, err)
92+
}

0 commit comments

Comments
 (0)