Ignore:
Timestamp:
07/17/09 13:31:39 (3 years ago)
Author:
dom
Message:

Fixed more bugs in directory_watcher.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/directory_watcher.erl

    r75 r80  
    66-export ([init/2]). 
    77-export ([init_recursive/2]). 
    8 -export([start_io/2]). 
    9 -export([start_io/0]). 
    108-include_lib ("kernel/include/file.hrl"). 
    119 
    12 start_io () -> 
    13     start_io (".", 3000). 
    14  
    15 start_io (Dir, Interval) -> 
    16     Watcher = spawn_link (?MODULE, init_recursive, [Dir, self ()]), 
    17     io_loop (Interval, Watcher). 
    18  
    19 io_loop (Interval, Watcher) -> 
    20     Watcher ! check, 
    21     receive 
    22         {directory_watcher, _, Event} -> 
    23             io: fwrite ("~p~n", [Event]), 
    24             io_loop (Interval, Watcher); 
    25         stop -> 
    26             bye 
    27     after Interval -> 
    28             io_loop (Interval, Watcher) 
    29     end. 
    30  
    3110init (Directory, F) -> 
    32     check (Directory, F, []). 
     11    D = filename: absname (Directory), 
     12    check (D, F, []). 
    3313 
    3414init_recursive (Directory, F) -> 
     15    D = filename: absname (Directory), 
    3516    Self = self (), 
    36     Watcher = spawn_link (?MODULE, init, [Directory, send (Self)]), 
    37     loop_recursive (Directory, F, [Watcher]). 
     17    Watcher = spawn_link (?MODULE, init, [D, send (Self)]), 
     18    loop_recursive (D, F, [Watcher]). 
    3819 
    3920check (Directory, F, State) -> 
    4021    New_state = list_dir (Directory, F), 
    41     compare (F, State, New_state), 
     22    sort_and_compare (F, State, New_state), 
    4223    loop (Directory, F, New_state). 
    4324 
     
    9980    end. 
    10081 
    101 compare (_, State, State) -> 
    102     done; 
    103 compare (Fun, [X | Xs], [X | Ys]) -> 
    104     compare (Fun, Xs, Ys); 
    105 compare (Fun, [{F, {regular, _}} | Xs], [{F, {regular, _}} | Ys]) -> 
    106     report_changed (Fun, F), 
    107     compare (Fun, Xs, Ys); 
    108 compare (Fun, [{F, _}=X | Xs], [{F, _} | Ys]) -> 
    109     report_lost (Fun, X), 
    110     report_found (Fun, F), 
    111     compare (Fun, Xs, Ys); 
    112 compare (Fun, [X, {F, V} | Xs], [{F, W} | Ys]) -> 
    113     report_lost (Fun, X), 
    114     compare (Fun, [{F, V} | Xs], [{F, W} | Ys]); 
    115 compare (Fun, [{F, V} | Xs], [Y, {F, W} | Ys]) -> 
    116     {Filename, _} = Y, 
    117     report_found (Fun, Filename), 
    118     compare (Fun, [{F, V} | Xs], [{F, W} | Ys]); 
    119 compare (Fun, [], [Y | Ys]) -> 
    120     {Filename, _} = Y, 
    121     report_found (Fun, Filename), 
    122     compare (Fun, [], Ys); 
    123 compare (Fun, [X | Xs], []) -> 
    124     report_lost (Fun, X), 
    125     compare (Fun, Xs, []); 
    126 compare (Fun, [X], [Y]) -> 
    127     report_lost (Fun, X), 
    128     {File, _} = Y, 
    129     report_found (Fun, File), 
    130     done; 
    131 compare (_, [], []) -> 
     82sort_and_compare (F, Old, New) -> 
     83    Original = dict: from_list (Old), 
     84    Modified = dict: from_list (New), 
     85    Compared = adlib: compare_dict (Modified, Original), 
     86    dict: fold (fun notify_new/3, F, dict: fetch (new, Compared)), 
     87    dict: fold (fun notify_lost/3, F, dict: fetch (lost, Compared)), 
     88    dict: fold (fun notify_changed/3, F, dict: fetch (changed, Compared)), 
    13289    done. 
     90     
     91notify_new (File_name, _, Fun) -> 
     92    report_found (Fun, File_name), 
     93    Fun. 
    13394 
    13495report_found (F, Filename) -> 
    13596    F ({type (Filename), Filename, found}). 
     97 
     98notify_lost (File_name, Value, Fun) -> 
     99    report_lost (Fun, {File_name, Value}), 
     100    Fun. 
    136101 
    137102report_lost (F, {Filename, directory}) -> 
     
    139104report_lost (F, {Filename, {regular, _}}) -> 
    140105    F ({{file, filename: extension (Filename)}, Filename, lost}). 
     106 
     107notify_changed (File_name, {{regular, _}, {regular, _}}, Fun) -> 
     108    report_changed (Fun, File_name), 
     109    Fun; 
     110notify_changed (File_name, {_, Original}, Fun) -> 
     111    report_lost (Fun, {File_name, Original}), 
     112    report_found (Fun, File_name), 
     113    Fun. 
    141114 
    142115report_changed (F, Filename) -> 
Note: See TracChangeset for help on using the changeset viewer.