forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRubyFileStat.java
More file actions
628 lines (500 loc) · 19.2 KB
/
Copy pathRubyFileStat.java
File metadata and controls
628 lines (500 loc) · 19.2 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
***** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004 Joey Gibson <joey@joeygibson.com>
* Copyright (C) 2004 Charles O Nutter <headius@headius.com>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby;
import java.io.FileDescriptor;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import jnr.posix.FileStat;
import jnr.posix.util.Platform;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.JRubyFile;
/**
* Implements File::Stat
*/
@JRubyClass(name="File::Stat", include="Comparable")
public class RubyFileStat extends RubyObject {
private static final long serialVersionUID = 1L;
private static final int S_IRUGO = (FileStat.S_IRUSR | FileStat.S_IRGRP | FileStat.S_IROTH);
private static final int S_IWUGO = (FileStat.S_IWUSR | FileStat.S_IWGRP | FileStat.S_IWOTH);
private static final int S_IXUGO = (FileStat.S_IXUSR | FileStat.S_IXGRP | FileStat.S_IXOTH);
private JRubyFile file;
private FileStat stat;
private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new RubyFileStat(runtime, klass);
}
};
public static RubyClass createFileStatClass(Ruby runtime) {
// TODO: NOT_ALLOCATABLE_ALLOCATOR is probably ok here. Confirm. JRUBY-415
final RubyClass fileStatClass = runtime.getFile().defineClassUnder("Stat",runtime.getObject(), ALLOCATOR);
runtime.setFileStat(fileStatClass);
fileStatClass.includeModule(runtime.getModule("Comparable"));
fileStatClass.defineAnnotatedMethods(RubyFileStat.class);
return fileStatClass;
}
protected RubyFileStat(Ruby runtime, RubyClass clazz) {
super(runtime, clazz);
}
public static RubyFileStat newFileStat(Ruby runtime, String filename, boolean lstat) {
RubyFileStat stat = new RubyFileStat(runtime, runtime.getFileStat());
stat.setup(filename, lstat);
return stat;
}
public static RubyFileStat newFileStat(Ruby runtime, FileDescriptor descriptor) {
RubyFileStat stat = new RubyFileStat(runtime, runtime.getFileStat());
stat.setup(descriptor);
return stat;
}
private void setup(FileDescriptor descriptor) {
stat = getRuntime().getPosix().fstat(descriptor);
}
private void setup(String filename, boolean lstat) {
if (Platform.IS_WINDOWS && filename.length() == 2
&& filename.charAt(1) == ':' && Character.isLetter(filename.charAt(0))) {
filename += "/";
}
if (filename.startsWith("file:") && filename.indexOf('!') != -1) {
// file: URL handling
String zipFileEntry = filename.substring(filename.indexOf("!") + 1);
if (zipFileEntry.length() > 0) {
if (zipFileEntry.charAt(0) == '/') {
if (zipFileEntry.length() > 1) {
zipFileEntry = zipFileEntry.substring(1);
} else {
throw getRuntime().newErrnoENOENTError("invalid jar/file URL: " + filename);
}
}
} else {
throw getRuntime().newErrnoENOENTError("invalid jar/file URL: " + filename);
}
String zipfilename = filename.substring(5, filename.indexOf("!"));
try {
ZipFile zipFile = new ZipFile(zipfilename);
ZipEntry zipEntry = RubyFile.getFileEntry(zipFile, zipFileEntry);
if (zipEntry == null) {
throw getRuntime().newErrnoENOENTError("invalid jar/file URL: " + filename);
}
stat = new ZipFileStat(zipEntry);
return;
} catch (IOException ioe) {
// fall through and use the zip file as the file to stat
}
filename = zipfilename;
}
file = JRubyFile.create(getRuntime().getCurrentDirectory(), filename);
if (lstat) {
stat = getRuntime().getPosix().lstat(file.getAbsolutePath());
} else {
stat = getRuntime().getPosix().stat(file.getAbsolutePath());
}
}
public static class ZipFileStat implements FileStat {
private final ZipEntry zipEntry;
public ZipFileStat(ZipEntry zipEntry) {
this.zipEntry = zipEntry;
}
public long atime() {
return zipEntry.getTime();
}
public long blocks() {
return zipEntry.getSize();
}
public long blockSize() {
return 1L;
}
public long ctime() {
return zipEntry.getTime();
}
public long dev() {
return -1;
}
public String ftype() {
return "zip file entry";
}
public int gid() {
return -1;
}
public boolean groupMember(int i) {
return false;
}
public long ino() {
return -1;
}
public boolean isBlockDev() {
return false;
}
public boolean isCharDev() {
return false;
}
public boolean isDirectory() {
return zipEntry.isDirectory();
}
public boolean isEmpty() {
return zipEntry.getSize() == 0;
}
public boolean isExecutable() {
return false;
}
public boolean isExecutableReal() {
return false;
}
public boolean isFifo() {
return false;
}
public boolean isFile() {
return !zipEntry.isDirectory();
}
public boolean isGroupOwned() {
return false;
}
public boolean isIdentical(FileStat fs) {
return fs instanceof ZipFileStat && ((ZipFileStat)fs).zipEntry.equals(zipEntry);
}
public boolean isNamedPipe() {
return false;
}
public boolean isOwned() {
return false;
}
public boolean isROwned() {
return false;
}
public boolean isReadable() {
return true;
}
public boolean isReadableReal() {
return true;
}
public boolean isWritable() {
return false;
}
public boolean isWritableReal() {
return false;
}
public boolean isSetgid() {
return false;
}
public boolean isSetuid() {
return false;
}
public boolean isSocket() {
return false;
}
public boolean isSticky() {
return false;
}
public boolean isSymlink() {
return false;
}
public int major(long l) {
return -1;
}
public int minor(long l) {
return -1;
}
public int mode() {
return -1;
}
public long mtime() {
return zipEntry.getTime();
}
public int nlink() {
return -1;
}
public long rdev() {
return -1;
}
public long st_size() {
return zipEntry.getSize();
}
public int uid() {
return 0;
}
}
@JRubyMethod(name = "initialize", required = 1, visibility = Visibility.PRIVATE, compat = CompatVersion.RUBY1_8)
public IRubyObject initialize(IRubyObject fname, Block unusedBlock) {
setup(fname.convertToString().toString(), false);
return this;
}
@JRubyMethod(name = "initialize", required = 1, visibility = Visibility.PRIVATE, compat = CompatVersion.RUBY1_9)
public IRubyObject initialize19(IRubyObject fname, Block unusedBlock) {
if (!(fname instanceof RubyString) && fname.respondsTo("to_path")) {
fname = fname.callMethod(getRuntime().getCurrentContext(), "to_path");
}
return initialize(fname, unusedBlock);
}
@JRubyMethod(name = "atime")
public IRubyObject atime() {
return getRuntime().newTime(stat.atime() * 1000);
}
@JRubyMethod(name = "blksize")
public RubyFixnum blksize() {
return getRuntime().newFixnum(stat.blockSize());
}
@JRubyMethod(name = "blockdev?")
public IRubyObject blockdev_p() {
return getRuntime().newBoolean(stat.isBlockDev());
}
@JRubyMethod(name = "blocks")
public IRubyObject blocks() {
return getRuntime().newFixnum(stat.blocks());
}
@JRubyMethod(name = "chardev?")
public IRubyObject chardev_p() {
return getRuntime().newBoolean(stat.isCharDev());
}
@JRubyMethod(name = "<=>", required = 1)
public IRubyObject cmp(IRubyObject other) {
if (!(other instanceof RubyFileStat)) getRuntime().getNil();
long time1 = stat.mtime();
long time2 = ((RubyFileStat) other).stat.mtime();
if (time1 == time2) {
return getRuntime().newFixnum(0);
} else if (time1 < time2) {
return getRuntime().newFixnum(-1);
}
return getRuntime().newFixnum(1);
}
@JRubyMethod(name = "ctime")
public IRubyObject ctime() {
return getRuntime().newTime(stat.ctime() * 1000);
}
@JRubyMethod(name = "dev")
public IRubyObject dev() {
return getRuntime().newFixnum(stat.dev());
}
@JRubyMethod(name = "dev_major")
public IRubyObject devMajor() {
return getRuntime().newFixnum(stat.major(stat.dev()));
}
@JRubyMethod(name = "dev_minor")
public IRubyObject devMinor() {
return getRuntime().newFixnum(stat.minor(stat.dev()));
}
@JRubyMethod(name = "directory?")
public RubyBoolean directory_p() {
return getRuntime().newBoolean(stat.isDirectory());
}
@JRubyMethod(name = "executable?")
public IRubyObject executable_p() {
return getRuntime().newBoolean(stat.isExecutable());
}
@JRubyMethod(name = "executable_real?")
public IRubyObject executableReal_p() {
return getRuntime().newBoolean(stat.isExecutableReal());
}
@JRubyMethod(name = "file?")
public RubyBoolean file_p() {
return getRuntime().newBoolean(stat.isFile());
}
@JRubyMethod(name = "ftype")
public RubyString ftype() {
return getRuntime().newString(stat.ftype());
}
@JRubyMethod(name = "gid")
public IRubyObject gid() {
return getRuntime().newFixnum(stat.gid());
}
@JRubyMethod(name = "grpowned?")
public IRubyObject group_owned_p() {
return getRuntime().newBoolean(stat.isGroupOwned());
}
@JRubyMethod(name = "initialize_copy", required = 1)
public IRubyObject initialize_copy(IRubyObject original) {
if (!(original instanceof RubyFileStat)) {
throw getRuntime().newTypeError("wrong argument class");
}
RubyFileStat originalFileStat = (RubyFileStat) original;
file = originalFileStat.file;
stat = originalFileStat.stat;
return this;
}
@JRubyMethod(name = "ino")
public IRubyObject ino() {
return getRuntime().newFixnum(stat.ino());
}
@JRubyMethod(name = "inspect")
public IRubyObject inspect() {
StringBuilder buf = new StringBuilder("#<");
buf.append(getMetaClass().getRealClass().getName());
buf.append(" ");
// FIXME: Obvious issue that not all platforms can display all attributes. Ugly hacks.
// Using generic posix library makes pushing inspect behavior into specific system impls
// rather painful.
try { buf.append("dev=0x").append(Long.toHexString(stat.dev())); } catch (Exception e) {} finally { buf.append(", "); }
try { buf.append("ino=").append(stat.ino()); } catch (Exception e) {} finally { buf.append(", "); }
buf.append("mode=0").append(Integer.toOctalString(stat.mode())).append(", ");
try { buf.append("nlink=").append(stat.nlink()); } catch (Exception e) {} finally { buf.append(", "); }
try { buf.append("uid=").append(stat.uid()); } catch (Exception e) {} finally { buf.append(", "); }
try { buf.append("gid=").append(stat.gid()); } catch (Exception e) {} finally { buf.append(", "); }
try { buf.append("rdev=0x").append(Long.toHexString(stat.rdev())); } catch (Exception e) {} finally { buf.append(", "); }
buf.append("size=").append(sizeInternal()).append(", ");
try { buf.append("blksize=").append(stat.blockSize()); } catch (Exception e) {} finally { buf.append(", "); }
try { buf.append("blocks=").append(stat.blocks()); } catch (Exception e) {} finally { buf.append(", "); }
buf.append("atime=").append(atime()).append(", ");
buf.append("mtime=").append(mtime()).append(", ");
buf.append("ctime=").append(ctime());
buf.append(">");
return getRuntime().newString(buf.toString());
}
@JRubyMethod(name = "uid")
public IRubyObject uid() {
return getRuntime().newFixnum(stat.uid());
}
@JRubyMethod(name = "mode")
public IRubyObject mode() {
return getRuntime().newFixnum(stat.mode());
}
@JRubyMethod(name = "mtime")
public IRubyObject mtime() {
return getRuntime().newTime(stat.mtime() * 1000);
}
public IRubyObject mtimeEquals(IRubyObject other) {
return getRuntime().newBoolean(stat.mtime() == newFileStat(getRuntime(), other.convertToString().toString(), false).stat.mtime());
}
public IRubyObject mtimeGreaterThan(IRubyObject other) {
return getRuntime().newBoolean(stat.mtime() > newFileStat(getRuntime(), other.convertToString().toString(), false).stat.mtime());
}
public IRubyObject mtimeLessThan(IRubyObject other) {
return getRuntime().newBoolean(stat.mtime() < newFileStat(getRuntime(), other.convertToString().toString(), false).stat.mtime());
}
@JRubyMethod(name = "nlink")
public IRubyObject nlink() {
return getRuntime().newFixnum(stat.nlink());
}
@JRubyMethod(name = "owned?")
public IRubyObject owned_p() {
return getRuntime().newBoolean(stat.isOwned());
}
@JRubyMethod(name = "pipe?")
public IRubyObject pipe_p() {
return getRuntime().newBoolean(stat.isNamedPipe());
}
@JRubyMethod(name = "rdev")
public IRubyObject rdev() {
return getRuntime().newFixnum(stat.rdev());
}
@JRubyMethod(name = "rdev_major")
public IRubyObject rdevMajor() {
return getRuntime().newFixnum(stat.major(stat.rdev()));
}
@JRubyMethod(name = "rdev_minor")
public IRubyObject rdevMinor() {
return getRuntime().newFixnum(stat.minor(stat.rdev()));
}
@JRubyMethod(name = "readable?")
public IRubyObject readable_p() {
return getRuntime().newBoolean(stat.isReadable());
}
@JRubyMethod(name = "readable_real?")
public IRubyObject readableReal_p() {
return getRuntime().newBoolean(stat.isReadableReal());
}
@JRubyMethod(name = "setgid?")
public IRubyObject setgid_p() {
return getRuntime().newBoolean(stat.isSetgid());
}
@JRubyMethod(name = "setuid?")
public IRubyObject setuid_p() {
return getRuntime().newBoolean(stat.isSetuid());
}
private long sizeInternal() {
// Workaround for JRUBY-4149
if (Platform.IS_WINDOWS && file != null) {
try {
return file.length();
} catch (SecurityException ex) {
return 0L;
}
} else {
return stat.st_size();
}
}
@JRubyMethod(name = "size")
public IRubyObject size() {
return getRuntime().newFixnum(sizeInternal());
}
@JRubyMethod(name = "size?")
public IRubyObject size_p() {
long size = sizeInternal();
if (size == 0) return getRuntime().getNil();
return getRuntime().newFixnum(size);
}
@JRubyMethod(name = "socket?")
public IRubyObject socket_p() {
return getRuntime().newBoolean(stat.isSocket());
}
@JRubyMethod(name = "sticky?")
public IRubyObject sticky_p() {
return getRuntime().newBoolean(stat.isSticky());
}
@JRubyMethod(name = "symlink?")
public IRubyObject symlink_p() {
return getRuntime().newBoolean(stat.isSymlink());
}
@JRubyMethod(name = "writable?")
public IRubyObject writable_p() {
return getRuntime().newBoolean(stat.isWritable());
}
@JRubyMethod(name = "writable_real?")
public IRubyObject writableReal_p() {
return getRuntime().newBoolean(stat.isWritableReal());
}
@JRubyMethod(name = "zero?")
public IRubyObject zero_p() {
return getRuntime().newBoolean(stat.isEmpty());
}
@JRubyMethod(name = "world_readable?", compat = CompatVersion.RUBY1_9)
public IRubyObject worldReadable(ThreadContext context) {
return getWorldMode(context, FileStat.S_IROTH);
}
@JRubyMethod(name = "world_writable?", compat = CompatVersion.RUBY1_9)
public IRubyObject worldWritable(ThreadContext context) {
return getWorldMode(context, FileStat.S_IWOTH);
}
private IRubyObject getWorldMode(ThreadContext context, int mode) {
if ((stat.mode() & mode) == mode) {
return RubyNumeric.int2fix(context.getRuntime(),
(stat.mode() & (S_IRUGO | S_IWUGO | S_IXUGO) ));
}
return context.getRuntime().getNil();
}
}