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