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 wmic.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 02ac282
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions deps/rabbit/src/rabbit_disk_monitor.erl
Expand Up @@ -252,12 +252,12 @@ get_disk_free(Dir, {win32, _}) ->
_ -> exit(could_not_determine_disk_free)
end;
DriveLetter ->
case win32_get_disk_free_fsutil(DriveLetter) of
case catch win32_get_disk_free_wmic(DriveLetter) of
{ok, Free0} -> Free0;
error ->
case win32_get_disk_free_pwsh(DriveLetter) of
_WmicNotOk ->
case catch win32_get_disk_free_pwsh(DriveLetter) of
{ok, Free1} -> Free1;
_ -> exit(could_not_determine_disk_free)
_PwshNotOk -> exit(could_not_determine_disk_free)
end
end
end.
Expand All @@ -278,28 +278,22 @@ win32_get_drive_letter([DriveLetter, $:, $/ | _]) when
win32_get_drive_letter(_) ->
error.

win32_get_disk_free_fsutil(DriveLetter) when
win32_get_disk_free_wmic(DriveLetter) when
(DriveLetter >= $a andalso DriveLetter =< $z) orelse
(DriveLetter >= $A andalso DriveLetter =< $Z) ->
% DriveLetter $c
FsutilCmd = "fsutil.exe volume diskfree " ++ [DriveLetter] ++ ":",
% C:\>wmic logicaldisk where caption="c:" get freespace
% FreeSpace
% 799904079872
Cmd = "wmic.exe logicaldisk where caption=\"" ++ [DriveLetter] ++ ":\" get freespace",

% 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
case run_cmd(Cmd) 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
Result ->
[_, FreeSpaceStr0] = string:tokens(Result, "\r\n"),
FreeSpaceStr1 = string:strip(FreeSpaceStr0),
{ok, list_to_integer(FreeSpaceStr1)}
end.

win32_get_disk_free_pwsh(DriveLetter) when
Expand Down

0 comments on commit 02ac282

Please sign in to comment.