anchor.diff:          {_, Error} -> {error, Error}
       end;
   
  -% check if file exists at specified path and truncate its
  -% to 0 if "overwrite" is set
  +%% Check if file exists at specified path and truncate its size
  +%% to 0 if "overwrite" is set.
   try_to_create_file(Path, [], Overwrite) ->
       case logical_files_manager:exists(Path) of
           true -> case logical_files_manager:getfileattr(Path) of
  
positive.code:     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(Req, Path) ->
      case cowboy_req:part(Req) of
          {ok, Headers, Req2} ->
  
negative.code:     end.
  
  
  %% stream_file_to_fslogic/3
  %% ====================================================================
  %% @doc Streams a data to file from socket (incoming multipart data)
  %% @end
  -spec stream_file_to_fslogic(Req :: req(), FullPath :: string(), BufferSize :: integer()) -> req().
  %% ====================================================================
  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.
  
  
  %% write_to_file/2
  %% ====================================================================
  %% @doc Writes a chunk of data to a file via logical_files_manager
  %% @end
  -spec write_to_file(Binary :: binary(), FullPath :: string()) -> done | no_return().
  %% ====================================================================
  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.
  
  
  %% get_upload_buffer_size/0
  %% ====================================================================
  %% @doc Returns buffer size used to send file to a client (from config), or default.
  %% @end
  -spec get_upload_buffer_size() -> integer().
  %% ====================================================================
  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.
  
  
  %% 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.
  
  
  %% try_to_create_file/2
  %% ====================================================================
  %% @doc Tries 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.
  %% @end
  -spec try_to_create_file(Path :: string(), Overwrite :: boolean()) -> ok | {error, term()}.
  %% ====================================================================
  try_to_create_file(Path, Overwrite) ->
      try_to_create_file("/", string:tokens(Path, "/"), Overwrite).
  
  %% Check if file can be created at specified path
  %% namely path is a valid filesystem path composed 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
  