Fix some resource leaks

This commit is contained in:
altrisi authored and gnembon committed 2022-08-28 18:34:39 +02:00
1 parent 2a4d678aa7
commit d273d4ffb8
3 files changed
+25 -14

No files matched your search

@@ -51,6 +51,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CarpetScriptServer extends ScriptServer
{
@@ -144,27 +145,30 @@ public class CarpetScriptServer extends ScriptServer
Path folder = server.getWorldPath(LevelResource.ROOT).resolve("scripts");
if (!Files.exists(folder))
Files.createDirectories(folder);
Optional<Path>
scriptPath = Files.list(folder)
try (Stream<Path> folderLister = Files.list(folder)) {
Optional<Path> scriptPath = folderLister
.filter(script ->
script.getFileName().toString().equalsIgnoreCase(name + ".sc") ||
(allowLibraries && script.getFileName().toString().equalsIgnoreCase(name+".scl"))
).findFirst();
if (scriptPath.isPresent())
return Module.fromPath(scriptPath.get());
}
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT)
{
Path globalFolder = FabricLoader.getInstance().getConfigDir().resolve("carpet/scripts");
if (!Files.exists(globalFolder))
Files.createDirectories(globalFolder);
scriptPath = Files.walk(globalFolder)
try (Stream<Path> folderWalker = Files.walk(globalFolder)) {
Optional<Path> scriptPath = folderWalker
.filter(script -> script.getFileName().toString().equalsIgnoreCase(name + ".sc") ||
(allowLibraries && script.getFileName().toString().equalsIgnoreCase(name + ".scl")))
.findFirst();
if (scriptPath.isPresent())
return Module.fromPath(scriptPath.get());
}
}
} catch (IOException e) {
CarpetSettings.LOG.error("Exception while loading the app: ", e);
}
@@ -204,19 +208,23 @@ public class CarpetScriptServer extends ScriptServer
Path worldScripts = server.getWorldPath(LevelResource.ROOT).resolve("scripts");
if (!Files.exists(worldScripts))
Files.createDirectories(worldScripts);
Files.list(worldScripts)
try (Stream<Path> folderLister = Files.list(worldScripts)) {
folderLister
.filter(f -> f.toString().endsWith(".sc"))
.forEach(f -> moduleNames.add(f.getFileName().toString().replaceFirst("\\.sc$","").toLowerCase(Locale.ROOT)));
}
if (includeBuiltIns && (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT))
{
Path globalScripts = FabricLoader.getInstance().getConfigDir().resolve("carpet/scripts");
if (!Files.exists(globalScripts))
Files.createDirectories(globalScripts);
Files.walk(globalScripts, FileVisitOption.FOLLOW_LINKS)
try (Stream<Path> folderWalker = Files.walk(globalScripts, FileVisitOption.FOLLOW_LINKS)) {
folderWalker
.filter(f -> f.toString().endsWith(".sc"))
.forEach(f -> moduleNames.add(f.getFileName().toString().replaceFirst("\\.sc$","").toLowerCase(Locale.ROOT)));
}
}
} catch (IOException e) {
CarpetSettings.LOG.error("Exception while searching for apps: ", e);
}
+8 -2
View File
@@ -89,7 +89,7 @@ public record Module(String name, String code, boolean library) {
{
Path dataFile = resolveResource(module);
if (dataFile == null) return null;
if (!Files.exists(dataFile) || !(dataFile.toFile().isFile())) return null;
if (!Files.exists(dataFile) || !(Files.isRegularFile(dataFile))) return null;
synchronized (FileArgument.writeIOSync) { return FileArgument.readTag(dataFile); }
}
@@ -97,7 +97,13 @@ public record Module(String name, String code, boolean library) {
{
Path dataFile = resolveResource(module);
if (dataFile == null) return;
if (!Files.exists(dataFile.getParent()) && !dataFile.toFile().getParentFile().mkdirs()) return;
if (!Files.exists(dataFile.getParent())) {
try {
Files.createDirectories(dataFile.getParent());
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
synchronized (FileArgument.writeIOSync) { FileArgument.writeTagDisk(globalState, dataFile, false); }
}
@@ -12,7 +12,6 @@ import carpet.script.value.Value;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import net.minecraft.ReportedException;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtAccounter;
@@ -264,18 +263,16 @@ public class FileArgument
public Stream<String> listFolder(Module module)
{
Stream<String> strings;
try { synchronized (writeIOSync)
try (Stream<Path> result = listFiles(module)) { synchronized (writeIOSync)
{
Stream<Path> result;
result = listFiles(module);
if (result == null) return null;
Path rootPath = moduleRootPath(module);
if (rootPath == null) return null;
String zipComponent = (zipContainer != null) ? rootPath.relativize(zipPath).toString() : null;
// need to evaluate the stream before exiting try-with-resources else there'll be no data to stream
strings = (zipContainer == null)
? result.map(p -> rootPath.relativize(p).toString().replaceAll("[\\\\/]+", "/"))
// need to remove ties to the zip file system before closing, so wrapping the stream
: result.map(p -> (zipComponent + '/'+ p.toString()).replaceAll("[\\\\/]+", "/")).collect(Collectors.toList()).stream();
? result.map(p -> rootPath.relativize(p).toString().replaceAll("[\\\\/]+", "/")).toList().stream()
: result.map(p -> (zipComponent + '/'+ p.toString()).replaceAll("[\\\\/]+", "/")).toList().stream();
} }
finally
{