1 33 package org.jruby; 34 35 import org.jruby.runtime.Block; 36 import org.jruby.runtime.CallbackFactory; 37 import org.jruby.runtime.ObjectAllocator; 38 import org.jruby.runtime.builtin.IRubyObject; 39 import org.jruby.util.JRubyFile; 40 41 45 public class RubyFileStat extends RubyObject { 46 private static final int READ = 0222; 47 private static final int WRITE = 0444; 48 49 private RubyFixnum blksize; 50 private RubyBoolean isDirectory; 51 private RubyBoolean isFile; 52 private RubyString ftype; 53 private RubyFixnum mode; 54 private RubyTime mtime; 55 private RubyTime ctime; 56 private RubyBoolean isReadable; 57 private RubyBoolean isWritable; 58 private RubyFixnum size; 59 private RubyBoolean isSymlink; 60 61 private static ObjectAllocator ALLOCATOR = new ObjectAllocator() { 62 public IRubyObject allocate(Ruby runtime, RubyClass klass) { 63 return new RubyFileStat(runtime, klass); 64 } 65 }; 66 67 public static RubyClass createFileStatClass(Ruby runtime) { 68 final RubyClass fileStatClass = runtime.getClass("File").defineClassUnder("Stat",runtime.getObject(), ALLOCATOR); 70 final CallbackFactory callbackFactory = runtime.callbackFactory(RubyFileStat.class); 71 72 fileStatClass.defineFastMethod("initialize",callbackFactory.getMethod("initialize", RubyKernel.IRUBY_OBJECT)); 73 fileStatClass.defineFastMethod("blksize", callbackFactory.getFastMethod("blksize")); 77 fileStatClass.defineFastMethod("ctime", callbackFactory.getFastMethod("ctime")); 81 fileStatClass.defineFastMethod("directory?", callbackFactory.getFastMethod("directory_p")); 85 fileStatClass.defineFastMethod("file?", callbackFactory.getFastMethod("file_p")); 88 fileStatClass.defineFastMethod("ftype", callbackFactory.getFastMethod("ftype")); 89 fileStatClass.defineFastMethod("ino", callbackFactory.getFastMethod("ino")); 92 fileStatClass.defineFastMethod("mode", callbackFactory.getFastMethod("mode")); 93 fileStatClass.defineFastMethod("mtime", callbackFactory.getFastMethod("mtime")); 94 fileStatClass.defineFastMethod("readable?", callbackFactory.getFastMethod("readable_p")); 101 fileStatClass.defineFastMethod("size", callbackFactory.getFastMethod("size")); 105 fileStatClass.defineFastMethod("symlink?", callbackFactory.getFastMethod("symlink_p")); 109 fileStatClass.defineFastMethod("writable?", callbackFactory.getFastMethod("writable")); 111 114 return fileStatClass; 115 } 116 117 protected RubyFileStat(Ruby runtime, RubyClass clazz) { 118 super(runtime, clazz); 119 120 } 121 122 public IRubyObject initialize(IRubyObject fname, Block unusedBlock) { 123 Ruby runtime = getRuntime(); 124 JRubyFile file = JRubyFile.create(runtime.getCurrentDirectory(),fname.toString()); 125 126 if(!file.exists()) { 127 throw runtime.newErrnoENOENTError("No such file or directory - " + file.getPath()); 128 } 129 130 blksize = runtime.newFixnum(4096); 132 isDirectory = runtime.newBoolean(file.isDirectory()); 133 isFile = runtime.newBoolean(file.isFile()); 134 ftype = file.isDirectory()? runtime.newString("directory") : (file.isFile() ? runtime.newString("file") : null); 135 136 int baseMode = 0100000; 138 if (file.canRead()) { 139 baseMode += READ; 140 } 141 if (file.canWrite()) { 142 baseMode += WRITE; 143 } 144 mode = runtime.newFixnum(baseMode); 145 mtime = runtime.newTime(file.lastModified()); 146 ctime = runtime.newTime(file.getParentFile().lastModified()); 147 isReadable = runtime.newBoolean(file.canRead()); 148 isWritable = runtime.newBoolean(file.canWrite()); 149 size = runtime.newFixnum(file.length()); 150 isSymlink = runtime.getFalse(); 152 return this; 153 } 154 155 public RubyFixnum blksize() { 156 return blksize; 157 } 158 159 public RubyBoolean directory_p() { 160 return isDirectory; 161 } 162 163 public RubyBoolean file_p() { 164 return isFile; 165 } 166 167 public RubyString ftype() { 168 return ftype; 169 } 170 171 public IRubyObject ino() { 173 return getRuntime().newFixnum(0); 174 } 175 176 public IRubyObject mode() { 177 return mode; 178 } 179 180 public IRubyObject mtime() { 181 return mtime; 182 } 183 184 public IRubyObject ctime() { 185 return ctime; 186 } 187 188 public IRubyObject readable_p() { 189 return isReadable; 190 } 191 192 public IRubyObject size() { 193 return size; 194 } 195 196 public IRubyObject symlink_p() { 197 return isSymlink; 198 } 199 200 public IRubyObject writable() { 201 return isWritable; 202 } 203 } 204 | Popular Tags |