anchor.diff:      end.
   
   
  -%% ensure_unique_filename/2
  -%% ====================================================================
  -%% @doc Tries to create a file until one is created (changing its name every time).
  -%% @end
  --spec ensure_unique_filename(Req :: req(), Counter :: integer()) -> string() | no_return().
  -%% ====================================================================
  -ensure_unique_filename(RequestedPath, 0) ->
  -    Ans = logical_files_manager:create(RequestedPath),
  -    case Ans of
  -        ok -> RequestedPath;
  -        _ -> ensure_unique_filename(RequestedPath, 1)
  -    end;
  -
  -ensure_unique_filename(_, 20) ->
  -    throw({"Error in ensure_unique_filename", counter_hit_20});
  -
  -ensure_unique_filename(RequestedPath, Counter) ->
  -    Ext = filename:extension(RequestedPath),
  -    Rootname = filename:rootname(RequestedPath),
  -    NewName = lists:flatten(io_lib:format("~s(~B)~s", [Rootname, Counter, Ext])),
  -    case logical_files_manager:create(NewName) of
  -        ok -> NewName;
  -        {_, file_exists} -> ensure_unique_filename(RequestedPath, Counter + 1);
  -        Error -> throw({"Error in ensure_unique_filename", Error})
  -    end.
  -
  -
  -%% ensure_unique_filename/2
  -%% ====================================================================
  -%% @doc Tries to create a file until one is created (changing its name every time).
  -%% @end
  --spec ensure_unique_filename(Req :: req(), Counter :: integer()) -> string() | no_return().
  -%% ====================================================================
  -stream_file_to_fslogic(Req, FullPath, BufferSize) ->
  -    case cowboy_req:part_body(Req, [{length, BufferSize}, {read_timeout, ?UPLOAD_PART_TIMEOUT}]) of
  -        {ok, Binary, Req2} ->
  -            write_to_file(Binary, FullPath),
  -            Req2;
  -        {more, Binary, Req2} ->
  -            write_to_file(Binary, FullPath),
  -            stream_file_to_fslogic(Req2, FullPath, BufferSize)
  -    end.
  -
  -
  -% Writes a chunk of data to a file via logical_files_manager
  -write_to_file(Binary, FullPath) ->
  -    Size = size(Binary),
  -    BytesWritten = logical_files_manager:write(FullPath, Binary),
  -    case BytesWritten of
  -        I when is_integer(I) ->
  -            case BytesWritten of
  -                Size -> done;
  -                Offset ->
  -                    write_to_file(binary:part(Binary, Offset, Size - Offset), FullPath)
  -            end;
  -        Error ->
  -            throw({"Error in write_to_file", Error})
  -    end.
  -
  -
  -% Returns buffer size used to send file to a client (from config), or default.
  -get_upload_buffer_size() ->
  -    _Size = case application:get_env(veil_cluster_node, control_panel_upload_buffer) of
  -                {ok, Value} ->
  -                    Value;
  -                _ ->
  -                    ?error("Could not read 'control_panel_upload_buffer' from config. Make sure it is present in config.yml and .app.src."),
  -                    ?UPLOAD_BUFFER_SIZE
  -            end.
  -
  -
  -%% ====================================================================
  -%% INTERNAL FUNCTIONS - REST upload handling
  -%% ====================================================================
  -
  -% parses a multipart data POST request and write its data to a file
  -% at specified path
  +%% parse_rest_upload/3
  +%% ====================================================================
  +%% @doc Parses a multipart data POST request and writes its data to a file
  +%% under specified path.
  +%% @end
  +-spec parse_rest_upload(Req :: req(), Path :: string()) -> {boolean(), req()}.
  +%% ====================================================================
   parse_rest_upload(Req, Path) ->
       case cowboy_req:part(Req) of
           {ok, Headers, Req2} ->
  
positive.code:     end.
  
  
  %%     Part = try multipart_data(Req) catch _:_ -> {false, Req} end,
  %%     case Part of
  %%         {eof, NewReq} ->
  %%             {true, NewReq};
  %%         {headers, Headers, NewReq} ->
  %%             case (length(Headers) == 1) of
  %%                 true -> {true, NewReq};
  %%                 false -> NewReq2 = try
  %%                     stream_file_to_fslogic(NewReq, Path, get_upload_buffer_size())
  %%                                    catch _:_ ->
  %%                                        logical_files_manager:delete(Path),
  %%                                        {false, NewReq}
  %%                                    end,
  %%                     {true, NewReq2}
  %%             end;
  %%         _ -> {false, Req}
  %%     end.
  
  % try to create empty file at specified path if it doesn't exist
  % or truncate its size to 0 if it exists and "overwrite" is set
  try_to_create_file(Path, Overwrite) ->
      try_to_create_file("/", string:tokens(Path, "/"), Overwrite).
  
  % checkig if file can be created at specified path
  % namely path is a valid filesystem path compose of directories
  % except last regular file
  try_to_create_file(Path, [Subdir | Subdirs], Overwrite) ->
      case logical_files_manager:exists(Path) of
          true -> case logical_files_manager:getfileattr(Path) of
  
negative.code:         {_, Error} -> {error, Error}
      end.
  
  
  %% create_file_and_required_parent_dirs/2
  %% ====================================================================
  %% @doc Creates all required parent directories to create a file
  %% specified as the last element on the list of subdirectories.
  %% @end
  -spec create_file_and_required_parent_dirs(Path :: string(), [string()]) -> ok | {error, term()}.
  %% ====================================================================
  create_file_and_required_parent_dirs(Path, [Subdir | Subdirs]) ->
      case logical_files_manager:mkdir(Path) of
          ok -> create_file_and_required_parent_dirs(Path ++ Subdir ++ "/", Subdirs);
          {_, Error} -> {error, Error}
      end;
  
  create_file_and_required_parent_dirs(Path, []) ->
      case logical_files_manager:create(Path) of
          ok -> ok;
          {_, Error} -> {error, Error}
      end.
  