-
Notifications
You must be signed in to change notification settings - Fork 6
/
FileMonitor.go
128 lines (114 loc) · 2.92 KB
/
FileMonitor.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
package fridago
/*
#include "frida-core.h"
extern void _on_file_change(FridaFileMonitor * file_monitor, gchar * path, gchar * other_path,
GFileMonitorEvent event_type, gpointer user_data);
*/
import "C"
import (
"unsafe"
"github.com/sirupsen/logrus"
)
var chFileMonitorEvent *chan *FileMonitorEvent
type FileMonitorEvent struct {
Path string
OtherPath string
Event string
}
func (fme *FileMonitorEvent) setEvent(eventType C.GFileMonitorEvent) {
var et string
switch eventType {
case C.G_FILE_MONITOR_EVENT_CHANGED:
et = "changed"
case C.G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
et = "changes done hint"
case C.G_FILE_MONITOR_EVENT_DELETED:
et = "deleted"
case C.G_FILE_MONITOR_EVENT_CREATED:
et = "created"
case C.G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
et = "attribute_changed"
case C.G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
et = "pre unmount"
case C.G_FILE_MONITOR_EVENT_UNMOUNTED:
et = "unmounted"
case C.G_FILE_MONITOR_EVENT_MOVED:
et = "moved"
case C.G_FILE_MONITOR_EVENT_RENAMED:
et = "renamed"
case C.G_FILE_MONITOR_EVENT_MOVED_IN:
et = "moved in"
case C.G_FILE_MONITOR_EVENT_MOVED_OUT:
et = "moved out"
default:
et = "unknown"
}
fme.Event = et
}
type FileMonitor struct {
ptr *C.FridaFileMonitor
Path string
}
func (fm *FileMonitor) Enable() (err error) {
var gerr *C.GError
cancel := C.g_cancellable_new()
C.frida_file_monitor_enable_sync(fm.ptr, cancel, &gerr)
if gerr != nil {
err = NewErrorFromGError(gerr)
}
return
}
func (fm *FileMonitor) Disable() (err error) {
var gerr *C.GError
cancel := C.g_cancellable_new()
C.frida_file_monitor_disable_sync(fm.ptr, cancel, &gerr)
if gerr != nil {
err = NewErrorFromGError(gerr)
}
return
}
func (fm *FileMonitor) connectSignal(sig string, cb unsafe.Pointer) {
C.g_signal_connect_data(C.gpointer(fm.ptr),
C.CString(sig), C.GCallback(cb),
nil, nil, 0)
}
func (fm *FileMonitor) On(sig string, ch interface{}) (err error) {
switch sig {
case "change":
if v, ok := ch.(chan *FileMonitorEvent); ok {
chFileMonitorEvent = &v
}
fm.connectSignal(sig, unsafe.Pointer(C._on_file_change))
default:
err = NewErrorAndLog("FileMonitor: signal unspported")
log.WithFields(logrus.Fields{
"signal": sig,
}).Error(err)
}
return
}
func NewFileMonitor(path string) (fm *FileMonitor, err error) {
monitor := C.frida_file_monitor_new(C.CString(path))
if IsNullCPointer(unsafe.Pointer(monitor)) {
err = NewErrorAndLog("FileMonitor: new failed")
return
}
fm = &FileMonitor{
ptr: monitor,
Path: path,
}
return
}
//export onFileChange
func onFileChange(fileMonitor *C.FridaFileMonitor, path *C.gchar, otherPath *C.gchar,
eventType C.GFileMonitorEvent, userData C.gpointer) {
log.Info("FileMonitor: On file change")
if chFileMonitorEvent != nil {
evt := &FileMonitorEvent{
Path: C.GoString(path),
OtherPath: C.GoString(otherPath),
}
evt.setEvent(eventType)
*chFileMonitorEvent <- evt
}
}