Skip to content

Commit fd413b7

Browse files
committed
add is_removable()
1 parent 82a2e3a commit fd413b7

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

+stdlib/is_removable.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
%% IS_REMOVABLE - Check if a file path is on a removable drive
2+
% Not necessarily perfectly reliable at detection, but works for most cases.
3+
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
17+
end
18+
19+
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
40+
41+
end

test/TestDisk.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ function test_disk_ac(tc, Ps, disk_ac_fun, disk_ac_name)
4242
end
4343

4444

45+
function test_is_removable(tc)
46+
47+
y = stdlib.is_removable(pwd());
48+
tc.verifyClass(y, 'logical')
49+
50+
end
51+
52+
4553
function test_hard_link_count(tc, hl_fun)
4654
fname = "hard_link_count";
4755
n = "stdlib." + hl_fun + "." + fname;

0 commit comments

Comments
 (0)