Changeset 80 for trunk/src/directory_watcher.erl
- Timestamp:
- 07/17/09 13:31:39 (3 years ago)
- File:
-
- 1 edited
-
trunk/src/directory_watcher.erl (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/directory_watcher.erl
r75 r80 6 6 -export ([init/2]). 7 7 -export ([init_recursive/2]). 8 -export([start_io/2]).9 -export([start_io/0]).10 8 -include_lib ("kernel/include/file.hrl"). 11 9 12 start_io () ->13 start_io (".", 3000).14 15 start_io (Dir, Interval) ->16 Watcher = spawn_link (?MODULE, init_recursive, [Dir, self ()]),17 io_loop (Interval, Watcher).18 19 io_loop (Interval, Watcher) ->20 Watcher ! check,21 receive22 {directory_watcher, _, Event} ->23 io: fwrite ("~p~n", [Event]),24 io_loop (Interval, Watcher);25 stop ->26 bye27 after Interval ->28 io_loop (Interval, Watcher)29 end.30 31 10 init (Directory, F) -> 32 check (Directory, F, []). 11 D = filename: absname (Directory), 12 check (D, F, []). 33 13 34 14 init_recursive (Directory, F) -> 15 D = filename: absname (Directory), 35 16 Self = self (), 36 Watcher = spawn_link (?MODULE, init, [D irectory, send (Self)]),37 loop_recursive (D irectory, F, [Watcher]).17 Watcher = spawn_link (?MODULE, init, [D, send (Self)]), 18 loop_recursive (D, F, [Watcher]). 38 19 39 20 check (Directory, F, State) -> 40 21 New_state = list_dir (Directory, F), 41 compare (F, State, New_state),22 sort_and_compare (F, State, New_state), 42 23 loop (Directory, F, New_state). 43 24 … … 99 80 end. 100 81 101 compare (_, State, State) -> 102 done; 103 compare (Fun, [X | Xs], [X | Ys]) -> 104 compare (Fun, Xs, Ys); 105 compare (Fun, [{F, {regular, _}} | Xs], [{F, {regular, _}} | Ys]) -> 106 report_changed (Fun, F), 107 compare (Fun, Xs, Ys); 108 compare (Fun, [{F, _}=X | Xs], [{F, _} | Ys]) -> 109 report_lost (Fun, X), 110 report_found (Fun, F), 111 compare (Fun, Xs, Ys); 112 compare (Fun, [X, {F, V} | Xs], [{F, W} | Ys]) -> 113 report_lost (Fun, X), 114 compare (Fun, [{F, V} | Xs], [{F, W} | Ys]); 115 compare (Fun, [{F, V} | Xs], [Y, {F, W} | Ys]) -> 116 {Filename, _} = Y, 117 report_found (Fun, Filename), 118 compare (Fun, [{F, V} | Xs], [{F, W} | Ys]); 119 compare (Fun, [], [Y | Ys]) -> 120 {Filename, _} = Y, 121 report_found (Fun, Filename), 122 compare (Fun, [], Ys); 123 compare (Fun, [X | Xs], []) -> 124 report_lost (Fun, X), 125 compare (Fun, Xs, []); 126 compare (Fun, [X], [Y]) -> 127 report_lost (Fun, X), 128 {File, _} = Y, 129 report_found (Fun, File), 130 done; 131 compare (_, [], []) -> 82 sort_and_compare (F, Old, New) -> 83 Original = dict: from_list (Old), 84 Modified = dict: from_list (New), 85 Compared = adlib: compare_dict (Modified, Original), 86 dict: fold (fun notify_new/3, F, dict: fetch (new, Compared)), 87 dict: fold (fun notify_lost/3, F, dict: fetch (lost, Compared)), 88 dict: fold (fun notify_changed/3, F, dict: fetch (changed, Compared)), 132 89 done. 90 91 notify_new (File_name, _, Fun) -> 92 report_found (Fun, File_name), 93 Fun. 133 94 134 95 report_found (F, Filename) -> 135 96 F ({type (Filename), Filename, found}). 97 98 notify_lost (File_name, Value, Fun) -> 99 report_lost (Fun, {File_name, Value}), 100 Fun. 136 101 137 102 report_lost (F, {Filename, directory}) -> … … 139 104 report_lost (F, {Filename, {regular, _}}) -> 140 105 F ({{file, filename: extension (Filename)}, Filename, lost}). 106 107 notify_changed (File_name, {{regular, _}, {regular, _}}, Fun) -> 108 report_changed (Fun, File_name), 109 Fun; 110 notify_changed (File_name, {_, Original}, Fun) -> 111 report_lost (Fun, {File_name, Original}), 112 report_found (Fun, File_name), 113 Fun. 141 114 142 115 report_changed (F, Filename) ->
Note: See TracChangeset
for help on using the changeset viewer.
