Skip to content

Commit 4a28d13

Browse files
committed
add is_mount()
1 parent 1704e80 commit 4a28d13

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

+stdlib/+python/is_mount.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
%% PYTHON.IS_MOUNT is path a mount point
2+
%
3+
% https://docs.python.org/3/library/os.path.html#os.path.ismount
4+
5+
function y = is_mount(filepath)
6+
7+
try
8+
y = py.os.path.ismount(filepath);
9+
catch e
10+
warning(e.identifier, "Python is_mount failed: %s", e.message);
11+
y = false;
12+
end
13+
14+
end

+stdlib/+sys/is_mount.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function y = is_mount(filepath)
2+
3+
4+
if ispc()
5+
if any(ismember(filepath, ["/", "\"])) || ...
6+
(endsWith(filepath, ["/", "\"]) && isfolder(filepath) && filepath == stdlib.root(filepath))
7+
y = true;
8+
return
9+
end
10+
cmd = strcat('pwsh -c "(Get-Item -Path ''', filepath, ''').Attributes.ToString().Contains(''ReparsePoint'')"');
11+
elseif ismac()
12+
cmd = "[ $(stat -f %d " + filepath + ") != $(stat -f %d " + stdlib.parent(filepath) + ")]";
13+
else
14+
cmd = "mountpoint -q " + filepath;
15+
end
16+
17+
[s, m] = system(cmd);
18+
19+
if ispc()
20+
y = s == 0 && m == "True";
21+
else
22+
y = s == 0;
23+
end
24+
25+
26+
end

+stdlib/is_mount.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%% IS_MOUNT is filepath a mount path
2+
%
3+
% Examples:
4+
%
5+
% * Windows: is_mount("c:") false; is_mount("C:\") true
6+
% * Linux, macOS, Windows: is_mount("/") true
7+
8+
function y = is_mount(filepath, method)
9+
arguments
10+
filepath {mustBeTextScalar}
11+
method (1,:) string = ["python", "sys"]
12+
end
13+
14+
fun = choose_method(method, "is_mount");
15+
y = fun(filepath);
16+
17+
end

test/TestDisk.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
hl_fun = {'java', 'python'}
1515
fst_fun = {'sys', 'dotnet', 'java', 'python'}
1616
is_remove = {'dotnet', 'sys'}
17+
py_sys = {'python', 'sys'}
1718
end
1819

1920
methods(TestClassSetup)
@@ -55,6 +56,27 @@ function test_is_removable(tc, is_remove)
5556
end
5657

5758

59+
function test_is_mount(tc, py_sys)
60+
try
61+
y = stdlib.is_mount(pwd(), py_sys);
62+
catch e
63+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError', e.message)
64+
return
65+
end
66+
67+
tc.verifyClass(y, 'logical')
68+
tc.verifyTrue(stdlib.is_mount("/", py_sys))
69+
70+
if ispc()
71+
sd = getenv("SystemDrive");
72+
tc.assertEqual(sd, stdlib.root_name(sd), sd)
73+
tc.verifyFalse(stdlib.is_mount(sd, py_sys), sd)
74+
tc.verifyTrue(stdlib.is_mount(sd + "/", py_sys), sd)
75+
tc.verifyTrue(stdlib.is_mount(sd + "\", py_sys), sd)
76+
end
77+
end
78+
79+
5880
function test_hard_link_count(tc, hl_fun)
5981
fname = "hard_link_count";
6082
n = "stdlib." + hl_fun + "." + fname;

0 commit comments

Comments
 (0)