Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integer-overflow - initial implementation #10

Merged
merged 14 commits into from
Oct 10, 2024
2 changes: 1 addition & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func InMaskToString(in_mask uint32) string {
// InotifyEvent is the go representation of inotify_event found in sys/inotify.h
type InotifyEvent struct {
// Watch descriptor
Wd uint32
Wd int
// File or directory name
Name string
// Contains bits that describe the event that occurred
Expand Down
27 changes: 15 additions & 12 deletions inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import (
"syscall"
"time"
"unsafe"

"github.com/illarion/gonotify/v2/syscallf"
)

const (
// maxEvents is the maximum number of events to read in one syscall
maxEvents = 1024
)

var TimeoutError = errors.New("Inotify timeout")

type addWatchRequest struct {
pathName string
mask uint32
Expand All @@ -36,7 +40,7 @@ type Inotify struct {
ctx context.Context
done chan struct{}
addWatchIn chan addWatchRequest
rmByWdIn chan uint32
rmByWdIn chan int
rmByPathIn chan string
eventsOut chan eventItem

Expand All @@ -58,13 +62,13 @@ func NewInotify(ctx context.Context) (*Inotify, error) {
ctx: ctx,
done: make(chan struct{}),
addWatchIn: make(chan addWatchRequest),
rmByWdIn: make(chan uint32),
rmByWdIn: make(chan int),
rmByPathIn: make(chan string),
eventsOut: make(chan eventItem, maxEvents),
}

type getPathRequest struct {
wd uint32
wd int
result chan string
}

Expand Down Expand Up @@ -155,7 +159,7 @@ func NewInotify(ctx context.Context) (*Inotify, error) {
offset = nameEnd
}

req := getPathRequest{wd: uint32(event.Wd), result: make(chan string)}
req := getPathRequest{wd: int(event.Wd), result: make(chan string)}
var watchName string

select {
Expand All @@ -178,7 +182,7 @@ func NewInotify(ctx context.Context) (*Inotify, error) {
name = filepath.Join(watchName, name)

inotifyEvent := InotifyEvent{
Wd: uint32(event.Wd),
Wd: int(event.Wd),
Name: name,
Mask: event.Mask,
Cookie: event.Cookie,
Expand Down Expand Up @@ -221,8 +225,8 @@ func NewInotify(ctx context.Context) (*Inotify, error) {
//defer cancel()
defer wg.Done()

watches := make(map[string]uint32)
paths := make(map[uint32]string)
watches := make(map[string]int)
paths := make(map[int]string)

for {
select {
Expand Down Expand Up @@ -250,7 +254,7 @@ func NewInotify(ctx context.Context) (*Inotify, error) {
}

for _, w := range watches {
_, err := syscall.InotifyRmWatch(fd, w)
_, err := syscallf.InotifyRmWatch(fd, w)
if err != nil {
continue
}
Expand All @@ -260,9 +264,8 @@ func NewInotify(ctx context.Context) (*Inotify, error) {
case req := <-inotify.addWatchIn:
wd, err := syscall.InotifyAddWatch(fd, req.pathName, req.mask)
if err == nil {
wdu32 := uint32(wd)
watches[req.pathName] = wdu32
paths[wdu32] = req.pathName
watches[req.pathName] = wd
paths[wd] = req.pathName
}
select {
case req.result <- err:
Expand Down Expand Up @@ -330,7 +333,7 @@ func (i *Inotify) AddWatch(pathName string, mask uint32) error {
}

// RmWd removes watch by watch descriptor
func (i *Inotify) RmWd(wd uint32) error {
func (i *Inotify) RmWd(wd int) error {
select {
case <-i.ctx.Done():
return i.ctx.Err()
Expand Down
1 change: 0 additions & 1 deletion inotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func BenchmarkWatch(b *testing.B) {
}

func TestInotify(t *testing.T) {

ctx := context.Background()

dir, err := ioutil.TempDir("", "TestInotify")
Expand Down
17 changes: 17 additions & 0 deletions syscallf/rm_watch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build linux

package syscallf

import "syscall"

func InotifyRmWatch(fd int, watchdesc int) (int, error) {
var success int
var err error

r0, _, e1 := syscall.RawSyscall(syscall.SYS_INOTIFY_RM_WATCH, uintptr(fd), uintptr(watchdesc), 0)
success = int(r0)
if e1 != 0 {
err = e1
}
return success, err
}
Loading