summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorralph isenmann2022-01-27 11:42:13 +0100
committerralph isenmann2022-01-27 11:42:13 +0100
commit07cbc76b1f3d408f5899caff9add8e4138871579 (patch)
treecec036184057b8ccc1ea97726df5958ac3c07087 /src
parentUpdate log4j because of the CVE-2021-45105 security flaw (diff)
downloadmaster-sync-shared-07cbc76b1f3d408f5899caff9add8e4138871579.tar.gz
master-sync-shared-07cbc76b1f3d408f5899caff9add8e4138871579.tar.xz
master-sync-shared-07cbc76b1f3d408f5899caff9add8e4138871579.zip
Add TarArchiveReader and TarArchiveWriter as Util classes.
- Wraps the jtar dependency
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/openslx/util/TarArchiveUtil.java127
-rw-r--r--src/main/java/org/openslx/virtualization/configuration/container/ContainerDefinition.java51
2 files changed, 133 insertions, 45 deletions
diff --git a/src/main/java/org/openslx/util/TarArchiveUtil.java b/src/main/java/org/openslx/util/TarArchiveUtil.java
index 09c4a2c..20af003 100644
--- a/src/main/java/org/openslx/util/TarArchiveUtil.java
+++ b/src/main/java/org/openslx/util/TarArchiveUtil.java
@@ -2,28 +2,133 @@ package org.openslx.util;
import org.kamranzafar.jtar.TarEntry;
import org.kamranzafar.jtar.TarHeader;
+import org.kamranzafar.jtar.TarInputStream;
import org.kamranzafar.jtar.TarOutputStream;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+
+
public class TarArchiveUtil {
+ private TarArchiveUtil() {}
+ public static class TarArchiveReader implements AutoCloseable {
+ private boolean isCompressed;
+ private final TarInputStream tarInputStream;
- public static void tarPutFile(TarOutputStream output, String fileName, String data) throws IOException
- {
- if (data == null)
- return;
- tarPutFile(output, fileName, data.getBytes(StandardCharsets.UTF_8));
+ private TarEntry currentEntry = null;
+
+ public TarArchiveReader(InputStream in) throws IOException {
+ this(in,true,false);
+ }
+
+ public TarArchiveReader(InputStream in, boolean isBuffered, boolean isCompressed) throws IOException {
+ this.isCompressed = isCompressed;
+
+ InputStream stream = in;
+ if (isBuffered) {
+ stream = new BufferedInputStream(stream);
+ }
+
+ if (isCompressed) {
+ stream = new GZIPInputStream(stream);
+ }
+
+ this.tarInputStream = new TarInputStream(stream);
+ }
+
+ public boolean hasNextEntry() throws IOException {
+ this.currentEntry = this.tarInputStream.getNextEntry();
+ if (this.currentEntry != null) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public String getEntryName() {
+ return this.currentEntry.getName();
+ }
+
+ public byte[] readCurrentEntry() throws IOException {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ byte[] rawData = new byte[1024];
+ int count = 0;
+
+ while ((count = this.tarInputStream.read(rawData)) != -1) {
+ output.write(rawData, 0, count);
+ }
+
+ return output.toByteArray();
+ }
+
+ @Override
+ public void close() throws IOException {
+ tarInputStream.close();
+ }
+
}
- public static void tarPutFile(TarOutputStream output, String fileName, byte[] data) throws IOException
+ public static class TarArchiveWriter implements AutoCloseable
{
- if (data == null)
- return;
- output.putNextEntry(new TarEntry(
- TarHeader.createHeader(fileName, data.length, Util.unixTime(), false, 0644)));
- output.write(data);
+ private boolean isBuffered;
+ private boolean isCompressed;
+ private final TarOutputStream tarOutputStream;
+
+ public TarArchiveWriter (OutputStream out) throws IOException {
+ this(out, true, true);
+ }
+
+ public TarArchiveWriter (OutputStream out, boolean isBuffered, boolean isCompressed) throws IOException {
+ this.isBuffered = isBuffered;
+ this.isCompressed = isCompressed;
+
+ OutputStream stream = out;
+
+ if (isBuffered) {
+ stream = new BufferedOutputStream(stream);
+ }
+
+ if (isCompressed) {
+ stream = new GZIPOutputStream(stream);
+ }
+
+ this.tarOutputStream = new TarOutputStream(stream);
+ }
+
+ public void writeFile(String filename, String data) throws IOException {
+
+ if (data == null)
+ return;
+ putFile(filename, data.getBytes(StandardCharsets.UTF_8));
+ }
+
+ public void writeFile(String filename, byte[] data) throws IOException {
+ if (data == null)
+ return;
+ putFile(filename, data);
+ }
+
+ private void putFile(String filename, byte[] data) throws IOException {
+ if (data == null)
+ return;
+ tarOutputStream.putNextEntry(new TarEntry(TarHeader.createHeader(filename,
+ data.length, Util.unixTime(), false, 0644)));
+ tarOutputStream.write(data);
+ }
+
+ @Override
+ public void close() throws Exception {
+ tarOutputStream.close();
+ }
}
}
diff --git a/src/main/java/org/openslx/virtualization/configuration/container/ContainerDefinition.java b/src/main/java/org/openslx/virtualization/configuration/container/ContainerDefinition.java
index 91cf50c..825d0c3 100644
--- a/src/main/java/org/openslx/virtualization/configuration/container/ContainerDefinition.java
+++ b/src/main/java/org/openslx/virtualization/configuration/container/ContainerDefinition.java
@@ -6,17 +6,14 @@ import com.google.gson.stream.JsonReader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import org.kamranzafar.jtar.TarEntry;
-import org.kamranzafar.jtar.TarInputStream;
-import org.kamranzafar.jtar.TarOutputStream;
-import org.openslx.util.TarArchiveUtil;
+import org.openslx.util.Util;
+import org.openslx.util.TarArchiveUtil.TarArchiveReader;
+import org.openslx.util.TarArchiveUtil.TarArchiveWriter;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
-import java.util.zip.GZIPInputStream;
-import java.util.zip.GZIPOutputStream;
public class ContainerDefinition {
@@ -64,26 +61,15 @@ public class ContainerDefinition {
ContainerDefinition containerDef = new ContainerDefinition();
try {
- TarInputStream tis = new TarInputStream(
- new GZIPInputStream(new BufferedInputStream(new ByteArrayInputStream(rawTarData))));
+ TarArchiveReader tarReader = new TarArchiveReader(new ByteArrayInputStream(rawTarData), true, true);
- TarEntry entry;
-
- while ((entry = tis.getNextEntry()) != null) {
- byte[] rawData = new byte[1024];
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- int count;
-
- // read everything from the TarInputStream for the current Entry
- while ((count = tis.read(rawData)) != -1) {
- output.write(rawData, 0, count);
- }
-
- if (entry.getName().equals(CONTAINER_FILE))
- containerDef.setContainerRecipe(output.toByteArray());
- if (entry.getName().equals(CONTAINER_META_FILE))
- containerDef.setContainerMeta(output.toByteArray());
+ while (tarReader.hasNextEntry()) {
+ if (tarReader.getEntryName().equals(CONTAINER_FILE))
+ containerDef.setContainerRecipe(tarReader.readCurrentEntry());
+ if (tarReader.getEntryName().equals(CONTAINER_META_FILE))
+ containerDef.setContainerMeta(tarReader.readCurrentEntry());
}
+ tarReader.close();
} catch (IOException e) {
LOGGER.error("Could not create a ContainerDefinition Object for rawTarData", e);
@@ -127,18 +113,15 @@ public class ContainerDefinition {
public ByteBuffer toByteBuffer() {
ByteBuffer containerDef = null;
-
- try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- TarOutputStream output = new TarOutputStream(new GZIPOutputStream(baos));
-
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- TarArchiveUtil.tarPutFile(output, CONTAINER_META_FILE, gson.toJson(containerMeta));
- TarArchiveUtil.tarPutFile(output, CONTAINER_FILE, containerRecipe);
- output.close();
+ Gson gson = new GsonBuilder().setPrettyPrinting().create();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try {
+ TarArchiveWriter tarWriter = new TarArchiveWriter(baos);
+ tarWriter.writeFile(CONTAINER_META_FILE, gson.toJson(containerMeta));
+ tarWriter.writeFile(CONTAINER_FILE, containerRecipe);
+ Util.safeClose(tarWriter);
containerDef = ByteBuffer.wrap(baos.toByteArray());
-
} catch (IOException e) {
LOGGER.warn("Could not create a tar file", e);
}