-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_enttype.go
More file actions
51 lines (44 loc) · 1.34 KB
/
type_enttype.go
File metadata and controls
51 lines (44 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package symwalker
import (
"os"
)
// entType is a simplified file type system.
type entType string
var (
entTypeDir entType = "dir"
entTypeLink entType = "link"
entTypeFile entType = "file"
entTypeOther entType = "other"
entTypeErrored entType = "errored"
)
// string returns the string representation of the entType.
func (st entType) string() string {
return string(st)
}
// isEntType determines the entType by retrieving the path's info (with os.Lstat).
// The function returns the result received by calling the entTypeFromInfo function,
// which is passed the info from os.Lstat.
func isEntType(path string) entType {
info, err := os.Lstat(path)
if err != nil {
return entTypeErrored
}
return entTypeFromInfo(info)
}
// entTypeFromInfo determines the entType based on the os.FileInfo provided.
// If the FileInfo represents a directory, the function will return entTypeDir.
// If the FileInfo represents a symbolic link, the function will return entTypeLink.
// If the FileInfo represents a regular file, the function will return entTypeFile.
// Otherwise, it will return entTypeOther.
func entTypeFromInfo(info os.FileInfo) (st entType) {
if info.IsDir() {
return entTypeDir
}
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
return entTypeLink
}
if info.Mode().IsRegular() {
return entTypeFile
}
return entTypeOther
}