- Timestamp:
- 04/16/09 19:35:21 (3 years ago)
- Location:
- trunk/src
- Files:
-
- 2 edited
-
directory_watcher.erl (modified) (2 diffs)
-
directory_watcher_test.erl (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/directory_watcher.erl
r50 r51 4 4 5 5 -module (directory_watcher). 6 -export ([init/ 3]).6 -export ([init/2]). 7 7 -export ([recursive/1]). 8 8 -include_lib ("kernel/include/file.hrl"). 9 9 10 10 recursive (Fun) -> 11 fun (directory, P, found , S) ->12 spawn_link (directory_watcher, init, [P, Fun , S]);13 (T, E, P , S) ->14 Fun (T, E, P , S)11 fun (directory, P, found) -> 12 spawn_link (directory_watcher, init, [P, Fun]); 13 (T, E, P) -> 14 Fun (T, E, P) 15 15 end. 16 16 17 init (Directory, Action , Action_state) ->18 check (Directory, Action, Action_state,[]).17 init (Directory, Action) -> 18 check (Directory, Action, []). 19 19 20 check (Directory, Action, Action_state,State) ->21 {New_action_state, New_state} = list_dir (Directory, Action, Action_state, State),22 Final_action_state = compare (Action, New_action_state, State, New_state),23 loop (Directory, Action, Final_action_state,New_state).20 check (Directory, Action, State) -> 21 New_state = list_dir (Directory, Action, State), 22 compare (Action, State, New_state), 23 loop (Directory, Action, New_state). 24 24 25 list_dir (Directory, Action, Action_state,State) ->26 list_dir (Directory, Action, Action_state,State, file: list_dir (Directory)).25 list_dir (Directory, Action, State) -> 26 list_dir (Directory, Action, State, file: list_dir (Directory)). 27 27 28 list_dir (Directory, _, Action_state,_, {ok, Filenames}) ->28 list_dir (Directory, _, _, {ok, Filenames}) -> 29 29 Paths = [filename: join (Directory, F) || F <- Filenames], 30 {Action_state, read_state (Paths)}; 31 list_dir (Directory, Action, Action_state, State, Error) -> 32 {Action (directory, Directory, Error, Action_state), State}. 30 read_state (Paths); 31 list_dir (Directory, Action, State, Error) -> 32 Action (directory, Directory, Error), 33 State. 33 34 34 35 read_state (Filenames) -> … … 49 50 directory. 50 51 51 loop (Directory, Action, Action_state,Filenames) ->52 loop (Directory, Action, Filenames) -> 52 53 receive 53 54 check -> 54 check (Directory, Action, Action_state,Filenames);55 check (Directory, Action, Filenames); 55 56 {Pid, stop} -> 56 57 Pid ! {self (), bye} 57 58 end. 58 59 59 compare (_, Action_state,State, State) ->60 Action_state;61 compare (Action, Action_state,[X | Xs], [X | Ys]) ->62 compare (Action, Action_state,Xs, Ys);63 compare (Action, Action_state,[{F, _} | Xs], [{F, _} | Ys]) ->64 New_action_state = report_changed (Action, Action_state, F),65 compare (Action, New_action_state,Xs, Ys);66 compare (Action, Action_state,[X, {F, V} | Xs], [{F, W} | Ys]) ->60 compare (_, State, State) -> 61 done; 62 compare (Action, [X | Xs], [X | Ys]) -> 63 compare (Action, Xs, Ys); 64 compare (Action, [{F, _} | Xs], [{F, _} | Ys]) -> 65 report_changed (Action, F), 66 compare (Action, Xs, Ys); 67 compare (Action, [X, {F, V} | Xs], [{F, W} | Ys]) -> 67 68 {Filename, _} = X, 68 New_action_state = report_lost (Action, Action_state, Filename),69 compare (Action, New_action_state,[{F, V} | Xs], [{F, W} | Ys]);70 compare (Action, Action_state,[{F, V} | Xs], [Y, {F, W} | Ys]) ->69 report_lost (Action, Filename), 70 compare (Action, [{F, V} | Xs], [{F, W} | Ys]); 71 compare (Action, [{F, V} | Xs], [Y, {F, W} | Ys]) -> 71 72 {Filename, _} = Y, 72 New_action_state = report_found (Action, Action_state, Filename),73 compare (Action, New_action_state,[{F, V} | Xs], [{F, W} | Ys]);74 compare (Action, Action_state,[], [Y | Ys]) ->73 report_found (Action, Filename), 74 compare (Action, [{F, V} | Xs], [{F, W} | Ys]); 75 compare (Action, [], [Y | Ys]) -> 75 76 {Filename, _} = Y, 76 New_action_state = report_found (Action, Action_state, Filename),77 compare (Action, New_action_state,[], Ys);78 compare (Action, Action_state,[X | Xs], []) ->77 report_found (Action, Filename), 78 compare (Action, [], Ys); 79 compare (Action, [X | Xs], []) -> 79 80 {Filename, _} = X, 80 New_action_state = report_lost (Action, Action_state, Filename),81 compare (Action, New_action_state,Xs, []);82 compare (_, Action_state,[], []) ->83 Action_state.81 report_lost (Action, Filename), 82 compare (Action, Xs, []); 83 compare (_, [], []) -> 84 done. 84 85 85 report_found (Action, Action_state,Filename) ->86 Action (type (Filename), Filename, found , Action_state).86 report_found (Action, Filename) -> 87 Action (type (Filename), Filename, found). 87 88 88 report_lost (Action, Action_state,Filename) ->89 Action (unknown, Filename, lost , Action_state).89 report_lost (Action, Filename) -> 90 Action (unknown, Filename, lost). 90 91 91 report_changed (Action, Action_state,Filename) ->92 Action (type (Filename), Filename, changed , Action_state).92 report_changed (Action, Filename) -> 93 Action (type (Filename), Filename, changed). 93 94 94 95 type (Path) -> -
trunk/src/directory_watcher_test.erl
r50 r51 16 16 tests_from_non_existent () -> 17 17 Dir = fixtures: temporary_pathname (), 18 State = make_ref (), 19 Watcher = spawn_link (directory_watcher, init, [Dir, notify (), State]), 20 {watcher, directory, Dir, {error, enoent}, State} = receive_one (), 18 Watcher = spawn_link (directory_watcher, init, [Dir, notify ()]), 19 {watcher, directory, Dir, {error, enoent}} = receive_one (), 21 20 Watcher ! check, 22 {watcher, directory, Dir, {error, enoent} , State} = receive_one (),21 {watcher, directory, Dir, {error, enoent}} = receive_one (), 23 22 ok = file: make_dir (Dir), 24 23 Watcher ! check, … … 46 45 recursive (Dir, _) -> 47 46 Notify = directory_watcher: recursive (notify ()), 48 Watcher = spawn_link (directory_watcher, init, [Dir, Notify , ignore]),47 Watcher = spawn_link (directory_watcher, init, [Dir, Notify]), 49 48 Filename = filename: join ([Dir, "subdir", "subfile.ext"]), 50 Expected = {watcher, {file, ".ext"}, Filename, found , ignore},49 Expected = {watcher, {file, ".ext"}, Filename, found}, 51 50 ok = receive_until_found (Expected). 52 51 53 52 bad_symlinks_are_ignored (Dir, _) -> 54 Watcher = spawn_link (directory_watcher, init, [Dir, notify () , ignore]),53 Watcher = spawn_link (directory_watcher, init, [Dir, notify ()]), 55 54 Link = filename: join (Dir, "titi.erl"), 56 55 Destination = filename: join (Dir, "nofile"), … … 65 64 66 65 tests_from_empty (Dir, []) -> 67 State = make_ref (), 68 Watcher = spawn_link (directory_watcher, init, [Dir, notify (), State]), 66 Watcher = spawn_link (directory_watcher, init, [Dir, notify ()]), 69 67 timeout = receive_one (), 70 68 … … 72 70 ok = file: write_file (Filename, list_to_binary ("Hello")), 73 71 Watcher ! check, 74 {watcher, {file, ".txt"}, Filename, found , State} = receive_one (),72 {watcher, {file, ".txt"}, Filename, found} = receive_one (), 75 73 76 74 Subdir = filename: join (Dir, "mydir"), 77 75 ok = file: make_dir (Subdir), 78 76 Watcher ! check, 79 {watcher, directory, Subdir, found , State} = receive_one (),77 {watcher, directory, Subdir, found} = receive_one (), 80 78 81 79 Subfile = filename: join (Subdir, "mysubfile.txt"), … … 87 85 ok = file: del_dir (Subdir), 88 86 Watcher ! check, 89 {watcher, unknown, Subdir, lost , State} = receive_one (),87 {watcher, unknown, Subdir, lost} = receive_one (), 90 88 91 89 rewrite_same_data (Filename), … … 95 93 ok = file: write_file (Filename, list_to_binary ("Bye")), 96 94 Watcher ! check, 97 {watcher, {file, ".txt"}, Filename, changed , State} = receive_one (),95 {watcher, {file, ".txt"}, Filename, changed} = receive_one (), 98 96 99 97 Self = self (), … … 108 106 tests_with_several (Dir, Tree) -> 109 107 [Foo, Bar, Toto] = [filename: join (Dir, Name) || {file, Name, _} <- Tree], 110 State = make_ref (), 111 Watcher = spawn_link (directory_watcher, init, [Dir, notify (), State]), 112 {watcher, {file, ".txt"}, Bar, found, State} = receive_one (), 113 {watcher, {file, ".txt"}, Foo, found, State} = receive_one (), 114 {watcher, {file, ".erl"}, Toto, found, State} = receive_one (), 108 Watcher = spawn_link (directory_watcher, init, [Dir, notify ()]), 109 {watcher, {file, ".txt"}, Bar, found} = receive_one (), 110 {watcher, {file, ".txt"}, Foo, found} = receive_one (), 111 {watcher, {file, ".erl"}, Toto, found} = receive_one (), 115 112 116 113 ok = file: write_file (Foo, list_to_binary ("Bye")), 117 114 Watcher ! check, 118 {watcher, {file, ".txt"}, Foo, changed , State} = receive_one (),115 {watcher, {file, ".txt"}, Foo, changed} = receive_one (), 119 116 120 117 ok = file: write_file (Foo, list_to_binary ("Not yet")), 121 118 ok = file: write_file (Bar, list_to_binary ("Bye")), 122 119 Watcher ! check, 123 {watcher, {file, ".txt"}, Bar, changed , State} = receive_one (),120 {watcher, {file, ".txt"}, Bar, changed} = receive_one (), 124 121 Watcher ! check, 125 {watcher, {file, ".txt"}, Foo, changed , State} = receive_one (),122 {watcher, {file, ".txt"}, Foo, changed} = receive_one (), 126 123 ok. 127 124 128 125 notify () -> 129 126 Self = self (), 130 fun (Type, Path, Event, State) -> 131 Self ! {watcher, Type, Path, Event, State}, 132 State 127 fun (Type, Path, Event) -> 128 Self ! {watcher, Type, Path, Event} 133 129 end. 134 130
Note: See TracChangeset
for help on using the changeset viewer.
