Skip to content

Commit 05b9793

Browse files
committed
.net filesystem_type: 9x faster
.net disk_capacity: faster .net disk_avail: faster .NET: filter exceptions
1 parent 7c744ff commit 05b9793

File tree

9 files changed

+66
-42
lines changed

9 files changed

+66
-42
lines changed

+stdlib/+dotnet/create_symlink.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
end
1414

1515
% https://learn.microsoft.com/en-us/dotnet/api/system.io.file.createsymboliclink
16-
try %#ok<TRYNC>
16+
try
1717
System.IO.File.CreateSymbolicLink(link, target);
1818
ok = true;
19+
catch e
20+
dotnetException(e)
1921
end
2022

2123
end

+stdlib/+dotnet/disk_available.m

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
%% DOTNET.DISK_AVAILABLE find the disk space available to the user
22

3-
function f = disk_available(file)
4-
arguments
5-
file (1,1) string
3+
function i = disk_available(file)
4+
5+
i = uint64([]);
6+
if ~stdlib.exists(file)
7+
return
68
end
79

8-
f = uint64([]);
10+
try
11+
% absolutizing is necessary for Windows especially
12+
i = System.IO.DriveInfo(System.IO.Path.GetFullPath(file)).AvailableFreeSpace();
13+
% https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.availablefreespace
14+
catch e
15+
dotnetException(e)
16+
end
917

10-
if ~stdlib.exists(file), return, end
18+
i = uint64(i);
1119

12-
f = uint64(System.IO.DriveInfo(stdlib.absolute(file)).AvailableFreeSpace());
13-
% https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.availablefreespace
1420
end

+stdlib/+dotnet/disk_capacity.m

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
%% DOTNET.DISK_CAPACITY find the overall disk capacity visible to user
22

3-
function f = disk_capacity(file)
4-
arguments
5-
file (1,1) string
6-
end
7-
8-
f = uint64([]);
3+
function i = disk_capacity(file)
94

10-
if ~stdlib.exists(file), return, end
5+
i = uint64([]);
6+
if ~stdlib.exists(file)
7+
return
8+
end
119

12-
f = uint64(System.IO.DriveInfo(stdlib.absolute(file)).TotalSize());
10+
try
11+
% absolutizing is necessary for Windows especially
12+
i = System.IO.DriveInfo(System.IO.Path.GetFullPath(file)).TotalSize();
1313
% https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.totalsize
14+
catch e
15+
dotnetException(e)
16+
end
17+
18+
i = uint64(i);
1419

1520
end

+stdlib/+dotnet/filesystem_type.m

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
%% DOTNET.FILESYSTEM_TYPE type of the partition e.g. NTFS, ext4, ...
22

33
function t = filesystem_type(file)
4-
arguments
5-
file (1,1) string
4+
5+
6+
t = '';
7+
if ~stdlib.exists(file)
8+
return
69
end
710

8-
if stdlib.exists(file)
9-
t = char(System.IO.DriveInfo(stdlib.absolute(file)).DriveFormat);
10-
% https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.driveformat
11-
else
12-
t = '';
11+
% https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.driveformat
12+
13+
try
14+
t = char(System.IO.DriveInfo(System.IO.Path.GetFullPath(file)).DriveFormat);
15+
catch e
16+
dotnetException(e)
1317
end
1418

1519
end

+stdlib/+dotnet/get_owner.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
%% DOTNET.GET_OWNER get the owner of a filepath
22

33
function o = get_owner(file)
4-
arguments
5-
file (1,1) string
6-
end
74

85
% This is not yet possible with .NET on Unix, even with .NET 10.
96
% It would require Pinvoke or external Mono.Unix

+stdlib/+dotnet/is_removable.m

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
% Ref: https://learn.microsoft.com/en-us/dotnet/api/system.io.drivetype
44

55
function y = is_removable(filepath)
6-
arguments
7-
filepath (1,1) string
8-
end
96

10-
if stdlib.exists(filepath)
11-
fmt = System.IO.DriveInfo(stdlib.absolute(filepath)).DriveType;
7+
try
8+
fmt = System.IO.DriveInfo(filepath).DriveType;
129
y = any(isequal(fmt, {System.IO.DriveType.Removable, ...
1310
System.IO.DriveType.CDRom}));
14-
else
11+
catch e
12+
dotnetException(e)
1513
y = false;
1614
end
1715

+stdlib/+dotnet/is_symlink.m

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
y = contains(attr, 'ReparsePoint');
1616
end
1717
catch e
18-
switch e.identifier
19-
case {'MATLAB:NET:CLRException:CreateObject', 'MATLAB:NET:CLRException:MethodInvoke'}
20-
y = false;
21-
otherwise, rethrow(e)
22-
end
18+
dotnetException(e)
19+
y = false;
2320
end
2421

2522
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function dotnetException(e)
2+
3+
switch class(e)
4+
case 'MException'
5+
switch e.identifier
6+
case {'MATLAB:NET:NetConversion:StringConversion'}
7+
otherwise, rethrow(e)
8+
end
9+
case 'NET.NetException'
10+
switch class(e.ExceptionObject)
11+
case {'System.IO.DriveNotFoundException', 'System.IO.FileNotFoundException', 'System.ArgumentException'}
12+
otherwise, rethrow(e)
13+
end
14+
end
15+
16+
end

+stdlib/+dotnet/read_symlink.m

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
%% DOTNET.READ_SYMLINK resolve the symbolic links of a filepath
22

33
function r = read_symlink(file)
4-
arguments
5-
file (1,1) string
6-
end
4+
5+
r = "";
76

87
try
98
h = System.IO.FileInfo(file);
@@ -12,8 +11,8 @@
1211
if isempty(r)
1312
r = "";
1413
end
15-
catch
16-
r = "";
14+
catch e
15+
dotnetException(e)
1716
end
1817

1918
end

0 commit comments

Comments
 (0)