Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermissions;
import org.scip_code.scip.Index;

Expand All @@ -16,15 +18,25 @@ public class ScipWriter implements AutoCloseable {
private final ScipAggregatorOptions options;

public ScipWriter(ScipAggregatorOptions options) throws IOException {
this.tmp =
Files.createTempFile(
"scip-aggregator",
"index.scip",
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-r--r--")));
this.tmp = createTempFile();
this.output = new ScipOutputStream(new BufferedOutputStream(Files.newOutputStream(tmp)));
this.options = options;
}

/**
* Creates the temporary output file with {@code rw-r--r--} permissions where the file system
* supports POSIX permissions. Other file systems (for example NTFS on Windows) reject the POSIX
* attribute, so the file is created with the default permissions there.
*/
private static Path createTempFile() throws IOException {
if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
FileAttribute<?> permissions =
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-r--r--"));
return Files.createTempFile("scip-aggregator", "index.scip", permissions);
}
return Files.createTempFile("scip-aggregator", "index.scip");
}

public void emitTyped(Index index) {
this.output.write(index.toByteArray());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.scip_code.scip_java.aggregator;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.scip_code.scip.ToolInfo;

/**
* Regression test for creating the temporary output file on file systems without POSIX permission
* support, such as NTFS on Windows.
*/
class ScipWriterTest {

@Test
void writesOutputFile(@TempDir Path dir) throws Exception {
Path output = dir.resolve("index.scip");
ScipAggregatorOptions options =
new ScipAggregatorOptions(
List.of(),
output,
dir,
new ScipAggregatorReporter() {},
ToolInfo.getDefaultInstance(),
false,
List.of(),
false,
true,
false);
try (ScipWriter writer = new ScipWriter(options)) {
writer.build();
}
assertTrue(Files.exists(output));
}
}
Loading