forked from rkallos/wrek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrek_utils.erl
41 lines (31 loc) · 869 Bytes
/
wrek_utils.erl
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
-module(wrek_utils).
-export([
rm/1,
rmdir/1,
sandbox/2
]).
-spec rm(file:filename_all()) -> ok | {error, atom()}.
rm(Path) ->
case filelib:is_dir(Path) of
true -> rmdir(Path);
false ->
case filelib:is_file(Path) of
true -> file:delete(Path);
false -> ok
end
end.
-spec rmdir(file:filename_all()) -> ok | {error, atom()}.
rmdir(Dir) ->
case file:list_dir(Dir) of
{error, enoent} -> ok;
{ok, Files} ->
lists:foreach(fun(F) -> rm(filename:join(Dir, F)) end, Files),
ok = file:del_dir(Dir)
end.
-spec sandbox(file:filename_all(), string()) -> file:filename_all().
sandbox(BaseDir, Name) ->
Dir = filename:join([BaseDir, Name]),
ok = rm(Dir),
ok = filelib:ensure_dir(Dir),
ok = file:make_dir(Dir),
Dir.