001: /*
002: * @(#)VxWorksFileSystem.java 1.9 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.io;
029:
030: import java.security.AccessController;
031: import sun.security.action.GetPropertyAction;
032:
033: class VxWorksFileSystem extends FileSystem {
034:
035: private final char slash;
036: private final char semicolon;
037:
038: public VxWorksFileSystem() {
039: slash = ((String) AccessController
040: .doPrivileged(new GetPropertyAction("file.separator")))
041: .charAt(0);
042: semicolon = ((String) AccessController
043: .doPrivileged(new GetPropertyAction("path.separator")))
044: .charAt(0);
045: }
046:
047: /* -- Normalization and construction -- */
048:
049: public char getSeparator() {
050: return slash;
051: }
052:
053: public char getPathSeparator() {
054: return semicolon;
055: }
056:
057: /* A normal VxWorks pathname contains no duplicate slashes and does not end
058: with a slash. It may be the empty string. */
059:
060: /* Normalize the given pathname, whose length is len, starting at the given
061: offset; everything before this offset is already normal. */
062: private String normalize(String pathname, int len, int off) {
063: if (len == 0)
064: return pathname;
065: int n = len;
066: while ((n > 0) && (pathname.charAt(n - 1) == '/'))
067: n--;
068: if (n == 0)
069: return "/";
070: StringBuffer sb = new StringBuffer(pathname.length());
071: if (off > 0)
072: sb.append(pathname.substring(0, off));
073: char prevChar = 0;
074: for (int i = off; i < n; i++) {
075: char c = pathname.charAt(i);
076: if ((prevChar == '/') && (c == '/'))
077: continue;
078: sb.append(c);
079: prevChar = c;
080: }
081: return sb.toString();
082: }
083:
084: /* Check that the given pathname is normal. If not, invoke the real
085: normalizer on the part of the pathname that requires normalization.
086: This way we iterate through the whole pathname string only once. */
087: public String normalize(String pathname) {
088: int n = pathname.length();
089: if (n > 0 && pathname.charAt(0) == slash) {
090: // Remove the leading slash inserted by File.toURL().
091: // See bugid 4288740
092: String sub = pathname.substring(1);
093: if (isAbsolute(sub)) {
094: String s = normalize(sub, n - 1, 0);
095: return s;
096: }
097: }
098: char prevChar = 0;
099: for (int i = 0; i < n; i++) {
100: char c = pathname.charAt(i);
101: if ((prevChar == '/') && (c == '/'))
102: return normalize(pathname, n, i - 1);
103: prevChar = c;
104: }
105: if (prevChar == '/')
106: return normalize(pathname, n, n - 1);
107: return pathname;
108: }
109:
110: public int prefixLength(String pathname) {
111: int n = pathname.length();
112: if (n == 0)
113: return 0;
114: // Support <hostname>:<path> pathnames
115: int prefixEnd = pathname.indexOf(':');
116: int i = prefixEnd + 1;
117: if (i < n && pathname.charAt(i) == slash) {
118: return i + 1;
119: } else {
120: return i;
121: }
122: }
123:
124: public String resolve(String parent, String child) {
125: if (child.equals(""))
126: return parent;
127: if (child.charAt(0) == '/') {
128: if (parent.equals("/"))
129: return child;
130: return parent + child;
131: }
132: if (parent.equals("/"))
133: return parent + child;
134: return parent + '/' + child;
135: }
136:
137: public String getDefaultParent() {
138: return "/";
139: }
140:
141: /* -- Path operations -- */
142:
143: private boolean isAbsolute(String pathname) {
144: int pl = prefixLength(pathname);
145: return pl > 0 ? (pathname.charAt(pl - 1) == slash) : false;
146: }
147:
148: public boolean isAbsolute(File f) {
149: return isAbsolute(f.getPath());
150: }
151:
152: public String resolve(File f) {
153: if (isAbsolute(f))
154: return f.getPath();
155: return resolve(System.getProperty("user.dir"), f.getPath());
156: }
157:
158: public native String canonicalize(String path) throws IOException;
159:
160: /* -- Attribute accessors -- */
161:
162: public native int getBooleanAttributes0(File f);
163:
164: public int getBooleanAttributes(File f) {
165: int rv = getBooleanAttributes0(f);
166: String name = f.getName();
167: boolean hidden = (name.length() > 0) && (name.charAt(0) == '.');
168: return rv | (hidden ? BA_HIDDEN : 0);
169: }
170:
171: public native boolean checkAccess(File f, boolean write);
172:
173: public native long getLastModifiedTime(File f);
174:
175: public native long getLength(File f);
176:
177: /* -- File operations -- */
178:
179: public native boolean createFileExclusively(String path)
180: throws IOException;
181:
182: public native boolean delete(File f);
183:
184: public synchronized native boolean deleteOnExit(File f);
185:
186: public native String[] list(File f);
187:
188: public native boolean createDirectory(File f);
189:
190: public native boolean rename(File f1, File f2);
191:
192: public native boolean setLastModifiedTime(File f, long time);
193:
194: public native boolean setReadOnly(File f);
195:
196: /* -- Filesystem interface -- */
197:
198: public File[] listRoots() {
199: try {
200: SecurityManager security = System.getSecurityManager();
201: if (security != null) {
202: security.checkRead("/");
203: }
204: return new File[] { new File("/") };
205: } catch (SecurityException x) {
206: return new File[0];
207: }
208: }
209:
210: /* -- Basic infrastructure -- */
211:
212: public int compare(File f1, File f2) {
213: return f1.getPath().compareTo(f2.getPath());
214: }
215:
216: public int hashCode(File f) {
217: return f.getPath().hashCode() ^ 1234321;
218: }
219:
220: private static native void initIDs();
221:
222: static {
223: initIDs();
224: }
225:
226: }
|