summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_tracing/src/rabbit_tracing_files.erl
blob: dc3a70b7cc2f267548981f0cf62ae84423d22973 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2020 VMware, Inc. or its affiliates.  All rights reserved.
%%

-module(rabbit_tracing_files).

-include_lib("kernel/include/file.hrl").

-export([list/0, exists/1, delete/1, full_path/1]).

%%--------------------------------------------------------------------

list() ->
    {ok, Dir} = application:get_env(rabbitmq_tracing, directory),
    ok = filelib:ensure_dir(Dir ++ "/a"),
    {ok, Names} = file:list_dir(Dir),
    [file_info(Name) || Name <- Names].

exists(Name) ->
    filelib:is_regular(full_path(Name)).

delete(Name) ->
    ok = file:delete(full_path(Name)).

full_path(Name0) when is_binary(Name0) ->
    full_path(binary_to_list(Name0));
full_path(Name0) ->
    {ok, Dir} = application:get_env(rabbitmq_tracing, directory),
    case rabbit_http_util:safe_relative_path(Name0) of
        undefined -> exit(how_rude);
        Name      -> Dir ++ "/" ++ Name
    end.

%%--------------------------------------------------------------------

file_info(Name) ->
    Size = case file:read_file_info(full_path(Name)) of
               {ok, Info} ->
                   Info#file_info.size;
               {error, Error} ->
                   rabbit_log:warning("error getting file info for ~s: ~p",
                                      [Name, Error]),
                   0
           end,
    [{name, list_to_binary(Name)}, {size, Size}].