src/Pure/General/file_watcher.scala
author wenzelm
Mon, 04 Nov 2024 20:55:01 +0100
changeset 81340 30f7eb65d679
parent 75394 42267c650205
permissions -rw-r--r--
clarified signature; clarified modules;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/file_watcher.scala
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
     3
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
     4
Watcher for file-system events.
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
     5
*/
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
     6
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
     7
package isabelle
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
     8
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
     9
73909
1d0d9772fff0 tuned imports;
wenzelm
parents: 73367
diff changeset
    10
import java.util.{List => JList}
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    11
import java.io.{File => JFile}
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    12
import java.nio.file.FileSystems
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    13
import java.nio.file.{WatchKey, WatchEvent, Path => JPath}
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    14
import java.nio.file.StandardWatchEventKinds.{ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY}
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    15
73353
279e45248e9d tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
    16
import scala.jdk.CollectionConverters._
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    17
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    18
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74140
diff changeset
    19
class File_Watcher private[File_Watcher] {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74140
diff changeset
    20
  // dummy template
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
    21
  def register(dir: JFile): Unit = {}
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
    22
  def register_parent(file: JFile): Unit = {}
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
    23
  def deregister(dir: JFile): Unit = {}
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
    24
  def purge(retain: Set[JFile]): Unit = {}
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
    25
  def shutdown(): Unit = {}
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    26
}
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    27
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74140
diff changeset
    28
object File_Watcher {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74140
diff changeset
    29
  val none: File_Watcher = new File_Watcher {
64859
e600cfdc9e97 tuned output;
wenzelm
parents: 64858
diff changeset
    30
    override def toString: String = "File_Watcher.none"
e600cfdc9e97 tuned output;
wenzelm
parents: 64858
diff changeset
    31
  }
64858
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64807
diff changeset
    32
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    33
  def apply(handle: Set[JFile] => Unit, delay: => Time = Time.seconds(0.5)): File_Watcher =
64858
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64807
diff changeset
    34
    if (Platform.is_windows) none else new Impl(handle, delay)
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    35
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    36
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    37
  /* proper implementation */
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    38
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    39
  sealed case class State(
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    40
    dirs: Map[JFile, WatchKey] = Map.empty,
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    41
    changed: Set[JFile] = Set.empty)
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    42
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74140
diff changeset
    43
  class Impl private[File_Watcher](handle: Set[JFile] => Unit, delay: Time) extends File_Watcher {
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    44
    private val state = Synchronized(File_Watcher.State())
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    45
    private val watcher = FileSystems.getDefault.newWatchService()
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    46
64859
e600cfdc9e97 tuned output;
wenzelm
parents: 64858
diff changeset
    47
    override def toString: String =
e600cfdc9e97 tuned output;
wenzelm
parents: 64858
diff changeset
    48
      state.value.dirs.keySet.mkString("File_Watcher(", ", ", ")")
e600cfdc9e97 tuned output;
wenzelm
parents: 64858
diff changeset
    49
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    50
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    51
    /* registered directories */
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    52
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    53
    override def register(dir: JFile): Unit =
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    54
      state.change(st =>
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    55
        st.dirs.get(dir) match {
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    56
          case Some(key) if key.isValid => st
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    57
          case _ =>
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    58
            val key = dir.toPath.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY)
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    59
            st.copy(dirs = st.dirs + (dir -> key))
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    60
        })
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    61
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74140
diff changeset
    62
    override def register_parent(file: JFile): Unit = {
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    63
      val dir = file.getParentFile
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    64
      if (dir != null && dir.isDirectory) register(dir)
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    65
    }
64782
wenzelm
parents: 64710
diff changeset
    66
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    67
    override def deregister(dir: JFile): Unit =
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    68
      state.change(st =>
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    69
        st.dirs.get(dir) match {
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    70
          case None => st
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    71
          case Some(key) =>
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
    72
            key.cancel()
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    73
            st.copy(dirs = st.dirs - dir)
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    74
        })
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    75
64867
e7220f4de11f support "purge" operation on document model;
wenzelm
parents: 64859
diff changeset
    76
    override def purge(retain: Set[JFile]): Unit =
e7220f4de11f support "purge" operation on document model;
wenzelm
parents: 64859
diff changeset
    77
      state.change(st =>
e7220f4de11f support "purge" operation on document model;
wenzelm
parents: 64859
diff changeset
    78
        st.copy(dirs = st.dirs --
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
    79
          (for ((dir, key) <- st.dirs.iterator if !retain(dir)) yield { key.cancel(); dir })))
64867
e7220f4de11f support "purge" operation on document model;
wenzelm
parents: 64859
diff changeset
    80
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    81
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    82
    /* changed directory entries */
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    83
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74140
diff changeset
    84
    private val delay_changed = Delay.last(delay) {
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    85
      val changed = state.change_result(st => (st.changed, st.copy(changed = Set.empty)))
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    86
      handle(changed)
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    87
    }
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
    88
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74140
diff changeset
    89
    private val watcher_thread = Isabelle_Thread.fork(name = "file_watcher", daemon = true) {
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    90
      try {
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    91
        while (true) {
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    92
          val key = watcher.take
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
    93
          val has_changed =
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    94
            state.change_result { st =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    95
              val (remove, changed) =
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    96
                st.dirs.collectFirst({ case (dir, key1) if key == key1 => dir }) match {
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    97
                  case Some(dir) =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    98
                    val events: Iterable[WatchEvent[JPath]] =
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    99
                      key.pollEvents.asInstanceOf[JList[WatchEvent[JPath]]].asScala
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   100
                    val remove = if (key.reset) None else Some(dir)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   101
                    val changed =
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   102
                      events.iterator.foldLeft(Set.empty[JFile]) {
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   103
                        case (set, event) => set + dir.toPath.resolve(event.context).toFile
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   104
                      }
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   105
                    (remove, changed)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   106
                  case None =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   107
                    key.pollEvents
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   108
                    key.reset
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   109
                    (None, Set.empty[JFile])
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   110
                }
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   111
              (changed.nonEmpty, st.copy(dirs = st.dirs -- remove, changed = st.changed ++ changed))
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   112
            }
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
   113
          if (has_changed) delay_changed.invoke()
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
   114
        }
64700
14deaeaa6476 more robust shutdown;
wenzelm
parents: 64699
diff changeset
   115
      }
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
   116
      catch { case Exn.Interrupt() => }
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
   117
    }
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
   118
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
   119
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
   120
    /* shutdown */
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
   121
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74140
diff changeset
   122
    override def shutdown(): Unit = {
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
   123
      watcher_thread.interrupt()
74140
8a5e02ef975c clarified signature;
wenzelm
parents: 73909
diff changeset
   124
      watcher_thread.join()
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
   125
      delay_changed.revoke()
64807
7d556bb6046b dummy File_Watcher for Windows (spurious crashes seen on Windows 7);
wenzelm
parents: 64782
diff changeset
   126
    }
64699
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
   127
  }
218c35908d5f watcher for file-system events;
wenzelm
parents:
diff changeset
   128
}