forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_other.go
43 lines (34 loc) · 898 Bytes
/
file_other.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
// +build !windows
package file
import (
"fmt"
"os"
"syscall"
)
type StateOS struct {
Inode uint64 `json:"inode,"`
Device uint64 `json:"device,"`
}
// GetOSState returns the FileStateOS for non windows systems
func GetOSState(info os.FileInfo) StateOS {
stat := info.Sys().(*syscall.Stat_t)
// Convert inode and dev to uint64 to be cross platform compatible
fileState := StateOS{
Inode: uint64(stat.Ino),
Device: uint64(stat.Dev),
}
return fileState
}
// IsSame file checks if the files are identical
func (fs StateOS) IsSame(state StateOS) bool {
return fs.Inode == state.Inode && fs.Device == state.Device
}
func (fs StateOS) String() string {
return fmt.Sprintf("%d-%d", fs.Inode, fs.Device)
}
// ReadOpen opens a file for reading only
func ReadOpen(path string) (*os.File, error) {
flag := os.O_RDONLY
perm := os.FileMode(0)
return os.OpenFile(path, flag, perm)
}