Skip to content

Commit b2baf7b

Browse files
committed
improve use of stat()
1 parent 1904550 commit b2baf7b

File tree

7 files changed

+35
-22
lines changed

7 files changed

+35
-22
lines changed

+stdlib/file_size.m

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
p {mustBeTextScalar,mustBeFile}
1111
end
1212

13+
s = [];
14+
1315
if stdlib.isoctave()
14-
s = stat(p);
15-
if ~isempty(s)
16-
s = s.size;
16+
[st, err] = stat(p);
17+
if err == 0
18+
s = st.size;
1719
end
1820
else
19-
s = dir(p);
20-
if ~isempty(s)
21-
s = s.bytes;
21+
d = dir(p);
22+
if ~isempty(d)
23+
s = d.bytes;
2224
end
2325
end
2426

+stdlib/get_modtime.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414

1515
if stdlib.isoctave()
16-
s = stat(p);
17-
if isempty(s)
18-
t = [];
19-
else
16+
[s, err] = stat(p);
17+
if err == 0
2018
t = s.mtime;
19+
else
20+
t = [];
2121
end
2222
return
2323
end

+stdlib/get_permissions.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
switch e.identifier
1616
case "MATLAB:io:filesystem:filePermissions:CannotFindLocation", return
1717
case "Octave:undefined-function"
18-
try %#ok<TRYNC>
19-
p = stat(f).modestr;
18+
[s, err] = stat(f);
19+
if err == 0
20+
p = s.modestr;
2021
end
2122
return
2223
case "MATLAB:UndefinedFunction"

+stdlib/hard_link_count.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
c = [];
1414

1515
if stdlib.isoctave()
16-
s = stat(p);
17-
if ~isempty(s)
16+
[s, err] = stat(p);
17+
if err == 0
1818
c = s.nlink;
1919
end
2020
elseif ispc() || ~isfile(p)

+stdlib/is_char_device.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
end
1111

1212
if stdlib.isoctave()
13-
ok = S_ISCHR(stat(p).mode);
13+
[s, err] = stat(p);
14+
ok = err == 0 && S_ISCHR(s.mode);
1415
else
1516
error("buildtool mex")
1617
end

+stdlib/is_symlink.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
catch e
1313
switch e.identifier
1414
case "MATLAB:UndefinedFunction", ok = java.nio.file.Files.isSymbolicLink(javaPathObject(stdlib.absolute(p)));
15-
case "Octave:undefined-function", ok = S_ISLNK(stat(p).mode);
15+
case "Octave:undefined-function"
16+
[s, err] = stat(p);
17+
ok = err == 0 && S_ISLNK(s.mode);
1618
otherwise, rethrow(e)
1719
end
1820
end

+stdlib/samepath.m

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
%
1010
%%% Inputs
1111
% * path1, path2: paths to compare
12-
%%% Outputs
13-
% issame: logical
1412

15-
16-
function issame = samepath(path1, path2)
13+
function y = samepath(path1, path2)
1714
arguments
1815
path1 {mustBeTextScalar}
1916
path2 {mustBeTextScalar}
@@ -22,9 +19,19 @@
2219
% simpler our way than
2320
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isSameFile(java.nio.file.Path,java.nio.file.Path)
2421

25-
issame = stdlib.exists(path1) && stdlib.exists(path2) && ...
26-
strcmp(stdlib.canonical(path1), stdlib.canonical(path2));
22+
y = stdlib.exists(path1) && stdlib.exists(path2);
23+
24+
if ~y, return; end
25+
26+
if ~ispc() && stdlib.isoctave()
27+
[r1, e1] = stat(path1);
28+
[r2, e2] = stat(path2);
29+
30+
y = e1 == 0 && e2 == 0 && ...
31+
r1.ino == r2.ino && r1.dev == r2.dev;
2732

33+
else
34+
y = strcmp(stdlib.canonical(path1), stdlib.canonical(path2));
2835
end
2936

3037
%!assert(samepath(".", "."))

0 commit comments

Comments
 (0)