Skip to content

Commit 26286b9

Browse files
committed
compute store flow path based on user_data folder
There are some enviroments whe erlang-red is running as a non-privileged user. Thus, if erlang-red is installed on /usr/ path it will not have the correct permissions when writing flows to disk. This change allows erlang-red compute a store flow path based on: 1. if flow_store application property has been configured, erlang-red will use that value. Otherwise: 2. get `user_data` [1] from filename:basedir/2 as a base folder for storing flows. e.g.: in my host (linux based) the store folder is /home/joaohf/.local/share/erlang-red/ 1: https://www.erlang.org/doc/apps/stdlib/filename.html#basedir/3
1 parent 2c4e0d3 commit 26286b9

4 files changed

Lines changed: 34 additions & 26 deletions

File tree

src/ered_flow_store.erl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-module(ered_flow_store).
2+
3+
-export([store_flow/0, store_flow_id/1, store_main_flow/0]).
4+
5+
store_flow_id(FlowId) when is_binary(FlowId) ->
6+
store_flow_id(binary_to_list(FlowId));
7+
store_flow_id(FlowId) when is_list(FlowId) ->
8+
filename:join([get_store_flow(), FlowId, "flows.json"]).
9+
10+
store_flow() ->
11+
get_store_flow().
12+
13+
store_main_flow() ->
14+
filename:join([get_store_flow(), "flows.json"]).
15+
16+
get_store_flow() ->
17+
application:get_env(
18+
erlang_red, flow_store, filename:basedir(user_data, "erlang-red")
19+
).

src/nodes/ered_node_flowhub_pull.erl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ handle_msg(_, NodeDef) ->
6969
%%
7070

7171
handle_flowid(FlowId, Msg, #{<<"wires">> := [WiresPort1 | _]} = NodeDef) ->
72-
FileName = io_lib:format(
73-
"~s/testflows/~s/flows.json",
74-
[code:priv_dir(erlang_red), FlowId]
75-
),
72+
FileName = ered_flow_store:store_flow_id(FlowId),
7673

7774
case file:read_file(FileName) of
7875
{ok, FileData} ->

src/servers/ered_credentials_store.erl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ terminate(Event, _State) ->
134134
%% ----------------- helpers
135135
%%
136136
load_initial_credentials() ->
137-
FileName = io_lib:format("~s/flows_cred.json", [code:priv_dir(erlang_red)]),
137+
FileName = filename:join(ered_flow_store:store_flow(), "flows_cred.json"),
138+
138139
case filelib:is_regular(FileName) of
139140
false ->
140141
#{store => #{}};
@@ -144,7 +145,7 @@ load_initial_credentials() ->
144145
end.
145146

146147
store_credentials_to_disk(#{store := Hsh} = _State) ->
147-
FileName = io_lib:format("~s/flows_cred.json", [code:priv_dir(erlang_red)]),
148+
FileName = filename:join(ered_flow_store:store_flow(), "flows_cred.json"),
148149

149150
case file:write_file(FileName, json:encode(Hsh)) of
150151
ok ->

src/servers/ered_flow_store_server.erl

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ handle_call({filename, FlowId}, _From, FlowStore) ->
122122
{reply, error, FlowStore}
123123
end;
124124
handle_call({retrieve_main_flow}, _From, FlowStore) ->
125-
SrcFileName = io_lib:format(
126-
"~s/flows.json",
127-
[code:priv_dir(erlang_red)]
128-
),
125+
SrcFileName = ered_flow_store:store_main_flow(),
129126

130127
{reply, file:read_file(SrcFileName), FlowStore};
131128
handle_call(_Msg, _From, FlowStore) ->
@@ -146,10 +143,7 @@ handle_info({store_main_flow, FlowData}, State) ->
146143
%% remove any reference to <<"credentials">>
147144
NodeAry = remove_credentials(NodeAryWithCreds),
148145

149-
DestFileName = io_lib:format(
150-
"~s/flows.json",
151-
[code:priv_dir(erlang_red)]
152-
),
146+
DestFileName = ered_flow_store:store_main_flow(),
153147

154148
filelib:ensure_dir(DestFileName),
155149
case file:write_file(DestFileName, encode_json(NodeAry)) of
@@ -170,10 +164,7 @@ handle_info({store_flow, FlowId, JsonText}, FlowStore) ->
170164
%% remove any reference to <<"credentials">>
171165
NodeAry = remove_credentials(json:decode(NodeAryWithCreds)),
172166

173-
DestFileName = io_lib:format(
174-
"~s/testflows/~s/flows.json",
175-
[code:priv_dir(erlang_red), FlowId]
176-
),
167+
DestFileName = ered_flow_store:store_flow_id(FlowId),
177168

178169
filelib:ensure_dir(DestFileName),
179170
case file:write_file(DestFileName, encode_json(NodeAry)) of
@@ -237,12 +228,10 @@ remove_credentials([NodeDef | Rest], Store) ->
237228
compile_file_list() ->
238229
{ok, MP} = re:compile("([A-Z0-9]{16})/flows.json", [caseless]),
239230

240-
TestFlowDir = io_lib:format("~s/testflows/", [code:priv_dir(erlang_red)]),
231+
TestFlowDir = filename:join(code:priv_dir(erlang_red), "testflows"),
232+
StoreFlowDir = ered_flow_store:store_flow(),
241233

242-
FileNames = filelib:fold_files(
243-
TestFlowDir,
244-
"flows.json",
245-
true,
234+
Fun =
246235
fun(Fname, Acc) ->
247236
case re:run(Fname, MP) of
248237
{match, [{_, _}, {S, L}]} ->
@@ -251,9 +240,11 @@ compile_file_list() ->
251240
Acc
252241
end
253242
end,
254-
[]
255-
),
256-
FileNames.
243+
244+
FileNames0 = filelib:fold_files(TestFlowDir, "flows.json", true, Fun, []),
245+
FileNames1 = filelib:fold_files(StoreFlowDir, "flows.json", true, Fun, []),
246+
247+
FileNames0 ++ FileNames1.
257248

258249
%% erlfmt:ignore lining stuff up
259250
compile_file_store([], FileStore) ->

0 commit comments

Comments
 (0)