Merge pull request #169 from Lignum/fd-limit

Configurable file descriptor limit
This commit is contained in:
Daniel Ratcliffe 2017-05-04 22:12:50 +01:00 committed by GitHub
commit f99caed4f2
3 changed files with 53 additions and 42 deletions

View File

@ -127,6 +127,7 @@ public class ComputerCraft
public static int computerSpaceLimit = 1000 * 1000;
public static int floppySpaceLimit = 125 * 1000;
public static int maximumFilesOpen = 128;
// Blocks and Items
public static class Blocks
@ -184,6 +185,7 @@ public static class Config {
public static Property computerSpaceLimit;
public static Property floppySpaceLimit;
public static Property maximumFilesOpen;
}
@ -260,6 +262,9 @@ public void preInit( FMLPreInitializationEvent event )
Config.turtlesNeedFuel = Config.config.get( Configuration.CATEGORY_GENERAL, "turtlesNeedFuel", turtlesNeedFuel );
Config.turtlesNeedFuel.setComment( "Set whether Turtles require fuel to move" );
Config.maximumFilesOpen = Config.config.get(Configuration.CATEGORY_GENERAL, "maximumFilesOpen", maximumFilesOpen);
Config.maximumFilesOpen.setComment( "How many files a computer can have open at the same time" );
Config.turtleFuelLimit = Config.config.get( Configuration.CATEGORY_GENERAL, "turtleFuelLimit", turtleFuelLimit );
Config.turtleFuelLimit.setComment( "The fuel limit for Turtles" );
@ -303,6 +308,7 @@ public static void syncConfig() {
computerSpaceLimit = Config.computerSpaceLimit.getInt();
floppySpaceLimit = Config.floppySpaceLimit.getInt();
maximumFilesOpen = Math.max( 0, Config.maximumFilesOpen.getInt() );
turtlesNeedFuel = Config.turtlesNeedFuel.getBoolean();
turtleFuelLimit = Config.turtleFuelLimit.getInt();

View File

@ -6,6 +6,7 @@
package dan200.computercraft.core.filesystem;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.api.filesystem.IWritableMount;
@ -289,8 +290,8 @@ private String toLocal( String path )
}
}
private Map<String, MountWrapper> m_mounts = new HashMap<String, MountWrapper>();
private Set<IMountedFile> m_openFiles = new HashSet<IMountedFile>();
private final Map<String, MountWrapper> m_mounts = new HashMap<String, MountWrapper>();
private final Set<IMountedFile> m_openFiles = new HashSet<IMountedFile>();
public FileSystem( String rootLabel, IMount rootMount ) throws FileSystemException
{
@ -649,6 +650,41 @@ private synchronized void copyRecursive( String sourcePath, MountWrapper sourceM
}
}
}
private synchronized <T extends IMountedFile> T openFile(T file, Closeable handle) throws FileSystemException
{
synchronized( m_openFiles )
{
if( m_openFiles.size() >= ComputerCraft.maximumFilesOpen )
{
if( handle != null )
{
try {
handle.close();
} catch ( IOException ignored ) {
// shrug
}
}
throw new FileSystemException("Too many files already open");
}
m_openFiles.add( file );
return file;
}
}
private synchronized void closeFile( IMountedFile file, Closeable handle ) throws IOException
{
synchronized( m_openFiles )
{
m_openFiles.remove( file );
if( handle != null )
{
handle.close();
}
}
}
public synchronized IMountedFileNormal openForRead( String path ) throws FileSystemException
{
@ -684,11 +720,7 @@ public void write(String s, int off, int len, boolean newLine) throws IOExceptio
@Override
public void close() throws IOException
{
synchronized( m_openFiles )
{
m_openFiles.remove( this );
reader.close();
}
closeFile( this, reader );
}
@Override
@ -697,11 +729,7 @@ public void flush() throws IOException
throw new UnsupportedOperationException();
}
};
synchronized( m_openFiles )
{
m_openFiles.add( file );
}
return file;
return openFile( file, reader );
}
return null;
}
@ -744,11 +772,7 @@ public void write( String s, int off, int len, boolean newLine ) throws IOExcept
@Override
public void close() throws IOException
{
synchronized( m_openFiles )
{
m_openFiles.remove( this );
writer.close();
}
closeFile( this, writer );
}
@Override
@ -757,11 +781,7 @@ public void flush() throws IOException
writer.flush();
}
};
synchronized( m_openFiles )
{
m_openFiles.add( file );
}
return file;
return openFile( file, writer );
}
return null;
}
@ -790,11 +810,7 @@ public void write(int i) throws IOException
@Override
public void close() throws IOException
{
synchronized( m_openFiles )
{
m_openFiles.remove( this );
stream.close();
}
closeFile( this, stream );
}
@Override
@ -803,11 +819,7 @@ public void flush() throws IOException
throw new UnsupportedOperationException();
}
};
synchronized( m_openFiles )
{
m_openFiles.add( file );
}
return file;
return openFile( file, stream );
}
return null;
}
@ -836,11 +848,7 @@ public void write(int i) throws IOException
@Override
public void close() throws IOException
{
synchronized( m_openFiles )
{
m_openFiles.remove( this );
stream.close();
}
closeFile( this, stream );
}
@Override
@ -849,11 +857,7 @@ public void flush() throws IOException
stream.flush();
}
};
synchronized( m_openFiles )
{
m_openFiles.add( file );
}
return file;
return openFile( file, stream );
}
return null;
}

View File

@ -56,3 +56,4 @@ gui.computercraft:config.turtle_fuel_limit=Turtle fuel limit
gui.computercraft:config.advanced_turtle_fuel_limit=Advanced Turtle fuel limit
gui.computercraft:config.turtles_obey_block_protection=Turtles obey block protection
gui.computercraft:config.turtles_can_push=Turtles can push entities
gui.computercraft:config.maximum_files_open=Maximum files open per computer