Changeset 51 for trunk


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

Simplifying: remove unneeded State carried around by directory_watcher's notify/action/fun.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/directory_watcher.erl

    r50 r51  
    44 
    55-module (directory_watcher). 
    6 -export ([init/3]). 
     6-export ([init/2]). 
    77-export ([recursive/1]). 
    88-include_lib ("kernel/include/file.hrl"). 
    99 
    1010recursive (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) 
    1515    end. 
    1616 
    17 init (Directory, Action, Action_state) -> 
    18     check (Directory, Action, Action_state, []). 
     17init (Directory, Action) -> 
     18    check (Directory, Action, []). 
    1919 
    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). 
     20check (Directory, Action, State) -> 
     21    New_state = list_dir (Directory, Action, State), 
     22    compare (Action, State, New_state), 
     23    loop (Directory, Action, New_state). 
    2424 
    25 list_dir (Directory, Action, Action_state, State) -> 
    26     list_dir (Directory, Action, Action_state, State, file: list_dir (Directory)). 
     25list_dir (Directory, Action, State) -> 
     26    list_dir (Directory, Action, State, file: list_dir (Directory)). 
    2727 
    28 list_dir (Directory, _, Action_state, _, {ok, Filenames}) -> 
     28list_dir (Directory, _, _, {ok, Filenames}) -> 
    2929    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); 
     31list_dir (Directory, Action, State, Error) -> 
     32    Action (directory, Directory, Error), 
     33    State. 
    3334 
    3435read_state (Filenames) -> 
     
    4950    directory. 
    5051 
    51 loop (Directory, Action, Action_state, Filenames) -> 
     52loop (Directory, Action, Filenames) -> 
    5253    receive 
    5354        check -> 
    54             check (Directory, Action, Action_state, Filenames); 
     55            check (Directory, Action, Filenames); 
    5556        {Pid, stop} -> 
    5657            Pid ! {self (), bye} 
    5758    end. 
    5859 
    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]) -> 
     60compare (_, State, State) -> 
     61    done; 
     62compare (Action, [X | Xs], [X | Ys]) -> 
     63    compare (Action, Xs, Ys); 
     64compare (Action, [{F, _} | Xs], [{F, _} | Ys]) -> 
     65    report_changed (Action, F), 
     66    compare (Action, Xs, Ys); 
     67compare (Action, [X, {F, V} | Xs], [{F, W} | Ys]) -> 
    6768    {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]); 
     71compare (Action, [{F, V} | Xs], [Y, {F, W} | Ys]) -> 
    7172    {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]); 
     75compare (Action, [], [Y | Ys]) -> 
    7576    {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); 
     79compare (Action, [X | Xs], []) -> 
    7980    {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, []); 
     83compare (_, [], []) -> 
     84    done. 
    8485 
    85 report_found (Action, Action_state, Filename) -> 
    86     Action (type (Filename), Filename, found, Action_state). 
     86report_found (Action, Filename) -> 
     87    Action (type (Filename), Filename, found). 
    8788 
    88 report_lost (Action, Action_state, Filename) -> 
    89     Action (unknown, Filename, lost, Action_state). 
     89report_lost (Action, Filename) -> 
     90    Action (unknown, Filename, lost). 
    9091 
    91 report_changed (Action, Action_state, Filename) -> 
    92     Action (type (Filename), Filename, changed, Action_state). 
     92report_changed (Action, Filename) -> 
     93    Action (type (Filename), Filename, changed). 
    9394     
    9495type (Path) -> 
  • trunk/src/directory_watcher_test.erl

    r50 r51  
    1616tests_from_non_existent () -> 
    1717    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 (), 
    2120    Watcher ! check, 
    22     {watcher, directory, Dir, {error, enoent}, State} = receive_one (), 
     21    {watcher, directory, Dir, {error, enoent}} = receive_one (), 
    2322    ok = file: make_dir (Dir), 
    2423    Watcher ! check, 
     
    4645recursive (Dir, _) -> 
    4746    Notify = directory_watcher: recursive (notify ()), 
    48     Watcher = spawn_link (directory_watcher, init, [Dir, Notify, ignore]), 
     47    Watcher = spawn_link (directory_watcher, init, [Dir, Notify]), 
    4948    Filename = filename: join ([Dir, "subdir", "subfile.ext"]), 
    50     Expected = {watcher, {file, ".ext"}, Filename, found, ignore}, 
     49    Expected = {watcher, {file, ".ext"}, Filename, found}, 
    5150    ok = receive_until_found (Expected). 
    5251 
    5352bad_symlinks_are_ignored (Dir, _) -> 
    54     Watcher = spawn_link (directory_watcher, init, [Dir, notify (), ignore]), 
     53    Watcher = spawn_link (directory_watcher, init, [Dir, notify ()]), 
    5554    Link = filename: join (Dir, "titi.erl"), 
    5655    Destination = filename: join (Dir, "nofile"), 
     
    6564     
    6665tests_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 ()]), 
    6967    timeout = receive_one (), 
    7068     
     
    7270    ok = file: write_file (Filename, list_to_binary ("Hello")), 
    7371    Watcher ! check, 
    74     {watcher, {file, ".txt"}, Filename, found, State} = receive_one (), 
     72    {watcher, {file, ".txt"}, Filename, found} = receive_one (), 
    7573 
    7674    Subdir = filename: join (Dir, "mydir"), 
    7775    ok = file: make_dir (Subdir), 
    7876    Watcher ! check, 
    79     {watcher, directory, Subdir, found, State} = receive_one (), 
     77    {watcher, directory, Subdir, found} = receive_one (), 
    8078     
    8179    Subfile = filename: join (Subdir, "mysubfile.txt"), 
     
    8785    ok = file: del_dir (Subdir), 
    8886    Watcher ! check, 
    89     {watcher, unknown, Subdir, lost, State} = receive_one (), 
     87    {watcher, unknown, Subdir, lost} = receive_one (), 
    9088 
    9189    rewrite_same_data (Filename), 
     
    9593    ok = file: write_file (Filename, list_to_binary ("Bye")), 
    9694    Watcher ! check, 
    97     {watcher, {file, ".txt"}, Filename, changed, State} = receive_one (), 
     95    {watcher, {file, ".txt"}, Filename, changed} = receive_one (), 
    9896     
    9997    Self = self (), 
     
    108106tests_with_several (Dir, Tree) -> 
    109107    [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 (), 
    115112     
    116113    ok = file: write_file (Foo, list_to_binary ("Bye")), 
    117114    Watcher ! check, 
    118     {watcher, {file, ".txt"}, Foo, changed, State} = receive_one (), 
     115    {watcher, {file, ".txt"}, Foo, changed} = receive_one (), 
    119116     
    120117    ok = file: write_file (Foo, list_to_binary ("Not yet")), 
    121118    ok = file: write_file (Bar, list_to_binary ("Bye")), 
    122119    Watcher ! check, 
    123     {watcher, {file, ".txt"}, Bar, changed, State} = receive_one (), 
     120    {watcher, {file, ".txt"}, Bar, changed} = receive_one (), 
    124121    Watcher ! check, 
    125     {watcher, {file, ".txt"}, Foo, changed, State} = receive_one (), 
     122    {watcher, {file, ".txt"}, Foo, changed} = receive_one (), 
    126123    ok. 
    127124                                
    128125notify () -> 
    129126    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} 
    133129    end. 
    134130 
Note: See TracChangeset for help on using the changeset viewer.