Improve creatTempFile/Dir usage (#2208)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-01-21 09:11:07 +02:00
committed by GitHub
parent d71a159db2
commit e64053fb59
4 changed files with 39 additions and 22 deletions

View File

@@ -93,7 +93,7 @@ public abstract class AbstractArtifactRepository implements ArtifactRepository {
} }
protected String storeTempFile(final InputStream content) throws IOException { 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))) { try (final OutputStream outputstream = new BufferedOutputStream(new FileOutputStream(file))) {
content.transferTo(outputstream); content.transferTo(outputstream);
outputstream.flush(); outputstream.flush();
@@ -104,16 +104,23 @@ public abstract class AbstractArtifactRepository implements ArtifactRepository {
protected abstract AbstractDbArtifact store(final String tenant, final DbArtifactHash base16Hashes, protected abstract AbstractDbArtifact store(final String tenant, final DbArtifactHash base16Hashes,
final String contentType, final String tempFile) throws IOException; final String contentType, final String tempFile) throws IOException;
private static File createTempFile() { static File createTempFile(final boolean directory) {
try { try {
final File file = Files.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX).toFile(); final File file = (directory
if (!file.setReadable(true, true) || ? Files.createTempDirectory(TEMP_FILE_PREFIX)
!file.setWritable(true, true)) { : Files.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX)).toFile();
throw new IOException("Can't set proper permissions!"); 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 // try, if not supported - ok
file.setExecutable(false); if (!file.setExecutable(false)) {
file.deleteOnExit(); log.debug("Can't set executable permissions for temp file {}", file);
}
return file; return file;
} catch (final IOException e) { } catch (final IOException e) {
throw new ArtifactStoreException("Cannot create temp file", e); throw new ArtifactStoreException("Cannot create temp file", e);

View File

@@ -14,7 +14,6 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.Random; import java.util.Random;
import io.qameta.allure.Description; import io.qameta.allure.Description;
@@ -40,9 +39,9 @@ class ArtifactFilesystemRepositoryTest {
private static ArtifactFilesystemRepository artifactFilesystemRepository; private static ArtifactFilesystemRepository artifactFilesystemRepository;
@BeforeAll @BeforeAll
static void setup() throws IOException { static void setup() {
artifactResourceProperties = new ArtifactFilesystemProperties(); artifactResourceProperties = new ArtifactFilesystemProperties();
artifactResourceProperties.setPath(Files.createTempDirectory(null).toString()); artifactResourceProperties.setPath(AbstractArtifactRepository.createTempFile(true).toString());
artifactFilesystemRepository = new ArtifactFilesystemRepository(artifactResourceProperties); artifactFilesystemRepository = new ArtifactFilesystemRepository(artifactResourceProperties);
} }

View File

@@ -15,7 +15,6 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import io.qameta.allure.Description; import io.qameta.allure.Description;
import io.qameta.allure.Feature; import io.qameta.allure.Feature;
@@ -43,12 +42,9 @@ class ArtifactFilesystemTest {
@Test @Test
@Description("Verifies that an InputStream can be opened if file exists") @Description("Verifies that an InputStream can be opened if file exists")
void getInputStreamOfExistingFile() throws IOException { void getInputStreamOfExistingFile() throws IOException {
final File createTempFile = Files.createTempFile(ArtifactFilesystemTest.class.getSimpleName(), "").toFile();
createTempFile.deleteOnExit();
final ArtifactFilesystem underTest = new ArtifactFilesystem( final ArtifactFilesystem underTest = new ArtifactFilesystem(
createTempFile, ArtifactFilesystemTest.class.getSimpleName(), new DbArtifactHash("1", "2", "3"), 0L, null); AbstractArtifactRepository.createTempFile(false), ArtifactFilesystemTest.class.getSimpleName(),
final byte[] buffer = new byte[1024]; new DbArtifactHash("1", "2", "3"), 0L, null);
assertThat(IOUtils.read(underTest.getFileInputStream(), buffer)).isZero(); assertThat(IOUtils.read(underTest.getFileInputStream(), new byte[16])).isZero();
} }
} }

View File

@@ -27,6 +27,7 @@ import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.eclipse.hawkbit.artifact.repository.ArtifactRepository; import org.eclipse.hawkbit.artifact.repository.ArtifactRepository;
import org.eclipse.hawkbit.artifact.repository.ArtifactStoreException;
import org.eclipse.hawkbit.cache.TenantAwareCacheManager; import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
import org.eclipse.hawkbit.repository.ArtifactManagement; import org.eclipse.hawkbit.repository.ArtifactManagement;
import org.eclipse.hawkbit.repository.ConfirmationManagement; import org.eclipse.hawkbit.repository.ConfirmationManagement;
@@ -188,7 +189,7 @@ public abstract class AbstractIntegrationTest {
protected ServiceMatcher serviceMatcher; protected ServiceMatcher serviceMatcher;
@Autowired @Autowired
protected ApplicationEventPublisher eventPublisher; protected ApplicationEventPublisher eventPublisher;
private static final String ARTIFACT_DIRECTORY = createTempDir(); private static final String ARTIFACT_DIRECTORY = createTempDir().toString();
@BeforeAll @BeforeAll
public static void beforeClass() { public static void beforeClass() {
@@ -483,11 +484,25 @@ public abstract class AbstractIntegrationTest {
} }
} }
private static String createTempDir() { private static File createTempDir() {
try { 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) { } catch (final IOException e) {
throw new IllegalStateException("Failed to create temp directory"); throw new ArtifactStoreException("Cannot create temp file", e);
} }
} }