1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2026-04-18 13:01:22 +00:00

Remove FileSystemMount and rewrite JarMount

FileSystemMount was originally added to allow using ReadableByteChannels
instead of InputStreams. However, as zip files do not allow seeking,
there is no benefit of using them over the original JarMount (which we
need to preserve for backwards compatibility).

Instead of maintaining two near-identical mounts, we remove the
FileSystemMount and rewrite the JarMount implementation with several
improvements:

 - Rewrite the jar scanning algorithm to be closer to 1.13+'s data pack
   mount. This means we no longer require the jar file to have
   directories before the file (though this was not a problem in
   practice).
 - Add all JarMounts to a ReferenceQueue, closing up the ZipFile when
   they have been garbage collected (fixes #100).
 - Cache the contents of all files for 60 seconds (with some constraints
   on size). This allows us to seek on ROM files too (assuming they are
   small), by reading the whole thing into memory.
   The cache is shared across all mounts, and has a 64MiB limit, and
   thus should not have an adverse impact on memory.
This commit is contained in:
SquidDev
2019-01-16 17:25:46 +00:00
parent a838595e1e
commit 443e0f8f76
5 changed files with 214 additions and 381 deletions

View File

@@ -1,63 +0,0 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.filesystem;
import dan200.computercraft.api.filesystem.IMount;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class FilesystemMountTest
{
private static final File ZIP_FILE = new File( "test-files/filesystem-mount.zip" );
@BeforeClass
public static void before() throws IOException
{
if( ZIP_FILE.exists() ) return;
ZIP_FILE.getParentFile().mkdirs();
try( ZipOutputStream stream = new ZipOutputStream( new FileOutputStream( ZIP_FILE ) ) )
{
stream.putNextEntry( new ZipEntry( "dir/" ) );
stream.closeEntry();
stream.putNextEntry( new ZipEntry( "dir/file.lua" ) );
stream.write( "print('testing')".getBytes( StandardCharsets.UTF_8 ) );
stream.closeEntry();
}
}
@Test
public void mountsDir() throws IOException
{
FileSystem fs = FileSystems.newFileSystem( ZIP_FILE.toPath(), getClass().getClassLoader() );
IMount mount = new FileSystemMount( fs, "dir" );
assertTrue( "Root should be directory", mount.isDirectory( "" ) );
assertTrue( "File should exist", mount.exists( "file.lua" ) );
}
@Test
public void mountsFile() throws IOException
{
FileSystem fs = FileSystems.newFileSystem( ZIP_FILE.toPath(), getClass().getClassLoader() );
IMount mount = new FileSystemMount( fs, "dir/file.lua" );
assertTrue( "Root should exist", mount.exists( "" ) );
assertFalse( "Root should be a file", mount.isDirectory( "" ) );
}
}

View File

@@ -6,6 +6,7 @@
package dan200.computercraft.core.filesystem;
import com.google.common.io.ByteStreams;
import dan200.computercraft.api.filesystem.IMount;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -13,12 +14,12 @@ import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
@SuppressWarnings( "deprecation" )
public class JarMountTest
@@ -57,4 +58,30 @@ public class JarMountTest
assertTrue( "Root should exist", mount.exists( "" ) );
assertFalse( "Root should be a file", mount.isDirectory( "" ) );
}
@Test
public void opensFileFromFile() throws IOException
{
IMount mount = new JarMount( ZIP_FILE, "dir/file.lua" );
byte[] contents;
try( InputStream stream = mount.openForRead( "" ) )
{
contents = ByteStreams.toByteArray( stream );
}
assertEquals( "print('testing')", new String( contents, StandardCharsets.UTF_8 ) );
}
@Test
public void opensFileFromDir() throws IOException
{
IMount mount = new JarMount( ZIP_FILE, "dir" );
byte[] contents;
try( InputStream stream = mount.openForRead( "file.lua" ) )
{
contents = ByteStreams.toByteArray( stream );
}
assertEquals( "print('testing')", new String( contents, StandardCharsets.UTF_8 ) );
}
}