Changeset 53 for trunk


Ignore:
Timestamp:
04/16/09 20:21:33 (3 years ago)
Author:
dom
Message:

Reintroduced recursivity to directory_watcher.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/directory_watcher.erl

    r52 r53  
    55-module (directory_watcher). 
    66-export ([init/2]). 
    7 %% -export ([recursive/1]). 
     7-export ([init_recursive/2]). 
    88-include_lib ("kernel/include/file.hrl"). 
    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. 
    169 
    1710init (Directory, Pid) -> 
    1811    check (Directory, Pid, []). 
     12 
     13init_recursive (Directory, Pid) -> 
     14    Watcher = spawn_link (?MODULE, init, [Directory, self ()]), 
     15    loop_recursive (Directory, Pid, [Watcher]). 
    1916 
    2017check (Directory, Pid, State) -> 
     
    5451        check -> 
    5552            check (Directory, Pid, Filenames); 
     53        stop -> 
     54            notify (Pid, bye) 
     55    end. 
     56 
     57loop_recursive (Directory, Pid, Watchers) -> 
     58    receive 
     59        {?MODULE, _, {directory, Dir, found}=Event} -> 
     60            Watcher = spawn_link (?MODULE, init, [Dir, self ()]), 
     61            notify (Pid, Event), 
     62            loop_recursive (Directory, Pid, [Watcher | Watchers]); 
     63        {?MODULE, _, Event} -> 
     64            notify (Pid, Event), 
     65            loop_recursive (Directory, Pid, Watchers); 
     66        check -> 
     67            lists: foreach (fun (P) -> P ! check end, Watchers); 
    5668        stop -> 
    5769            notify (Pid, bye) 
  • trunk/src/directory_watcher_test.erl

    r52 r53  
    99-export ([tests_with_several/0]). 
    1010-export ([bad_symlinks_are_ignored/0]). 
    11 %% -export ([recursive/0]). 
     11-export ([recursive/0]). 
    1212 
    1313tests_from_empty () -> 
     
    3636    ok = fixtures: use_tree ([], fun bad_symlinks_are_ignored/2). 
    3737 
    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). 
     38recursive () -> 
     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). 
    4444 
    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). 
     45recursive (Dir, _) -> 
     46    Watcher = spawn_link (directory_watcher, init_recursive, [Dir, self ()]), 
     47    Filename = filename: join ([Dir, "subdir", "subfile.ext"]), 
     48    Expected = {{file, ".ext"}, Filename, found}, 
     49    ok = receive_until_found (Expected). 
    5150 
    5251bad_symlinks_are_ignored (Dir, _) -> 
     
    127126    end. 
    128127 
    129 %% receive_until_found (Message) -> 
    130 %%     receive Message -> ok after 500 -> timeout end. 
     128receive_until_found (Event) -> 
     129    receive {directory_watcher, _, Event} -> ok 
     130    after 500 -> timeout 
     131    end. 
Note: See TracChangeset for help on using the changeset viewer.