Skip to content

Commit 2694c0d

Browse files
committed
.net filesystem_type: 9x faster
.net disk_capacity: faster .net disk_avail: faster
1 parent 7c744ff commit 2694c0d

File tree

4 files changed

+38
-31
lines changed

4 files changed

+38
-31
lines changed

+stdlib/+dotnet/disk_available.m

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
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
6-
end
3+
function i = disk_available(file)
74

8-
f = uint64([]);
5+
try
6+
i = System.IO.DriveInfo(file).AvailableFreeSpace();
7+
% https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.availablefreespace
8+
catch e
9+
switch class(e.ExceptionObject)
10+
case {'System.IO.DriveNotFoundException', 'System.ArgumentException'}, i = [];
11+
otherwise, rethrow(e)
12+
end
13+
end
914

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

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

+stdlib/+dotnet/disk_capacity.m

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
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([]);
9-
10-
if ~stdlib.exists(file), return, end
3+
function i = disk_capacity(file)
114

12-
f = uint64(System.IO.DriveInfo(stdlib.absolute(file)).TotalSize());
5+
try
6+
i = System.IO.DriveInfo(file).TotalSize();
137
% https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.totalsize
8+
catch e
9+
switch class(e.ExceptionObject)
10+
case {'System.IO.DriveNotFoundException', 'System.ArgumentException'}, i = [];
11+
otherwise, rethrow(e)
12+
end
13+
end
14+
15+
i = uint64(i);
1416

1517
end

+stdlib/+dotnet/filesystem_type.m

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
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
6-
end
74

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
5+
% https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.driveformat
6+
7+
try
8+
t = char(System.IO.DriveInfo(file).DriveFormat);
9+
catch e
10+
switch class(e.ExceptionObject)
11+
case {'System.IO.DriveNotFoundException', 'System.ArgumentException'}
12+
otherwise, rethrow(e)
13+
end
1214
t = '';
1315
end
1416

+stdlib/+dotnet/is_removable.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
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
15-
y = false;
11+
catch e
12+
switch class(e.ExceptionObject)
13+
case {'System.IO.DriveNotFoundException', 'System.ArgumentException'}, y = false;
14+
otherwise, rethrow(e)
15+
end
1616
end
1717

1818
end

0 commit comments

Comments
 (0)