提交 e78bb783 authored 作者: Thomas Mueller's avatar Thomas Mueller

Improved compatibility with the Java 7 FileSystem abstraction.

上级 6992e031
......@@ -21,7 +21,6 @@ import org.h2.engine.SessionRemote;
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;
......@@ -79,11 +78,6 @@ public class FileLock implements Runnable {
*/
private volatile ServerSocket serverSocket;
/**
* The file system.
*/
private FileSystem fs;
/**
* The number of milliseconds to sleep after checking a file.
*/
......@@ -125,7 +119,6 @@ public class FileLock implements Runnable {
* @throws DbException if locking was not successful
*/
public synchronized void lock(int fileLockMethod) {
this.fs = FileSystem.getInstance(fileName);
checkServer();
if (locked) {
DbException.throwInternalError("already locked");
......@@ -157,7 +150,7 @@ public class FileLock implements Runnable {
try {
if (fileName != null) {
if (load().equals(properties)) {
fs.delete(fileName);
IOUtils.delete(fileName);
}
}
if (serverSocket != null) {
......@@ -201,13 +194,13 @@ public class FileLock implements Runnable {
*/
public Properties save() {
try {
OutputStream out = fs.openFileOutputStream(fileName, false);
OutputStream out = IOUtils.openFileOutputStream(fileName, false);
try {
properties.store(out, MAGIC);
} finally {
out.close();
}
lastWrite = fs.getLastModified(fileName);
lastWrite = IOUtils.getLastModified(fileName);
if (trace.isDebugEnabled()) {
trace.debug("save " + properties);
}
......@@ -271,7 +264,7 @@ public class FileLock implements Runnable {
private void waitUntilOld() {
for (int i = 0; i < 2 * TIME_GRANULARITY / SLEEP_GAP; i++) {
long last = fs.getLastModified(fileName);
long last = IOUtils.getLastModified(fileName);
long dist = System.currentTimeMillis() - last;
if (dist < -TIME_GRANULARITY) {
// lock file modified in the future -
......@@ -303,8 +296,8 @@ public class FileLock implements Runnable {
private void lockSerialized() {
method = SERIALIZED;
IOUtils.createDirectories(fs.getParent(fileName));
if (fs.createNewFile(fileName)) {
IOUtils.createDirectories(IOUtils.getParent(fileName));
if (IOUtils.createNewFile(fileName)) {
properties = new SortedProperties();
properties.setProperty("method", String.valueOf(method));
setUniqueId();
......@@ -326,8 +319,8 @@ public class FileLock implements Runnable {
properties = new SortedProperties();
properties.setProperty("method", String.valueOf(method));
setUniqueId();
IOUtils.createDirectories(fs.getParent(fileName));
if (!fs.createNewFile(fileName)) {
IOUtils.createDirectories(IOUtils.getParent(fileName));
if (!IOUtils.createNewFile(fileName)) {
waitUntilOld();
String m2 = load().getProperty("method", FILE);
if (!m2.equals(FILE)) {
......@@ -338,8 +331,8 @@ public class FileLock implements Runnable {
if (!load().equals(properties)) {
throw getExceptionAlreadyInUse("Locked by another process");
}
fs.delete(fileName);
if (!fs.createNewFile(fileName)) {
IOUtils.delete(fileName);
if (!IOUtils.createNewFile(fileName)) {
throw getExceptionFatal("Another process was faster", null);
}
}
......@@ -363,10 +356,10 @@ public class FileLock implements Runnable {
// if this returns 127.0.0.1,
// the computer is probably not networked
ipAddress = NetUtils.getLocalAddress();
IOUtils.createDirectories(fs.getParent(fileName));
if (!fs.createNewFile(fileName)) {
IOUtils.createDirectories(IOUtils.getParent(fileName));
if (!IOUtils.createNewFile(fileName)) {
waitUntilOld();
long read = fs.getLastModified(fileName);
long read = IOUtils.getLastModified(fileName);
Properties p2 = load();
String m2 = p2.getProperty("method", SOCKET);
if (m2.equals(FILE)) {
......@@ -400,11 +393,11 @@ public class FileLock implements Runnable {
throw getExceptionFatal("IOException", null);
}
}
if (read != fs.getLastModified(fileName)) {
if (read != IOUtils.getLastModified(fileName)) {
throw getExceptionFatal("Concurrent update", null);
}
fs.delete(fileName);
if (!fs.createNewFile(fileName)) {
IOUtils.delete(fileName);
if (!IOUtils.createNewFile(fileName)) {
throw getExceptionFatal("Another process was faster", null);
}
}
......@@ -487,7 +480,7 @@ public class FileLock implements Runnable {
while (fileName != null) {
// trace.debug("watchdog check");
try {
if (!fs.exists(fileName) || fs.getLastModified(fileName) != lastWrite) {
if (!IOUtils.exists(fileName) || IOUtils.getLastModified(fileName) != lastWrite) {
save();
}
Thread.sleep(sleep);
......
......@@ -14,7 +14,6 @@ import org.h2.engine.Constants;
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;
......@@ -71,7 +70,6 @@ public class FileStore {
* @param mode the access mode ("r", "rw", "rws", "rwd")
*/
protected FileStore(DataHandler handler, String name, String mode) {
FileSystem fs = FileSystem.getInstance(name);
this.handler = handler;
this.name = name;
this.mode = mode;
......@@ -79,14 +77,14 @@ public class FileStore {
tempFileDeleter = handler.getTempFileDeleter();
}
try {
boolean exists = fs.exists(name);
if (exists && !fs.canWrite(name)) {
boolean exists = IOUtils.exists(name);
if (exists && !IOUtils.canWrite(name)) {
mode = "r";
this.mode = mode;
} else {
IOUtils.createDirectories(fs.getParent(name));
IOUtils.createDirectories(IOUtils.getParent(name));
}
file = fs.openFileObject(name, mode);
file = IOUtils.openFileObject(name, mode);
if (mode.length() > 2) {
synchronousMode = true;
}
......@@ -494,7 +492,7 @@ public class FileStore {
*/
public void openFile() throws IOException {
if (file == null) {
file = FileSystem.getInstance(name).openFileObject(name, mode);
file = IOUtils.openFileObject(name, mode);
file.seek(filePos);
}
}
......
......@@ -24,8 +24,6 @@ import java.util.Random;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.engine.Constants;
import org.h2.store.fs.FileObject;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.tools.Csv;
import org.h2.util.IOUtils;
......@@ -223,17 +221,16 @@ public class TestCsv extends TestBase {
deleteDb("csv");
String fileName = getBaseDir() + "/testNull.csv";
FileSystem fs = FileSystem.getInstance(fileName);
fs.delete(fileName);
IOUtils.delete(fileName);
FileObject file = fs.openFileObject(fileName, "rw");
OutputStream out = IOUtils.openFileOutputStream(fileName, false);
String csvContent = "\"A\",\"B\",\"C\",\"D\"\n\\N,\"\",\"\\N\",";
byte[] b = csvContent.getBytes("UTF-8");
file.write(b, 0, b.length);
file.close();
out.write(b, 0, b.length);
out.close();
Csv csv = Csv.getInstance();
csv.setNullString("\\N");
ResultSet rs = csv.read(file.getName(), null, "UTF8");
ResultSet rs = csv.read(fileName, null, "UTF8");
ResultSetMetaData meta = rs.getMetaData();
assertEquals(4, meta.getColumnCount());
assertEquals("A", meta.getColumnLabel(1));
......@@ -251,16 +248,16 @@ public class TestCsv extends TestBase {
Connection conn = getConnection("csv");
Statement stat = conn.createStatement();
stat.execute("call csvwrite('" + file.getName() +
stat.execute("call csvwrite('" + fileName +
"', 'select NULL as a, '''' as b, ''\\N'' as c, NULL as d', 'UTF8', ',', '\"', NULL, '\\N', '\n')");
InputStreamReader reader = new InputStreamReader(fs.openFileInputStream(fileName));
InputStreamReader reader = new InputStreamReader(IOUtils.openFileInputStream(fileName));
// on read, an empty string is treated like null,
// but on write a null is always written with the nullString
String data = IOUtils.readStringAndClose(reader, -1);
assertEquals(csvContent + "\\N", data.trim());
conn.close();
fs.delete(fileName);
IOUtils.delete(fileName);
}
private void testRandomData() throws SQLException {
......@@ -336,12 +333,11 @@ public class TestCsv extends TestBase {
private void testFieldDelimiter() throws Exception {
String fileName = getBaseDir() + "/test.csv";
String fileName2 = getBaseDir() + "/test2.csv";
FileSystem fs = FileSystem.getInstance(fileName);
fs.delete(fileName);
FileObject file = fs.openFileObject(fileName, "rw");
IOUtils.delete(fileName);
OutputStream out = IOUtils.openFileOutputStream(fileName, false);
byte[] b = "'A'; 'B'\n\'It\\'s nice\'; '\nHello\\*\n'".getBytes();
file.write(b, 0, b.length);
file.close();
out.write(b, 0, b.length);
out.close();
Connection conn = getConnection("csv");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from csvread('" + fileName + "', null, null, ';', '''', '\\')");
......@@ -367,8 +363,8 @@ public class TestCsv extends TestBase {
assertEquals("\nHello*\n", rs.getString(2));
assertFalse(rs.next());
conn.close();
fs.delete(fileName);
fs.delete(fileName2);
IOUtils.delete(fileName);
IOUtils.delete(fileName2);
}
private void testPipe() throws SQLException {
......@@ -414,12 +410,11 @@ public class TestCsv extends TestBase {
private void testRead() throws Exception {
String fileName = getBaseDir() + "/test.csv";
FileSystem fs = FileSystem.getInstance(fileName);
fs.delete(fileName);
FileObject file = fs.openFileObject(fileName, "rw");
IOUtils.delete(fileName);
OutputStream out = IOUtils.openFileOutputStream(fileName, false);
byte[] b = "a,b,c,d\n201,-2,0,18\n, \"abc\"\"\" ,,\"\"\n 1 ,2 , 3, 4 \n5, 6, 7, 8".getBytes();
file.write(b, 0, b.length);
file.close();
out.write(b, 0, b.length);
out.close();
ResultSet rs = Csv.getInstance().read(fileName, null, "UTF8");
ResultSetMetaData meta = rs.getMetaData();
assertEquals(4, meta.getColumnCount());
......@@ -456,7 +451,7 @@ public class TestCsv extends TestBase {
// 201,2,0,18
// 201,2,0,18
// 201,2,0,18
fs.delete(fileName);
IOUtils.delete(fileName);
}
private void testWriteRead() throws SQLException {
......
......@@ -16,7 +16,6 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import org.h2.constant.ErrorCode;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.util.IOUtils;
......@@ -637,8 +636,8 @@ public class TestLinkedTable extends TestBase {
for (String file : files) {
String name = IOUtils.getFileName(file);
if ((name.startsWith("testLinkedTableInReadOnlyDb")) && (!name.endsWith(".trace.db"))) {
FileSystem.getInstance(file).setReadOnly(file);
boolean isReadOnly = FileSystem.getInstance(file).isReadOnly(file);
IOUtils.setReadOnly(file);
boolean isReadOnly = IOUtils.isReadOnly(file);
if (!isReadOnly) {
fail("File " + file + " is not read only. Can't test it.");
}
......
......@@ -12,15 +12,13 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.api.DatabaseEventListener;
import org.h2.constant.ErrorCode;
import org.h2.store.fs.FileObject;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.tools.Restore;
import org.h2.util.Task;
import org.h2.util.IOUtils;
import org.h2.util.Task;
/**
* Tests opening and closing a database.
......@@ -64,17 +62,17 @@ public class TestOpenClose extends TestBase implements DatabaseEventListener {
if (config.memory || config.reopen) {
return;
}
FileSystem.getInstance("split:").delete("split:" + getBaseDir() + "/openClose2.h2.db");
IOUtils.delete("split:" + getBaseDir() + "/openClose2.h2.db");
Connection conn;
conn = DriverManager.getConnection("jdbc:h2:split:18:" + getBaseDir() + "/openClose2");
conn.createStatement().execute("create table test(id int, name varchar) as select 1, space(1000000)");
conn.close();
FileObject f = FileSystem.getInstance(getBaseDir()).openFileObject(getBaseDir() + "/openClose2.h2.db.1.part", "rw");
FileObject f = IOUtils.openFileObject(getBaseDir() + "/openClose2.h2.db.1.part", "rw");
f.setFileLength(f.length() * 2);
f.close();
assertThrows(ErrorCode.IO_EXCEPTION_2, this).
getConnection("jdbc:h2:split:18:" + getBaseDir() + "/openClose2");
FileSystem.getInstance("split:").delete("split:" + getBaseDir() + "/openClose2.h2.db");
IOUtils.delete("split:" + getBaseDir() + "/openClose2.h2.db");
}
private void testCloseDelay() throws Exception {
......
......@@ -15,8 +15,8 @@ import java.sql.Statement;
import java.util.ArrayList;
import org.h2.constant.ErrorCode;
import org.h2.store.FileLister;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.util.IOUtils;
/**
* Test for the read-only database feature.
......@@ -127,10 +127,9 @@ public class TestReadOnly extends TestBase {
}
private void setReadOnly() {
FileSystem fs = FileSystem.getInstance(getBaseDir());
ArrayList<String> list = FileLister.getDatabaseFiles(getBaseDir(), "readonly", true);
for (String fileName : list) {
fs.setReadOnly(fileName);
IOUtils.setReadOnly(fileName);
}
}
......
......@@ -20,7 +20,6 @@ import java.util.Random;
import org.h2.compress.CompressLZF;
import org.h2.compress.Compressor;
import org.h2.engine.Constants;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.tools.CompressTool;
import org.h2.util.IOUtils;
......@@ -158,7 +157,7 @@ public class TestCompress extends TestBase {
for (int j = 0; j < 4; j++) {
long time = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
InputStream in = FileSystem.getInstance("memFS:").openFileInputStream("memFS:compress.h2.db");
InputStream in = IOUtils.openFileInputStream("memFS:compress.h2.db");
int total = 0;
while (true) {
int len = in.read(buff);
......@@ -174,7 +173,7 @@ public class TestCompress extends TestBase {
for (int j = 0; j < 4; j++) {
ArrayList<byte[]> comp = New.arrayList();
InputStream in = FileSystem.getInstance("memFS:").openFileInputStream("memFS:compress.h2.db");
InputStream in = IOUtils.openFileInputStream("memFS:compress.h2.db");
while (true) {
int len = in.read(buff);
if (len < 0) {
......
......@@ -17,7 +17,6 @@ import java.util.Arrays;
import java.util.List;
import org.h2.constant.ErrorCode;
import org.h2.jdbc.JdbcConnection;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.util.IOUtils;
import org.h2.util.SortedProperties;
......@@ -605,9 +604,7 @@ public class TestFileLockSerialized extends TestBase {
stat.execute("insert into test values(0)");
conn.close();
FileSystem fs = FileSystem.getInstance(getBaseDir());
List<String> filesWithoutSerialized = Arrays.asList(fs.listFiles(getBaseDir()));
List<String> filesWithoutSerialized = Arrays.asList(IOUtils.listFiles(getBaseDir()));
deleteDb("fileLockSerialized");
// with serialized
......@@ -619,7 +616,7 @@ public class TestFileLockSerialized extends TestBase {
stat.execute("insert into test values(0)");
conn.close();
List<String> filesWithSerialized = Arrays.asList(fs.listFiles(getBaseDir()));
List<String> filesWithSerialized = Arrays.asList(IOUtils.listFiles(getBaseDir()));
if (filesWithoutSerialized.size() != filesWithSerialized.size()) {
for (int i = 0; i < filesWithoutSerialized.size(); i++) {
if (!filesWithSerialized.contains(filesWithoutSerialized.get(i))) {
......
......@@ -20,7 +20,6 @@ import java.sql.Statement;
import java.util.Random;
import org.h2.dev.fs.FileSystemCrypt;
import org.h2.store.fs.FileObject;
import org.h2.store.fs.FileSystem;
import org.h2.store.fs.FileSystemMemory;
import org.h2.test.TestBase;
import org.h2.tools.Backup;
......@@ -55,7 +54,7 @@ public class TestFileSystem extends TestBase {
testDatabaseInJar();
// set default part size to 1 << 10
String f = "split:10:" + getBaseDir() + "/fs";
FileSystem.getInstance(f).getCanonicalPath(f);
IOUtils.getCanonicalPath(f);
testFileSystem(getBaseDir() + "/fs");
testFileSystem(FileSystemMemory.PREFIX);
FileSystemDatabase fs = FileSystemDatabase.register("jdbc:h2:mem:fs");
......@@ -85,10 +84,8 @@ public class TestFileSystem extends TestBase {
}
private void testMemFsDir() throws IOException {
String dir = "memFS:data/test";
FileSystem fs = FileSystem.getInstance(dir);
fs.openFileOutputStream("memFS:data/test/a.txt", false).close();
String[] list = fs.listFiles("memFS:data/test");
IOUtils.openFileOutputStream("memFS:data/test/a.txt", false).close();
String[] list = IOUtils.listFiles("memFS:data/test");
assertEquals(1, list.length);
IOUtils.deleteRecursive("memFS:", false);
}
......@@ -173,12 +170,11 @@ public class TestFileSystem extends TestBase {
conn.close();
deleteDb("fsJar");
FileSystem fs = FileSystem.getInstance("zip:" + getBaseDir() + "/fsJar.zip");
for (String f : fs.listFiles("zip:" + getBaseDir() + "/fsJar.zip")) {
assertTrue(fs.isAbsolute(f));
assertTrue(!fs.isDirectory(f));
assertTrue(fs.length(f) > 0);
assertTrue(f.endsWith(fs.getFileName(f)));
for (String f : IOUtils.listFiles("zip:" + getBaseDir() + "/fsJar.zip")) {
assertTrue(IOUtils.isAbsolute(f));
assertTrue(!IOUtils.isDirectory(f));
assertTrue(IOUtils.length(f) > 0);
assertTrue(f.endsWith(IOUtils.getFileName(f)));
}
String urlJar = "jdbc:h2:zip:" + getBaseDir() + "/fsJar.zip!/fsJar";
conn = DriverManager.getConnection(urlJar, "sa", "sa");
......@@ -199,8 +195,7 @@ public class TestFileSystem extends TestBase {
}
private void testUserHome() {
FileSystem fs = FileSystem.getInstance("~/test");
String fileName = fs.getCanonicalPath("~/test");
String fileName = IOUtils.getCanonicalPath("~/test");
String userDir = System.getProperty("user.home");
assertTrue(fileName.startsWith(userDir));
}
......@@ -212,17 +207,16 @@ public class TestFileSystem extends TestBase {
}
private void testSimple(String fsBase) throws Exception {
FileSystem fs = FileSystem.getInstance(fsBase);
long time = System.currentTimeMillis();
for (String s : fs.listFiles(fsBase)) {
fs.delete(s);
for (String s : IOUtils.listFiles(fsBase)) {
IOUtils.delete(s);
}
IOUtils.createDirectories(fsBase + "/test");
fs.delete(fsBase + "/test");
fs.delete(fsBase + "/test2");
assertTrue(fs.createNewFile(fsBase + "/test"));
assertTrue(fs.canWrite(fsBase + "/test"));
FileObject fo = fs.openFileObject(fsBase + "/test", "rw");
IOUtils.delete(fsBase + "/test");
IOUtils.delete(fsBase + "/test2");
assertTrue(IOUtils.createNewFile(fsBase + "/test"));
assertTrue(IOUtils.canWrite(fsBase + "/test"));
FileObject fo = IOUtils.openFileObject(fsBase + "/test", "rw");
byte[] buffer = new byte[10000];
Random random = new Random(1);
random.nextBytes(buffer);
......@@ -232,36 +226,36 @@ public class TestFileSystem extends TestBase {
assertEquals(20000, fo.getFilePointer());
assertThrows(EOFException.class, fo).readFully(buffer, 0, 1);
assertEquals(fsBase + "/test", fo.getName().replace('\\', '/'));
assertEquals("test", fs.getFileName(fo.getName()));
assertEquals(fsBase, fs.getParent(fo.getName()).replace('\\', '/'));
assertEquals("test", IOUtils.getFileName(fo.getName()));
assertEquals(fsBase, IOUtils.getParent(fo.getName()).replace('\\', '/'));
fo.tryLock();
fo.releaseLock();
assertEquals(10000, fo.length());
fo.close();
assertEquals(10000, fs.length(fsBase + "/test"));
fo = fs.openFileObject(fsBase + "/test", "r");
assertEquals(10000, IOUtils.length(fsBase + "/test"));
fo = IOUtils.openFileObject(fsBase + "/test", "r");
byte[] test = new byte[10000];
fo.readFully(test, 0, 10000);
assertEquals(buffer, test);
assertThrows(IOException.class, fo).write(test, 0, 10);
assertThrows(IOException.class, fo).setFileLength(10);
fo.close();
long lastMod = fs.getLastModified(fsBase + "/test");
long lastMod = IOUtils.getLastModified(fsBase + "/test");
if (lastMod < time - 1999) {
// at most 2 seconds difference
assertEquals(time, lastMod);
}
assertEquals(10000, fs.length(fsBase + "/test"));
String[] list = fs.listFiles(fsBase);
assertEquals(10000, IOUtils.length(fsBase + "/test"));
String[] list = IOUtils.listFiles(fsBase);
assertEquals(1, list.length);
assertTrue(list[0].endsWith("test"));
IOUtils.copy(fsBase + "/test", fsBase + "/test3");
fs.rename(fsBase + "/test3", fsBase + "/test2");
assertTrue(!fs.exists(fsBase + "/test3"));
assertTrue(fs.exists(fsBase + "/test2"));
assertEquals(10000, fs.length(fsBase + "/test2"));
IOUtils.rename(fsBase + "/test3", fsBase + "/test2");
assertTrue(!IOUtils.exists(fsBase + "/test3"));
assertTrue(IOUtils.exists(fsBase + "/test2"));
assertEquals(10000, IOUtils.length(fsBase + "/test2"));
byte[] buffer2 = new byte[10000];
InputStream in = fs.openFileInputStream(fsBase + "/test2");
InputStream in = IOUtils.openFileInputStream(fsBase + "/test2");
int pos = 0;
while (true) {
int l = in.read(buffer2, pos, Math.min(10000 - pos, 1000));
......@@ -274,14 +268,14 @@ public class TestFileSystem extends TestBase {
assertEquals(10000, pos);
assertEquals(buffer, buffer2);
assertTrue(fs.tryDelete(fsBase + "/test2"));
fs.delete(fsBase + "/test");
assertTrue(IOUtils.tryDelete(fsBase + "/test2"));
IOUtils.delete(fsBase + "/test");
if (fsBase.indexOf(FileSystemMemory.PREFIX) < 0 && fsBase.indexOf(FileSystemMemory.PREFIX_LZF) < 0) {
IOUtils.createDirectories(fsBase + "/testDir");
assertTrue(fs.isDirectory(fsBase + "/testDir"));
assertTrue(IOUtils.isDirectory(fsBase + "/testDir"));
if (!fsBase.startsWith("jdbc:")) {
IOUtils.deleteRecursive(fsBase + "/testDir", false);
assertTrue(!fs.exists(fsBase + "/testDir"));
assertTrue(!IOUtils.exists(fsBase + "/testDir"));
}
}
}
......@@ -292,14 +286,13 @@ public class TestFileSystem extends TestBase {
private void testRandomAccess(String fsBase, int seed) throws Exception {
StringBuilder buff = new StringBuilder();
FileSystem fs = FileSystem.getInstance(fsBase);
String s = fs.createTempFile(fsBase + "/tmp", ".tmp", false, false);
String s = IOUtils.createTempFile(fsBase + "/tmp", ".tmp", false, false);
File file = new File(TestBase.BASE_TEST_DIR + "/tmp");
file.getParentFile().mkdirs();
file.delete();
RandomAccessFile ra = new RandomAccessFile(file, "rw");
fs.delete(s);
FileObject f = fs.openFileObject(s, "rw");
IOUtils.delete(s);
FileObject f = IOUtils.openFileObject(s, "rw");
assertThrows(EOFException.class, f).readFully(new byte[1], 0, 1);
f.sync();
Random random = new Random(seed);
......@@ -367,7 +360,7 @@ public class TestFileSystem extends TestBase {
f.close();
ra.close();
ra = new RandomAccessFile(file, "rw");
f = fs.openFileObject(s, "rw");
f = IOUtils.openFileObject(s, "rw");
assertEquals(ra.length(), f.length());
break;
}
......@@ -381,22 +374,21 @@ public class TestFileSystem extends TestBase {
f.close();
ra.close();
file.delete();
fs.delete(s);
IOUtils.delete(s);
}
}
private void testTempFile(String fsBase) throws Exception {
int len = 10000;
FileSystem fs = FileSystem.getInstance(fsBase);
String s = fs.createTempFile(fsBase + "/tmp", ".tmp", false, false);
OutputStream out = fs.openFileOutputStream(s, false);
String s = IOUtils.createTempFile(fsBase + "/tmp", ".tmp", false, false);
OutputStream out = IOUtils.openFileOutputStream(s, false);
byte[] buffer = new byte[len];
out.write(buffer);
out.close();
out = fs.openFileOutputStream(s, true);
out = IOUtils.openFileOutputStream(s, true);
out.write(1);
out.close();
InputStream in = fs.openFileInputStream(s);
InputStream in = IOUtils.openFileInputStream(s);
for (int i = 0; i < len; i++) {
assertEquals(0, in.read());
}
......@@ -404,7 +396,7 @@ public class TestFileSystem extends TestBase {
assertEquals(-1, in.read());
in.close();
out.close();
fs.delete(s);
IOUtils.delete(s);
}
}
......@@ -13,7 +13,6 @@ import java.sql.Statement;
import org.h2.constant.ErrorCode;
import org.h2.engine.Constants;
import org.h2.store.fs.FileObject;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.tools.Restore;
import org.h2.util.IOUtils;
......@@ -230,11 +229,11 @@ public class TestPageStoreCoverage extends TestBase {
stat.execute("drop table if exists INFORMATION_SCHEMA.LOB_DATA");
stat.execute("drop table if exists INFORMATION_SCHEMA.LOB_MAP");
conn.close();
FileObject f = FileSystem.getInstance(fileName).openFileObject(fileName, "rw");
FileObject f = IOUtils.openFileObject(fileName, "rw");
// create a new database
conn = getConnection("pageStore");
conn.close();
f = FileSystem.getInstance(fileName).openFileObject(fileName, "rw");
f = IOUtils.openFileObject(fileName, "rw");
f.setFileLength(16);
// create a new database
conn = getConnection("pageStore");
......
......@@ -15,7 +15,6 @@ import java.sql.SQLException;
import java.sql.Statement;
import org.h2.engine.Constants;
import org.h2.store.fs.FileObject;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.tools.DeleteDbFiles;
import org.h2.tools.Recover;
......@@ -134,7 +133,7 @@ public class TestRecovery extends TestBase {
Statement stat = conn.createStatement();
stat.execute("create table test(id int, name varchar) as select 1, 'Hello World1'");
conn.close();
FileObject f = FileSystem.getInstance(getBaseDir()).openFileObject(getBaseDir() + "/recovery.h2.db", "rw");
FileObject f = IOUtils.openFileObject(getBaseDir() + "/recovery.h2.db", "rw");
byte[] buff = new byte[Constants.DEFAULT_PAGE_SIZE];
while (f.getFilePointer() < f.length()) {
f.readFully(buff, 0, buff.length);
......
......@@ -32,7 +32,6 @@ import java.util.Random;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.store.FileLister;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.test.trace.Player;
import org.h2.test.utils.AssertThrows;
......@@ -493,15 +492,14 @@ public class TestTools extends TestBase {
ConvertTraceFile.main("-traceFile", getBaseDir() + "/toolsConvertTraceFile.trace.db",
"-javaClass", getBaseDir() + "/Test",
"-script", getBaseDir() + "/test.sql");
FileSystem fs = FileSystem.getInstance(getBaseDir());
fs.delete(getBaseDir() + "/Test.java");
IOUtils.delete(getBaseDir() + "/Test.java");
String trace = getBaseDir() + "/toolsConvertTraceFile.trace.db";
assertTrue(fs.exists(trace));
assertTrue(IOUtils.exists(trace));
String newTrace = getBaseDir() + "/test.trace.db";
fs.delete(newTrace);
assertFalse(fs.exists(newTrace));
fs.rename(trace, newTrace);
IOUtils.delete(newTrace);
assertFalse(IOUtils.exists(newTrace));
IOUtils.rename(trace, newTrace);
deleteDb("toolsConvertTraceFile");
Player.main(getBaseDir() + "/test.trace.db");
testTraceFile(url);
......
......@@ -9,7 +9,6 @@ package org.h2.test.unit;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.h2.message.TraceSystem;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase;
import org.h2.util.IOUtils;
......@@ -57,7 +56,7 @@ public class TestTraceSystem extends TestBase {
String readOnlyFile = getBaseDir() + "/readOnly.log";
IOUtils.delete(readOnlyFile);
IOUtils.openFileOutputStream(readOnlyFile, false).close();
FileSystem.getInstance(getBaseDir()).setReadOnly(readOnlyFile);
IOUtils.setReadOnly(readOnlyFile);
TraceSystem ts = new TraceSystem(readOnlyFile);
ts.setLevelFile(TraceSystem.INFO);
ts.getTrace("test").info("test");
......
......@@ -15,7 +15,6 @@ import java.net.InetAddress;
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;
......@@ -27,7 +26,6 @@ public class FtpControl extends Thread {
private static final String SERVER_NAME = "Small FTP Server";
private FtpServer server;
private FileSystem fs;
private Socket control;
private FtpData data;
private PrintWriter output;
......@@ -42,7 +40,6 @@ public class FtpControl extends Thread {
FtpControl(Socket control, FtpServer server, boolean stop) {
this.server = server;
this.fs = server.getFileSystem();
this.control = control;
this.stop = stop;
}
......@@ -149,7 +146,7 @@ public class FtpControl extends Thread {
if ("CWD".equals(command)) {
String path = getPath(param);
String fileName = getFileName(path);
if (fs.exists(fileName) && fs.isDirectory(fileName)) {
if (IOUtils.exists(fileName) && IOUtils.isDirectory(fileName)) {
if (!path.endsWith("/")) {
path += "/";
}
......@@ -171,7 +168,7 @@ public class FtpControl extends Thread {
case 'D':
if ("DELE".equals(command)) {
String fileName = getFileName(param);
if (!readonly && fs.exists(fileName) && !fs.isDirectory(fileName) && fs.tryDelete(fileName)) {
if (!readonly && IOUtils.exists(fileName) && !IOUtils.isDirectory(fileName) && IOUtils.tryDelete(fileName)) {
if (server.getAllowTask() && fileName.endsWith(FtpServer.TASK_SUFFIX)) {
server.stopTask(fileName);
}
......@@ -197,7 +194,7 @@ public class FtpControl extends Thread {
}
} else if ("MDTM".equals(command)) {
String fileName = getFileName(param);
if (fs.exists(fileName) && !fs.isDirectory(fileName)) {
if (IOUtils.exists(fileName) && !IOUtils.isDirectory(fileName)) {
reply(213, server.formatLastModified(fileName));
} else {
reply(550, "Failed");
......@@ -237,7 +234,7 @@ public class FtpControl extends Thread {
case 'R':
if ("RNFR".equals(command)) {
String fileName = getFileName(param);
if (fs.exists(fileName)) {
if (IOUtils.exists(fileName)) {
renameFrom = fileName;
reply(350, "Ok");
} else {
......@@ -252,7 +249,7 @@ public class FtpControl extends Thread {
boolean ok = false;
if (!readonly) {
try {
fs.rename(fileOld, fileNew);
IOUtils.rename(fileOld, fileNew);
reply(250, "Ok");
ok = true;
} catch (Exception e) {
......@@ -265,10 +262,10 @@ public class FtpControl extends Thread {
}
} else if ("RETR".equals(command)) {
String fileName = getFileName(param);
if (fs.exists(fileName) && !fs.isDirectory(fileName)) {
if (IOUtils.exists(fileName) && !IOUtils.isDirectory(fileName)) {
reply(150, "Starting transfer");
try {
data.send(fs, fileName, restart);
data.send(fileName, restart);
reply(226, "Ok");
} catch (IOException e) {
server.traceError(e);
......@@ -299,17 +296,17 @@ public class FtpControl extends Thread {
reply(500, "Not understood");
} else if ("SIZE".equals(command)) {
param = getFileName(param);
if (fs.exists(param) && !fs.isDirectory(param)) {
reply(250, String.valueOf(fs.length(param)));
if (IOUtils.exists(param) && !IOUtils.isDirectory(param)) {
reply(250, String.valueOf(IOUtils.length(param)));
} else {
reply(500, "Failed");
}
} else if ("STOR".equals(command)) {
String fileName = getFileName(param);
if (!readonly && !fs.exists(fileName) || !fs.isDirectory(fileName)) {
if (!readonly && !IOUtils.exists(fileName) || !IOUtils.isDirectory(fileName)) {
reply(150, "Starting transfer");
try {
data.receive(fs, fileName);
data.receive(fileName);
if (server.getAllowTask() && param.endsWith(FtpServer.TASK_SUFFIX)) {
server.startTask(fileName);
}
......@@ -370,7 +367,7 @@ public class FtpControl extends Thread {
private void processRemoveDir(String param) {
String fileName = getFileName(param);
if (!readonly && fs.exists(fileName) && fs.isDirectory(fileName) && fs.tryDelete(fileName)) {
if (!readonly && IOUtils.exists(fileName) && IOUtils.isDirectory(fileName) && IOUtils.tryDelete(fileName)) {
reply(250, "Ok");
} else {
reply(500, "Failed");
......@@ -387,10 +384,10 @@ public class FtpControl extends Thread {
private void processList(String param, boolean directories) throws IOException {
String directory = getFileName(param);
if (!fs.exists(directory)) {
if (!IOUtils.exists(directory)) {
reply(450, "Directory does not exist");
return;
} else if (!fs.isDirectory(directory)) {
} else if (!IOUtils.isDirectory(directory)) {
reply(450, "Not a directory");
return;
}
......
......@@ -12,7 +12,6 @@ import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import org.h2.store.fs.FileSystem;
import org.h2.util.IOUtils;
/**
......@@ -91,11 +90,11 @@ public class FtpData extends Thread {
* @param fs the target file system
* @param fileName the target file name
*/
synchronized void receive(FileSystem fs, String fileName) throws IOException {
synchronized void receive(String fileName) throws IOException {
connect();
try {
InputStream in = socket.getInputStream();
OutputStream out = fs.openFileOutputStream(fileName, false);
OutputStream out = IOUtils.openFileOutputStream(fileName, false);
IOUtils.copy(in, out);
out.close();
} finally {
......@@ -112,11 +111,11 @@ public class FtpData extends Thread {
* @param fileName the source file name
* @param skip the number of bytes to skip
*/
synchronized void send(FileSystem fs, String fileName, long skip) throws IOException {
synchronized void send(String fileName, long skip) throws IOException {
connect();
try {
OutputStream out = socket.getOutputStream();
InputStream in = fs.openFileInputStream(fileName);
InputStream in = IOUtils.openFileInputStream(fileName);
IOUtils.skipFully(in, skip);
IOUtils.copy(in, out);
in.close();
......
......@@ -19,7 +19,6 @@ import java.util.HashMap;
import java.util.Locale;
import java.util.Properties;
import org.h2.server.Service;
import org.h2.store.fs.FileSystem;
import org.h2.tools.Server;
import org.h2.util.IOUtils;
import org.h2.util.NetUtils;
......@@ -79,7 +78,6 @@ public class FtpServer extends Tool implements Service {
private String readUserName = DEFAULT_READ;
private HashMap<String, Process> tasks = new HashMap<String, Process>();
private FileSystem fs;
private boolean trace;
private boolean allowTask;
......@@ -220,17 +218,17 @@ public class FtpServer extends Tool implements Service {
}
private void appendFile(StringBuilder buff, String fileName) {
buff.append(fs.isDirectory(fileName) ? 'd' : '-');
buff.append(IOUtils.isDirectory(fileName) ? 'd' : '-');
buff.append('r');
buff.append(fs.canWrite(fileName) ? 'w' : '-');
buff.append(IOUtils.canWrite(fileName) ? 'w' : '-');
buff.append("------- 1 owner group ");
String size = String.valueOf(fs.length(fileName));
String size = String.valueOf(IOUtils.length(fileName));
for (int i = size.length(); i < 15; i++) {
buff.append(' ');
}
buff.append(size);
buff.append(' ');
Date now = new Date(), mod = new Date(fs.getLastModified(fileName));
Date now = new Date(), mod = new Date(IOUtils.getLastModified(fileName));
String date;
if (mod.after(now) || Math.abs((now.getTime() - mod.getTime()) / 1000 / 60 / 60 / 24) > 180) {
synchronized (dateFormatOld) {
......@@ -256,7 +254,7 @@ public class FtpServer extends Tool implements Service {
*/
String formatLastModified(String fileName) {
synchronized (dateFormat) {
return dateFormat.format(new Date(fs.getLastModified(fileName)));
return dateFormat.format(new Date(IOUtils.getLastModified(fileName)));
}
}
......@@ -293,8 +291,8 @@ public class FtpServer extends Tool implements Service {
*/
String getDirectoryListing(String directory, boolean listDirectories) {
StringBuilder buff = new StringBuilder();
for (String fileName : fs.listFiles(directory)) {
if (!fs.isDirectory(fileName) || (fs.isDirectory(fileName) && listDirectories)) {
for (String fileName : IOUtils.listFiles(directory)) {
if (!IOUtils.isDirectory(fileName) || (IOUtils.isDirectory(fileName) && listDirectories)) {
appendFile(buff, fileName);
}
}
......@@ -352,8 +350,7 @@ public class FtpServer extends Tool implements Service {
}
public void start() {
fs = FileSystem.getInstance(root);
root = fs.getCanonicalPath(root);
root = IOUtils.getCanonicalPath(root);
IOUtils.createDirectories(root);
serverSocket = NetUtils.createServerSocket(port, false);
port = serverSocket.getLocalPort();
......@@ -511,15 +508,6 @@ public class FtpServer extends Tool implements Service {
p.destroy();
}
/**
* Get the file system used by this FTP server.
*
* @return the file system
*/
FileSystem getFileSystem() {
return fs;
}
/**
* Set the event listener. Only one listener can be registered.
*
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论