mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-12 02:10:30 +00:00
Merge pull request #169 from Lignum/fd-limit
Configurable file descriptor limit
This commit is contained in:
commit
f99caed4f2
@ -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 class ComputerCraft
|
||||
|
||||
public static Property computerSpaceLimit;
|
||||
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.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 class ComputerCraft
|
||||
|
||||
computerSpaceLimit = Config.computerSpaceLimit.getInt();
|
||||
floppySpaceLimit = Config.floppySpaceLimit.getInt();
|
||||
maximumFilesOpen = Math.max( 0, Config.maximumFilesOpen.getInt() );
|
||||
|
||||
turtlesNeedFuel = Config.turtlesNeedFuel.getBoolean();
|
||||
turtleFuelLimit = Config.turtleFuelLimit.getInt();
|
||||
|
@ -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 @@ public class FileSystem
|
||||
}
|
||||
}
|
||||
|
||||
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 @@ 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
|
||||
{
|
||||
@ -684,11 +720,7 @@ public class FileSystem
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
m_openFiles.remove( this );
|
||||
reader.close();
|
||||
}
|
||||
closeFile( this, reader );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -697,11 +729,7 @@ public class FileSystem
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
m_openFiles.add( file );
|
||||
}
|
||||
return file;
|
||||
return openFile( file, reader );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -744,11 +772,7 @@ public class FileSystem
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
m_openFiles.remove( this );
|
||||
writer.close();
|
||||
}
|
||||
closeFile( this, writer );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -757,11 +781,7 @@ public class FileSystem
|
||||
writer.flush();
|
||||
}
|
||||
};
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
m_openFiles.add( file );
|
||||
}
|
||||
return file;
|
||||
return openFile( file, writer );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -790,11 +810,7 @@ public class FileSystem
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
m_openFiles.remove( this );
|
||||
stream.close();
|
||||
}
|
||||
closeFile( this, stream );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -803,11 +819,7 @@ public class FileSystem
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
m_openFiles.add( file );
|
||||
}
|
||||
return file;
|
||||
return openFile( file, stream );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -836,11 +848,7 @@ public class FileSystem
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
m_openFiles.remove( this );
|
||||
stream.close();
|
||||
}
|
||||
closeFile( this, stream );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -849,11 +857,7 @@ public class FileSystem
|
||||
stream.flush();
|
||||
}
|
||||
};
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
m_openFiles.add( file );
|
||||
}
|
||||
return file;
|
||||
return openFile( file, stream );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user