mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-23 07:29:00 -08:00
fix: safely check if the directory exists (#7353)
Signed-off-by: nikpivkin <nikita.pivkin@smartforce.io>
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package fsutils
|
package fsutils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
@@ -59,18 +58,13 @@ func CopyFile(src, dst string) (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DirExists(path string) bool {
|
func DirExists(path string) bool {
|
||||||
if f, err := os.Stat(path); os.IsNotExist(err) || !f.IsDir() {
|
f, err := os.Stat(path)
|
||||||
return false
|
return err == nil && f.IsDir()
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FileExists(filename string) bool {
|
func FileExists(filename string) bool {
|
||||||
_, err := os.Stat(filename)
|
f, err := os.Stat(filename)
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
return err == nil && !f.IsDir()
|
||||||
return false
|
|
||||||
}
|
|
||||||
return err == nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type WalkDirRequiredFunc func(path string, d fs.DirEntry) bool
|
type WalkDirRequiredFunc func(path string, d fs.DirEntry) bool
|
||||||
|
|||||||
@@ -2,29 +2,13 @@ package fsutils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func touch(t *testing.T, name string) {
|
|
||||||
f, err := os.Create(name)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := f.Close(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func write(t *testing.T, name, content string) {
|
|
||||||
err := os.WriteFile(name, []byte(content), 0666)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCopyFile(t *testing.T) {
|
func TestCopyFile(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
src string
|
src string
|
||||||
@@ -72,3 +56,47 @@ func TestCopyFile(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDirExists(t *testing.T) {
|
||||||
|
t.Run("invalid path", func(t *testing.T) {
|
||||||
|
assert.False(t, DirExists("\000invalid:path"))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("valid path", func(t *testing.T) {
|
||||||
|
assert.True(t, DirExists(t.TempDir()))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("dir not exist", func(t *testing.T) {
|
||||||
|
assert.False(t, DirExists(filepath.Join(t.TempDir(), "tmp")))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("file path", func(t *testing.T) {
|
||||||
|
filePath := filepath.Join(t.TempDir(), "tmp")
|
||||||
|
f, err := os.Create(filePath)
|
||||||
|
require.NoError(t, f.Close())
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.False(t, DirExists(filePath))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileExists(t *testing.T) {
|
||||||
|
t.Run("invalid path", func(t *testing.T) {
|
||||||
|
assert.False(t, FileExists("\000invalid:path"))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("valid path", func(t *testing.T) {
|
||||||
|
filePath := filepath.Join(t.TempDir(), "tmp")
|
||||||
|
f, err := os.Create(filePath)
|
||||||
|
require.NoError(t, f.Close())
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.True(t, FileExists(filePath))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("file not exist", func(t *testing.T) {
|
||||||
|
assert.False(t, FileExists(filepath.Join(t.TempDir(), "tmp")))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("dir path", func(t *testing.T) {
|
||||||
|
assert.False(t, FileExists(t.TempDir()))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user