1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-12 10:20:28 +00:00

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 computerSpaceLimit = 1000 * 1000;
public static int floppySpaceLimit = 125 * 1000; public static int floppySpaceLimit = 125 * 1000;
public static int maximumFilesOpen = 128;
// Blocks and Items // Blocks and Items
public static class Blocks public static class Blocks
@ -184,6 +185,7 @@ public class ComputerCraft
public static Property computerSpaceLimit; public static Property computerSpaceLimit;
public static Property floppySpaceLimit; public static Property floppySpaceLimit;
public static Property maximumFilesOpen;
} }
@ -260,6 +262,9 @@ public class ComputerCraft
Config.turtlesNeedFuel = Config.config.get( Configuration.CATEGORY_GENERAL, "turtlesNeedFuel", turtlesNeedFuel ); Config.turtlesNeedFuel = Config.config.get( Configuration.CATEGORY_GENERAL, "turtlesNeedFuel", turtlesNeedFuel );
Config.turtlesNeedFuel.setComment( "Set whether Turtles require fuel to move" ); 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 = Config.config.get( Configuration.CATEGORY_GENERAL, "turtleFuelLimit", turtleFuelLimit );
Config.turtleFuelLimit.setComment( "The fuel limit for Turtles" ); Config.turtleFuelLimit.setComment( "The fuel limit for Turtles" );
@ -303,6 +308,7 @@ public class ComputerCraft
computerSpaceLimit = Config.computerSpaceLimit.getInt(); computerSpaceLimit = Config.computerSpaceLimit.getInt();
floppySpaceLimit = Config.floppySpaceLimit.getInt(); floppySpaceLimit = Config.floppySpaceLimit.getInt();
maximumFilesOpen = Math.max( 0, Config.maximumFilesOpen.getInt() );
turtlesNeedFuel = Config.turtlesNeedFuel.getBoolean(); turtlesNeedFuel = Config.turtlesNeedFuel.getBoolean();
turtleFuelLimit = Config.turtleFuelLimit.getInt(); turtleFuelLimit = Config.turtleFuelLimit.getInt();

View File

@ -6,6 +6,7 @@
package dan200.computercraft.core.filesystem; package dan200.computercraft.core.filesystem;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.filesystem.IMount; import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.api.filesystem.IWritableMount; import dan200.computercraft.api.filesystem.IWritableMount;
@ -289,8 +290,8 @@ public class FileSystem
} }
} }
private Map<String, MountWrapper> m_mounts = new HashMap<String, MountWrapper>(); private final Map<String, MountWrapper> m_mounts = new HashMap<String, MountWrapper>();
private Set<IMountedFile> m_openFiles = new HashSet<IMountedFile>(); private final Set<IMountedFile> m_openFiles = new HashSet<IMountedFile>();
public FileSystem( String rootLabel, IMount rootMount ) throws FileSystemException public FileSystem( String rootLabel, IMount rootMount ) throws FileSystemException
{ {
@ -650,6 +651,41 @@ public class FileSystem
} }
} }
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 public synchronized IMountedFileNormal openForRead( String path ) throws FileSystemException
{ {
path = sanitizePath ( path ); path = sanitizePath ( path );
@ -684,11 +720,7 @@ public class FileSystem
@Override @Override
public void close() throws IOException public void close() throws IOException
{ {
synchronized( m_openFiles ) closeFile( this, reader );
{
m_openFiles.remove( this );
reader.close();
}
} }
@Override @Override
@ -697,11 +729,7 @@ public class FileSystem
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
}; };
synchronized( m_openFiles ) return openFile( file, reader );
{
m_openFiles.add( file );
}
return file;
} }
return null; return null;
} }
@ -744,11 +772,7 @@ public class FileSystem
@Override @Override
public void close() throws IOException public void close() throws IOException
{ {
synchronized( m_openFiles ) closeFile( this, writer );
{
m_openFiles.remove( this );
writer.close();
}
} }
@Override @Override
@ -757,11 +781,7 @@ public class FileSystem
writer.flush(); writer.flush();
} }
}; };
synchronized( m_openFiles ) return openFile( file, writer );
{
m_openFiles.add( file );
}
return file;
} }
return null; return null;
} }
@ -790,11 +810,7 @@ public class FileSystem
@Override @Override
public void close() throws IOException public void close() throws IOException
{ {
synchronized( m_openFiles ) closeFile( this, stream );
{
m_openFiles.remove( this );
stream.close();
}
} }
@Override @Override
@ -803,11 +819,7 @@ public class FileSystem
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
}; };
synchronized( m_openFiles ) return openFile( file, stream );
{
m_openFiles.add( file );
}
return file;
} }
return null; return null;
} }
@ -836,11 +848,7 @@ public class FileSystem
@Override @Override
public void close() throws IOException public void close() throws IOException
{ {
synchronized( m_openFiles ) closeFile( this, stream );
{
m_openFiles.remove( this );
stream.close();
}
} }
@Override @Override
@ -849,11 +857,7 @@ public class FileSystem
stream.flush(); stream.flush();
} }
}; };
synchronized( m_openFiles ) return openFile( file, stream );
{
m_openFiles.add( file );
}
return file;
} }
return null; 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.advanced_turtle_fuel_limit=Advanced Turtle fuel limit
gui.computercraft:config.turtles_obey_block_protection=Turtles obey block protection gui.computercraft:config.turtles_obey_block_protection=Turtles obey block protection
gui.computercraft:config.turtles_can_push=Turtles can push entities gui.computercraft:config.turtles_can_push=Turtles can push entities
gui.computercraft:config.maximum_files_open=Maximum files open per computer