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

Improved compatibility with the Java 7 FileSystem abstraction.

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