Skip to content

Commit

Permalink
Fix issue with fsutil
Browse files Browse the repository at this point in the history
Fsutil has language-specific messages. Fix by using powershell.exe instead.

Follow-up to #3895

Reported here:
https://groups.google.com/g/rabbitmq-users/c/ypk51AtmrSM
  • Loading branch information
lukebakken committed Jan 8, 2022
1 parent 4a3d926 commit fd78144
Showing 1 changed file with 5 additions and 34 deletions.
39 changes: 5 additions & 34 deletions deps/rabbit/src/rabbit_disk_monitor.erl
Expand Up @@ -252,13 +252,9 @@ get_disk_free(Dir, {win32, _}) ->
_ -> exit(could_not_determine_disk_free)
end;
DriveLetter ->
case win32_get_disk_free_fsutil(DriveLetter) of
{ok, Free0} -> Free0;
error ->
case win32_get_disk_free_pwsh(DriveLetter) of
{ok, Free1} -> Free1;
_ -> exit(could_not_determine_disk_free)
end
case catch win32_get_disk_free_pwsh(DriveLetter) of
{ok, Free1} -> Free1;
_PwshNotOk -> exit(could_not_determine_disk_free)
end
end.

Expand All @@ -278,30 +274,6 @@ win32_get_drive_letter([DriveLetter, $:, $/ | _]) when
win32_get_drive_letter(_) ->
error.

win32_get_disk_free_fsutil(DriveLetter) when
(DriveLetter >= $a andalso DriveLetter =< $z) orelse
(DriveLetter >= $A andalso DriveLetter =< $Z) ->
% DriveLetter $c
FsutilCmd = "fsutil.exe volume diskfree " ++ [DriveLetter] ++ ":",

% C:\windows\system32>fsutil volume diskfree c:
% Total free bytes : 812,733,878,272 (756.9 GB)
% Total bytes : 1,013,310,287,872 (943.7 GB)
% Total quota free bytes : 812,733,878,272 (756.9 GB)
case run_cmd(FsutilCmd) of
{error, timeout} ->
error;
FsutilResult ->
case string:slice(FsutilResult, 0, 5) of
"Error" ->
error;
"Total" ->
FirstLine = hd(string:tokens(FsutilResult, "\r\n")),
{match, [FreeStr]} = re:run(FirstLine, "(\\d+,?)+", [{capture, first, list}]),
{ok, list_to_integer(lists:flatten(string:tokens(FreeStr, ",")))}
end
end.

win32_get_disk_free_pwsh(DriveLetter) when
(DriveLetter >= $a andalso DriveLetter =< $z) orelse
(DriveLetter >= $A andalso DriveLetter =< $Z) ->
Expand All @@ -310,10 +282,9 @@ win32_get_disk_free_pwsh(DriveLetter) when
case run_cmd(PoshCmd) of
{error, timeout} ->
error;
PoshResultStr ->
PoshResult ->
% Note: remove \r\n
PoshResult = string:slice(PoshResultStr, 0, length(PoshResultStr) - 2),
{ok, list_to_integer(PoshResult)}
{ok, list_to_integer(string:trim(PoshResult))}
end.

win32_get_disk_free_dir(Dir) ->
Expand Down

0 comments on commit fd78144

Please sign in to comment.