-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStat.java
More file actions
259 lines (226 loc) · 11.3 KB
/
Stat.java
File metadata and controls
259 lines (226 loc) · 11.3 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
package org.perlonjava.runtime.operators;
import org.perlonjava.runtime.io.ClosedIOHandle;
import org.perlonjava.runtime.runtimetypes.RuntimeBase;
import org.perlonjava.runtime.runtimetypes.RuntimeContextType;
import org.perlonjava.runtime.runtimetypes.RuntimeIO;
import org.perlonjava.runtime.runtimetypes.RuntimeList;
import org.perlonjava.runtime.runtimetypes.RuntimeScalar;
import org.perlonjava.runtime.runtimetypes.RuntimeScalarType;
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.lastBasicAttr;
import static org.perlonjava.runtime.operators.FileTestOperator.lastFileHandle;
import static org.perlonjava.runtime.operators.FileTestOperator.lastPosixAttr;
import static org.perlonjava.runtime.operators.FileTestOperator.lastStatOk;
import static org.perlonjava.runtime.operators.FileTestOperator.updateLastStat;
import static org.perlonjava.runtime.runtimetypes.GlobalVariable.getGlobalVariable;
import static org.perlonjava.runtime.runtimetypes.RuntimeIO.resolvePath;
import static org.perlonjava.runtime.runtimetypes.RuntimeScalarCache.getScalarInt;
import static org.perlonjava.runtime.runtimetypes.RuntimeScalarCache.scalarTrue;
import static org.perlonjava.runtime.runtimetypes.RuntimeScalarCache.scalarUndef;
/**
* stat with context awareness
* @param arg the file or filehandle to stat
* @param ctx the calling context (SCALAR, LIST, VOID, or RUNTIME)
* @return RuntimeScalar in scalar context, RuntimeList otherwise
*/
public class Stat {
// Helper method to get permissions in octal format
private static int getPermissionsOctal(BasicFileAttributes basicAttr, PosixFileAttributes attr) {
int permissions = 0;
// File type bits (first)
if (basicAttr.isDirectory()) permissions |= 0040000; // Directory
if (basicAttr.isRegularFile()) permissions |= 0100000; // Regular file
if (basicAttr.isSymbolicLink()) permissions |= 0120000; // Symbolic link
// Then add permission bits
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 RuntimeBase statLastHandle(int ctx) {
if (!lastStatOk) {
getGlobalVariable("main::!").set(9);
RuntimeList empty = new RuntimeList();
if (ctx == RuntimeContextType.SCALAR) {
return empty.statScalar();
}
return empty;
}
RuntimeList res = new RuntimeList();
statInternal(res, lastBasicAttr, lastPosixAttr);
if (ctx == RuntimeContextType.SCALAR) {
return res.statScalar();
}
return res;
}
public static RuntimeBase lstatLastHandle(int ctx) {
if (!lastStatOk) {
getGlobalVariable("main::!").set(9);
RuntimeList empty = new RuntimeList();
if (ctx == RuntimeContextType.SCALAR) {
return empty.statScalar();
}
return empty;
}
RuntimeList res = new RuntimeList();
statInternal(res, lastBasicAttr, lastPosixAttr);
if (ctx == RuntimeContextType.SCALAR) {
return res.statScalar();
}
return res;
}
/**
* stat with context awareness
* @param arg the file or filehandle to stat
* @param ctx the calling context (SCALAR, LIST, VOID, or RUNTIME)
* @return RuntimeScalar in scalar context, RuntimeList otherwise
*/
public static RuntimeBase stat(RuntimeScalar arg, int ctx) {
RuntimeList result = stat(arg);
if (ctx == RuntimeContextType.SCALAR) {
// stat in scalar context: empty list -> "", non-empty list -> 1
return result.isEmpty() ? new RuntimeScalar("") : scalarTrue;
}
return result;
}
/**
* lstat with context awareness
* @param arg the file or filehandle to lstat
* @param ctx the calling context (SCALAR, LIST, VOID, or RUNTIME)
* @return RuntimeScalar in scalar context, RuntimeList otherwise
*/
public static RuntimeBase lstat(RuntimeScalar arg, int ctx) {
RuntimeList result = lstat(arg);
if (ctx == RuntimeContextType.SCALAR) {
// lstat in scalar context: empty list -> "", non-empty list -> 1
return result.isEmpty() ? new RuntimeScalar("") : scalarTrue;
}
return result;
}
public static RuntimeList stat(RuntimeScalar arg) {
lastFileHandle.set(arg);
RuntimeList res = new RuntimeList();
// Check if the argument is a file handle (GLOB or GLOBREFERENCE)
if (arg.type == RuntimeScalarType.GLOB || arg.type == RuntimeScalarType.GLOBREFERENCE) {
RuntimeIO fh = arg.getRuntimeIO();
// Check if fh is null (invalid filehandle)
if (fh == null) {
// Set $! to EBADF (Bad file descriptor) = 9
getGlobalVariable("main::!").set(9);
updateLastStat(arg, false, 9, false);
return res; // Return empty list
}
// Check for closed handle or no valid IO handles
if ((fh.ioHandle == null || fh.ioHandle instanceof ClosedIOHandle) &&
fh.directoryIO == null) {
// Set $! to EBADF (Bad file descriptor) = 9
getGlobalVariable("main::!").set(9);
updateLastStat(arg, false, 9, false);
return res; // Return empty list
}
// Try to stat via the stored file path
if (fh.filePath != null) {
return statPath(arg, fh.filePath, res, false);
}
// For in-memory file handles (like PerlIO::scalar), we can't stat them
getGlobalVariable("main::!").set(9);
updateLastStat(arg, false, 9, false);
return res;
}
// Handle string arguments
String filename = arg.toString();
Path path = resolvePath(filename);
return statPath(arg, path, res, false);
}
private static RuntimeList statPath(RuntimeScalar arg, Path path, RuntimeList res, boolean lstat) {
try {
BasicFileAttributes basicAttr;
PosixFileAttributes posixAttr;
if (lstat) {
basicAttr = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
posixAttr = Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
} else {
basicAttr = Files.readAttributes(path, BasicFileAttributes.class);
posixAttr = Files.readAttributes(path, PosixFileAttributes.class);
}
lastBasicAttr = basicAttr;
lastPosixAttr = posixAttr;
statInternal(res, basicAttr, posixAttr);
getGlobalVariable("main::!").set(0);
updateLastStat(arg, true, 0, lstat);
} catch (NoSuchFileException e) {
getGlobalVariable("main::!").set(2);
updateLastStat(arg, false, 2, lstat);
} catch (IOException e) {
getGlobalVariable("main::!").set(5);
updateLastStat(arg, false, 5, lstat);
}
return res;
}
public static RuntimeList lstat(RuntimeScalar arg) {
lastFileHandle.set(arg);
RuntimeList res = new RuntimeList();
// Check if the argument is a file handle (GLOB or GLOBREFERENCE)
if (arg.type == RuntimeScalarType.GLOB || arg.type == RuntimeScalarType.GLOBREFERENCE) {
RuntimeIO fh = arg.getRuntimeIO();
// Check if fh is null (invalid filehandle)
if (fh == null) {
// Set $! to EBADF (Bad file descriptor) = 9
getGlobalVariable("main::!").set(9);
updateLastStat(arg, false, 9, true);
return res; // Return empty list
}
// Check for closed handle or no valid IO handles
if ((fh.ioHandle == null || fh.ioHandle instanceof ClosedIOHandle) &&
fh.directoryIO == null) {
// Set $! to EBADF (Bad file descriptor) = 9
getGlobalVariable("main::!").set(9);
updateLastStat(arg, false, 9, true);
return res; // Return empty list
}
// Try to lstat via the stored file path
if (fh.filePath != null) {
return statPath(arg, fh.filePath, res, true);
}
// For in-memory file handles (like PerlIO::scalar), we can't lstat them
getGlobalVariable("main::!").set(9);
updateLastStat(arg, false, 9, true);
return res;
}
// Handle string arguments
String filename = arg.toString();
Path path = resolvePath(filename);
return statPath(arg, path, res, true);
}
public static void statInternal(RuntimeList res, BasicFileAttributes basicAttr, PosixFileAttributes posixAttr) {
// Emulating Perl's stat return list
res.add(scalarUndef); // 0 dev (device number) - not directly available in Java
res.add(scalarUndef); // 1 ino (inode number) - not directly available in Java
res.add(getScalarInt(getPermissionsOctal(basicAttr, posixAttr))); // 2 mode (file mode/permissions)
res.add(getScalarInt(1)); // 3 nlink (number of hard links) - not easily obtainable in standard Java
res.add(scalarUndef); // 4 uid (user ID) - posixAttr.owner().getName() returns String
res.add(scalarUndef); // 5 gid (group ID) - posixAttr.group().getName() returns String
res.add(scalarUndef); // 6 rdev (device identifier for special files) - not available
res.add(getScalarInt(basicAttr.size())); // 7 size (file size in bytes)
res.add(getScalarInt(basicAttr.lastAccessTime().toMillis() / 1000)); // 8 atime (last access time)
res.add(getScalarInt(basicAttr.lastModifiedTime().toMillis() / 1000)); // 9 mtime (last modified time)
res.add(getScalarInt(basicAttr.creationTime().toMillis() / 1000)); // 10 ctime (file creation time)
res.add(scalarUndef); // 11 blksize (preferred I/O block size) - not directly available
res.add(scalarUndef); // 12 blocks (number of blocks) - not directly available
}
}