Skip to content

Commit

Permalink
Rework Event to have platform-specific event types
Browse files Browse the repository at this point in the history
  • Loading branch information
rjeczalik committed Sep 20, 2014
1 parent 9493a2d commit 22da03d
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 53 deletions.
25 changes: 25 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package notify

import "strings"

// Event TODO
type Event int

// String implements fmt.Stringer interface.
func (e Event) String() string {
var s []string
for ev, str := range estr {
if e&ev == ev {
s = append(s, str)
}
}
return strings.Join(s, "|")
}

// EventInfo TODO
type EventInfo interface {
Event() Event // TODO
IsDir() bool // TODO
Name() string // TODO
Sys() interface{} // TODO
}
55 changes: 55 additions & 0 deletions event_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// +build linux

package notify

import "syscall"

var (
Create = IN_CREATE
Delete = IN_DELETE
Write = IN_MODIFY
Move = IN_MOVE
)

// All TODO
var All = IN_ALL_EVENTS

// Events TODO
const (
IN_ACCESS = Event(syscall.IN_ACCESS)
IN_MODIFY = Event(syscall.IN_MODIFY)
IN_ATTRIB = Event(syscall.IN_ATTRIB)
IN_CLOSE_WRITE = Event(syscall.IN_CLOSE_WRITE)
IN_CLOSE_NOWRITE = Event(syscall.IN_CLOSE_NOWRITE)
IN_OPEN = Event(syscall.IN_OPEN)
IN_MOVED_FROM = Event(syscall.IN_MOVED_FROM)
IN_MOVED_TO = Event(syscall.IN_MOVED_TO)
IN_CREATE = Event(syscall.IN_CREATE)
IN_DELETE = Event(syscall.IN_DELETE)
IN_DELETE_SELF = Event(syscall.IN_DELETE_SELF)
IN_MOVE_SELF = Event(syscall.IN_MOVE_SELF)
IN_CLOSE = Event(syscall.IN_CLOSE)
IN_MOVE = Event(syscall.IN_MOVE)
IN_ALL_EVENTS = Event(syscall.IN_ALL_EVENTS)
)

var estr = map[Event]string{
Create: "create",
Delete: "delete",
Write: "write",
Move: "move",
IN_ACCESS: "syscall.IN_ACCESS",
IN_MODIFY: "syscall.IN_MODIFY",
IN_ATTRIB: "syscall.IN_ATTRIB",
IN_CLOSE_WRITE: "syscall.IN_CLOSE_WRITE",
IN_CLOSE_NOWRITE: "syscall.IN_CLOSE_NO_WRITE",
IN_OPEN: "syscall.IN_OPEN",
IN_MOVED_FROM: "syscall.IN_MOVED_FROM",
IN_MOVED_TO: "syscall.IN_MOVED_TO",
IN_CREATE: "syscall.IN_CREATE",
IN_DELETE: "syscall.IN_DELETE",
IN_DELETE_SELF: "syscall.IN_DELETE_SELF",
IN_MOVE_SELF: "syscall.IN_MOVE_SELF",
IN_CLOSE: "syscall.IN_CLOSE",
IN_MOVE: "syscall.IN_MOVE",
}
29 changes: 29 additions & 0 deletions event_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// +build !linux

package notify

var (
Create = EV_STUB_CREATE
Delete = EV_STUB_DELETE
Write = EV_STUB_WRITE
Move = EV_STUB_MOVE
)

// All TODO
var All = EV_STUB_ALL

// Events TODO
const (
EV_STUB_CREATE = Event(0x00000001)
EV_STUB_DELETE = Event(0x00000002)
EV_STUB_WRITE = Event(0x00000004)
EV_STUB_MOVE = Event(0x00000008)
EV_STUB_ALL = Event(0x0000000f)
)

var estr = map[Event]string{
Create: "create",
Delete: "delete",
Write: "write",
Move: "move",
}
33 changes: 33 additions & 0 deletions event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package notify

import "testing"

func mock(m map[Event]string) func() {
old := estr
estr = m
return func() {
estr = old
}
}

// This test is not safe to run in parallel with others.
func TestEventString(t *testing.T) {
m := map[Event]string{
0x01: "A",
0x02: "B",
0x04: "C",
0x08: "D",
0x0F: "E",
}
defer mock(m)()
cases := map[Event]string{
0x01: "A",
0x03: "A|B",
0x07: "A|B|C",
}
for e, str := range cases {
if s := e.String(); s != str {
t.Errorf("want s=%s; got %s (e=%#x)", str, s, e)
}
}
}
38 changes: 0 additions & 38 deletions notify.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,5 @@
package notify

import "strings"

// Event TODO
type Event uint8

const (
Create Event = 1 << iota
Delete
Write
Move
)

const All Event = Create | Delete | Write | Move

var s = map[Event]string{
Create: "Create",
Delete: "Delete",
Write: "Write",
Move: "Move",
}

// String TODO
func (e Event) String() string {
var z []string
for _, event := range splitevents(e) {
z = append(z, s[event])
}
return strings.Join(z, ",")
}

// EventInfo TODO
type EventInfo interface {
Event() Event // TODO
IsDir() bool // TODO
Name() string // TODO
Sys() interface{} // TODO
}

// Watch TODO
func Watch(name string, c chan<- EventInfo, events ...Event) {
global.Watch(name, c, events...)
Expand Down
11 changes: 0 additions & 11 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ func joinevents(events []Event) (e Event) {
return
}

// Splitevents TODO
func splitevents(e Event) (events []Event) {
// TODO(rjeczalik): All is sum of all events, add AllSlice?
for _, event := range []Event{Create, Delete, Write, Move} {
if e&event != 0 {
events = append(events, event)
}
}
return
}

// Walkpath TODO
func walkpath(p string, fn func(string) bool) bool {
if p == "" || p == "." {
Expand Down
4 changes: 0 additions & 4 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ func TestJoinevents(t *testing.T) {
}
}

func TestSplitevents(t *testing.T) {
t.Skip("TODO(rjeczalik)")
}

func TestWalkpath(t *testing.T) {
cases := map[string]struct {
p []string
Expand Down

0 comments on commit 22da03d

Please sign in to comment.