Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ipfs/kubo
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 702101e877118caf975ac763b3a910f611114005
Choose a base ref
..
head repository: ipfs/kubo
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 60d8d5c78593d4904a5b82b99dbaf3106b5a7b90
Choose a head ref
4 changes: 2 additions & 2 deletions client/rpc/api.go
Original file line number Diff line number Diff line change
@@ -20,10 +20,10 @@ import (
ipfs "github.com/ipfs/kubo"
iface "github.com/ipfs/kubo/core/coreiface"
caopts "github.com/ipfs/kubo/core/coreiface/options"
"github.com/ipfs/kubo/misc/fsutil"
dagpb "github.com/ipld/go-codec-dagpb"
_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/mitchellh/go-homedir"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
@@ -82,7 +82,7 @@ func NewPathApi(ipfspath string) (*HttpApi, error) {

// ApiAddr reads api file in specified ipfs path.
func ApiAddr(ipfspath string) (ma.Multiaddr, error) {
baseDir, err := homedir.Expand(ipfspath)
baseDir, err := fsutil.ExpandHome(ipfspath)
if err != nil {
return nil, err
}
4 changes: 2 additions & 2 deletions cmd/ipfswatch/main.go
Original file line number Diff line number Diff line change
@@ -16,12 +16,12 @@ import (
core "github.com/ipfs/kubo/core"
coreapi "github.com/ipfs/kubo/core/coreapi"
corehttp "github.com/ipfs/kubo/core/corehttp"
"github.com/ipfs/kubo/misc/fsutil"
fsrepo "github.com/ipfs/kubo/repo/fsrepo"

fsnotify "github.com/fsnotify/fsnotify"
"github.com/ipfs/boxo/files"
process "github.com/jbenet/goprocess"
homedir "github.com/mitchellh/go-homedir"
)

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

ipfsPath, err := homedir.Expand(ipfsPath)
ipfsPath, err := fsutil.ExpandHome(ipfsPath)
if err != nil {
return err
}
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import (
"path/filepath"
"strings"

"github.com/mitchellh/go-homedir"
"github.com/ipfs/kubo/misc/fsutil"
)

// Config is used to load ipfs config files.
@@ -59,7 +59,7 @@ func PathRoot() (string, error) {
dir := os.Getenv(EnvDir)
var err error
if len(dir) == 0 {
dir, err = homedir.Expand(DefaultPathRoot)
dir, err = fsutil.ExpandHome(DefaultPathRoot)
}
return dir, err
}
9 changes: 9 additions & 0 deletions docs/changelogs/v0.32.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
- [🔦 Highlights](#-highlights)
- [🎯 AutoTLS: Automatic Certificates for libp2p WebSockets via `libp2p.direct`](#-autotls-automatic-certificates-for-libp2p-websockets-via-libp2pdirect)
- [📦️ Boxo and go-libp2p updates](#-boxo-and-go-libp2p-updates)
- [Replaced dependency on archived `github.com/mitchellh/go-homedir`](replaced-go-homedir)
- [📝 Changelog](#-changelog)
- [👨‍👩‍👧‍👦 Contributors](#-contributors)

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

### Replaced 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.
- The new `github.com/ipfs/kubo/misc/fsutil` package contains file utility code previously located elsewhere in kubo.

### 📝 Changelog

### 👨‍👩‍👧‍👦 Contributors
3 changes: 1 addition & 2 deletions docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ replace github.com/ipfs/kubo => ./../../..
require (
github.com/ipfs/boxo v0.24.2
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
github.com/libp2p/go-libp2p v0.37.1-0.20241031193008-ccd1609deb55
github.com/libp2p/go-libp2p v0.37.1-0.20241104193226-9cefe2c8e6f9
github.com/multiformats/go-multiaddr v0.13.0
)

@@ -144,7 +144,6 @@ require (
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
5 changes: 2 additions & 3 deletions docs/examples/kubo-as-a-library/go.sum
Original file line number Diff line number Diff line change
@@ -460,8 +460,8 @@ github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZ
github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs=
github.com/libp2p/go-flow-metrics v0.2.0 h1:EIZzjmeOE6c8Dav0sNv35vhZxATIXWZg6j/C08XmmDw=
github.com/libp2p/go-flow-metrics v0.2.0/go.mod h1:st3qqfu8+pMfh+9Mzqb2GTiwrAGjIPszEjZmtksN8Jc=
github.com/libp2p/go-libp2p v0.37.1-0.20241031193008-ccd1609deb55 h1:BlJJ5JqTtv7SkZvbUE3v0jteW8J6MkYueCv0Jfzpig0=
github.com/libp2p/go-libp2p v0.37.1-0.20241031193008-ccd1609deb55/go.mod h1:GOKmSN99scDuYGTwaTbQPR8Nt6dxrK3ue7OjW2NGDg4=
github.com/libp2p/go-libp2p v0.37.1-0.20241104193226-9cefe2c8e6f9 h1:c8mJk6NBIzzuQcatj9ag5UvAG4lNkd31wdP2SkR3WV8=
github.com/libp2p/go-libp2p v0.37.1-0.20241104193226-9cefe2c8e6f9/go.mod h1:GOKmSN99scDuYGTwaTbQPR8Nt6dxrK3ue7OjW2NGDg4=
github.com/libp2p/go-libp2p-asn-util v0.4.1 h1:xqL7++IKD9TBFMgnLPZR6/6iYhawHKHl950SO9L6n94=
github.com/libp2p/go-libp2p-asn-util v0.4.1/go.mod h1:d/NI6XZ9qxw67b4e+NgpQexCIiFYJjErASrYW4PFDN8=
github.com/libp2p/go-libp2p-core v0.2.4/go.mod h1:STh4fdfa5vDYr0/SzYYeqnt+E6KfEV5VxfIrm0bcI0g=
@@ -535,7 +535,6 @@ github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ require (
github.com/ipfs/go-ds-measure v0.2.0
github.com/ipfs/go-ds-pebble v0.4.0
github.com/ipfs/go-fs-lock v0.0.7
github.com/ipfs/go-ipfs-cmds v0.13.0
github.com/ipfs/go-ipfs-cmds v0.14.0
github.com/ipfs/go-ipld-cbor v0.2.0
github.com/ipfs/go-ipld-format v0.6.0
github.com/ipfs/go-ipld-git v0.1.1
@@ -54,7 +54,7 @@ require (
github.com/jbenet/goprocess v0.1.4
github.com/julienschmidt/httprouter v1.3.0
github.com/libp2p/go-doh-resolver v0.4.0
github.com/libp2p/go-libp2p v0.37.1-0.20241031193008-ccd1609deb55
github.com/libp2p/go-libp2p v0.37.1-0.20241104193226-9cefe2c8e6f9
github.com/libp2p/go-libp2p-http v0.5.0
github.com/libp2p/go-libp2p-kad-dht v0.27.0
github.com/libp2p/go-libp2p-kbucket v0.6.4
@@ -64,7 +64,6 @@ require (
github.com/libp2p/go-libp2p-routing-helpers v0.7.4
github.com/libp2p/go-libp2p-testing v0.12.0
github.com/libp2p/go-socket-activation v0.1.0
github.com/mitchellh/go-homedir v1.1.0
github.com/multiformats/go-multiaddr v0.13.0
github.com/multiformats/go-multiaddr-dns v0.4.0
github.com/multiformats/go-multibase v0.2.0
@@ -230,7 +229,7 @@ require (
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/rs/cors v1.11.1 // indirect
github.com/samber/lo v1.46.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/texttheater/golang-levenshtein v1.0.1 // indirect
13 changes: 6 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -404,8 +404,8 @@ github.com/ipfs/go-ipfs-blockstore v1.3.1 h1:cEI9ci7V0sRNivqaOr0elDsamxXFxJMMMy7
github.com/ipfs/go-ipfs-blockstore v1.3.1/go.mod h1:KgtZyc9fq+P2xJUiCAzbRdhhqJHvsw8u2Dlqy2MyRTE=
github.com/ipfs/go-ipfs-blocksutil v0.0.1 h1:Eh/H4pc1hsvhzsQoMEP3Bke/aW5P5rVM1IWFJMcGIPQ=
github.com/ipfs/go-ipfs-blocksutil v0.0.1/go.mod h1:Yq4M86uIOmxmGPUHv/uI7uKqZNtLb449gwKqXjIsnRk=
github.com/ipfs/go-ipfs-cmds v0.13.0 h1:+WVHZMrQNkPqwAQdrSFGbJgHpOc8H2G8eszNxnvoCQA=
github.com/ipfs/go-ipfs-cmds v0.13.0/go.mod h1:GYqjGSt6u9k9tyxIDT7M0ROWeB2raPGH94uuVnpWgY0=
github.com/ipfs/go-ipfs-cmds v0.14.0 h1:sxdurhAHSdQr5VrSNJjc+t92uJObSNq+gRVm/wLZGMM=
github.com/ipfs/go-ipfs-cmds v0.14.0/go.mod h1:zj2jN7bHJ4pDucRmqdq863AQYcsqdxXrfVkr9eqPfvo=
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
github.com/ipfs/go-ipfs-delay v0.0.1 h1:r/UXYyRcddO6thwOnhiznIAiSvxMECGgtv35Xs1IeRQ=
github.com/ipfs/go-ipfs-delay v0.0.1/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
@@ -538,8 +538,8 @@ github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZ
github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs=
github.com/libp2p/go-flow-metrics v0.2.0 h1:EIZzjmeOE6c8Dav0sNv35vhZxATIXWZg6j/C08XmmDw=
github.com/libp2p/go-flow-metrics v0.2.0/go.mod h1:st3qqfu8+pMfh+9Mzqb2GTiwrAGjIPszEjZmtksN8Jc=
github.com/libp2p/go-libp2p v0.37.1-0.20241031193008-ccd1609deb55 h1:BlJJ5JqTtv7SkZvbUE3v0jteW8J6MkYueCv0Jfzpig0=
github.com/libp2p/go-libp2p v0.37.1-0.20241031193008-ccd1609deb55/go.mod h1:GOKmSN99scDuYGTwaTbQPR8Nt6dxrK3ue7OjW2NGDg4=
github.com/libp2p/go-libp2p v0.37.1-0.20241104193226-9cefe2c8e6f9 h1:c8mJk6NBIzzuQcatj9ag5UvAG4lNkd31wdP2SkR3WV8=
github.com/libp2p/go-libp2p v0.37.1-0.20241104193226-9cefe2c8e6f9/go.mod h1:GOKmSN99scDuYGTwaTbQPR8Nt6dxrK3ue7OjW2NGDg4=
github.com/libp2p/go-libp2p-asn-util v0.4.1 h1:xqL7++IKD9TBFMgnLPZR6/6iYhawHKHl950SO9L6n94=
github.com/libp2p/go-libp2p-asn-util v0.4.1/go.mod h1:d/NI6XZ9qxw67b4e+NgpQexCIiFYJjErASrYW4PFDN8=
github.com/libp2p/go-libp2p-core v0.2.4/go.mod h1:STh4fdfa5vDYr0/SzYYeqnt+E6KfEV5VxfIrm0bcI0g=
@@ -629,7 +629,6 @@ github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -828,8 +827,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
82 changes: 82 additions & 0 deletions misc/fsutil/fsutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package fsutil

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
)

// DirWritable checks if a directory is writable. If the directory does
// not exist it is created with writable permission.
func DirWritable(dir string) error {
if dir == "" {
return errors.New("directory not specified")
}

var err error
dir, err = ExpandHome(dir)
if err != nil {
return err
}

fi, err := os.Stat(dir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
// Directory does not exist, so create it.
err = os.Mkdir(dir, 0775)
if err == nil {
return nil
}
}
if errors.Is(err, fs.ErrPermission) {
err = fs.ErrPermission
}
return fmt.Errorf("directory not writable: %s: %w", dir, err)
}
if !fi.IsDir() {
return fmt.Errorf("not a directory: %s", dir)
}

// Directory exists, check that a file can be written.
file, err := os.CreateTemp(dir, "writetest")
if err != nil {
if errors.Is(err, fs.ErrPermission) {
err = fs.ErrPermission
}
return fmt.Errorf("directory not writable: %s: %w", dir, err)
}
file.Close()
return os.Remove(file.Name())
}

// ExpandHome expands the path to include the home directory if the path is
// prefixed with `~`. If it isn't prefixed with `~`, the path is returned
// as-is.
func ExpandHome(path string) (string, error) {
if path == "" {
return path, nil
}

if path[0] != '~' {
return path, nil
}

if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
return "", errors.New("cannot expand user-specific home dir")
}

dir, err := os.UserHomeDir()
if err != nil {
return "", err
}

return filepath.Join(dir, path[1:]), nil
}

// FileExists return true if the file exists
func FileExists(filename string) bool {
_, err := os.Lstat(filename)
return !errors.Is(err, os.ErrNotExist)
}
92 changes: 92 additions & 0 deletions misc/fsutil/fsutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package fsutil_test

import (
"io/fs"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/ipfs/kubo/misc/fsutil"
"github.com/stretchr/testify/require"
)

func TestDirWritable(t *testing.T) {
err := fsutil.DirWritable("")
require.Error(t, err)

err = fsutil.DirWritable("~nosuchuser/tmp")
require.Error(t, err)

tmpDir := t.TempDir()

wrDir := filepath.Join(tmpDir, "readwrite")
err = fsutil.DirWritable(wrDir)
require.NoError(t, err)

// Check that DirWritable created directory.
fi, err := os.Stat(wrDir)
require.NoError(t, err)
require.True(t, fi.IsDir())

err = fsutil.DirWritable(wrDir)
require.NoError(t, err)

// If running on Windows, skip read-only directory tests.
if runtime.GOOS == "windows" {
t.SkipNow()
}

roDir := filepath.Join(tmpDir, "readonly")
require.NoError(t, os.Mkdir(roDir, 0500))
err = fsutil.DirWritable(roDir)
require.ErrorIs(t, err, fs.ErrPermission)

roChild := filepath.Join(roDir, "child")
err = fsutil.DirWritable(roChild)
require.ErrorIs(t, err, fs.ErrPermission)
}

func TestFileExists(t *testing.T) {
fileName := filepath.Join(t.TempDir(), "somefile")
require.False(t, fsutil.FileExists(fileName))

file, err := os.Create(fileName)
require.NoError(t, err)
file.Close()

require.True(t, fsutil.FileExists(fileName))
}

func TestExpandHome(t *testing.T) {
dir, err := fsutil.ExpandHome("")
require.NoError(t, err)
require.Equal(t, "", dir)

origDir := filepath.Join("somedir", "somesub")
dir, err = fsutil.ExpandHome(origDir)
require.NoError(t, err)
require.Equal(t, origDir, dir)

_, err = fsutil.ExpandHome(filepath.FromSlash("~nosuchuser/somedir"))
require.Error(t, err)

homeEnv := "HOME"
if runtime.GOOS == "windows" {
homeEnv = "USERPROFILE"
}
origHome := os.Getenv(homeEnv)
defer os.Setenv(homeEnv, origHome)
homeDir := filepath.Join(t.TempDir(), "testhome")
os.Setenv(homeEnv, homeDir)

const subDir = "mytmp"
origDir = filepath.Join("~", subDir)
dir, err = fsutil.ExpandHome(origDir)
require.NoError(t, err)
require.Equal(t, filepath.Join(homeDir, subDir), dir)

os.Unsetenv(homeEnv)
_, err = fsutil.ExpandHome(origDir)
require.Error(t, err)
}
Loading