Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursively Find Validator Database File In Slashing Protection Commands #8518

Merged
merged 13 commits into from
Feb 26, 2021
31 changes: 31 additions & 0 deletions shared/fileutil/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,37 @@ func FileExists(filename string) bool {
return info != nil && !info.IsDir()
}

// RecursiveFileFind returns true, and the path, if a file is not a directory and exists
// at dir or any of its subdirectories. Finds the first instant based on the Walk order and returns.
// Define non-fatal error to stop the recursive directory walk
var stopWalk = errors.New("stop walking")

func RecursiveFileFind(filename, dir string) (bool, string, error) {
var found bool
var fpath string
dir = filepath.Clean(dir)
found = false
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// checks if its a file and has the exact name as the filename
// need to break the walk function by using a non-fatal error
if !info.IsDir() && filename == info.Name() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the best way to check if its a file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep this is a fine approach

found = true
fpath = path
return stopWalk
}

// no errors or file found
return nil
})
if err != nil && err != stopWalk {
return false, "", err
}
return found, fpath, nil
}

// ReadFileAsBytes expands a file name's absolute path and reads it as bytes from disk.
func ReadFileAsBytes(filename string) ([]byte, error) {
filePath, err := ExpandPath(filename)
Expand Down
69 changes: 69 additions & 0 deletions shared/fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,57 @@ func TestDirFiles(t *testing.T) {
}
}

func TestRecursiveFileFind(t *testing.T) {
tmpDir, _ := tmpDirWithContentsForRecursiveFind(t)
tests := []struct {
name string
root string
path string
found bool
}{
{
name: "file1",
root: tmpDir,
path: "subfolder1/subfolder11/file1",
found: true,
},
{
name: "file2",
root: tmpDir,
path: "subfolder2/file2",
found: true,
},
{
name: "file1",
root: tmpDir + "/subfolder1",
path: "subfolder11/file1",
found: true,
},
{
name: "file3",
root: tmpDir,
path: "file3",
found: true,
},
{
name: "file4",
root: tmpDir,
path: "",
found: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
found, _, err := fileutil.RecursiveFileFind(tt.name, tt.root)
require.NoError(t, err)

assert.DeepEqual(t, tt.found, found)
//assert.DeepEqual(t, tt.path, fpath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok will do.

})
}
}

func deepCompare(t *testing.T, file1, file2 string) bool {
sf, err := os.Open(file1)
assert.NoError(t, err)
Expand Down Expand Up @@ -281,6 +332,24 @@ func tmpDirWithContents(t *testing.T) (string, []string) {
return dir, fnames
}

// tmpDirWithContentsForRecursiveFind returns path to temporary directory having some folders/files in it.
// Directory is automatically removed by internal testing cleanup methods.
func tmpDirWithContentsForRecursiveFind(t *testing.T) (string, []string) {
dir := t.TempDir()
fnames := []string{
"subfolder1/subfolder11/file1",
"subfolder2/file2",
"file3",
}
require.NoError(t, os.MkdirAll(filepath.Join(dir, "subfolder1", "subfolder11"), 0777))
require.NoError(t, os.MkdirAll(filepath.Join(dir, "subfolder2"), 0777))
for _, fname := range fnames {
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, fname), []byte(fname), 0777))
}
sort.Strings(fnames)
return dir, fnames
}

func TestHasReadWritePermissions(t *testing.T) {
type args struct {
itemPath string
Expand Down