Skip to content

Commit 62b1768

Browse files
committed
sys: hard_link_count
1 parent 13a356d commit 62b1768

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

+stdlib/+sys/hard_link_count.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
%% SYS.HARD_LINK_COUNT number of hard links to file
2+
%
3+
% Powershell is 0, 1, or 2
4+
5+
function n = hard_link_count(file)
6+
7+
if ~stdlib.exists(file)
8+
n = 0;
9+
return
10+
end
11+
12+
if ispc()
13+
cmd = sprintf('pwsh -c "(Get-Item ''%s'').LinkType"', file);
14+
elseif ismac()
15+
cmd = sprintf('stat -f %%l ''%s''', file);
16+
else
17+
cmd = sprintf('stat -c %%h ''%s''', file);
18+
end
19+
20+
[status, output] = system(cmd);
21+
if status == 0
22+
if ispc()
23+
n = 1 + startsWith(output, "HardLink");
24+
else
25+
n = str2double(output);
26+
end
27+
end
28+
29+
end

+stdlib/hard_link_count.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
%% HARD_LINK_COUNT get the number of hard links to a file
22
%
33
%
4-
% Ref:
4+
%% Java backend references:
5+
%
56
% * https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#getPosixFileAttributes(java.nio.file.Path,java.nio.file.LinkOption...)
67
% * https://docs.oracle.com/javase/tutorial/essential/io/links.html
78

89
function c = hard_link_count(file, backend)
910
arguments
1011
file {mustBeTextScalar}
11-
backend (1,:) string = ["java", "python"]
12+
backend (1,:) string = ["java", "python", "sys"]
1213
end
1314

1415
fun = hbackend(backend, "hard_link_count");

+stdlib/has_java.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
%% HAS_JAVA detect if JVM is available
2+
%
3+
% Ref: https://www.mathworks.com/help/matlab/ref/usejava.html
24

35
function y = has_java()
46

test/TestDisk.m

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
properties (TestParameter)
88
Ps = {".", "", "/", getenv("SystemDrive"), "not-exist"}
99
Po = {mfilename("fullpath") + ".m", pwd(), ".", "", "not-exist"}
10-
id_fun = {'sys', 'java', 'python'}
10+
java_python_sys = {'sys', 'java', 'python'}
1111
id_name = {"inode", "device"}
1212
all_fun = {'sys', 'dotnet', 'java', 'python'}
1313
disk_ac_name = {'disk_available', 'disk_capacity'}
14-
hl_fun = {'java', 'python'}
1514
is_remove = {'dotnet', 'sys'}
1615
py_sys = {'python', 'sys'}
1716
end
@@ -77,14 +76,14 @@ function test_is_mount(tc, py_sys)
7776
end
7877

7978

80-
function test_hard_link_count(tc, hl_fun)
79+
function test_hard_link_count(tc, java_python_sys)
8180
fname = "hard_link_count";
82-
n = "stdlib." + hl_fun + "." + fname;
81+
n = "stdlib." + java_python_sys + "." + fname;
8382
h = str2func("stdlib." + fname);
8483
tc.assertNotEmpty(which(n))
8584
P = mfilename("fullpath") + ".m";
8685
try
87-
r = h(P, hl_fun);
86+
r = h(P, java_python_sys);
8887
tc.verifyGreaterThanOrEqual(r, 1)
8988
catch e
9089
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
@@ -128,16 +127,16 @@ function test_remove_file(tc)
128127
end
129128

130129

131-
function test_inode_device(tc, id_fun, id_name)
132-
n = "stdlib." + id_fun + "." + id_name;
130+
function test_inode_device(tc, java_python_sys, id_name)
131+
n = "stdlib." + java_python_sys + "." + id_name;
133132
h = str2func("stdlib." + id_name);
134133
tc.assertNotEmpty(which(n))
135134

136135
try
137-
ip = h(pwd(), id_fun);
136+
ip = h(pwd(), java_python_sys);
138137
tc.verifyClass(ip, 'uint64')
139138
tc.verifyGreaterThan(ip, 0)
140-
tc.verifyEqual(h(".", id_fun), ip)
139+
tc.verifyEqual(h(".", java_python_sys), ip)
141140
catch e
142141
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
143142
end

0 commit comments

Comments
 (0)