dtas.git  about / heads / tags
duct tape audio suite for *nix
blob d0f37af8289c6c9f40f8892d826d4a1e24596600 1843 bytes (raw)
$ git show v0.16.1:lib/dtas/watchable.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 
# Copyright (C) 2013-2019 all contributors <dtas-all@nongnu.org>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
# frozen_string_literal: true
begin
require 'sleepy_penguin'

# used to restart DTAS::Source::SplitFX processing in dtas-player
# if the YAML file is edited
module DTAS::Watchable # :nodoc:
  class InotifyReadableIter < SleepyPenguin::Inotify # :nodoc:
    def self.new
      super(:CLOEXEC)
    end

    FLAGS = CLOSE_WRITE | MOVED_TO

    def readable_iter
      or_call = false
      while event = take(true) # drain the buffer
        w = @watches[event.wd] or next
        if (event.mask & FLAGS) != 0 && w[event.name]
          or_call = true
        end
      end
      if or_call
        @on_readable.call
        :delete
      else
        :wait_readable
      end
    end

    # we must watch the directory, since
    def watch_files(paths, blk)
      @watches = {} # wd -> { basename -> true }
      @on_readable = blk
      @dir2wd = {}
      Array(paths).each do |path|
        watchdir, watchbase = File.split(File.expand_path(path))
        begin
          wd = @dir2wd[watchdir] ||= add_watch(watchdir, FLAGS)
          m = @watches[wd] ||= {}
          m[watchbase] = true
        rescue SystemCallError => e
          warn "#{watchdir.dump}: #{e.message} (#{e.class})"
        end
      end
    end
  end

  def watch_begin(blk)
    @ino = InotifyReadableIter.new
    @ino.watch_files(@watch_extra << @infile, blk)
    @ino
  end

  def watch_extra(paths)
    @ino.watch_extra(paths)
  end

  # Closing the inotify descriptor (instead of using inotify_rm_watch)
  # is cleaner because it avoids EINVAL on race conditions in case
  # a directory is deleted: https://lkml.org/lkml/2007/7/9/3
  def watch_end(srv)
    srv.wait_ctl(@ino, :delete)
    @ino = @ino.close
  end
end

rescue LoadError
end

git clone git://80x24.org/dtas.git
git clone https://80x24.org/dtas.git