Skip to content

Commit 4009ad3

Browse files
authored
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.
1 parent e17dc21 commit 4009ad3

File tree

14 files changed

+200
-94
lines changed

14 files changed

+200
-94
lines changed

client/rpc/api.go

+2-2
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

+2-2
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

+2-2
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

+9
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

-1
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

-1
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

-1
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

-1
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

+82
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

+92
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+
}

plugin/plugins/pebbleds/pebbleds.go

+2-70
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package pebbleds
22

33
import (
4-
"errors"
54
"fmt"
6-
"io/fs"
7-
"os"
85
"path/filepath"
96
"time"
107

118
"github.com/cockroachdb/pebble"
129
pebbleds "github.com/ipfs/go-ds-pebble"
10+
"github.com/ipfs/kubo/misc/fsutil"
1311
"github.com/ipfs/kubo/plugin"
1412
"github.com/ipfs/kubo/repo"
1513
"github.com/ipfs/kubo/repo/fsrepo"
@@ -172,75 +170,9 @@ func (c *datastoreConfig) Create(path string) (repo.Datastore, error) {
172170
p = filepath.Join(path, p)
173171
}
174172

175-
if err := dirWritable(p); err != nil {
173+
if err := fsutil.DirWritable(p); err != nil {
176174
return nil, err
177175
}
178176

179177
return pebbleds.NewDatastore(p, pebbleds.WithCacheSize(c.cacheSize), pebbleds.WithPebbleOpts(c.pebbleOpts))
180178
}
181-
182-
// dirWritable checks if a directory is writable. If the directory does
183-
// not exist it is created with writable permission.
184-
func dirWritable(dir string) error {
185-
if dir == "" {
186-
return errors.New("directory not specified")
187-
}
188-
var err error
189-
dir, err = expandHome(dir)
190-
if err != nil {
191-
return err
192-
}
193-
194-
fi, err := os.Stat(dir)
195-
if err != nil {
196-
if errors.Is(err, fs.ErrNotExist) {
197-
// Directory does not exist, so create it.
198-
err = os.Mkdir(dir, 0775)
199-
if err == nil {
200-
return nil
201-
}
202-
}
203-
if errors.Is(err, fs.ErrPermission) {
204-
err = fs.ErrPermission
205-
}
206-
return fmt.Errorf("directory not writable: %s: %w", dir, err)
207-
}
208-
if !fi.IsDir() {
209-
return fmt.Errorf("not a directory: %s", dir)
210-
}
211-
212-
// Directory exists, check that a file can be written.
213-
file, err := os.CreateTemp(dir, "writetest")
214-
if err != nil {
215-
if errors.Is(err, fs.ErrPermission) {
216-
err = fs.ErrPermission
217-
}
218-
return fmt.Errorf("directory not writable: %s: %w", dir, err)
219-
}
220-
file.Close()
221-
return os.Remove(file.Name())
222-
}
223-
224-
// expandHome expands the path to include the home directory if the path is
225-
// prefixed with `~`. If it isn't prefixed with `~`, the path is returned
226-
// as-is.
227-
func expandHome(path string) (string, error) {
228-
if path == "" {
229-
return path, nil
230-
}
231-
232-
if path[0] != '~' {
233-
return path, nil
234-
}
235-
236-
if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
237-
return "", errors.New("cannot expand user-specific home dir")
238-
}
239-
240-
dir, err := os.UserHomeDir()
241-
if err != nil {
242-
return "", err
243-
}
244-
245-
return filepath.Join(dir, path[1:]), nil
246-
}

0 commit comments

Comments
 (0)