Skip to content

Commit 03d72ef

Browse files
committed
is_removable: add dotnet
1 parent 3ad4fe3 commit 03d72ef

File tree

4 files changed

+66
-36
lines changed

4 files changed

+66
-36
lines changed

+stdlib/+dotnet/is_removable.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%% DOTNET.IS_REMOVEABLE detect removable drive
2+
%
3+
% Ref: https://learn.microsoft.com/en-us/dotnet/api/system.io.drivetype
4+
5+
function y = is_removable(filepath)
6+
7+
if stdlib.exists(filepath)
8+
fmt = System.IO.DriveInfo(stdlib.absolute(filepath)).DriveType;
9+
y = any(isequal(fmt, {System.IO.DriveType.Removable, ...
10+
System.IO.DriveType.CDRom}));
11+
else
12+
y = false;
13+
end
14+
15+
end

+stdlib/+sys/is_removable.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function y = is_removable(filepath)
2+
3+
y = false;
4+
5+
if ispc()
6+
drive = stdlib.root_name(filepath);
7+
cmd1 = strcat('wmic logicaldisk where "DeviceID=''', drive, '''" get DriveType');
8+
else
9+
cmd1 = "df " + filepath + " | tail -n 1 | awk '{print $1}'";
10+
end
11+
[s1, m1] = system(cmd1);
12+
if s1 ~= 0
13+
return
14+
end
15+
16+
17+
if ispc()
18+
19+
y = any(ismember(strtrim(extractAfter(m1, "DriveType")), ["2", "5"]));
20+
% https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-logicaldisk
21+
22+
elseif ismac()
23+
24+
cmd2 = ['diskutil info ', m1];
25+
[s2, m2] = system(cmd2);
26+
y = s2 == 0 && contains(m2, "Removable Media:" + whitespacePattern + "Removable");
27+
28+
else
29+
30+
dev = strtrim(extractAfter(m1, '/dev/'));
31+
f1 = strcat('/sys/class/block/', dev, '/removable');
32+
if isfile(f1)
33+
y = strtrim(fileread(f1)) == "1";
34+
end
35+
36+
end
37+
38+
end

+stdlib/is_removable.m

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,14 @@
11
%% IS_REMOVABLE - Check if a file path is on a removable drive
22
% Not necessarily perfectly reliable at detection, but works for most cases.
33

4-
function y = is_removable(filepath)
5-
6-
y = false;
7-
8-
if ispc()
9-
drive = stdlib.root_name(filepath);
10-
cmd1 = strcat('wmic logicaldisk where "DeviceID=''', drive, '''" get DriveType');
11-
else
12-
cmd1 = "df " + filepath + " | tail -n 1 | awk '{print $1}'";
13-
end
14-
[s1, m1] = system(cmd1);
15-
if s1 ~= 0
16-
return
4+
function y = is_removable(filepath, method)
5+
arguments
6+
filepath {mustBeTextScalar}
7+
method (1,:) string = ["dotnet", "sys"]
178
end
189

10+
fun = choose_method(method, "is_removable");
1911

20-
if ispc()
21-
22-
y = any(ismember(strtrim(extractAfter(m1, "DriveType")), ["2", "5"]));
23-
% https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-logicaldisk
24-
25-
elseif ismac()
26-
27-
cmd2 = ['diskutil info ', m1];
28-
[s2, m2] = system(cmd2);
29-
y = s2 == 0 && contains(m2, "Removable Media:" + whitespacePattern + "Removable");
30-
31-
else
32-
33-
dev = strtrim(extractAfter(m1, '/dev/'));
34-
f1 = strcat('/sys/class/block/', dev, '/removable');
35-
if isfile(f1)
36-
y = strtrim(fileread(f1)) == "1";
37-
end
38-
39-
end
12+
y = fun(filepath);
4013

4114
end

test/TestDisk.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
disk_ac_name = {'disk_available', 'disk_capacity'}
1414
hl_fun = {'java', 'python'}
1515
fst_fun = {'sys', 'dotnet', 'java', 'python'}
16+
is_remove = {'dotnet', 'sys'}
1617
end
1718

1819
methods(TestClassSetup)
@@ -42,11 +43,14 @@ function test_disk_ac(tc, Ps, disk_ac_fun, disk_ac_name)
4243
end
4344

4445

45-
function test_is_removable(tc)
46+
function test_is_removable(tc, is_remove)
47+
try
48+
y = stdlib.is_removable(pwd(), is_remove);
49+
catch e
50+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError', e.message)
51+
end
4652

47-
y = stdlib.is_removable(pwd());
4853
tc.verifyClass(y, 'logical')
49-
5054
end
5155

5256

0 commit comments

Comments
 (0)