Skip to content

Commit 7740628

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

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

+stdlib/is_removable.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function y = is_removable(filepath)
2+
3+
y = false;
4+
5+
if ispc()
6+
drive = stdlib.root_name(filepath);
7+
cmd = ['wmic logicaldisk where "DeviceID=''', drive, '''" get DriveType'];
8+
[s, m] = system(cmd);
9+
if s == 0
10+
y = strtrim(extractAfter(m, "DriveType")) == "2";
11+
end
12+
elseif ismac()
13+
cmd1 = ['df ', filepath, ' | tail -n 1 | awk ''{print $1}'''];
14+
[s1, m1] = system(cmd1);
15+
if s1 == 0
16+
cmd2 = ['diskutil info ', m1];
17+
[s2, m2] = system(cmd2);
18+
y = s2 == 0 && contains(m2, "Removable Media:" + whitespacePattern + "Removable");
19+
end
20+
else
21+
cmd1 = ['df ', filepath, ' | tail -n 1 | awk ''{print $1}'''];
22+
[s1, m1] = system(cmd1);
23+
if s1 == 0
24+
f1 = ['/sys/class/block/', m1, '/removable'];
25+
t1 = fileread(f1);
26+
y = strtrim(t1) == "1";
27+
end
28+
end
29+
30+
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)