fortrangoingonforty/fgof-watch / 975aa17

Browse files

Expand watch model

Authored by espadonne
SHA
975aa178b0a888ab1a87057e6a5b6a4f6d58754d
Parents
3e11d13
Tree
d72ad6d

4 changed files

StatusFile+-
M src/fgof_watch.f90 15 10
M src/fgof_watch_types.f90 17 0
A test/test_event_model.f90 28 0
M test/test_scaffold.f90 5 6
src/fgof_watch.f90modified
@@ -1,5 +1,5 @@
11
 module fgof_watch
2
-  use fgof_watch_types, only : FGOF_WATCH_EVT_NONE, watch_event, watch_options, watch_session
2
+  use fgof_watch_types, only : watch_event, watch_options, watch_session
33
   implicit none
44
   private
55
 
@@ -19,19 +19,20 @@ contains
1919
     end if
2020
 
2121
     session%root = root
22
-    session%active = len_trim(root) > 0
22
+    session%active = len(root) > 0
23
+    allocate(session%entries(0))
2324
   end subroutine init_watch
2425
 
25
-  function poll_watch(session) result(event)
26
-    type(watch_session), intent(in) :: session
27
-    type(watch_event) :: event
26
+  function poll_watch(session) result(events)
27
+    type(watch_session), intent(inout) :: session
28
+    type(watch_event), allocatable :: events(:)
2829
 
29
-    event%kind = FGOF_WATCH_EVT_NONE
30
-    if (allocated(session%root)) then
31
-      event%path = session%root
32
-    else
33
-      event%path = ""
30
+    if (.not. session%active) then
31
+      allocate(events(0))
32
+      return
3433
     end if
34
+
35
+    allocate(events(0))
3536
   end function poll_watch
3637
 
3738
   subroutine reset_watch(session)
@@ -41,6 +42,10 @@ contains
4142
       deallocate(session%root)
4243
     end if
4344
 
45
+    if (allocated(session%entries)) then
46
+      deallocate(session%entries)
47
+    end if
48
+
4449
     session%options = watch_options()
4550
     session%active = .false.
4651
   end subroutine reset_watch
src/fgof_watch_types.f90modified
@@ -1,8 +1,13 @@
11
 module fgof_watch_types
2
+  use, intrinsic :: iso_fortran_env, only : int64
23
   implicit none
34
   private
45
 
56
   integer, parameter, public :: FGOF_WATCH_EVT_NONE = 0
7
+  integer, parameter, public :: FGOF_WATCH_EVT_CREATED = 1
8
+  integer, parameter, public :: FGOF_WATCH_EVT_MODIFIED = 2
9
+  integer, parameter, public :: FGOF_WATCH_EVT_REMOVED = 3
10
+  integer, parameter, public :: FGOF_WATCH_EVT_MOVED = 4
611
 
712
   public :: watch_event
813
   public :: watch_options
@@ -10,7 +15,9 @@ module fgof_watch_types
1015
 
1116
   type :: watch_event
1217
     integer :: kind = FGOF_WATCH_EVT_NONE
18
+    logical :: is_directory = .false.
1319
     character(len=:), allocatable :: path
20
+    character(len=:), allocatable :: previous_path
1421
   end type watch_event
1522
 
1623
   type :: watch_options
@@ -18,9 +25,19 @@ module fgof_watch_types
1825
     logical :: recursive = .true.
1926
   end type watch_options
2027
 
28
+  type :: watch_entry
29
+    character(len=:), allocatable :: path
30
+    integer(int64) :: inode = 0_int64
31
+    integer(int64) :: size = 0_int64
32
+    integer(int64) :: mtime_sec = 0_int64
33
+    integer(int64) :: mtime_nsec = 0_int64
34
+    logical :: is_directory = .false.
35
+  end type watch_entry
36
+
2137
   type :: watch_session
2238
     character(len=:), allocatable :: root
2339
     type(watch_options) :: options
2440
     logical :: active = .false.
41
+    type(watch_entry), allocatable :: entries(:)
2542
   end type watch_session
2643
 end module fgof_watch_types
test/test_event_model.f90added
@@ -0,0 +1,28 @@
1
+program test_event_model
2
+  use fgof_watch_types, only : &
3
+    FGOF_WATCH_EVT_CREATED, &
4
+    FGOF_WATCH_EVT_MODIFIED, &
5
+    FGOF_WATCH_EVT_MOVED, &
6
+    FGOF_WATCH_EVT_NONE, &
7
+    FGOF_WATCH_EVT_REMOVED, &
8
+    watch_event
9
+  implicit none
10
+
11
+  type(watch_event) :: event
12
+
13
+  if (FGOF_WATCH_EVT_NONE /= 0) error stop "none event constant should be zero"
14
+  if (FGOF_WATCH_EVT_CREATED <= FGOF_WATCH_EVT_NONE) error stop "created event should be positive"
15
+  if (FGOF_WATCH_EVT_MODIFIED <= FGOF_WATCH_EVT_CREATED) error stop "modified event ordering should be stable"
16
+  if (FGOF_WATCH_EVT_REMOVED <= FGOF_WATCH_EVT_MODIFIED) error stop "removed event ordering should be stable"
17
+  if (FGOF_WATCH_EVT_MOVED <= FGOF_WATCH_EVT_REMOVED) error stop "moved event ordering should be stable"
18
+
19
+  event%kind = FGOF_WATCH_EVT_MOVED
20
+  event%is_directory = .true.
21
+  event%path = "new-path"
22
+  event%previous_path = "old-path"
23
+
24
+  if (event%kind /= FGOF_WATCH_EVT_MOVED) error stop "event kind should store move values"
25
+  if (.not. event%is_directory) error stop "event directory flag should be stored"
26
+  if (event%path /= "new-path") error stop "event path should be stored"
27
+  if (event%previous_path /= "old-path") error stop "event previous path should be stored"
28
+end program test_event_model
test/test_scaffold.f90modified
@@ -1,9 +1,9 @@
11
 program test_scaffold
22
   use fgof_watch, only : init_watch, poll_watch, reset_watch
3
-  use fgof_watch_types, only : FGOF_WATCH_EVT_NONE, watch_event, watch_options, watch_session
3
+  use fgof_watch_types, only : watch_event, watch_options, watch_session
44
   implicit none
55
 
6
-  type(watch_event) :: event
6
+  type(watch_event), allocatable :: events(:)
77
   type(watch_options) :: options
88
   type(watch_session) :: session
99
 
@@ -16,10 +16,9 @@ program test_scaffold
1616
   if (session%options%poll_interval_ms /= 100) error stop "poll interval should be stored"
1717
   if (session%options%recursive) error stop "recursive flag should follow options"
1818
 
19
-  event = poll_watch(session)
20
-  if (event%kind /= FGOF_WATCH_EVT_NONE) error stop "scaffold poll should report none"
21
-  if (.not. allocated(event%path)) error stop "scaffold poll should return a path"
22
-  if (event%path /= "src") error stop "scaffold poll should echo root path"
19
+  events = poll_watch(session)
20
+  if (.not. allocated(events)) error stop "poll should always allocate an event batch"
21
+  if (size(events) /= 0) error stop "placeholder poll should return an empty batch"
2322
 
2423
   call reset_watch(session)
2524
   if (session%active) error stop "reset should deactivate session"