Prevent infinite loops caused by symlinks
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-05-02 20:07:47 +02:00
parent cc2dfe234f
commit b4dcc697f0

22
main.go
View File

@@ -31,7 +31,7 @@ var (
junkCounter byte = 0 // to prevent any compiler optimizations
)
var version = "1.1"
var version = "1.2"
type volume struct {
path string
@@ -46,7 +46,7 @@ type metaFile struct {
type dirSearch struct {
dir *os.File
path string
entriesBatch []string
entriesBatch []os.DirEntry
}
type lockStats struct {
@@ -277,7 +277,7 @@ func processVolume(volume *volume, onlyStats bool, stats *lockStats) error {
if len(dir.entriesBatch) == 0 {
// Get next batch of directory entries
names, err := dir.dir.Readdirnames(1024)
entries, err := dir.dir.ReadDir(1024)
if err == io.EOF {
dir.dir.Close()
continue
@@ -286,14 +286,14 @@ func processVolume(volume *volume, onlyStats bool, stats *lockStats) error {
dir.dir.Close()
continue
}
dir.entriesBatch = names
dir.entriesBatch = entries
}
var batchDone bool = true
for i, name := range dir.entriesBatch {
for i, entry := range dir.entriesBatch {
// Objects are directories that contain xl.meta file
// PERF: Most probable case is an object
metaPath := filepath.Join(dir.path, name, metaFileName)
metaPath := filepath.Join(dir.path, entry.Name(), metaFileName)
metaInfo, err := os.Stat(metaPath)
if err == nil {
// We found an object
@@ -328,16 +328,16 @@ func processVolume(volume *volume, onlyStats bool, stats *lockStats) error {
continue
}
} else {
// Descend into subdirectories and ignore regular files
entryPath := filepath.Join(dir.path, name)
entryInfo, err := os.Stat(entryPath)
} else if entry.IsDir() {
// Descend into subdirectories that are not symlinks
entryPath := filepath.Join(dir.path, entry.Name())
entryInfo, err := os.Lstat(entryPath)
if err != nil {
// Race condition the file/directory disappeared
continue
}
if entryInfo.IsDir() {
if entryInfo.Mode()&os.ModeSymlink != os.ModeSymlink {
subdir, err := os.Open(entryPath)
if err != nil {
// Race condition the file/directory disappeared or no permission