-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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)
- Loading branch information
Showing
14 changed files
with
200 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.