2020-05-01 17:31:42 +02:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2020-05-03 14:40:59 +02:00
|
|
|
// SkipWalk returns true if the file or directory should be skipped in godirwalk.Walk()
|
2020-07-18 20:58:35 +02:00
|
|
|
func SkipWalk(fileName string, isDir, isSymlink bool, done Done, ignore *IgnoreList) (skip bool, result error) {
|
|
|
|
isDone := done[fileName].Exists()
|
2020-05-03 14:40:59 +02:00
|
|
|
isIgnored := ignore.Ignore(fileName)
|
2020-05-01 17:31:42 +02:00
|
|
|
|
|
|
|
if isSymlink {
|
2020-05-03 14:40:59 +02:00
|
|
|
// Symlinks are skipped by default unless they are links to directories
|
2020-05-01 17:31:42 +02:00
|
|
|
skip = true
|
|
|
|
|
|
|
|
// Symlink points to directory?
|
2020-05-03 14:40:59 +02:00
|
|
|
if link, err := os.Stat(fileName); err == nil && link.IsDir() {
|
|
|
|
resolved, err := filepath.EvalSymlinks(fileName)
|
|
|
|
|
2020-07-18 20:58:35 +02:00
|
|
|
if err != nil || isIgnored || isDone || done[resolved].Exists() {
|
2020-05-03 14:40:59 +02:00
|
|
|
result = filepath.SkipDir
|
|
|
|
} else {
|
|
|
|
// Don't traverse symlinks if they are hidden or already done...
|
2020-07-18 20:58:35 +02:00
|
|
|
done[resolved] = Found
|
2020-05-03 14:40:59 +02:00
|
|
|
}
|
2020-07-19 14:45:49 +02:00
|
|
|
} else {
|
|
|
|
result = filepath.SkipDir
|
2020-05-01 17:31:42 +02:00
|
|
|
}
|
|
|
|
} else if isDir {
|
|
|
|
skip = true
|
|
|
|
|
2020-05-03 14:40:59 +02:00
|
|
|
if isIgnored || isDone {
|
2020-05-01 17:31:42 +02:00
|
|
|
// Don't traverse directories if they are hidden or already done...
|
|
|
|
result = filepath.SkipDir
|
|
|
|
}
|
2020-05-03 14:40:59 +02:00
|
|
|
} else if isIgnored || isDone {
|
2020-05-01 17:31:42 +02:00
|
|
|
// Skip files that are hidden or already done...
|
|
|
|
skip = true
|
|
|
|
}
|
|
|
|
|
2020-05-03 16:15:54 +02:00
|
|
|
if skip {
|
2020-07-18 20:58:35 +02:00
|
|
|
done[fileName] = Found
|
2020-05-03 16:15:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 17:31:42 +02:00
|
|
|
return skip, result
|
|
|
|
}
|