Skip to content

Commit

Permalink
Merge pull request #802 from reganheath/win-delete-symlink
Browse files Browse the repository at this point in the history
Correctly delete symlinks on windows (resolves #801)
  • Loading branch information
tsloughter committed May 25, 2020
1 parent 82d80f5 commit 0d9d625
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
2 changes: 2 additions & 0 deletions shelltests/powershell_release/rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
{extended_start_script, true},
{start_script_type, "powershell"}]
}.

{profiles, [{dev, [{relx, [{dev_mode, true}]}]}]}.
29 changes: 27 additions & 2 deletions shelltests/wintest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ Pop-Location
""

# Function to run rebar3
function Rebar() { & $erlpath\bin\escript.exe "$rebar3_dir\rebar3" @args }
function Rebar() {
& $erlpath\bin\escript.exe "$rebar3_dir\rebar3" @args
if ($LASTEXITCODE -ne 0) {
Write-Error "rebar3 ${args} exited with status $LASTEXITCODE"
}
}

# Our release source
Set-Location ".\$release\"
Expand All @@ -54,25 +59,45 @@ Set-Location ".\$release\"
Rebar release
""

"*** Rebuild dev release (test for symlink issues)"
Rebar as dev release
Rebar as dev release
""

# Go to release
Set-Location "_build\default\rel\$release\bin"

"*** Install service"
& ".\$release.ps1" install
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to install service"
}
""

"*** Start service"
& ".\$release.ps1" start
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to start service"
}
""

"*** Ping service"
& ".\$release.ps1" ping
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to ping service"
}
""

"*** Stop service"
& ".\$release.ps1" stop
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to stop service"
}
""

"*** Uninstall service"
& ".\$release.ps1" uninstall
""
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to uninstall service"
}
""
31 changes: 26 additions & 5 deletions src/rlx_file_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -295,31 +295,52 @@ sub_files(From) ->
{ok, SubFiles} = file:list_dir(From),
[filename:join(From, SubFile) || SubFile <- SubFiles].

%% @doc delete a file. Use the recursive option for directories.
%% @doc delete a file. Use the recursive option for whole directory trees.
-spec remove(file:name(), [] | [recursive]) -> ok | {error, term()}.
remove(Path, Options) ->
case lists:member(recursive, Options) of
false -> file:delete(Path);
false -> remove(Path);
true -> remove_recursive(Path, Options)
end.

%% @doc delete a file.
%% @doc delete a file or directory, including symlinks and junctions
-spec remove(file:name()) -> ok | {error, term()}.
remove(Path) ->
remove(Path, []).
case is_symlink(Path) of
true -> remove_symlink(Path);
false ->
case is_dir(Path) of
true -> file:del_dir(Path);
false -> file:delete(Path)
end
end.

-spec remove_recursive(file:name(), list()) -> ok | {error, term()}.
remove_recursive(Path, Options) ->
case is_dir(Path) of
false ->
file:delete(Path);
remove(Path);
true ->
lists:foreach(fun(ChildPath) ->
remove_recursive(ChildPath, Options)
end, sub_files(Path)),
file:del_dir(Path)
end.

-spec remove_symlink(file:name()) -> ok | {error, term()}.
remove_symlink(Path) ->
case os:type() of
{win32, _} ->
% On windows we remove symlinks and junctions differently if they refer to directories.
case filelib:is_dir(Path) of
true -> file:del_dir(Path);
false -> file:delete(Path)
end;
_ ->
% On other systems we use can use file:delete() in all cases.
file:delete(Path)
end.

%% returns the path for printing to logs. if get_cwd succeeds it
%% attempts to return the relative path. It falls back on juts
%% returning the original path.
Expand Down

0 comments on commit 0d9d625

Please sign in to comment.