|
| 1 | +package os.watch.inotify |
| 2 | + |
| 3 | +import com.sun.jna.{LastErrorException, Library, Native} |
| 4 | +import geny.Generator |
| 5 | + |
| 6 | +import java.nio.{ByteBuffer, ByteOrder} |
| 7 | +import java.util.concurrent.atomic.AtomicReference |
| 8 | +import scala.annotation.tailrec |
| 9 | +import scala.collection.mutable |
| 10 | +import scala.util.Try |
| 11 | + |
| 12 | +trait Notify extends Library { |
| 13 | + //@throws[LastErrorException] |
| 14 | + def inotify_init() : Int; |
| 15 | + |
| 16 | + //@throws[LastErrorException] |
| 17 | + def inotify_init1(flags: Int) : Int |
| 18 | + |
| 19 | + //@throws[LastErrorException] |
| 20 | + def inotify_add_watch(fd: Int, path: String, mask: Int): Int |
| 21 | + |
| 22 | + //@throws[LastErrorException] |
| 23 | + def inotify_rm_watch(fd: Int, wd: Int): Int |
| 24 | + |
| 25 | + //@throws[LastErrorException] |
| 26 | + def read(fd: Int, buf: Array[Byte], count: Long): Long |
| 27 | + |
| 28 | + //@throws[LastErrorException] |
| 29 | + def close(fd: Int): Int |
| 30 | +} |
| 31 | + |
| 32 | +object Notify { |
| 33 | + val it : Notify = Native.load("c",classOf[Notify]) |
| 34 | + |
| 35 | + val O_NONBLOCK: Int = 1 << 11 |
| 36 | + |
| 37 | + |
| 38 | + import Generator._ |
| 39 | + |
| 40 | + // convenience |
| 41 | + def add_watch(fd: Int, path : os.Path, actions: Mask) : Int = { |
| 42 | + it.inotify_add_watch(fd,path.toString,actions.mask) |
| 43 | + } |
| 44 | + |
| 45 | + def events(buf: ByteBuffer): Generator[Event] = new Generator[Event]() { |
| 46 | + override def generate(handleItem: Event => Action): Action = { |
| 47 | + while (buf.hasRemaining) { |
| 48 | + if (handleItem(Event(buf)) == End) return End |
| 49 | + } |
| 50 | + Continue |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + def buffers(fd: => AtomicReference[Option[Int]]): Generator[ByteBuffer] = { |
| 55 | + new Generator[ByteBuffer] { |
| 56 | + override def generate(handleItem: ByteBuffer => Action): Action = { |
| 57 | + |
| 58 | + @tailrec |
| 59 | + def loop(arr: Array[Byte]): Action = fd.get match { |
| 60 | + case Some(fd) => |
| 61 | + it.read(fd, arr, arr.length) match { |
| 62 | + case 0 => |
| 63 | + Continue |
| 64 | + case n if n < 0 => |
| 65 | + val errno = Native.getLastError() |
| 66 | + if (errno == Errno.EAGAIN) { |
| 67 | + Thread.sleep(10) |
| 68 | + loop(arr) |
| 69 | + } else { |
| 70 | + throw new NotifyException(s"read error ${Native.getLastError()}, fd = $fd") |
| 71 | + } |
| 72 | + //throw new Exception (s"n = $n") |
| 73 | + case n => |
| 74 | + val buf = ByteBuffer.wrap(arr, 0, n.toInt).order(ByteOrder.nativeOrder()) |
| 75 | + if (handleItem(buf) == End) { |
| 76 | + End |
| 77 | + } else { |
| 78 | + loop(arr) |
| 79 | + } |
| 80 | + } |
| 81 | + case None => |
| 82 | + End |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + loop(Array.fill[Byte](1000)(0)) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + def events(fd: AtomicReference[Option[Int]]): Generator[Event] = for { |
| 92 | + b <- buffers(fd) |
| 93 | + e <- events(b) |
| 94 | + } yield e |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +} |
| 101 | + |
0 commit comments