当前位置: 首页>>代码示例>>Golang>>正文


Golang syscall.InotifyInit函数代码示例

本文整理汇总了Golang中syscall.InotifyInit函数的典型用法代码示例。如果您正苦于以下问题:Golang InotifyInit函数的具体用法?Golang InotifyInit怎么用?Golang InotifyInit使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了InotifyInit函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: main

func main() {
	flag.Parse()
	args = flag.Args()

	if len(args) <= 0 {
		fmt.Fprintf(os.Stderr, "Must specify at least one argument to run:\n")
		fmt.Fprintf(os.Stderr, "\t%s <options> <command> <arguments>...\n", os.Args[0])
		flag.PrintDefaults()
		return
	}

	startCommand(false)

	usr1c := make(chan os.Signal)
	signal.Notify(usr1c, syscall.SIGUSR1)

	go func() {
		for {
			<-usr1c
			if canExecute() {
				startCommand(true)
			}
		}
	}()

	for {
		inotifyFd, err := syscall.InotifyInit()
		if err != nil {
			log.Fatalf("Inotify init failed: %v", err)
		}

		recdepth := 0
		if *recurse {
			recdepth = *depth
		}

		registerDirectory(inotifyFd, ".", recdepth)

		inotifyBuf := make([]byte, 1024*syscall.SizeofInotifyEvent+16)

		for {
			n, err := syscall.Read(inotifyFd, inotifyBuf[0:])
			if err == io.EOF {
				break
			}
			if err != nil {
				log.Printf("Can not read inotify: %v", err)
				break
			}

			if n > syscall.SizeofInotifyEvent {
				if canExecute() {
					startCommand(true)
				}
			}
		}

		syscall.Close(inotifyFd)
	}
}
开发者ID:aarzilli,项目名称:util,代码行数:60,代码来源:Watch.go

示例2: monitor

func monitor(root string, action chan uint32) {
	fd, err := syscall.InotifyInit()
	if fd == -1 || err != nil {
		end <- true
		return
	}

	flags := syscall.IN_MODIFY | syscall.IN_CREATE | syscall.IN_DELETE // 2/128/512
	wd, _ := syscall.InotifyAddWatch(fd, root, uint32(flags))
	if wd == -1 {
		end <- true
		return
	}
	var (
		buf [syscall.SizeofInotifyEvent * 10]byte
		n   int
	)

	for {
		n, _ = syscall.Read(fd, buf[0:])
		if n > syscall.SizeofInotifyEvent {
			var offset = 0
			for offset < n {
				raw := (*syscall.InotifyEvent)(unsafe.Pointer(&buf[offset]))
				mask := uint32(raw.Mask)
				offset = offset + int(raw.Len) + syscall.SizeofInotifyEvent
				action <- mask

				log.Println("action:", mask)
			}
		}
	}
}
开发者ID:na-lan,项目名称:go-superviser,代码行数:33,代码来源:monitor_linux.go

示例3: lazyinit

// lazyinit sets up all required file descriptors and starts 1+consumersCount
// goroutines. The producer goroutine blocks until file-system notifications
// occur. Then, all events are read from system buffer and sent to consumer
// goroutines which construct valid notify events. This method uses
// Double-Checked Locking optimization.
func (i *inotify) lazyinit() error {
	if atomic.LoadInt32(&i.fd) == invalidDescriptor {
		i.Lock()
		defer i.Unlock()
		if atomic.LoadInt32(&i.fd) == invalidDescriptor {
			fd, err := syscall.InotifyInit()
			if err != nil {
				return err
			}
			i.fd = int32(fd)
			if err = i.epollinit(); err != nil {
				_, _ = i.epollclose(), syscall.Close(int(fd)) // Ignore errors.
				i.fd = invalidDescriptor
				return err
			}
			esch := make(chan []*event)
			go i.loop(esch)
			i.wg.Add(consumersCount)
			for n := 0; n < consumersCount; n++ {
				go i.send(esch)
			}
		}
	}
	return nil
}
开发者ID:Codzart,项目名称:go-ethereum,代码行数:30,代码来源:watcher_inotify.go

示例4: NewWatcher

// NewWatcher establishes a new watcher with the underlying OS and begins waiting for events.
func NewWatcher() (*Watcher, error) {
	// Create inotify fd
	fd, errno := syscall.InotifyInit()
	if fd == -1 {
		return nil, errno
	}
	// Create epoll
	poller, err := newFdPoller(fd)
	if err != nil {
		syscall.Close(fd)
		return nil, err
	}
	w := &Watcher{
		fd:       fd,
		poller:   poller,
		watches:  make(map[string]*watch),
		paths:    make(map[int]string),
		Events:   make(chan Event),
		Errors:   make(chan error),
		done:     make(chan struct{}),
		doneResp: make(chan struct{}),
	}

	go w.readEvents()
	return w, nil
}
开发者ID:DaveDaCoda,项目名称:docker,代码行数:27,代码来源:inotify.go

示例5: main

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	inotify_fd, err := syscall.InotifyInit()
	if err != nil {
		log.Fatal(err)
	}
	paths := make(map[int]string)
	add_watch_r(inotify_fd, "/home/feng/workspace/rssminer", paths)
	log.Print("watcher added")

	for {
		var (
			buf [syscall.SizeofInotifyEvent * 4096]byte
		)

		n, _ := syscall.Read(inotify_fd, buf[0:])

		offset := 0
		for offset <= n-syscall.SizeofInotifyEvent {
			raw := (*syscall.InotifyEvent)(unsafe.Pointer(&buf[offset]))
			wd := int(raw.Wd)
			file := paths[wd]
			mask := raw.Mask
			printEvent(file, mask)
			offset += syscall.SizeofInotifyEvent + int(raw.Len)
		}
	}
}
开发者ID:aimeiyan,项目名称:gocode,代码行数:29,代码来源:inotify.go

示例6: newinotify

func newinotify() (*inotify, error) {
	fd, err := syscall.InotifyInit()
	if fd == -1 {
		return nil, os.NewSyscallError("inotify_init", err)
	}
	return &inotify{
		fd: fd,
	}, nil
}
开发者ID:davecheney,项目名称:fswatch,代码行数:9,代码来源:inotify.go

示例7: NewWatcher

func NewWatcher() (IFSEventsWatcher, error) {
	if fd, errno := syscall.InotifyInit(); errno != nil {
		return nil, os.NewSyscallError("inotify_init", errno)
	} else {
		w := &watcher{
			fd: fd,
		}

		return w, nil
	}
}
开发者ID:darvik80,项目名称:monitor,代码行数:11,代码来源:fsevents_linux.go

示例8: eventProcessor

// eventProcessor processes events from the file system
func (pin *Pin) eventProcessor() error {
	fd, err := syscall.InotifyInit()
	pin.Fd = fd
	if err != nil {
		return err
	}

	_, err = syscall.InotifyAddWatch(fd, pin.ValuePath, syscall.IN_MODIFY)
	if err != nil {
		return err
	}

	go pin.watcher()
	return nil
}
开发者ID:jsgoecke,项目名称:gpio,代码行数:16,代码来源:gpio.go

示例9: NewInotify

func NewInotify(ctrler Controller) (Watcher, error) {
	watchfd, errno := syscall.InotifyInit()
	if watchfd == -1 {
		return nil, os.NewSyscallError("inotify_init", errno)
	}
	w := &inotify{
		watchfd: watchfd,
		watches: make(map[Id]int32),
		ids:     make(map[int32]Id),
		done:    make(chan bool, 1),
		ctrler:  ctrler,
	}
	go w.readEvents()
	return w, nil
}
开发者ID:napsy,项目名称:lab,代码行数:15,代码来源:watcher_linux.go

示例10: main

func main() {
	fd, err := syscall.InotifyInit()
	if err != nil {
		log.Fatal(err)
	}
	defer syscall.Close(fd)

	wd, err := syscall.InotifyAddWatch(fd, "test1.log", syscall.IN_ALL_EVENTS)
	_, err = syscall.InotifyAddWatch(fd, "../test2.log", syscall.IN_ALL_EVENTS)
	//_, err = syscall.InotifyAddWatch(fd, ".", syscall.IN_ALL_EVENTS)
	if err != nil {
		log.Fatal(err)
	}
	defer syscall.InotifyRmWatch(fd, uint32(wd))

	fmt.Printf("WD is %d\n", wd)

	for {
		// Room for at least 128 events
		buffer := make([]byte, syscall.SizeofInotifyEvent*128)
		bytesRead, err := syscall.Read(fd, buffer)
		if err != nil {
			log.Fatal(err)
		}

		if bytesRead < syscall.SizeofInotifyEvent {
			// No point trying if we don't have at least one event
			continue
		}

		fmt.Printf("Size of InotifyEvent is %s\n", syscall.SizeofInotifyEvent)
		fmt.Printf("Bytes read: %d\n", bytesRead)

		offset := 0
		for offset < bytesRead-syscall.SizeofInotifyEvent {
			event := (*syscall.InotifyEvent)(unsafe.Pointer(&buffer[offset]))
			fmt.Printf("%+v\n", event)

			if (event.Mask & syscall.IN_ACCESS) > 0 {
				fmt.Printf("Saw IN_ACCESS for %+v\n", event)
			}

			// We need to account for the length of the name
			offset += syscall.SizeofInotifyEvent + int(event.Len)
		}
	}

}
开发者ID:tomnomnom,项目名称:go-learning,代码行数:48,代码来源:inotify.go

示例11: NewWatcher

// NewWatcher creates and returns a new inotify instance using inotify_init(2)
func NewWatcher() (*Watcher, error) {
	fd, errno := syscall.InotifyInit()
	if fd == -1 {
		return nil, os.NewSyscallError("inotify_init", errno)
	}
	w := &Watcher{
		fd:      fd,
		watches: make(map[string]*watch),
		paths:   make(map[int]string),
		Event:   make(chan *Event),
		Error:   make(chan error),
		state:   new(int32),
	}

	return w, nil
}
开发者ID:smillaedler,项目名称:inotify,代码行数:17,代码来源:inotify_linux.go

示例12: NewWatcher

// NewWatcher creates and returns a new inotify instance using inotify_init(2)
func NewWatcher() (*Watcher, error) {
	fd, errno := syscall.InotifyInit()
	if fd == -1 {
		return nil, os.NewSyscallError("inotify_init", errno)
	}
	w := &Watcher{
		fd:      fd,
		watches: make(map[string]*watch),
		paths:   make(map[int]string),
		Event:   make(chan *Event),
		Error:   make(chan error),
		done:    make(chan bool, 1),
	}

	go w.readEvents()
	return w, nil
}
开发者ID:40a,项目名称:bootkube,代码行数:18,代码来源:inotify_linux.go

示例13: runInotify

//starts inotify tracking
func runInotify() {
	fd, err := syscall.InotifyInit()
	if err != nil {
		log.Fatal("error initializing Inotify: ", err)
		return
	}
	addFilesToInotify(fd, path)

	var buffer []byte = make([]byte, 1024*EVENT_SIZE)

	for {
		n, err := syscall.Read(fd, buffer)
		if err != nil {
			log.Fatal("Read failed: ", err)
			return
		}
		processBuffer(n, buffer)
	}
}
开发者ID:kod3r,项目名称:eyenotify,代码行数:20,代码来源:eyenotify_linux.go

示例14: NewWatcher

// NewWatcher establishes a new watcher with the underlying OS and begins waiting for events.
func NewWatcher() (*Watcher, error) {
	fd, errno := syscall.InotifyInit()
	if fd == -1 {
		return nil, os.NewSyscallError("inotify_init", errno)
	}
	w := &Watcher{
		fd:      fd,
		watches: make(map[string]*watch),
		paths:   make(map[int]string),
		Events:  make(chan Event),
		Errors:  make(chan error),
		done:    make(chan bool),
		closed:  make(chan bool),
	}
	w.cv = sync.NewCond(&w.mu)

	rp, wp, err := os.Pipe() // for done
	if err != nil {
		return nil, err
	}
	epfd, err := syscall.EpollCreate1(0)
	if err != nil {
		return nil, os.NewSyscallError("epoll_create1", err)
	}
	event := &syscall.EpollEvent{syscall.EPOLLIN, int32(w.fd), 0}
	if err = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, w.fd, event); err != nil {
		return nil, os.NewSyscallError("epoll_ctl", err)
	}
	event = &syscall.EpollEvent{syscall.EPOLLIN, int32(rp.Fd()), 0}
	if err = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, int(rp.Fd()), event); err != nil {
		return nil, os.NewSyscallError("epoll_ctl", err)
	}

	go func() {
		<-w.done
		wp.Close() // make rp readable
	}()
	go w.epollEvents(epfd, rp)

	return w, nil
}
开发者ID:makarski,项目名称:fsnotify,代码行数:42,代码来源:inotify.go

示例15: NewWatcher

// NewWatcher creates and returns a new inotify instance using inotify_init(2)
func NewWatcher() (*Watcher, error) {
	fd, errno := syscall.InotifyInit()
	if fd == -1 {
		return nil, os.NewSyscallError("inotify_init", errno)
	}
	mf := &filter{
		memo:     make(map[string]time.Time),
		interval: time.Minute,
	}
	w := &Watcher{
		fd:      fd,
		watches: make(map[string]*watch),
		paths:   make(map[int]string),
		Event:   make(chan *Event),
		Error:   make(chan error),
		done:    make(chan bool, 1),
		mfilter: mf,
	}

	go w.readEvents()
	return w, nil
}
开发者ID:qjpcpu,项目名称:watchman,代码行数:23,代码来源:inotify_linux.go


注:本文中的syscall.InotifyInit函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。