-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
155 lines (137 loc) · 2.88 KB
/
utils.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"errors"
"io"
"os"
"path/filepath"
"sync"
)
// CreateFileIfNotExist attempts to create the full path and file if it does not exist
func CreateFileIfNotExist(filePath string) error {
// First check to see if given filePath isn't a directory
isDir, _ := IsDirectory(filePath)
if isDir == true {
return errors.New("given file path is a directory and not a path to a file")
}
// Check and create the base path if needed
dir, _ := filepath.Split(filePath)
if len(dir) > 0 {
err := CreateDirIfNotExist(dir)
if err != nil {
return err
}
}
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
f, err := os.Create(filePath)
defer f.Close()
if err != nil {
return err
}
}
return nil
}
// CreateDirIfNotExist attempts to create the directory if it does not exist
func CreateDirIfNotExist(path string) error {
_, err := os.Stat(path)
if err == nil {
return nil
}
if os.IsNotExist(err) {
err := os.MkdirAll(path, os.ModePerm)
if err == nil {
return nil
}
}
return nil
}
// IsDirectory return true if the path is a directory, false if it is a different
// type of file and and error if it doesn't exist at all
func IsDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
return fileInfo.IsDir(), err
}
func IsRegularFile(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
return fileInfo.Mode().IsRegular(), err
}
// PathExists return true if the file/directory exists
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
func IsDirEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()
// read in ONLY one file
_, err = f.Readdir(1)
// and if the file is EOF... well, the dir is empty.
if err == io.EOF {
return true, nil
}
return false, err
}
// task is an arbitrary item that needs to processed
type task interface {
run(int, chan<- task) bool
}
func NewTaskQueue(workercount int) (chan<- task, <-chan task, *sync.WaitGroup) {
send := make(chan task)
receive := make(chan task)
wg := &sync.WaitGroup{}
go func() {
queue := make([]task, 0)
for {
if len(queue) == 0 {
if send == nil {
close(receive)
return
}
data, ok := <-send
if !ok {
close(receive)
return
}
wg.Add(1)
queue = append(queue, data)
} else {
select {
case receive <- queue[0]:
queue = queue[1:]
case value, ok := <-send:
if ok {
wg.Add(1)
queue = append(queue, value)
} else {
send = nil
}
}
}
}
}()
for i := 0; i < workercount; i++ {
wn := i
go func() {
for task := range receive {
task.run(wn, send)
wg.Done()
}
}()
}
return send, receive, wg
}