-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStat.java
More file actions
367 lines (334 loc) · 14 KB
/
Stat.java
File metadata and controls
367 lines (334 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package org.perlonjava.runtime.operators;
import org.perlonjava.runtime.io.*;
import org.perlonjava.runtime.nativ.NativeUtils;
import org.perlonjava.runtime.nativ.ffm.FFMPosix;
import org.perlonjava.runtime.nativ.ffm.FFMPosixInterface;
import org.perlonjava.runtime.runtimetypes.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;
import static org.perlonjava.runtime.operators.FileTestOperator.*;
import static org.perlonjava.runtime.runtimetypes.GlobalVariable.getGlobalVariable;
import static org.perlonjava.runtime.runtimetypes.RuntimeIO.resolvePath;
import static org.perlonjava.runtime.runtimetypes.RuntimeScalarCache.*;
public class Stat {
static NativeStatFields lastNativeStatFields;
// FFM POSIX implementation
private static final FFMPosixInterface posix = FFMPosix.get();
/**
* Checks if a glob argument is the special underscore glob (*_ or \*_).
*/
private static boolean isUnderscoreGlob(RuntimeScalar arg) {
if (arg.value instanceof RuntimeGlob rg) {
return rg.globName != null && rg.globName.endsWith("::_");
}
if (arg.value instanceof RuntimeIO rio) {
return rio.globName != null && rio.globName.endsWith("::_");
}
return false;
}
static NativeStatFields nativeStat(String path, boolean followLinks) {
try {
if (NativeUtils.IS_WINDOWS) return null;
FFMPosixInterface.StatResult sr = followLinks
? posix.stat(path)
: posix.lstat(path);
if (sr == null) return null;
return new NativeStatFields(
sr.dev(), sr.ino(), sr.mode(), sr.nlink(),
sr.uid(), sr.gid(), sr.rdev(), sr.size(),
sr.atime(), sr.mtime(), sr.ctime(),
sr.blksize(), sr.blocks()
);
} catch (Throwable e) {
return null;
}
}
private static int getPermissionsOctal(BasicFileAttributes basicAttr, PosixFileAttributes attr) {
int permissions = 0;
if (basicAttr.isDirectory()) permissions |= 0040000;
if (basicAttr.isRegularFile()) permissions |= 0100000;
if (basicAttr.isSymbolicLink()) permissions |= 0120000;
Set<PosixFilePermission> perms = attr.permissions();
if (perms.contains(PosixFilePermission.OWNER_READ)) permissions |= 0400;
if (perms.contains(PosixFilePermission.OWNER_WRITE)) permissions |= 0200;
if (perms.contains(PosixFilePermission.OWNER_EXECUTE)) permissions |= 0100;
if (perms.contains(PosixFilePermission.GROUP_READ)) permissions |= 0040;
if (perms.contains(PosixFilePermission.GROUP_WRITE)) permissions |= 0020;
if (perms.contains(PosixFilePermission.GROUP_EXECUTE)) permissions |= 0010;
if (perms.contains(PosixFilePermission.OTHERS_READ)) permissions |= 0004;
if (perms.contains(PosixFilePermission.OTHERS_WRITE)) permissions |= 0002;
if (perms.contains(PosixFilePermission.OTHERS_EXECUTE)) permissions |= 0001;
return permissions;
}
public static RuntimeList statLastHandle() {
if (!lastStatOk) {
getGlobalVariable("main::!").set(9);
return new RuntimeList();
}
RuntimeList res = new RuntimeList();
if (lastNativeStatFields != null) {
statInternalNative(res, lastNativeStatFields);
} else {
statInternal(res, lastBasicAttr, lastPosixAttr);
}
getGlobalVariable("main::!").set(0);
return res;
}
public static RuntimeBase statLastHandle(int ctx) {
if (ctx == RuntimeContextType.SCALAR) {
if (!lastStatOk) {
getGlobalVariable("main::!").set(9);
return new RuntimeScalar("");
}
getGlobalVariable("main::!").set(0);
return scalarTrue;
}
return statLastHandle();
}
public static RuntimeList lstatLastHandle() {
if (!lastStatWasLstat) {
throw new PerlCompilerException("The stat preceding lstat() wasn't an lstat");
}
if (!lastStatOk) {
getGlobalVariable("main::!").set(9);
return new RuntimeList();
}
RuntimeList res = new RuntimeList();
if (lastNativeStatFields != null) {
statInternalNative(res, lastNativeStatFields);
} else {
statInternal(res, lastBasicAttr, lastPosixAttr);
}
getGlobalVariable("main::!").set(0);
return res;
}
public static RuntimeBase lstatLastHandle(int ctx) {
if (!lastStatWasLstat) {
throw new PerlCompilerException("The stat preceding lstat() wasn't an lstat");
}
if (ctx == RuntimeContextType.SCALAR) {
if (!lastStatOk) {
getGlobalVariable("main::!").set(9);
return new RuntimeScalar("");
}
getGlobalVariable("main::!").set(0);
return scalarTrue;
}
return lstatLastHandle();
}
public static RuntimeBase stat(RuntimeScalar arg, int ctx) {
RuntimeList result = stat(arg);
if (ctx == RuntimeContextType.SCALAR) {
return result.isEmpty() ? new RuntimeScalar("") : scalarTrue;
}
return result;
}
public static RuntimeBase lstat(RuntimeScalar arg, int ctx) {
RuntimeList result = lstat(arg);
if (ctx == RuntimeContextType.SCALAR) {
return result.isEmpty() ? new RuntimeScalar("") : scalarTrue;
}
return result;
}
public static RuntimeList stat(RuntimeScalar arg) {
lastFileHandle.set(arg);
RuntimeList res = new RuntimeList();
if (arg.type == RuntimeScalarType.GLOB || arg.type == RuntimeScalarType.GLOBREFERENCE) {
RuntimeIO fh = arg.getRuntimeIO();
if (fh == null) {
getGlobalVariable("main::!").set(9);
updateLastStat(arg, false, 9, false);
return res;
}
if ((fh.ioHandle == null || fh.ioHandle instanceof ClosedIOHandle) &&
fh.directoryIO == null) {
getGlobalVariable("main::!").set(9);
updateLastStat(arg, false, 9, false);
return res;
}
IOHandle innerHandle = fh.ioHandle;
while (true) {
if (innerHandle instanceof LayeredIOHandle lh) {
innerHandle = lh.getDelegate();
} else if (innerHandle instanceof DupIOHandle dh) {
innerHandle = dh.getDelegate();
} else if (innerHandle instanceof BorrowedIOHandle bh) {
innerHandle = bh.getDelegate();
} else {
break;
}
}
if (innerHandle instanceof CustomFileChannel cfc) {
Path path = cfc.getFilePath();
if (path != null) {
return stat(new RuntimeScalar(path.toString()));
}
}
// Check for directory handle
if (fh.directoryIO != null) {
Path dirPath = fh.directoryIO.getAbsoluteDirectoryPath();
if (dirPath != null) {
return stat(new RuntimeScalar(dirPath.toString()));
}
}
getGlobalVariable("main::!").set(9);
updateLastStat(arg, false, 9, false);
return res;
}
String filename = arg.toString();
try {
Path path = resolvePath(filename);
if (path == null) {
getGlobalVariable("main::!").set(2);
updateLastStat(arg, false, 2, false);
return res;
}
NativeStatFields nf = nativeStat(path.toString(), true);
BasicFileAttributes basicAttr = Files.readAttributes(path, BasicFileAttributes.class);
PosixFileAttributes posixAttr = null;
try {
posixAttr = Files.readAttributes(path, PosixFileAttributes.class);
} catch (UnsupportedOperationException e) {
// Not available on Windows
}
if (nf != null) {
statInternalNative(res, nf);
} else if (posixAttr != null) {
statInternal(res, basicAttr, posixAttr);
} else {
statInternalBasic(res, basicAttr);
}
getGlobalVariable("main::!").set(0);
updateLastStat(arg, true, 0, false);
// Set attributes after updateLastStat (which resets them to null)
lastBasicAttr = basicAttr;
lastPosixAttr = posixAttr;
lastNativeStatFields = nf;
} catch (NoSuchFileException e) {
getGlobalVariable("main::!").set(2);
updateLastStat(arg, false, 2, false);
} catch (IOException e) {
getGlobalVariable("main::!").set(5);
updateLastStat(arg, false, 5, false);
}
return res;
}
public static RuntimeList lstat(RuntimeScalar arg) {
lastFileHandle.set(arg);
RuntimeList res = new RuntimeList();
if (arg.type == RuntimeScalarType.GLOB || arg.type == RuntimeScalarType.GLOBREFERENCE) {
// Check if this is the special underscore glob (*_ or \*_)
if (isUnderscoreGlob(arg)) {
// lstat on *_ or \*_ after stat should croak
if (!lastStatWasLstat) {
throw new PerlCompilerException("The stat preceding lstat() wasn't an lstat");
}
return lstatLastHandle();
}
// Perl: lstat on a filehandle reverts to regular stat (fstat)
return stat(arg);
}
String filename = arg.toString();
try {
Path path = resolvePath(filename);
if (path == null) {
getGlobalVariable("main::!").set(2);
updateLastStat(arg, false, 2, true);
return res;
}
NativeStatFields nf = nativeStat(path.toString(), false);
BasicFileAttributes basicAttr = Files.readAttributes(path,
BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
PosixFileAttributes posixAttr = null;
try {
posixAttr = Files.readAttributes(path,
PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
} catch (UnsupportedOperationException e) {
// Not available on Windows
}
if (nf != null) {
statInternalNative(res, nf);
} else if (posixAttr != null) {
statInternal(res, basicAttr, posixAttr);
} else {
statInternalBasic(res, basicAttr);
}
getGlobalVariable("main::!").set(0);
updateLastStat(arg, true, 0, true);
// Set attributes after updateLastStat (which resets them to null)
lastBasicAttr = basicAttr;
lastPosixAttr = posixAttr;
lastNativeStatFields = nf;
} catch (NoSuchFileException e) {
getGlobalVariable("main::!").set(2);
updateLastStat(arg, false, 2, true);
} catch (IOException e) {
getGlobalVariable("main::!").set(5);
updateLastStat(arg, false, 5, true);
}
return res;
}
private static void statInternalNative(RuntimeList res, NativeStatFields nf) {
res.add(getScalarInt(nf.dev));
res.add(getScalarInt(nf.ino));
res.add(getScalarInt(nf.mode));
res.add(getScalarInt(nf.nlink));
res.add(getScalarInt(nf.uid));
res.add(getScalarInt(nf.gid));
res.add(getScalarInt(nf.rdev));
res.add(getScalarInt(nf.size));
res.add(getScalarInt(nf.atime));
res.add(getScalarInt(nf.mtime));
res.add(getScalarInt(nf.ctime));
res.add(getScalarInt(nf.blksize));
res.add(getScalarInt(nf.blocks));
}
public static void statInternal(RuntimeList res, BasicFileAttributes basicAttr, PosixFileAttributes posixAttr) {
res.add(scalarUndef);
res.add(scalarUndef);
res.add(getScalarInt(getPermissionsOctal(basicAttr, posixAttr)));
res.add(getScalarInt(1));
res.add(scalarUndef);
res.add(scalarUndef);
res.add(scalarUndef);
res.add(getScalarInt(basicAttr.size()));
res.add(getScalarInt(basicAttr.lastAccessTime().toMillis() / 1000));
res.add(getScalarInt(basicAttr.lastModifiedTime().toMillis() / 1000));
res.add(getScalarInt(basicAttr.creationTime().toMillis() / 1000));
res.add(scalarUndef);
res.add(scalarUndef);
}
private static void statInternalBasic(RuntimeList res, BasicFileAttributes basicAttr) {
int mode = 0;
if (basicAttr.isDirectory()) mode = 0040755;
else if (basicAttr.isRegularFile()) mode = 0100644;
else if (basicAttr.isSymbolicLink()) mode = 0120777;
res.add(scalarUndef);
res.add(scalarUndef);
res.add(getScalarInt(mode));
res.add(getScalarInt(1));
res.add(scalarUndef);
res.add(scalarUndef);
res.add(scalarUndef);
res.add(getScalarInt(basicAttr.size()));
res.add(getScalarInt(basicAttr.lastAccessTime().toMillis() / 1000));
res.add(getScalarInt(basicAttr.lastModifiedTime().toMillis() / 1000));
res.add(getScalarInt(basicAttr.creationTime().toMillis() / 1000));
res.add(scalarUndef);
res.add(scalarUndef);
}
record NativeStatFields(
long dev, long ino, long mode, long nlink,
long uid, long gid, long rdev, long size,
long atime, long mtime, long ctime,
long blksize, long blocks
) {
}
}