Changeset 52


Ignore:
Timestamp:
04/16/09 19:54:51 (3 years ago)
Author:
dom
Message:

Simplifying: replace directory_watcher's notify/action/fun by a Pid which receives a message (temporarily comment out recursivity).

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/directory_watcher.erl

    r51 r52  
    55-module (directory_watcher). 
    66-export ([init/2]). 
    7 -export ([recursive/1]). 
     7%% -export ([recursive/1]). 
    88-include_lib ("kernel/include/file.hrl"). 
    99 
    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. 
    1616 
    17 init (Directory, Action) -> 
    18     check (Directory, Action, []). 
     17init (Directory, Pid) -> 
     18    check (Directory, Pid, []). 
    1919 
    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). 
     20check (Directory, Pid, State) -> 
     21    New_state = list_dir (Directory, Pid, State), 
     22    compare (Pid, State, New_state), 
     23    loop (Directory, Pid, New_state). 
    2424 
    25 list_dir (Directory, Action, State) -> 
    26     list_dir (Directory, Action, State, file: list_dir (Directory)). 
     25list_dir (Directory, Pid, State) -> 
     26    list_dir (Directory, Pid, State, file: list_dir (Directory)). 
    2727 
    2828list_dir (Directory, _, _, {ok, Filenames}) -> 
    2929    Paths = [filename: join (Directory, F) || F <- Filenames], 
    3030    read_state (Paths); 
    31 list_dir (Directory, Action, State, Error) -> 
    32     Action (directory, Directory, Error), 
     31list_dir (Directory, Pid, State, Error) -> 
     32    notify (Pid, {directory, Directory, Error}), 
    3333    State. 
    3434 
     
    5050    directory. 
    5151 
    52 loop (Directory, Action, Filenames) -> 
     52loop (Directory, Pid, Filenames) -> 
    5353    receive 
    5454        check -> 
    55             check (Directory, Action, Filenames); 
    56         {Pid, stop} -> 
    57             Pid ! {self (), bye} 
     55            check (Directory, Pid, Filenames); 
     56        stop -> 
     57            notify (Pid, bye) 
    5858    end. 
    5959 
    6060compare (_, State, State) -> 
    6161    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]) -> 
     62compare (Pid, [X | Xs], [X | Ys]) -> 
     63    compare (Pid, Xs, Ys); 
     64compare (Pid, [{F, _} | Xs], [{F, _} | Ys]) -> 
     65    report_changed (Pid, F), 
     66    compare (Pid, Xs, Ys); 
     67compare (Pid, [X, {F, V} | Xs], [{F, W} | Ys]) -> 
    6868    {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]); 
     71compare (Pid, [{F, V} | Xs], [Y, {F, W} | Ys]) -> 
    7272    {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]); 
     75compare (Pid, [], [Y | Ys]) -> 
    7676    {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); 
     79compare (Pid, [X | Xs], []) -> 
    8080    {Filename, _} = X, 
    81     report_lost (Action, Filename), 
    82     compare (Action, Xs, []); 
     81    report_lost (Pid, Filename), 
     82    compare (Pid, Xs, []); 
    8383compare (_, [], []) -> 
    8484    done. 
    8585 
    86 report_found (Action, Filename) -> 
    87     Action (type (Filename), Filename, found). 
     86report_found (Pid, Filename) -> 
     87    notify (Pid, {type (Filename), Filename, found}). 
    8888 
    89 report_lost (Action, Filename) -> 
    90     Action (unknown, Filename, lost). 
     89report_lost (Pid, Filename) -> 
     90    notify (Pid, {unknown, Filename, lost}). 
    9191 
    92 report_changed (Action, Filename) -> 
    93     Action (type (Filename), Filename, changed). 
     92report_changed (Pid, Filename) -> 
     93    notify (Pid, {type (Filename), Filename, changed}). 
    9494     
    9595type (Path) -> 
     
    100100type ({ok, #file_info{type=regular}}, Path) -> 
    101101    {file, filename: extension (Path)}. 
     102 
     103notify (Pid, Event) -> 
     104    Pid ! {?MODULE, self (), Event}. 
  • trunk/src/directory_watcher_test.erl

    r51 r52  
    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 () -> 
     
    1616tests_from_non_existent () -> 
    1717    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 (), 
    2020    Watcher ! check, 
    21     {watcher, directory, Dir, {error, enoent}} = receive_one (), 
     21    {directory, Dir, {error, enoent}} = receive_one (), 
    2222    ok = file: make_dir (Dir), 
    2323    Watcher ! check, 
     
    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). 
     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). 
    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). 
     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). 
    5151 
    5252bad_symlinks_are_ignored (Dir, _) -> 
    53     Watcher = spawn_link (directory_watcher, init, [Dir, notify ()]), 
     53    Watcher = spawn_link (directory_watcher, init, [Dir, self ()]), 
    5454    Link = filename: join (Dir, "titi.erl"), 
    5555    Destination = filename: join (Dir, "nofile"), 
     
    6464     
    6565tests_from_empty (Dir, []) -> 
    66     Watcher = spawn_link (directory_watcher, init, [Dir, notify ()]), 
     66    Watcher = spawn_link (directory_watcher, init, [Dir, self ()]), 
    6767    timeout = receive_one (), 
    6868     
     
    7070    ok = file: write_file (Filename, list_to_binary ("Hello")), 
    7171    Watcher ! check, 
    72     {watcher, {file, ".txt"}, Filename, found} = receive_one (), 
     72    {{file, ".txt"}, Filename, found} = receive_one (), 
    7373 
    7474    Subdir = filename: join (Dir, "mydir"), 
    7575    ok = file: make_dir (Subdir), 
    7676    Watcher ! check, 
    77     {watcher, directory, Subdir, found} = receive_one (), 
     77    {directory, Subdir, found} = receive_one (), 
    7878     
    7979    Subfile = filename: join (Subdir, "mysubfile.txt"), 
     
    8585    ok = file: del_dir (Subdir), 
    8686    Watcher ! check, 
    87     {watcher, unknown, Subdir, lost} = receive_one (), 
     87    {unknown, Subdir, lost} = receive_one (), 
    8888 
    8989    rewrite_same_data (Filename), 
     
    9393    ok = file: write_file (Filename, list_to_binary ("Bye")), 
    9494    Watcher ! check, 
    95     {watcher, {file, ".txt"}, Filename, changed} = receive_one (), 
     95    {{file, ".txt"}, Filename, changed} = receive_one (), 
    9696     
    97     Self = self (), 
    98     Watcher ! {Self, stop}, 
    99     {Watcher, bye} = receive_one (), 
     97    Watcher ! stop, 
     98    bye = receive_one (), 
    10099    false = is_process_alive (Watcher), 
    101100    ok. 
     
    106105tests_with_several (Dir, Tree) -> 
    107106    [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 (), 
    112111     
    113112    ok = file: write_file (Foo, list_to_binary ("Bye")), 
    114113    Watcher ! check, 
    115     {watcher, {file, ".txt"}, Foo, changed} = receive_one (), 
     114    {{file, ".txt"}, Foo, changed} = receive_one (), 
    116115     
    117116    ok = file: write_file (Foo, list_to_binary ("Not yet")), 
    118117    ok = file: write_file (Bar, list_to_binary ("Bye")), 
    119118    Watcher ! check, 
    120     {watcher, {file, ".txt"}, Bar, changed} = receive_one (), 
     119    {{file, ".txt"}, Bar, changed} = receive_one (), 
    121120    Watcher ! check, 
    122     {watcher, {file, ".txt"}, Foo, changed} = receive_one (), 
     121    {{file, ".txt"}, Foo, changed} = receive_one (), 
    123122    ok. 
    124                                 
    125 notify () -> 
    126     Self = self (), 
    127     fun (Type, Path, Event) -> 
    128             Self ! {watcher, Type, Path, Event} 
     123 
     124receive_one () -> 
     125    receive {directory_watcher, _, Event} -> Event 
     126    after 500 -> timeout 
    129127    end. 
    130128 
    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.