Skip to content

Commit 05d3aa7

Browse files
committed
file_checksum: sys backend
choose_method => hbackend
1 parent 8155be1 commit 05d3aa7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+280
-283
lines changed

+stdlib/+dotnet/file_checksum.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
%% DOTNET.FILE_CHECKSUM compute checksum has of file
2+
3+
% Ref: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#getInstance(java.lang.String)
4+
5+
function hash = file_checksum(file, hash_method)
6+
7+
if any(strcmp(hash_method, {'sha256', 'SHA256'}))
8+
hash_method = "SHA-256";
9+
end
10+
11+
file_chunk = 10e6; % arbitrary (bytes) didn't seem to be very sensitive for speed
12+
13+
fid = fopen(file, 'r');
14+
assert(fid > 1, "could not open file %s", file)
15+
16+
inst = System.Security.Cryptography.HashAlgorithm.Create(hash_method);
17+
while ~feof(fid)
18+
% https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.hashalgorithm.computehash
19+
inst.ComputeHash(fread(fid, file_chunk, '*uint8'));
20+
end
21+
h = uint8(inst.Hash);
22+
23+
fclose(fid);
24+
25+
hash = sprintf('%.2x', h);
26+
27+
end

+stdlib/+java/file_checksum.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
%% JAVA.FILE_CHECKSUM compute checksum hash of file
2+
% Ref: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#getInstance(java.lang.String)
3+
4+
function hash = file_checksum(file, hash_method)
5+
6+
if any(strcmp(hash_method, {'sha256', 'SHA256'}))
7+
hash_method = "SHA-256";
8+
end
9+
10+
file_chunk = 10e6; % arbitrary (bytes) didn't seem to be very sensitive for speed
11+
12+
fid = fopen(file, 'r');
13+
assert(fid > 1, "could not open file %s", file)
14+
15+
inst = javaMethod("getInstance", "java.security.MessageDigest", hash_method);
16+
while ~feof(fid)
17+
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#update(byte)
18+
inst.update(fread(fid, file_chunk, '*uint8'));
19+
end
20+
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#digest()
21+
h = typecast(inst.digest, 'uint8');
22+
23+
fclose(fid);
24+
25+
hash = sprintf('%.2x', h);
26+
27+
end

+stdlib/+sys/file_checksum.m

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
%% SYS.FILE_CHECKSUM compute checksum of file
2+
function hash = file_checksum(file, hash_method)
3+
4+
switch lower(hash_method)
5+
case {"sha-256", "sha256"}
6+
if ismac()
7+
cmd = "shasum --algorithm 256 --binary " + file;
8+
elseif ispc()
9+
cmd = "CertUtil -hashfile " + file + " SHA256";
10+
else
11+
cmd = "sha256sum --binary " + file;
12+
end
13+
case "md5"
14+
if ismac()
15+
cmd = "md5 -r " + file;
16+
elseif ispc()
17+
cmd = "CertUtil -hashfile " + file + " MD5";
18+
else
19+
cmd = "md5sum " + file;
20+
end
21+
otherwise, error('unhandled hash method %s', hash_method)
22+
end
23+
24+
[s, m] = system(cmd);
25+
26+
assert(s == 0, "failed to compute SHA256 hash of %s: %s", file, m)
27+
28+
switch lower(hash_method)
29+
case {"sha-256", "sha256"}
30+
hash = regexp(m, '^\w{64}','match','once','lineanchors');
31+
assert(strlength(hash)==64, 'SHA256 hash is 64 characters')
32+
case "md5"
33+
hash = regexp(m, '^\w{32}','match','once','lineanchors');
34+
assert(strlength(hash)==32, 'MD5 hash is 32 characters')
35+
end
36+
37+
end

+stdlib/canonical.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
%%% Outputs
1313
% * c: canonical path, if determined
1414

15-
function c = canonical(p, strict, method)
15+
function c = canonical(p, strict, backend)
1616
arguments
1717
p {mustBeTextScalar}
1818
strict (1,1) logical = false
19-
method (1,:) string = ["native", "legacy"]
19+
backend (1,:) string = ["native", "legacy"]
2020
end
2121

22-
fun = choose_method(method, "canonical", 'R2024a');
22+
fun = hbackend(backend, "canonical", 'R2024a');
2323

2424
c = fun(p, strict);
2525

+stdlib/cpu_arch.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
%% CPU_ARCH get the CPU architecture
22

3-
function a = cpu_arch(method)
3+
function a = cpu_arch(backend)
44
arguments
5-
method (1,:) string = ["java", "dotnet", "native"]
5+
backend (1,:) string = ["java", "dotnet", "native"]
66
end
77

8-
fun = choose_method(method, "cpu_arch");
8+
fun = hbackend(backend, "cpu_arch");
99

1010
a = fun();
1111

+stdlib/cpu_load.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
% Returns the "recent cpu usage" for the whole system.
44
%
55
% This value is a double greater than 0.
6-
% If the system recent cpu usage is not available, the method returns a negative or NaN value.
6+
% If the system recent cpu usage is not available, the backend returns a negative or NaN value.
77

8-
function L = cpu_load(method)
8+
function L = cpu_load(backend)
99
arguments
10-
method (1,:) string = ["java", "python", "sys"]
10+
backend (1,:) string = ["java", "python", "sys"]
1111
end
1212

13-
fun = choose_method(method, "cpu_load");
13+
fun = hbackend(backend, "cpu_load");
1414

1515
L = fun();
1616

+stdlib/create_symlink.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
%%% Outputs
77
% * ok: true if successful
88

9-
function ok = create_symlink(target, link, method)
9+
function ok = create_symlink(target, link, backend)
1010
arguments
1111
target {mustBeTextScalar}
1212
link {mustBeTextScalar}
13-
method (1,:) string = ["native", "dotnet", "python", "sys"]
13+
backend (1,:) string = ["native", "dotnet", "python", "sys"]
1414
end
1515

16-
fun = choose_method(method, "create_symlink", 'R2024b');
16+
fun = hbackend(backend, "create_symlink", 'R2024b');
1717

1818
ok = fun(target, link);
1919

+stdlib/device.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
%% DEVICE filesystem device index of path
22

3-
function i = device(file, method)
3+
function i = device(file, backend)
44
arguments
55
file {mustBeTextScalar}
6-
method (1,:) string = ["java", "python", "sys"]
6+
backend (1,:) string = ["java", "python", "sys"]
77
end
88

9-
fun = choose_method(method, "device");
9+
fun = hbackend(backend, "device");
1010
i = fun(file);
1111

1212
end

+stdlib/disk_available.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
%
55
% Ref: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getUsableSpace()
66

7-
function f = disk_available(filepath, method)
7+
function f = disk_available(filepath, backend)
88
arguments
99
filepath {mustBeTextScalar}
10-
method (1,:) string = ["java", "dotnet", "python", "sys"]
10+
backend (1,:) string = ["java", "dotnet", "python", "sys"]
1111
end
1212

13-
fun = choose_method(method, "disk_available");
13+
fun = hbackend(backend, "disk_available");
1414

1515
f = fun(filepath);
1616

+stdlib/disk_capacity.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
%
33
% example: stdlib.disk_capacity('/')
44

5-
function f = disk_capacity(filepath, method)
5+
function f = disk_capacity(filepath, backend)
66
arguments
77
filepath {mustBeTextScalar}
8-
method (1,:) string = ["java", "dotnet", "python", "sys"]
8+
backend (1,:) string = ["java", "dotnet", "python", "sys"]
99
end
1010

11-
fun = choose_method(method, "disk_capacity");
11+
fun = hbackend(backend, "disk_capacity");
1212

1313
f = fun(filepath);
1414

0 commit comments

Comments
 (0)