-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinotify_unix.go
174 lines (162 loc) · 4.55 KB
/
inotify_unix.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"syscall"
"unsafe"
"github.com/mattn/go-gtk/gdk"
"github.com/mattn/go-gtk/gtk"
)
var name_by_wd map[int32]string
var wd_by_name map[string]int32
var inotify_fd int
var event_size int
var epoll_fd int
const NEVENTS int = 1024
func init_inotify() {
var err error
name_by_wd = make(map[int32]string)
wd_by_name = make(map[string]int32)
var event syscall.InotifyEvent
event_size = int(unsafe.Sizeof(event))
inotify_fd, _ = syscall.InotifyInit()
if -1 == inotify_fd {
bump_message("InotifyInit failed, file changes outside of tabby " +
"will remain unnoticed")
return
}
epoll_fd, err = syscall.EpollCreate(1)
if -1 == epoll_fd {
tabby_log("init_inotify: " + err.Error())
}
var epoll_event syscall.EpollEvent
epoll_event.Events = syscall.EPOLLIN
syscall.EpollCtl(epoll_fd, syscall.EPOLL_CTL_ADD, inotify_fd, &epoll_event)
go inotify_observe()
}
func inotify_add_watch(name string) {
wd, err := syscall.InotifyAddWatch(inotify_fd, name,
syscall.IN_MODIFY|syscall.IN_DELETE_SELF|syscall.IN_MOVE_SELF)
if -1 == wd {
if err == syscall.ENOENT {
// Dirty hack.
return
}
tabby_log("InotifyAddWatch failed, changes of file " + name +
" outside of tabby will remain unnoticed, errno = " + err.Error())
return
}
name_by_wd[int32(wd)] = name
wd_by_name[name] = int32(wd)
}
func inotify_rm_watch(name string) {
wd, found := wd_by_name[name]
if false == found {
return
}
retval, _ /*err*/ := syscall.InotifyRmWatch(inotify_fd, uint32(wd))
if -1 == retval {
//println("tabby: InotifyRmWatch failed, errno = ", err)
return
}
delete(name_by_wd, wd)
delete(wd_by_name, name)
}
func inotify_observe() {
buf := make([]byte, event_size*NEVENTS)
for {
collect := inotify_observe_collect(buf)
if 0 == len(collect) {
continue
}
gdk.ThreadsEnter()
file_save_current()
reload := inotify_dialog(collect)
for name, _ := range collect {
rec, rec_found := file_map[name]
if false == rec_found {
tabby_log("inotify_observe: " + name + " not found in file_map")
continue
}
if reload {
// Reload file content.
read_ok, buf := open_file_read_to_buf(name, true)
if read_ok {
rec.buf = buf
rec.modified = false
inotify_rm_watch(name)
inotify_add_watch(name)
} else {
rec.modified = true
}
} else {
// Keep file as is.
rec.modified = true
}
}
file_tree_store()
// So as to renew current TextBuffer it is required to switch to cur_file.
file_switch_to(cur_file)
gdk.ThreadsLeave()
}
}
func inotify_observe_collect(buf []byte) map[string]int {
epoll_buf := make([]syscall.EpollEvent, 1)
collect := make(map[string]int)
for {
nread, _ := syscall.Read(inotify_fd, buf)
for offset := 0; offset < nread; offset += event_size {
event := (*syscall.InotifyEvent)(unsafe.Pointer(&buf[offset]))
if syscall.IN_IGNORED == event.Mask {
continue
}
collect[name_by_wd[event.Wd]] = 1
}
nevents, err := syscall.EpollWait(epoll_fd, epoll_buf, 500)
if 0 >= nevents {
if -1 == nevents {
tabby_log("inotify_observe_collect: " + err.Error())
}
break
}
}
return collect
}
// Returns true in case of reloading files, and false in case of keeping as is.
func inotify_dialog(s map[string]int) bool {
if nil == accel_group {
accel_group = gtk.NewAccelGroup()
}
inotify_dlg := gtk.NewDialog()
defer inotify_dlg.Destroy()
inotify_dlg.SetTitle("Some files have beed modified outside of tabby")
inotify_dlg.AddButton("_Reload all", gtk.RESPONSE_ACCEPT)
inotify_dlg.AddButton("_Keep all as is", gtk.RESPONSE_CANCEL)
w := inotify_dlg.GetWidgetForResponse(int(gtk.RESPONSE_ACCEPT))
inotify_dlg.AddAccelGroup(accel_group)
w.AddAccelerator("clicked", accel_group, gdk.KEY_Return,
0, gtk.ACCEL_VISIBLE)
inotify_dlg.SetSizeRequest(800, 350)
inotify_store := gtk.NewTreeStore(gtk.TYPE_STRING)
inotify_view := gtk.NewTreeView()
inotify_view.AppendColumn(
gtk.NewTreeViewColumnWithAttributes("text", gtk.NewCellRendererText(), "text", 0))
inotify_view.ModifyFontEasy("Regular 8")
inotify_model := inotify_store.ToTreeModel()
inotify_view.SetModel(inotify_model)
inotify_view.SetHeadersVisible(false)
var iter gtk.TreeIter
for name, _ := range s {
inotify_store.Append(&iter, nil)
inotify_store.Set(&iter, name)
}
inotify_view.SetVisible(true)
view_window := gtk.NewScrolledWindow(nil, nil)
view_window.SetPolicy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
view_window.SetVisible(true)
view_window.Add(inotify_view)
vbox := inotify_dlg.GetVBox()
vbox.Add(view_window)
if gtk.RESPONSE_ACCEPT == inotify_dlg.Run() {
return true
}
return false
}