提交 6992e031 authored 作者: Thomas Mueller's avatar Thomas Mueller

Improved compatibility with the Java 7 FileSystem abstraction.

上级 19ae5c8b
......@@ -18,7 +18,7 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>-
<ul><li>Improved compatibility with the Java 7 FileSystem abstraction.
</li></ul>
<h2>Version 1.3.160 (2011-09-11)</h2>
......
......@@ -296,7 +296,7 @@ public class TraceSystem implements TraceWriter {
private boolean openWriter() {
if (printWriter == null) {
try {
IOUtils.createDirs(fileName);
IOUtils.createDirectories(IOUtils.getParent(fileName));
if (IOUtils.exists(fileName) && IOUtils.isReadOnly(fileName)) {
// read only database: don't log error if the trace file
// can't be opened
......
......@@ -22,6 +22,7 @@ import org.h2.message.DbException;
import org.h2.message.Trace;
import org.h2.message.TraceSystem;
import org.h2.store.fs.FileSystem;
import org.h2.util.IOUtils;
import org.h2.util.MathUtils;
import org.h2.util.NetUtils;
import org.h2.util.SortedProperties;
......@@ -302,7 +303,7 @@ public class FileLock implements Runnable {
private void lockSerialized() {
method = SERIALIZED;
fs.createDirs(fileName);
IOUtils.createDirectories(fs.getParent(fileName));
if (fs.createNewFile(fileName)) {
properties = new SortedProperties();
properties.setProperty("method", String.valueOf(method));
......@@ -325,7 +326,7 @@ public class FileLock implements Runnable {
properties = new SortedProperties();
properties.setProperty("method", String.valueOf(method));
setUniqueId();
fs.createDirs(fileName);
IOUtils.createDirectories(fs.getParent(fileName));
if (!fs.createNewFile(fileName)) {
waitUntilOld();
String m2 = load().getProperty("method", FILE);
......@@ -362,7 +363,7 @@ public class FileLock implements Runnable {
// if this returns 127.0.0.1,
// the computer is probably not networked
ipAddress = NetUtils.getLocalAddress();
fs.createDirs(fileName);
IOUtils.createDirectories(fs.getParent(fileName));
if (!fs.createNewFile(fileName)) {
waitUntilOld();
long read = fs.getLastModified(fileName);
......
......@@ -15,6 +15,7 @@ import org.h2.message.DbException;
import org.h2.security.SecureFileStore;
import org.h2.store.fs.FileObject;
import org.h2.store.fs.FileSystem;
import org.h2.util.IOUtils;
import org.h2.util.TempFileDeleter;
import org.h2.util.Utils;
......@@ -83,7 +84,7 @@ public class FileStore {
mode = "r";
this.mode = mode;
} else {
fs.createDirs(name);
IOUtils.createDirectories(fs.getParent(name));
}
file = fs.openFileObject(name, mode);
if (mode.length() > 2) {
......
......@@ -150,14 +150,6 @@ public abstract class FileSystem {
*/
public abstract String[] listFiles(String directory);
/**
* Delete a directory or file and all subdirectories and files.
*
* @param directory the directory
* @param tryOnly whether errors should be ignored
*/
public abstract void deleteRecursive(String directory, boolean tryOnly);
/**
* Check if a file is read-only.
*
......@@ -215,11 +207,11 @@ public abstract class FileSystem {
public abstract boolean canWrite(String fileName);
/**
* Create all required directories that are required for this file.
* Create a directory (all required parent directories already exist).
*
* @param fileName the file name (not directory name)
* @param directoryName the directory name
*/
public abstract void createDirs(String fileName);
public abstract void createDirectory(String directoryName);
/**
* Get the file name (without directory part).
......
......@@ -215,23 +215,6 @@ public class FileSystemDisk extends FileSystem {
}
}
public void deleteRecursive(String fileName, boolean tryOnly) {
fileName = translateFileName(fileName);
if (IOUtils.isDirectory(fileName)) {
String[] list = listFiles(fileName);
if (list != null) {
for (String l : list) {
deleteRecursive(l, tryOnly);
}
}
}
if (tryOnly) {
tryDelete(fileName);
} else {
delete(fileName);
}
}
public boolean isReadOnly(String fileName) {
fileName = translateFileName(fileName);
File f = new File(fileName);
......@@ -310,22 +293,18 @@ public class FileSystemDisk extends FileSystem {
}
}
public void createDirs(String fileName) {
fileName = translateFileName(fileName);
File f = new File(fileName);
public void createDirectory(String directoryName) {
directoryName = translateFileName(directoryName);
File f = new File(directoryName);
if (!f.exists()) {
String parent = f.getParent();
if (parent == null) {
return;
}
File dir = new File(parent);
File dir = new File(directoryName);
for (int i = 0; i < SysProperties.MAX_FILE_RETRY; i++) {
if ((dir.exists() && dir.isDirectory()) || dir.mkdirs()) {
if ((dir.exists() && dir.isDirectory()) || dir.mkdir()) {
return;
}
wait(i);
}
throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1, parent);
throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1, directoryName);
}
}
......@@ -348,7 +327,10 @@ public class FileSystemDisk extends FileSystem {
fileName = translateFileName(fileName);
try {
File file = new File(fileName);
createDirs(file.getAbsolutePath());
File parent = file.getParentFile();
if (parent != null) {
IOUtils.createDirectories(parent.getAbsolutePath());
}
FileOutputStream out = new FileOutputStream(fileName, append);
IOUtils.trace("openFileOutputStream", fileName, out);
return out;
......
......@@ -10,7 +10,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;
import org.h2.message.DbException;
import org.h2.util.New;
......@@ -69,6 +68,9 @@ public class FileSystemMemory extends FileSystem {
public boolean exists(String fileName) {
fileName = getCanonicalPath(fileName);
if (fileName.equals(PREFIX) || fileName.equals(PREFIX_LZF)) {
return true;
}
synchronized (MEMORY_FILES) {
return MEMORY_FILES.get(fileName) != null;
}
......@@ -103,21 +105,6 @@ public class FileSystemMemory extends FileSystem {
}
}
public void deleteRecursive(String fileName, boolean tryOnly) {
fileName = getCanonicalPath(fileName);
synchronized (MEMORY_FILES) {
Iterator<String> it = MEMORY_FILES.tailMap(fileName).keySet().iterator();
while (it.hasNext()) {
String name = it.next();
if (name.startsWith(fileName)) {
it.remove();
} else {
break;
}
}
}
}
public boolean isReadOnly(String fileName) {
return !getMemoryFile(fileName).canWrite();
}
......@@ -163,7 +150,7 @@ public class FileSystemMemory extends FileSystem {
return true;
}
public void createDirs(String fileName) {
public void createDirectory(String directoryName) {
// TODO directories are not really supported
}
......
......@@ -33,8 +33,8 @@ public abstract class FileSystemWrapper extends FileSystem {
return IOUtils.setReadOnly(unwrap(fileName));
}
public void createDirs(String fileName) {
IOUtils.createDirs(unwrap(fileName));
public void createDirectory(String directoryName) {
IOUtils.createDirectory(unwrap(directoryName));
}
public boolean createNewFile(String fileName) {
......@@ -50,10 +50,6 @@ public abstract class FileSystemWrapper extends FileSystem {
IOUtils.delete(unwrap(fileName));
}
public void deleteRecursive(String directory, boolean tryOnly) {
IOUtils.deleteRecursive(unwrap(directory), tryOnly);
}
public boolean exists(String fileName) {
return IOUtils.exists(unwrap(fileName));
}
......
......@@ -33,7 +33,7 @@ public class FileSystemZip extends FileSystem {
return false;
}
public void createDirs(String fileName) {
public void createDirectory(String directoryName) {
// ignore
}
......@@ -52,10 +52,6 @@ public class FileSystemZip extends FileSystem {
throw DbException.getUnsupportedException("write");
}
public void deleteRecursive(String fileName, boolean tryOnly) {
throw DbException.getUnsupportedException("write");
}
public boolean exists(String fileName) {
try {
String entryName = getEntryName(fileName);
......
......@@ -18,9 +18,9 @@ public interface Recorder {
int COPY = 3;
/**
* Create all parent directories.
* Create a directory.
*/
int CREATE_DIRS = 4;
int CREATE_DIRECTORY = 4;
/**
* Create a new file.
......@@ -37,21 +37,16 @@ public interface Recorder {
*/
int DELETE = 7;
/**
* Delete all files and directories recursively.
*/
int DELETE_RECURSIVE = 8;
/**
* Open a file output stream.
*/
int OPEN_OUTPUT_STREAM = 9;
int OPEN_OUTPUT_STREAM = 8;
/**
* Rename a file. The file name contains the source and the target file
* separated with a colon.
*/
int RENAME = 10;
int RENAME = 9;
/**
* Set the length of the file.
......
......@@ -41,9 +41,9 @@ public class RecordingFileSystem extends FileSystemWrapper {
RecordingFileSystem.recorder = recorder;
}
public void createDirs(String fileName) {
log(Recorder.CREATE_DIRS, unwrap(fileName));
super.createDirs(fileName);
public void createDirectories(String directoryName) {
log(Recorder.CREATE_DIRECTORY, unwrap(directoryName));
super.createDirectory(directoryName);
}
public boolean createNewFile(String fileName) {
......@@ -62,11 +62,6 @@ public class RecordingFileSystem extends FileSystemWrapper {
super.delete(fileName);
}
public void deleteRecursive(String directory, boolean tryOnly) {
log(Recorder.DELETE_RECURSIVE, unwrap(directory));
super.deleteRecursive(directory, tryOnly);
}
public FileObject openFileObject(String fileName, String mode) throws IOException {
return new RecordingFileObject(this, super.openFileObject(fileName, mode));
}
......
......@@ -735,12 +735,12 @@ public class IOUtils {
}
/**
* Create all required directories that are required for this file.
* Create a directory (all required parent directories already exist).
*
* @param fileName the file name (not directory name)
* @param directoryName the directory name
*/
public static void createDirs(String fileName) {
getFileSystem(fileName).createDirs(fileName);
public static void createDirectory(String directoryName) {
getFileSystem(directoryName).createDirectory(directoryName);
}
/**
......@@ -752,16 +752,6 @@ public class IOUtils {
getFileSystem(fileName).delete(fileName);
}
/**
* Delete a directory or file and all subdirectories and files.
*
* @param directory the directory
* @param tryOnly whether errors should be ignored
*/
public static void deleteRecursive(String directory, boolean tryOnly) {
getFileSystem(directory).deleteRecursive(directory, tryOnly);
}
/**
* Get the last modified date of a file.
*
......@@ -800,4 +790,38 @@ public class IOUtils {
}
}
/**
* Delete a directory or file and all subdirectories and files.
*
* @param path the path
* @param tryOnly whether errors should be ignored
*/
public static void deleteRecursive(String path, boolean tryOnly) {
if (exists(path)) {
if (isDirectory(path)) {
for (String s : listFiles(path)) {
deleteRecursive(s, tryOnly);
}
}
if (tryOnly) {
tryDelete(path);
} else {
delete(path);
}
}
}
/**
* Create the directory and all required parent directories.
*
* @param dir the directory name
*/
public static void createDirectories(String dir) {
String parent = getParent(dir);
if (!exists(parent)) {
createDirectories(parent);
}
createDirectory(dir);
}
}
......@@ -10,7 +10,6 @@ import java.sql.SQLException;
import java.util.Properties;
import org.h2.Driver;
import org.h2.engine.Constants;
import org.h2.store.fs.FileSystemDisk;
import org.h2.store.fs.RecordingFileSystem;
import org.h2.test.bench.TestPerformance;
import org.h2.test.db.TestAlter;
......@@ -165,6 +164,7 @@ import org.h2.test.utils.OutputCatcher;
import org.h2.test.utils.SelfDestructor;
import org.h2.tools.DeleteDbFiles;
import org.h2.tools.Server;
import org.h2.util.IOUtils;
import org.h2.util.Profiler;
import org.h2.util.Utils;
import org.h2.util.StringUtils;
......@@ -714,9 +714,9 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
*/
void beforeTest() throws SQLException {
Driver.load();
FileSystemDisk.getInstance().deleteRecursive(TestBase.BASE_TEST_DIR, true);
IOUtils.deleteRecursive(TestBase.BASE_TEST_DIR, true);
DeleteDbFiles.execute(TestBase.BASE_TEST_DIR, null, true);
FileSystemDisk.getInstance().deleteRecursive("trace.db", false);
IOUtils.deleteRecursive("trace.db", false);
if (networked) {
String[] args = ssl ? new String[] { "-tcpSSL", "true", "-tcpPort", "9192" } : new String[] { "-tcpPort",
"9192" };
......@@ -731,11 +731,11 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
}
private void afterTest() {
FileSystemDisk.getInstance().deleteRecursive("trace.db", true);
IOUtils.deleteRecursive("trace.db", true);
if (networked && server != null) {
server.stop();
}
FileSystemDisk.getInstance().deleteRecursive(TestBase.BASE_TEST_DIR, true);
IOUtils.deleteRecursive(TestBase.BASE_TEST_DIR, true);
}
/**
......
......@@ -15,7 +15,6 @@ import java.sql.Statement;
import java.util.Random;
import java.util.StringTokenizer;
import org.h2.fulltext.FullText;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.util.IOUtils;
import org.h2.util.Task;
......@@ -182,7 +181,7 @@ public class TestFullText extends TestBase {
FullText.dropAll(conn);
conn.close();
deleteDb("fullTextTransaction");
FileSystem.getInstance(getBaseDir()).deleteRecursive(getBaseDir() + "/fullTextTransaction", false);
IOUtils.deleteRecursive(getBaseDir() + "/fullTextTransaction", false);
}
private void testMultiThreaded(boolean lucene) throws Exception {
......@@ -332,7 +331,7 @@ public class TestFullText extends TestBase {
stat = conn.createStatement();
stat.execute("INSERT INTO TEST VALUES(3, 'Hello')");
conn.close();
FileSystem.getInstance(getBaseDir()).deleteRecursive(getBaseDir() + "/fullTextReopen", false);
IOUtils.deleteRecursive(getBaseDir() + "/fullTextReopen", false);
}
private void testPerformance(boolean lucene) throws SQLException {
......
......@@ -8,9 +8,9 @@ package org.h2.test.poweroff;
import java.io.InputStream;
import java.util.Random;
import org.h2.store.fs.FileSystemDisk;
import org.h2.test.TestBase;
import org.h2.test.synth.OutputCatcher;
import org.h2.util.IOUtils;
/**
* Run the TestRecover test case in a loop. The process is killed after 10
......@@ -33,7 +33,7 @@ public class TestRecoverKillLoop extends TestBase {
}
private void runTest(int count) throws Exception {
FileSystemDisk.getInstance().deleteRecursive("data/db", false);
IOUtils.deleteRecursive("data/db", false);
Random random = new Random(1);
for (int i = 0; i < count; i++) {
String[] procDef = {
......
......@@ -35,7 +35,7 @@ public class TestInit extends TestBase {
String init2 = getBaseDir() + "/test-init-2.sql";
// Create two scripts that we will run via "INIT"
IOUtils.createDirs(init1);
IOUtils.createDirectories(IOUtils.getParent(init1));
Writer w = new OutputStreamWriter(IOUtils.openFileOutputStream(init1, false));
......
......@@ -19,7 +19,6 @@ import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import org.h2.Driver;
import org.h2.message.DbException;
import org.h2.store.fs.FileObject;
......@@ -175,34 +174,17 @@ public class FileSystemDatabase extends FileSystem {
return true;
}
public void createDirs(String fileName) {
fileName = unwrap(fileName);
public void createDirectory(String directoryName) {
directoryName = unwrap(directoryName);
try {
String[] path = StringUtils.arraySplit(fileName, '/', false);
long parentId = 0;
int len = path.length;
if (fileName.endsWith("/")) {
len--;
}
len--;
for (int i = 1; i < len; i++) {
PreparedStatement prep = prepare("SELECT ID FROM FILES WHERE PARENTID=? AND NAME=?");
prep.setLong(1, parentId);
prep.setString(2, path[i]);
ResultSet rs = prep.executeQuery();
if (!rs.next()) {
prep = prepare("INSERT INTO FILES(NAME, PARENTID, LASTMODIFIED) VALUES(?, ?, ?)");
prep.setString(1, path[i]);
prep.setLong(2, parentId);
prep.setLong(3, System.currentTimeMillis());
prep.execute();
rs = prep.getGeneratedKeys();
rs.next();
parentId = rs.getLong(1);
} else {
parentId = rs.getLong(1);
}
}
String parent = getParent(directoryName);
String name = getFileName(directoryName);
long parentId = getId(parent, false);
PreparedStatement prep = prepare("INSERT INTO FILES(NAME, PARENTID, LASTMODIFIED) VALUES(?, ?, ?)");
prep.setString(1, name);
prep.setLong(2, parentId);
prep.setLong(3, System.currentTimeMillis());
prep.execute();
commit();
} catch (SQLException e) {
rollback();
......@@ -238,10 +220,6 @@ public class FileSystemDatabase extends FileSystem {
}
}
public void deleteRecursive(String fileName, boolean tryOnly) {
throw DbException.getUnsupportedException("db");
}
public boolean exists(String fileName) {
long id = getId(fileName, false);
return id >= 0;
......
......@@ -90,7 +90,7 @@ public class TestFileSystem extends TestBase {
fs.openFileOutputStream("memFS:data/test/a.txt", false).close();
String[] list = fs.listFiles("memFS:data/test");
assertEquals(1, list.length);
fs.deleteRecursive("memFS:", false);
IOUtils.deleteRecursive("memFS:", false);
}
private void testClasspath() throws IOException {
......@@ -217,7 +217,7 @@ public class TestFileSystem extends TestBase {
for (String s : fs.listFiles(fsBase)) {
fs.delete(s);
}
fs.createDirs(fsBase + "/test/x");
IOUtils.createDirectories(fsBase + "/test");
fs.delete(fsBase + "/test");
fs.delete(fsBase + "/test2");
assertTrue(fs.createNewFile(fsBase + "/test"));
......@@ -277,10 +277,10 @@ public class TestFileSystem extends TestBase {
assertTrue(fs.tryDelete(fsBase + "/test2"));
fs.delete(fsBase + "/test");
if (fsBase.indexOf(FileSystemMemory.PREFIX) < 0 && fsBase.indexOf(FileSystemMemory.PREFIX_LZF) < 0) {
fs.createDirs(fsBase + "/testDir/test");
IOUtils.createDirectories(fsBase + "/testDir");
assertTrue(fs.isDirectory(fsBase + "/testDir"));
if (!fsBase.startsWith("jdbc:")) {
fs.deleteRecursive(fsBase + "/testDir", false);
IOUtils.deleteRecursive(fsBase + "/testDir", false);
assertTrue(!fs.exists(fsBase + "/testDir"));
}
}
......
......@@ -53,7 +53,7 @@ public class TestStreams extends TestBase {
private void testLZFStreamClose() throws IOException {
String fileName = getBaseDir() + "/temp";
IOUtils.createDirs(fileName);
IOUtils.createDirectories(IOUtils.getParent(fileName));
OutputStream fo = IOUtils.openFileOutputStream(fileName, false);
LZFOutputStream out = new LZFOutputStream(fo);
out.write("Hello".getBytes());
......
......@@ -65,9 +65,9 @@ public class DebugFileSystem extends FileSystemWrapper {
return super.canWrite(fileName);
}
public void createDirs(String fileName) {
trace(fileName, "createDirs");
super.createDirs(fileName);
public void createDirectory(String directoryName) {
trace(directoryName, "createDirectory");
super.createDirectory(directoryName);
}
public boolean createNewFile(String fileName) {
......@@ -86,11 +86,6 @@ public class DebugFileSystem extends FileSystemWrapper {
super.delete(fileName);
}
public void deleteRecursive(String directory, boolean tryOnly) {
trace(directory, "deleteRecursive", tryOnly);
super.deleteRecursive(directory, tryOnly);
}
public boolean exists(String fileName) {
trace(fileName, "exists");
return super.exists(fileName);
......
......@@ -225,9 +225,9 @@ public class FileShell extends Tool {
println(buff.toString());
}
} else if ("mkdir".equals(c)) {
String dir = getFile(list[i++] + "/dummy");
String dir = getFile(list[i++]);
end(list, i);
IOUtils.createDirs(dir);
IOUtils.createDirectories(dir);
} else if ("mv".equals(c)) {
String source = getFile(list[i++]);
String target = getFile(list[i++]);
......
......@@ -48,7 +48,7 @@ public class FileSystemZip2 extends FileSystem {
return false;
}
public void createDirs(String fileName) {
public void createDirectory(String directoryName) {
// ignore
}
......@@ -67,10 +67,6 @@ public class FileSystemZip2 extends FileSystem {
throw DbException.getUnsupportedException("write");
}
public void deleteRecursive(String fileName, boolean tryOnly) {
throw DbException.getUnsupportedException("write");
}
public boolean exists(String fileName) {
try {
String entryName = getEntryName(fileName);
......
......@@ -16,6 +16,7 @@ import java.net.ServerSocket;
import java.net.Socket;
import org.h2.engine.Constants;
import org.h2.store.fs.FileSystem;
import org.h2.util.IOUtils;
import org.h2.util.StringUtils;
/**
......@@ -355,7 +356,7 @@ public class FtpControl extends Thread {
boolean ok = false;
if (!readonly) {
try {
fs.createDirs(fileName + "/x");
IOUtils.createDirectories(fileName);
reply(257, StringUtils.quoteIdentifier(param) + " directory");
ok = true;
} catch (Exception e) {
......
......@@ -354,7 +354,7 @@ public class FtpServer extends Tool implements Service {
public void start() {
fs = FileSystem.getInstance(root);
root = fs.getCanonicalPath(root);
fs.createDirs(root + "/x");
IOUtils.createDirectories(root);
serverSocket = NetUtils.createServerSocket(port, false);
port = serverSocket.getLocalPort();
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论