Skip to content

Commit cb59cd6

Browse files
committed
samepath: functionalize native
1 parent 620dc50 commit cb59cd6

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

+stdlib/+native/samepath.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%% NATIVE.SAMEPATH are two paths equivalent
2+
%
3+
% this canonical string method is less preferred to using device + inode.
4+
5+
function y = samepath(path1, path2)
6+
7+
if stdlib.strempty(path1) || stdlib.strempty(path2)
8+
y = false;
9+
else
10+
c1 = stdlib.canonical(path1, true);
11+
c2 = stdlib.canonical(path2, true);
12+
y = ~stdlib.strempty(c1) && strcmp(c1, c2);
13+
end
14+
15+
end

+stdlib/+sys/samepath.m

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
function y = samepath(path1, path2)
22

3-
y = false;
3+
assert(~ispc(), "Unix-like only")
44

5-
if stdlib.strempty(path1) || stdlib.strempty(path2)
6-
return
7-
end
5+
y = false;
86

9-
if ispc()
10-
c1 = stdlib.canonical(path1, true);
11-
c2 = stdlib.canonical(path2, true);
12-
y = strlength(c1) > 0 && strcmp(c1, c2);
7+
if ~stdlib.exists(path1) || ~stdlib.exists(path2)
138
return
149
end
1510

@@ -22,7 +17,10 @@
2217
cmd = "stat " + flag + " %d:%i " + path1 + " && stat " + flag + " %d:%i " + path2;
2318

2419
[s, m] = system(cmd);
25-
if s ~= 0, return, end
20+
if s ~= 0
21+
warning("stdlib:sys:samepath:RuntimeError", "stdlib.sys.samepath(%s, %s) failed: %s", path1, path2, m)
22+
return
23+
end
2624

2725
m = splitlines(m);
2826
assert(length(m) >= 2, "samepath(%s, %s) failed: unexpected output", path1, path2);

+stdlib/samepath.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
if stdlib.has_python()
2020
y = stdlib.python.samepath(path1, path2);
21-
else
21+
elseif isunix()
2222
y = stdlib.sys.samepath(path1, path2);
23+
else
24+
y = stdlib.native.samepath(path1, path2);
2325
end
2426

2527
%!assert(samepath(".", "."))

test/TestSame.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{"..", pwd() + "/.."}, ...
77
{pwd(), pwd() + "/."}}
88

9-
same_fun = {@stdlib.samepath, @stdlib.sys.samepath, @stdlib.python.samepath}
9+
same_fun = {@stdlib.samepath, @stdlib.sys.samepath, @stdlib.python.samepath, @stdlib.native.samepath}
1010
end
1111

1212
methods(TestClassSetup)

test/is_capable.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ function is_capable(tc, f)
4646
end
4747

4848
elseif contains(n, ".sys.")
49-
% pass
49+
50+
if endsWith(n, "samepath")
51+
tc.assumeTrue(isunix(), "unix only function")
52+
end
5053

5154
elseif contains(n, ".native.")
5255

0 commit comments

Comments
 (0)