提交 2eb31e32 authored 作者: Thomas Mueller's avatar Thomas Mueller

There were some smaller changes in the encrypting file system (work in progress).

上级 ae656fbd
......@@ -39,11 +39,15 @@ public class TestFileSystem extends TestBase {
*/
public static void main(String... a) throws Exception {
TestBase test = TestBase.createCaller().init();
test.config.traceTest = true;
// test.config.traceTest = true;
test.test();
}
public void test() throws Exception {
FileSystemCrypt.register();
// DebugFileSystem.register().setTrace(true);
// testFileSystem("crypt:aes:x:" + getBaseDir() + "/fs");
testSplitDatabaseInZip();
testDatabaseInMemFileSys();
testDatabaseInJar();
......@@ -270,6 +274,10 @@ public class TestFileSystem extends TestBase {
}
private void testRandomAccess(String fsBase) throws Exception {
testRandomAccess(fsBase, 1);
}
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);
......@@ -286,10 +294,11 @@ public class TestFileSystem extends TestBase {
// expected
}
f.sync();
Random random = new Random(1);
Random random = new Random(seed);
int size = getSize(100, 500);
try {
for (int i = 0; i < size; i++) {
trace("op " + i);
int pos = random.nextInt(10000);
switch(random.nextInt(7)) {
case 0: {
......@@ -310,13 +319,13 @@ public class TestFileSystem extends TestBase {
break;
}
case 2: {
trace("setLength " + pos);
f.setFileLength(pos);
ra.setLength(pos);
if (ra.getFilePointer() > pos) {
f.seek(0);
ra.seek(0);
}
trace("setLength " + pos);
buff.append("setLength " + pos + "\n");
break;
}
......@@ -325,9 +334,9 @@ public class TestFileSystem extends TestBase {
len = (int) Math.min(len, ra.length() - ra.getFilePointer());
byte[] b1 = new byte[len];
byte[] b2 = new byte[len];
trace("readFully " + len);
ra.readFully(b1, 0, len);
f.readFully(b2, 0, len);
trace("readFully " + len);
buff.append("readFully " + len + "\n");
assertEquals(b1, b2);
break;
......@@ -357,28 +366,30 @@ public class TestFileSystem extends TestBase {
default:
}
}
} catch (Exception e) {
} catch (Throwable e) {
e.printStackTrace();
fail("Exception: " + e + "\n"+ buff.toString());
} finally {
f.close();
ra.close();
file.delete();
fs.delete(s);
}
f.close();
ra.close();
file.delete();
fs.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);
byte[] buffer = new byte[10000];
byte[] buffer = new byte[len];
out.write(buffer);
out.close();
out = fs.openFileOutputStream(s, true);
out.write(1);
out.close();
InputStream in = fs.openFileInputStream(s);
for (int i = 0; i < 10000; i++) {
for (int i = 0; i < len; i++) {
assertEquals(0, in.read());
}
assertEquals(1, in.read());
......
......@@ -41,7 +41,7 @@ public class FileObjectCrypt implements FileObject {
public FileObjectCrypt(String name, String algorithm, String password, FileObject file) throws IOException {
this.name = name;
this.file = file;
boolean newFile = file.length() < 2 * HEADER_LENGTH;
boolean newFile = file.length() < HEADER_LENGTH + BLOCK_SIZE;
byte[] filePasswordHash;
if (algorithm.endsWith("-hash")) {
filePasswordHash = StringUtils.convertStringToBytes(password);
......@@ -81,7 +81,7 @@ public class FileObjectCrypt implements FileObject {
}
public long length() throws IOException {
return Math.max(0, file.length() - 2 * HEADER_LENGTH);
return Math.max(0, file.length() - HEADER_LENGTH - BLOCK_SIZE);
}
public void releaseLock() {
......@@ -108,9 +108,9 @@ public class FileObjectCrypt implements FileObject {
if (newLength < length()) {
int mod = (int) (newLength % BLOCK_SIZE);
if (mod == 0) {
file.setFileLength(newLength + HEADER_LENGTH);
file.setFileLength(HEADER_LENGTH + newLength);
} else {
file.setFileLength(newLength + HEADER_LENGTH + BLOCK_SIZE - mod);
file.setFileLength(HEADER_LENGTH + newLength + BLOCK_SIZE - mod);
byte[] buff = new byte[BLOCK_SIZE - mod];
long pos = getFilePointer();
seek(newLength);
......@@ -118,7 +118,7 @@ public class FileObjectCrypt implements FileObject {
seek(pos);
}
}
file.setFileLength(newLength + HEADER_LENGTH + BLOCK_SIZE);
file.setFileLength(HEADER_LENGTH + newLength + BLOCK_SIZE);
if (newLength < getFilePointer()) {
seek(newLength);
}
......@@ -131,12 +131,15 @@ public class FileObjectCrypt implements FileObject {
throw new EOFException("pos: " + pos + " len: " + len + " length: " + length);
}
int posMod = (int) (pos % BLOCK_SIZE);
int lenMod = len % BLOCK_SIZE;
if (posMod == 0 && lenMod == 0) {
if (posMod == 0 && len % BLOCK_SIZE == 0) {
readAligned(pos, b, off, len);
} else {
long p = pos - posMod;
int l = len + 2 * BLOCK_SIZE - lenMod;
int l = len;
if (posMod != 0) {
l += posMod;
}
l = MathUtils.roundUpInt(l, BLOCK_SIZE);
seek(p);
byte[] temp = new byte[l];
try {
......@@ -151,18 +154,21 @@ public class FileObjectCrypt implements FileObject {
public void write(byte[] b, int off, int len) throws IOException {
long pos = getFilePointer();
int posMod = (int) (pos % BLOCK_SIZE);
int lenMod = len % BLOCK_SIZE;
if (posMod == 0 && lenMod == 0) {
if (posMod == 0 && len % BLOCK_SIZE == 0) {
byte[] temp = new byte[len];
System.arraycopy(b, off, temp, 0, len);
writeAligned(pos, temp, 0, len);
} else {
long p = pos - posMod;
int l = len + 2 * BLOCK_SIZE - lenMod;
int l = len;
if (posMod != 0) {
l += posMod;
}
l = MathUtils.roundUpInt(l, BLOCK_SIZE);
seek(p);
byte[] temp = new byte[l];
if (file.length() < pos + l + HEADER_LENGTH) {
file.setFileLength(pos + l + HEADER_LENGTH);
if (file.length() < HEADER_LENGTH + p + l) {
file.setFileLength(HEADER_LENGTH + p + l);
}
readAligned(p, temp, 0, l);
System.arraycopy(b, off, temp, posMod, len);
......@@ -174,8 +180,8 @@ public class FileObjectCrypt implements FileObject {
}
}
pos = file.getFilePointer();
if (file.length() < pos + HEADER_LENGTH) {
file.setFileLength(pos + HEADER_LENGTH);
if (file.length() < pos + BLOCK_SIZE) {
file.setFileLength(pos + BLOCK_SIZE);
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论