001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.util.FTFileUtil
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.util;
023:
024: import java.io.FileWriter;
025: import java.io.File;
026: import java.security.AccessController;
027: import java.security.PrivilegedAction;
028: import java.security.PrivilegedActionException;
029: import java.security.PrivilegedExceptionAction;
030:
031: /**
032: Convience functions for performing file manipulations
033: in ij scripts.
034: */
035: public class FTFileUtil {
036: /**
037: Create a file.
038:
039: @param name the file's name.
040: @length the number of bytes of test data in the file.
041: @exception Exception oops.
042: */
043: public static void mkFile(String fileName, int length)
044: throws Exception {
045: FileWriter fw = new FileWriter(fileName);
046: int offset = 0;
047: String data = "Amber!";
048: for (int ix = 0; ix < length; ix++) {
049: fw.write(data, offset, 1);
050: offset++;
051: if (offset >= data.length())
052: offset = 0;
053: }
054: fw.close();
055: }
056:
057: /**
058: * rename a file.
059: * This method is called by some tests through a SQL procedure:
060: * RENAME_FILE(LOCATION VARCHAR(32000), NAME VARCHAR(32000),
061: * NEW_NAME VARCHAR(32000))
062: * @param location location of the file
063: * @param name the file's name
064: * @param newName the file's new name
065: */
066: public static void renameFile(String location, String name,
067: String newName) throws Exception {
068: final File src = new File(location, name);
069: final File dst = new File(location, newName);
070:
071: // needs to run in a privileged block as it will be
072: // called through a SQL statement and thus a generated
073: // class. The generated class on the stack has no permissions
074: // granted to it.
075: AccessController.doPrivileged(new PrivilegedExceptionAction() {
076: public Object run() throws Exception {
077: if (!src.renameTo(dst)) {
078: throw new Exception("unable to rename File: "
079: + src.getAbsolutePath() + " To: "
080: + dst.getAbsolutePath());
081: }
082:
083: return null; // nothing to return
084: }
085: });
086: }
087:
088: /**
089: * Check if a file exists ?
090: *
091: * This method is called by some tests through a SQL function:
092: * fileExists(fileName varchar(128))returns VARCHAR(100)
093: *
094: * @param name the file's name.
095: * @return <tt>"true"</tt> if the given file exists
096: * <tt>"false"</tt> otherwise.
097: * @exception Exception if any exception occurs
098: */
099: public static String fileExists(String fileName)
100: throws PrivilegedActionException {
101: final File fl = new File(fileName);
102:
103: // needs to run in a privileged block as it will be
104: // called through a SQL statement and thus a generated
105: // class. The generated class on the stack has no permissions
106: // granted to it.
107:
108: return (String) AccessController
109: .doPrivileged(new PrivilegedExceptionAction() {
110: public Object run() {
111: if (fl.exists()) {
112: return "true";
113: } else {
114: return "false";
115: }
116: }
117: });
118: }
119:
120: /**
121: * Remove a directory and all of its contents.
122: *
123: * @param name the file's name.
124: * @return <tt>true</tt> if the omplete directory was removed
125: * <tt>false</tt> otherwise.f false is returned then some of
126: * the files in the directory may have been removed.
127: */
128:
129: private static boolean removeDirectory(File directory) {
130:
131: if (directory == null)
132: return false;
133: if (!directory.exists())
134: return true;
135: if (!directory.isDirectory())
136: return false;
137:
138: String[] list = directory.list();
139:
140: if (list != null) {
141: for (int i = 0; i < list.length; i++) {
142: File entry = new File(directory, list[i]);
143:
144: if (entry.isDirectory()) {
145: if (!removeDirectory(entry))
146: return false;
147: } else {
148: if (!entry.delete())
149: return false;
150: }
151: }
152: }
153:
154: return directory.delete();
155: }
156:
157: /**
158: * Remove a directory and all of its contents.
159: * This method is called by some tests through a SQL function:
160: * removeDirectory(fileName varchar(128)) returns VARCHAR(100)
161: *
162: * @param name the file's name.
163: * @return <tt>"true"</tt> if the omplete directory was removed
164: * <tt>"false"</tt> otherwise.f false is returned then some of
165: * the files in the directory may have been removed.
166: */
167:
168: public static String removeDirectory(final String directory)
169: throws PrivilegedActionException {
170: // needs to run in a privileged block as it will be
171: // called through a SQL statement and thus a generated
172: // class. The generated class on the stack has no permissions
173: // granted to it.
174:
175: return (String) AccessController
176: .doPrivileged(new PrivilegedExceptionAction() {
177: public Object run() {
178: return (removeDirectory(new File(directory)) ? "true"
179: : "false");
180: }
181: });
182: }
183:
184: }
|