

gobox中处理文件系统通知
source link: http://blog.7rule.com/2018/07/15/gobox-inotify.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

今天来说下使用gobox中inotify来处理文件系统通知
inotify介绍
inotify是linux系统的文件事件通知机制,例如我们想得知某个文件是否被改变,目录的读写情况等,都可以使用这个机制。
用法示例
package main import ( "github.com/goinbox/inotify" "fmt" "path/filepath" ) func main() { path := "/tmp/a.log" watcher, _ := inotify.NewWatcher() watcher.AddWatch(path, inotify.IN_ALL_EVENTS) watcher.AddWatch(filepath.Dir(path), inotify.IN_ALL_EVENTS) for i := 0; i < 5; i++ { events, _ := watcher.ReadEvents() for _, event := range events { if watcher.IsUnreadEvent(event) { fmt.Println("it is a last remaining event") } showEvent(event) } } watcher.Free() fmt.Println("bye") } func showEvent(event *inotify.Event) { fmt.Println(event) if event.InIgnored() { fmt.Println("inotify.IN_IGNORED") } if event.InAttrib() { fmt.Println("inotify.IN_ATTRIB") } if event.InModify() { fmt.Println("inotify.IN_MODIFY") } if event.InMoveSelf() { fmt.Println("inotify.IN_MOVE_SELF") } if event.InMovedFrom() { fmt.Println("inotify.IN_MOVED_FROM") } if event.InMovedTo() { fmt.Println("inotify.IN_MOVED_TO") } if event.InDeleteSelf() { fmt.Println("inotify.IN_DELETE_SELF") } if event.InDelete() { fmt.Println("inotify.IN_DELETE") } if event.InCreate() { fmt.Println("inotify.IN_CREATE") } }
如示例,我们监听了 /tmp/a.log
和 /tmp
目录的文件系统事件。
执行程序,接下来我们做如下操作:
ligang@vm-xubuntu /tmp $ echo a > a.log ligang@vm-xubuntu /tmp $ echo aa >> a.log ligang@vm-xubuntu /tmp $ mv a.log b.log ligang@vm-xubuntu /tmp $ rm b.log rm: remove regular file 'b.log'? y
得到输出:
&{1 256 0 /tmp a.log} inotify.IN_CREATE &{1 32 0 /tmp a.log} &{1 2 0 /tmp a.log} inotify.IN_MODIFY &{1 8 0 /tmp a.log} &{1 32 0 /tmp a.log} &{1 2 0 /tmp a.log} inotify.IN_MODIFY &{1 8 0 /tmp a.log} &{1 64 1801 /tmp a.log} inotify.IN_MOVED_FROM &{1 128 1801 /tmp b.log} inotify.IN_MOVED_TO &{1 512 0 /tmp b.log} inotify.IN_DELETE bye
inotify对系统编程开发有着很大的意义, 如果对inotify不是很熟悉,我在这里推荐阅读 Linux/UNIX系统编程手册:https://book.douban.com/subject/25809330/
欢迎大家使用,使用中有遇到问题随时反馈,我们会尽快响应,谢谢!
Recommend
-
42
今天来说下gobox中的连接池底层实现pool 为什么需要连接池 我们的系统在访问外部资源(redis、mysql等)时,为了提高性能,通常会用到的一个优化方法就是把已经使用过的tcp连接保存起来,这样当需要再次使用时,就可...
-
40
今天来说下使用gobox中的log操作 log级别定义 const ( LEVEL_EMERGENCY = 0 LEVEL_ALERT = 1 LEVEL_CRITICAL = 2 LEVEL_ERROR = 3 LEVEL_WARNING = 4 LEVEL_NOTICE = 5 LEVEL_INFO = 6...
-
49
今天来说下使用gobox中的http请求处理框架 http请求处理架构图 重要的对象 System system用于实现g...
-
39
我们都会有从异步队列中消费的需求,今天来说下gobox中的consumer处理框架 consumer处理架构图 重要的对象 IM...
-
11
gobox中的常用工具包gomisc Sep 8, 2018 有一些常用的工具函数,我们把它们放到gomisc这个包中。 Slice中的值Unique func IntSliceUnique(s []int) []int func StringSli...
-
13
gobox中的httpclient Aug 18, 2018 今天来说下使用gobox中httpclient,这个包就相当于命令行的curl工具,用于发起http请求。 重要的对象 config const ( DEFAUL...
-
15
gobox中的分页操作 Aug 3, 2018 今天来说下使用gobox中的分页操作 分页也是我们开发时的一个常见需求,gobox中提供了page包做这个事情 package main import ( "github....
-
16
gobox中redis操作 Jul 29, 2018 今天来说下使用gobox中redis操作相关 本包的driver部分使用了redigo:https://github.com/garyburd/redigo package main import ( "git...
-
11
gobox中mysql操作 Jul 21, 2018 今天来说下使用gobox中mysql操作相关 本包的driver部分使用了go-sql-driver:https://github.com/go-sql-driver/mysql 示例表结构为: | de...
-
9
gobox中的simplecache和levelcache Jun 10, 2018 今天来说下gobox中的simplecache和levelcache simplecache simplecache提供了一个简单的内存kv package main i...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK