Handle resources, never left left opened file handler on windows (#1857)

On windows unit tests are failing because junit checks if all @tempdir directries are empty and can be deleted after test run.
But due to opened file handles (not closed resource streams) Windows keeps files, which are "in use".
Linux is less strict in this area.
Additionally I want highlight that XMLStreamReaderImpl/XMLStreamWriterImpl from apache.xerces library (in OpenJDK11 at least) which are picked at runtime as xml parser implementation - they don't close associated resources.
BTW, I thing that relying on some runtime (sometimes - unpredictable) dependencies - is bad practice, but this it up to separate topic.
Additional fix: in IOUtil is file is locked (due to permissions or opened handle) - it will undlessly try-and-retry to delete it until end of the world, on windows.
This commit is contained in:
StNekroman
2021-11-22 11:22:46 +02:00
committed by GitHub
parent 47842cc071
commit b09284f1f5
12 changed files with 150 additions and 147 deletions

View File

@@ -93,8 +93,8 @@ class RepositoryImportStep implements ImportStep {
private void importFromTemporaryPath(ImportState state, Path path) {
LOG.debug("Importing repository from temporary location in work dir");
state.getLogger().step("importing repository from temporary location");
try {
unbundleRepository(state, Files.newInputStream(path));
try (InputStream is = Files.newInputStream(path)) {
unbundleRepository(state, is);
} catch (IOException e) {
throw new ImportFailedException(
entity(state.getRepository()).build(),

View File

@@ -34,17 +34,19 @@ import sonia.scm.version.Version;
import sonia.scm.xml.XmlStreams;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
@@ -74,8 +76,8 @@ abstract class DifferentiateBetweenConfigAndConfigEntryUpdateStep {
private boolean isConfigEntryFile(Path potentialFile) {
LOG.trace("Testing whether file is config entry file without mark: {}", potentialFile);
XMLStreamReader reader = null;
try {
reader = XmlStreams.createReader(potentialFile);
try (Reader inputReader = Files.newBufferedReader(potentialFile, StandardCharsets.UTF_8)) {
reader = XmlStreams.createReader(inputReader);
reader.nextTag();
if ("configuration".equals(reader.getLocalName())) {
reader.nextTag();
@@ -104,26 +106,25 @@ abstract class DifferentiateBetweenConfigAndConfigEntryUpdateStep {
}
private void writeXmlDocument(Document configEntryDocument, Path temporaryFile) throws TransformerException {
try {
TransformerFactory factory = TransformerFactory.newInstance();
try (OutputStream os = Files.newOutputStream(temporaryFile)) {
final TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
Transformer transformer = factory.newTransformer();
DOMSource domSource = new DOMSource(configEntryDocument);
StreamResult streamResult = new StreamResult(Files.newOutputStream(temporaryFile));
transformer.transform(domSource, streamResult);
final DOMSource domSource = new DOMSource(configEntryDocument);
factory.newTransformer().transform(domSource, new StreamResult(os));
} catch (IOException e) {
throw new UpdateException("Could not write modified config entry file", e);
}
}
private Document readAsXmlDocument(Path configFile) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(Files.newInputStream(configFile));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
try (InputStream is = Files.newInputStream(configFile)) {
return factory.newDocumentBuilder().parse(is);
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new UpdateException("Could not read config entry file", e);
}