forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
56 lines (45 loc) · 1.15 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
package file
import (
"os"
"github.com/elastic/beats/libbeat/logp"
)
type File struct {
File *os.File
FileInfo os.FileInfo
Path string
State *State
}
// Check that the file isn't a symlink, mode is regular or file is nil
func (f *File) IsRegular() bool {
if f.File == nil {
logp.Critical("Harvester: BUG: f arg is nil")
return false
}
info, e := f.File.Stat()
if e != nil {
logp.Err("File check fault: stat error: %s", e.Error())
return false
}
if !info.Mode().IsRegular() {
logp.Warn("Harvester: not a regular file: %q %s", info.Mode(), info.Name())
return false
}
return true
}
// Checks if the two files are the same.
func (f *File) IsSameFile(f2 *File) bool {
return os.SameFile(f.FileInfo, f2.FileInfo)
}
// IsSameFile checks if the given File path corresponds with the FileInfo given
func IsSameFile(path string, info os.FileInfo) bool {
fileInfo, err := os.Stat(path)
if err != nil {
logp.Err("Error during file comparison: %s with %s - Error: %s", path, info.Name(), err)
return false
}
return os.SameFile(fileInfo, info)
}
func IsRegular(file *os.File) bool {
f := &File{File: file}
return f.IsRegular()
}