Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve creatTempFile/Dir usage #2208

Merged
Merged
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 @@ -93,7 +93,7 @@ protected void deleteTempFile(final String tempFile) {
}

protected String storeTempFile(final InputStream content) throws IOException {
final File file = createTempFile();
final File file = createTempFile(false);
try (final OutputStream outputstream = new BufferedOutputStream(new FileOutputStream(file))) {
content.transferTo(outputstream);
outputstream.flush();
Expand All @@ -104,16 +104,23 @@ protected String storeTempFile(final InputStream content) throws IOException {
protected abstract AbstractDbArtifact store(final String tenant, final DbArtifactHash base16Hashes,
final String contentType, final String tempFile) throws IOException;

private static File createTempFile() {
static File createTempFile(final boolean directory) {
try {
final File file = Files.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX).toFile();
if (!file.setReadable(true, true) ||
!file.setWritable(true, true)) {
throw new IOException("Can't set proper permissions!");
final File file = (directory
? Files.createTempDirectory(TEMP_FILE_PREFIX)
: Files.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX)).toFile();
file.deleteOnExit();
if (!file.setReadable(true, true) || !file.setWritable(true, true)) {
if (file.delete()) { // try to delete immediately, if failed - on exit
throw new IOException("Can't set proper permissions!");
} else {
throw new IOException("Can't set proper permissions (failed to delete the file immediately(!");
}
}
// try, if not supported - ok
file.setExecutable(false);
file.deleteOnExit();
if (!file.setExecutable(false)) {
log.debug("Can't set executable permissions for temp file {}", file);
}
return file;
} catch (final IOException e) {
throw new ArtifactStoreException("Cannot create temp file", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Random;

import io.qameta.allure.Description;
Expand All @@ -40,9 +39,9 @@ class ArtifactFilesystemRepositoryTest {
private static ArtifactFilesystemRepository artifactFilesystemRepository;

@BeforeAll
static void setup() throws IOException {
static void setup() {
artifactResourceProperties = new ArtifactFilesystemProperties();
artifactResourceProperties.setPath(Files.createTempDirectory(null).toString());
artifactResourceProperties.setPath(AbstractArtifactRepository.createTempFile(true).toString());

artifactFilesystemRepository = new ArtifactFilesystemRepository(artifactResourceProperties);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;

import io.qameta.allure.Description;
import io.qameta.allure.Feature;
Expand Down Expand Up @@ -43,12 +42,9 @@ void getInputStreamOfNonExistingFileThrowsException() {
@Test
@Description("Verifies that an InputStream can be opened if file exists")
void getInputStreamOfExistingFile() throws IOException {
final File createTempFile = Files.createTempFile(ArtifactFilesystemTest.class.getSimpleName(), "").toFile();
createTempFile.deleteOnExit();

final ArtifactFilesystem underTest = new ArtifactFilesystem(
createTempFile, ArtifactFilesystemTest.class.getSimpleName(), new DbArtifactHash("1", "2", "3"), 0L, null);
final byte[] buffer = new byte[1024];
assertThat(IOUtils.read(underTest.getFileInputStream(), buffer)).isZero();
AbstractArtifactRepository.createTempFile(false), ArtifactFilesystemTest.class.getSimpleName(),
new DbArtifactHash("1", "2", "3"), 0L, null);
assertThat(IOUtils.read(underTest.getFileInputStream(), new byte[16])).isZero();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.eclipse.hawkbit.artifact.repository.ArtifactRepository;
import org.eclipse.hawkbit.artifact.repository.ArtifactStoreException;
import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
import org.eclipse.hawkbit.repository.ArtifactManagement;
import org.eclipse.hawkbit.repository.ConfirmationManagement;
Expand Down Expand Up @@ -191,7 +192,7 @@ public abstract class AbstractIntegrationTest {
protected ServiceMatcher serviceMatcher;
@Autowired
protected ApplicationEventPublisher eventPublisher;
private static final String ARTIFACT_DIRECTORY = createTempDir();
private static final String ARTIFACT_DIRECTORY = createTempDir().toString();

@BeforeAll
public static void beforeClass() {
Expand Down Expand Up @@ -486,11 +487,25 @@ protected void waitNextMillis() {
}
}

private static String createTempDir() {
private static File createTempDir() {
try {
return Files.createTempDirectory(null).toString() + "/" + randomString(20);
final File file = Files.createTempFile(String.valueOf(System.currentTimeMillis()), "hawkbit_test").toFile();
file.deleteOnExit();
if (!file.setReadable(true, true) ||
!file.setWritable(true, true)) {
if (file.delete()) { // try to delete immediately, if failed - on exit
throw new IOException("Can't set proper permissions!");
} else {
throw new IOException("Can't set proper permissions (failed to delete the file immediately(!");
}
}
// try, if not supported - ok
if (!file.setExecutable(false)) {
log.debug("Can't set executable permissions for temp file {}", file);
}
return file;
} catch (final IOException e) {
throw new IllegalStateException("Failed to create temp directory");
throw new ArtifactStoreException("Cannot create temp file", e);
}
}

Expand Down
Loading