Skip to content

Commit 734f86a

Browse files
committed
device: works on Windows
1 parent 53b1233 commit 734f86a

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

+stdlib/device.m

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
%% DEVICE filesystem device index of path
2-
% requires: java
3-
% Windows always returns 0, Unix returns device number.
2+
% optional: java
43

54
function i = device(path)
65
arguments
@@ -9,7 +8,30 @@
98

109
i = [];
1110

12-
if stdlib.isoctave()
11+
if ispc()
12+
h = NET.addAssembly('System.Management');
13+
14+
r = stdlib.root_name(path);
15+
queryLogicalDisk = sprintf('SELECT VolumeSerialNumber FROM Win32_LogicalDisk WHERE Caption = ''%s''', r);
16+
17+
% Create a ManagementObjectSearcher instance
18+
searcher = System.Management.ManagementObjectSearcher(queryLogicalDisk);
19+
20+
% Get the collection of ManagementObject instances
21+
logicalDisks = searcher.Get();
22+
23+
% Get the first result (should be at most one for a drive letter)
24+
logicalDiskEnumerator = logicalDisks.GetEnumerator();
25+
if logicalDiskEnumerator.MoveNext()
26+
logicalDisk = logicalDiskEnumerator.Current;
27+
28+
% Get the VolumeSerialNumber property
29+
% This property is a string in WMI, representing a 32-bit hexadecimal integer.
30+
i = hex2dec(char(logicalDisk.GetPropertyValue('VolumeSerialNumber')));
31+
end
32+
33+
delete(h)
34+
elseif stdlib.isoctave()
1335
[s, err] = stat(path);
1436
if err == 0
1537
i = s.dev;

test/TestDisk.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,19 @@ function test_filesystem_type(tc, Ps)
4949
end
5050
end
5151

52+
53+
function test_device(tc)
54+
55+
tc.assumeTrue(ispc() || stdlib.has_java())
56+
57+
if ispc()
58+
tc.verifyGreaterThan(stdlib.device(pwd()), 0)
59+
else
60+
tc.assumeGreaterThanOrEqual(stdlib.java_api(), 11)
61+
62+
tc.verifyEqual(stdlib.device("."), stdlib.device(pwd()))
63+
end
64+
end
65+
5266
end
5367
end

test/TestJava.m

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ function test_inode(tc)
1515
end
1616

1717

18-
function test_device(tc)
19-
tc.assumeFalse(ispc(), "not for Windows")
20-
tc.assumeGreaterThanOrEqual(stdlib.java_api(), 11)
21-
22-
tc.verifyEqual(stdlib.device("."), stdlib.device(pwd()))
23-
end
24-
2518
function test_owner(tc, Ps)
2619

2720
s = stdlib.get_owner(Ps);

0 commit comments

Comments
 (0)