Allow people to specify append, but silently map it to write bug21673
authorMatthew Sackman <matthew@lshift.net>
Thu Apr 29 16:45:23 2010 +0100 (2010-04-29)
branchbug21673
changeset 32255c473bfae335
parent 3224 c39bb7eff0b9
child 3226 6b3eb3160ea6
Allow people to specify append, but silently map it to write
src/file_handle_cache.erl
     1.1 --- a/src/file_handle_cache.erl	Thu Apr 29 16:30:03 2010 +0100
     1.2 +++ b/src/file_handle_cache.erl	Thu Apr 29 16:45:23 2010 +0100
     1.3 @@ -224,36 +224,32 @@
     1.4      gen_server:call(?SERVER, {register_callback, self(), {M, F, A}}, infinity).
     1.5  
     1.6  open(Path, Mode, Options) ->
     1.7 -    case is_appender(Mode) of
     1.8 -        true  ->
     1.9 -            {error, append_not_supported};
    1.10 -        false ->
    1.11 -            Path1 = filename:absname(Path),
    1.12 -            File1 = #file { reader_count = RCount, has_writer = HasWriter } =
    1.13 -                case get({Path1, fhc_file}) of
    1.14 -                    File = #file {} -> File;
    1.15 -                    undefined       -> #file { reader_count = 0,
    1.16 -                                               has_writer = false }
    1.17 -                end,
    1.18 -            IsWriter = is_writer(Mode),
    1.19 -            case IsWriter andalso HasWriter of
    1.20 -                true  -> {error, writer_exists};
    1.21 -                false -> Ref = make_ref(),
    1.22 -                         case open1(Path1, Mode, Options, Ref, bof, new) of
    1.23 -                             {ok, _Handle} ->
    1.24 -                                 RCount1 = case is_reader(Mode) of
    1.25 -                                               true  -> RCount + 1;
    1.26 -                                               false -> RCount
    1.27 -                                           end,
    1.28 -                                 HasWriter1 = HasWriter orelse IsWriter,
    1.29 -                                 put({Path1, fhc_file},
    1.30 -                                     File1 #file { reader_count = RCount1,
    1.31 -                                                   has_writer = HasWriter1}),
    1.32 -                                 {ok, Ref};
    1.33 -                             Error ->
    1.34 -                                 Error
    1.35 -                         end
    1.36 -            end
    1.37 +    Path1 = filename:absname(Path),
    1.38 +    File1 = #file { reader_count = RCount, has_writer = HasWriter } =
    1.39 +        case get({Path1, fhc_file}) of
    1.40 +            File = #file {} -> File;
    1.41 +            undefined       -> #file { reader_count = 0,
    1.42 +                                       has_writer = false }
    1.43 +        end,
    1.44 +    Mode1 = append_to_write(Mode),
    1.45 +    IsWriter = is_writer(Mode1),
    1.46 +    case IsWriter andalso HasWriter of
    1.47 +        true  -> {error, writer_exists};
    1.48 +        false -> Ref = make_ref(),
    1.49 +                 case open1(Path1, Mode1, Options, Ref, bof, new) of
    1.50 +                     {ok, _Handle} ->
    1.51 +                         RCount1 = case is_reader(Mode1) of
    1.52 +                                       true  -> RCount + 1;
    1.53 +                                       false -> RCount
    1.54 +                                   end,
    1.55 +                         HasWriter1 = HasWriter orelse IsWriter,
    1.56 +                         put({Path1, fhc_file},
    1.57 +                             File1 #file { reader_count = RCount1,
    1.58 +                                           has_writer = HasWriter1}),
    1.59 +                         {ok, Ref};
    1.60 +                     Error ->
    1.61 +                         Error
    1.62 +                 end
    1.63      end.
    1.64  
    1.65  close(Ref) ->
    1.66 @@ -462,7 +458,11 @@
    1.67  
    1.68  is_writer(Mode) -> lists:member(write, Mode).
    1.69  
    1.70 -is_appender(Mode) -> lists:member(append, Mode).
    1.71 +append_to_write(Mode) ->
    1.72 +    case lists:member(append, Mode) of
    1.73 +        true  -> [write | lists:subtract(Mode, [append, write])];
    1.74 +        false -> Mode
    1.75 +    end.
    1.76  
    1.77  with_handles(Refs, Fun) ->
    1.78      ResHandles = lists:foldl(