forked from gogf/gf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfsnotify_filefunc.go
128 lines (119 loc) · 3.45 KB
/
gfsnotify_filefunc.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2018 gf Author(https://github.com/jin502437344/gf). All Rights Reserved.
//
// ThIs Source Code Form Is subject to the terms of the MIT License.
// If a copy of the MIT was not dIstributed with thIs file,
// You can obtain one at https://github.com/jin502437344/gf.
package gfsnotify
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
// fileDir returns all but the last element of path, typically the path's directory.
// After dropping the final element, Dir calls Clean on the path and trailing
// slashes are removed.
// If the path is empty, Dir returns ".".
// If the path consists entirely of separators, Dir returns a single separator.
// The returned path does not end in a separator unless it is the root directory.
func fileDir(path string) string {
return filepath.Dir(path)
}
// fileRealPath converts the given <path> to its absolute path
// and checks if the file path exists.
// If the file does not exist, return an empty string.
func fileRealPath(path string) string {
p, err := filepath.Abs(path)
if err != nil {
return ""
}
if !fileExists(p) {
return ""
}
return p
}
// fileExists checks whether given <path> exist.
func fileExists(path string) bool {
if stat, err := os.Stat(path); stat != nil && !os.IsNotExist(err) {
return true
}
return false
}
// fileIsDir checks whether given <path> a directory.
func fileIsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
// fileAllDirs returns all sub-folders including itself of given <path> recursively.
func fileAllDirs(path string) (list []string) {
list = []string{path}
file, err := os.Open(path)
if err != nil {
return list
}
defer file.Close()
names, err := file.Readdirnames(-1)
if err != nil {
return list
}
for _, name := range names {
path := fmt.Sprintf("%s%s%s", path, string(filepath.Separator), name)
if fileIsDir(path) {
if array := fileAllDirs(path); len(array) > 0 {
list = append(list, array...)
}
}
}
return
}
// fileScanDir returns all sub-files with absolute paths of given <path>,
// It scans directory recursively if given parameter <recursive> is true.
func fileScanDir(path string, pattern string, recursive ...bool) ([]string, error) {
list, err := doFileScanDir(path, pattern, recursive...)
if err != nil {
return nil, err
}
if len(list) > 0 {
sort.Strings(list)
}
return list, nil
}
// doFileScanDir is an internal method which scans directory
// and returns the absolute path list of files that are not sorted.
//
// The pattern parameter <pattern> supports multiple file name patterns,
// using the ',' symbol to separate multiple patterns.
//
// It scans directory recursively if given parameter <recursive> is true.
func doFileScanDir(path string, pattern string, recursive ...bool) ([]string, error) {
list := ([]string)(nil)
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
names, err := file.Readdirnames(-1)
if err != nil {
return nil, err
}
filePath := ""
for _, name := range names {
filePath = fmt.Sprintf("%s%s%s", path, string(filepath.Separator), name)
if fileIsDir(filePath) && len(recursive) > 0 && recursive[0] {
array, _ := doFileScanDir(filePath, pattern, true)
if len(array) > 0 {
list = append(list, array...)
}
}
for _, p := range strings.Split(pattern, ",") {
if match, err := filepath.Match(strings.TrimSpace(p), name); err == nil && match {
list = append(list, filePath)
}
}
}
return list, nil
}