Skip to content

Commit 0710bd3

Browse files
committed
hard_link_count: more robust
1 parent b6c56e1 commit 0710bd3

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

+stdlib/+java/hard_link_count.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
function i = hard_link_count(file)
22

3+
4+
i = [];
5+
if stdlib.strempty(file)
6+
return
7+
end
8+
39
try
410
i = java.nio.file.Files.getAttribute(javaAbsolutePath(file), "unix:nlink", java.nio.file.LinkOption.values());
511
catch e
612
javaException(e)
7-
i = [];
813
end
14+
915
end

+stdlib/+python/hard_link_count.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
try
44
c = double(py.os.stat(file).st_nlink);
55
catch e
6+
pythonException(e)
67
c = [];
78
end
89

+stdlib/+sys/hard_link_count.m

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
cmd = sprintf('stat -c %%h ''%s''', file);
1818
end
1919

20-
if stdlib.exists(file)
21-
[status, output] = system(cmd);
22-
if status == 0
23-
if ispc()
24-
n = 1 + startsWith(output, "HardLink");
25-
else
26-
n = str2double(output);
27-
end
20+
if stdlib.strempty(file)
21+
return
22+
end
23+
24+
[status, output] = system(cmd);
25+
if status == 0
26+
if ispc()
27+
n = 1 + startsWith(output, "HardLink");
28+
else
29+
n = str2double(output);
2830
end
2931
end
3032

test/TestDisk.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ function test_hard_link_count(tc, B_hard_link_count)
9999
tc.assertEqual(char(b), B_hard_link_count)
100100

101101
tc.verifyGreaterThanOrEqual(i, 1)
102+
103+
i = stdlib.hard_link_count('');
104+
tc.verifyEmpty(i)
102105
end
103106

104107

0 commit comments

Comments
 (0)