-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathfile.go
123 lines (108 loc) · 2.95 KB
/
file.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
/*
* Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package x
import (
"io"
"os"
"path/filepath"
"strings"
"github.com/dgraph-io/dgo/x"
"github.com/golang/glog"
)
// WriteFileSync is the same as bufio.WriteFile, but syncs the data before closing.
func WriteFileSync(filename string, data []byte, perm os.FileMode) error {
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
}
if _, err := f.Write(data); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
return f.Close()
}
// FindFilesFunc walks the directory 'dir' and collects all file names matched by
// func f. It will skip over directories.
// Returns empty string slice if nothing found, otherwise returns all matched file names.
func FindFilesFunc(dir string, f func(string) bool) []string {
var files []string
err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if !fi.IsDir() && f(path) {
files = append(files, path)
}
return nil
})
if err != nil {
glog.Errorf("Error while scanning %q: %s", dir, err)
}
return files
}
// FindDataFiles returns a list of data files as a string array. If str is a comma-separated list
// of paths, it returns that list. If str is a single path that is not a directory, it returns that
// path. If str is a directory, it returns the files in it that have one of the extensions in ext.
func FindDataFiles(str string, ext []string) []string {
if len(str) == 0 {
return []string{}
}
list := strings.Split(str, ",")
if len(list) == 1 {
fi, err := os.Stat(str)
if os.IsNotExist(err) {
glog.Errorf("File or directory does not exist: %s", str)
return []string{}
}
x.Check(err)
if fi.IsDir() {
match_fn := func(f string) bool {
for _, e := range ext {
if strings.HasSuffix(f, e) {
return true
}
}
return false
}
list = FindFilesFunc(str, match_fn)
}
}
return list
}
// IsMissingOrEmptyDir returns true if the path either does not exist
// or is a directory that is empty.
func IsMissingOrEmptyDir(path string) bool {
fi, err := os.Stat(path)
if os.IsNotExist(err) {
return true
}
x.Check(err)
if !fi.IsDir() {
return false
}
file, err := os.Open(path)
defer file.Close()
x.Check(err)
_, err = file.Readdir(1)
if err == nil {
return false
} else if err != io.EOF {
x.Check(err)
}
return true
}