1%% This Source Code Form is subject to the terms of the Mozilla Public
2%% License, v. 2.0. If a copy of the MPL was not distributed with this
3%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4%%
5%% Copyright (c) 2007-2021 VMware, Inc. or its affiliates.  All rights reserved.
6%%
7
8-module(rabbit_tracing_files).
9
10-include_lib("kernel/include/file.hrl").
11
12-export([list/0, exists/1, delete/1, full_path/1]).
13
14%%--------------------------------------------------------------------
15
16list() ->
17    {ok, Dir} = application:get_env(rabbitmq_tracing, directory),
18    ok = filelib:ensure_dir(Dir ++ "/a"),
19    {ok, Names} = file:list_dir(Dir),
20    [file_info(Name) || Name <- Names].
21
22exists(Name) ->
23    filelib:is_regular(full_path(Name)).
24
25delete(Name) ->
26    ok = file:delete(full_path(Name)).
27
28full_path(Name0) when is_binary(Name0) ->
29    full_path(binary_to_list(Name0));
30full_path(Name0) ->
31    {ok, Dir} = application:get_env(rabbitmq_tracing, directory),
32    case rabbit_http_util:safe_relative_path(Name0) of
33        undefined -> exit(how_rude);
34        Name      -> Dir ++ "/" ++ Name
35    end.
36
37%%--------------------------------------------------------------------
38
39file_info(Name) ->
40    Size = case file:read_file_info(full_path(Name)) of
41               {ok, Info} ->
42                   Info#file_info.size;
43               {error, Error} ->
44                   rabbit_log:warning("error getting file info for ~s: ~p",
45                                      [Name, Error]),
46                   0
47           end,
48    [{name, list_to_binary(Name)}, {size, Size}].
49