KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > RubyFileStat


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
15  * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
16  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
17  * Copyright (C) 2004 Joey Gibson <joey@joeygibson.com>
18  * Copyright (C) 2004 Charles O Nutter <headius@headius.com>
19  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * either of the GNU General Public License Version 2 or later (the "GPL"),
23  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
24  * in which case the provisions of the GPL or the LGPL are applicable instead
25  * of those above. If you wish to allow use of your version of this file only
26  * under the terms of either the GPL or the LGPL, and not to allow others to
27  * use your version of this file under the terms of the CPL, indicate your
28  * decision by deleting the provisions above and replace them with the notice
29  * and other provisions required by the GPL or the LGPL. If you do not delete
30  * the provisions above, a recipient may use your version of this file under
31  * the terms of any one of the CPL, the GPL or the LGPL.
32  ***** END LICENSE BLOCK *****/

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 /**
42  * note: renamed from FileStatClass.java
43  * Implements File::Stat
44  */

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         // TODO: NOT_ALLOCATABLE_ALLOCATOR is probably ok here. Confirm. JRUBY-415
69
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.defineMethod("<=>", callbackFactory.getMethod(""));
74
// fileStateClass.includeModule(runtime.getModule("Comparable"));
75
// fileStatClass.defineMethod("atime", callbackFactory.getMethod(""));
76
fileStatClass.defineFastMethod("blksize", callbackFactory.getFastMethod("blksize"));
77         // fileStatClass.defineMethod("blockdev?", callbackFactory.getMethod(""));
78
// fileStatClass.defineMethod("blocks", callbackFactory.getMethod(""));
79
// fileStatClass.defineMethod("chardev?", callbackFactory.getMethod(""));
80
fileStatClass.defineFastMethod("ctime", callbackFactory.getFastMethod("ctime"));
81         // fileStatClass.defineMethod("dev", callbackFactory.getMethod(""));
82
// fileStatClass.defineMethod("dev_major", callbackFactory.getMethod(""));
83
// fileStatClass.defineMethod("dev_minor", callbackFactory.getMethod(""));
84
fileStatClass.defineFastMethod("directory?", callbackFactory.getFastMethod("directory_p"));
85         // fileStatClass.defineMethod("executable?", callbackFactory.getMethod(""));
86
// fileStatClass.defineMethod("executable_real?", callbackFactory.getMethod(""));
87
fileStatClass.defineFastMethod("file?", callbackFactory.getFastMethod("file_p"));
88         fileStatClass.defineFastMethod("ftype", callbackFactory.getFastMethod("ftype"));
89         // fileStatClass.defineMethod("gid", callbackFactory.getMethod(""));
90
// fileStatClass.defineMethod("grpowned?", callbackFactory.getMethod(""));
91
fileStatClass.defineFastMethod("ino", callbackFactory.getFastMethod("ino"));
92         fileStatClass.defineFastMethod("mode", callbackFactory.getFastMethod("mode"));
93         fileStatClass.defineFastMethod("mtime", callbackFactory.getFastMethod("mtime"));
94         // fileStatClass.defineMethod("nlink", callbackFactory.getMethod(""));
95
// fileStatClass.defineMethod("owned?", callbackFactory.getMethod(""));
96
// fileStatClass.defineMethod("pipe?", callbackFactory.getMethod(""));
97
// fileStatClass.defineMethod("rdev", callbackFactory.getMethod(""));
98
// fileStatClass.defineMethod("rdev_major", callbackFactory.getMethod(""));
99
// fileStatClass.defineMethod("rdev_minor", callbackFactory.getMethod(""));
100
fileStatClass.defineFastMethod("readable?", callbackFactory.getFastMethod("readable_p"));
101         // fileStatClass.defineMethod("readable_real?", callbackFactory.getMethod(""));
102
// fileStatClass.defineMethod("setgid?", callbackFactory.getMethod(""));
103
// fileStatClass.defineMethod("setuid?", callbackFactory.getMethod(""));
104
fileStatClass.defineFastMethod("size", callbackFactory.getFastMethod("size"));
105         // fileStatClass.defineMethod("size?", callbackFactory.getMethod(""));
106
// fileStatClass.defineMethod("socket?", callbackFactory.getMethod(""));
107
// fileStatClass.defineMethod("sticky?", callbackFactory.getMethod(""));
108
fileStatClass.defineFastMethod("symlink?", callbackFactory.getFastMethod("symlink_p"));
109         // fileStatClass.defineMethod("uid", callbackFactory.getMethod(""));
110
fileStatClass.defineFastMethod("writable?", callbackFactory.getFastMethod("writable"));
111         // fileStatClass.defineMethod("writable_real?", callbackFactory.getMethod(""));
112
// fileStatClass.defineMethod("zero?", callbackFactory.getMethod(""));
113

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         // We cannot determine, so always return 4096 (better than blowing up)
131
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         // implementation to lowest common denominator...Windows has no file mode, but C ruby returns either 0100444 or 0100666
137
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         // We cannot determine this in Java, so we will always return false (better than blowing up)
151
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     // Limitation: We have no pure-java way of getting inode. webrick needs this defined to work.
172
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