1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-26 07:03:22 +00:00

Handle missing file metadata on zip files

- Return EPOCH if a zip entry's creation/modification/access time is
   missing.
 - If a BasicFileAttributes.*Time method returns null, use 0 as our
   time. This shouldn't happen, but is a good sanity check.

Fixes #371
This commit is contained in:
SquidDev 2020-04-10 14:43:42 +01:00
parent e52d98ad8b
commit 062977336a
3 changed files with 46 additions and 10 deletions

View File

@ -23,6 +23,7 @@
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.HashMap;
import java.util.Map;
import java.util.OptionalLong;
@ -359,8 +360,8 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
{
BasicFileAttributes attributes = m_fileSystem.getAttributes( path );
Map<String, Object> result = new HashMap<>();
result.put( "modification", attributes.lastModifiedTime().toMillis() );
result.put( "created", attributes.creationTime().toMillis() );
result.put( "modification", getFileTime( attributes.lastModifiedTime() ) );
result.put( "created", getFileTime( attributes.creationTime() ) );
result.put( "size", attributes.isDirectory() ? 0 : attributes.size() );
result.put( "isDir", attributes.isDirectory() );
return new Object[] { result };
@ -375,4 +376,9 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
return null;
}
}
private static long getFileTime( FileTime time )
{
return time == null ? 0 : time.toMillis();
}
}

View File

@ -25,6 +25,7 @@
import java.nio.channels.ReadableByteChannel;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
@ -290,19 +291,20 @@ private static class ZipEntryAttributes implements BasicFileAttributes
@Override
public FileTime lastModifiedTime()
{
return entry.getLastModifiedTime();
return orEpoch( entry.getLastModifiedTime() );
}
@Override
public FileTime lastAccessTime()
{
return entry.getLastAccessTime();
return orEpoch( entry.getLastAccessTime() );
}
@Override
public FileTime creationTime()
{
return entry.getCreationTime();
FileTime time = entry.getCreationTime();
return time == null ? lastModifiedTime() : time;
}
@Override
@ -340,5 +342,12 @@ public Object fileKey()
{
return null;
}
private static final FileTime EPOCH = FileTime.from( Instant.EPOCH );
private static FileTime orEpoch( FileTime time )
{
return time == null ? EPOCH : time;
}
}
}

View File

@ -15,20 +15,24 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static org.junit.jupiter.api.Assertions.*;
@SuppressWarnings( "deprecation" )
public class JarMountTest
{
private static final File ZIP_FILE = new File( "test-files/jar-mount.zip" );
private static final FileTime MODIFY_TIME = FileTime.from( Instant.EPOCH.plus( 2, ChronoUnit.DAYS ) );
@BeforeAll
public static void before() throws IOException
{
if( ZIP_FILE.exists() ) return;
ZIP_FILE.getParentFile().mkdirs();
try( ZipOutputStream stream = new ZipOutputStream( new FileOutputStream( ZIP_FILE ) ) )
@ -36,7 +40,7 @@ public static void before() throws IOException
stream.putNextEntry( new ZipEntry( "dir/" ) );
stream.closeEntry();
stream.putNextEntry( new ZipEntry( "dir/file.lua" ) );
stream.putNextEntry( new ZipEntry( "dir/file.lua" ).setLastModifiedTime( MODIFY_TIME ) );
stream.write( "print('testing')".getBytes( StandardCharsets.UTF_8 ) );
stream.closeEntry();
}
@ -63,7 +67,7 @@ public void opensFileFromFile() throws IOException
{
IMount mount = new JarMount( ZIP_FILE, "dir/file.lua" );
byte[] contents;
try( InputStream stream = mount.openForRead( "" ) )
try( @SuppressWarnings( "deprecation" ) InputStream stream = mount.openForRead( "" ) )
{
contents = ByteStreams.toByteArray( stream );
}
@ -76,11 +80,28 @@ public void opensFileFromDir() throws IOException
{
IMount mount = new JarMount( ZIP_FILE, "dir" );
byte[] contents;
try( InputStream stream = mount.openForRead( "file.lua" ) )
try( @SuppressWarnings( "deprecation" ) InputStream stream = mount.openForRead( "file.lua" ) )
{
contents = ByteStreams.toByteArray( stream );
}
assertEquals( new String( contents, StandardCharsets.UTF_8 ), "print('testing')" );
}
@Test
public void fileAttributes() throws IOException
{
BasicFileAttributes attributes = new JarMount( ZIP_FILE, "dir" ).getAttributes( "file.lua" );
assertFalse( attributes.isDirectory() );
assertEquals( "print('testing')".length(), attributes.size() );
assertEquals( MODIFY_TIME, attributes.lastModifiedTime() );
}
@Test
public void directoryAttributes() throws IOException
{
BasicFileAttributes attributes = new JarMount( ZIP_FILE, "dir" ).getAttributes( "" );
assertTrue( attributes.isDirectory() );
assertEquals( 0, attributes.size() );
}
}