summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx
diff options
context:
space:
mode:
authorSimon Rettberg2020-01-08 15:05:36 +0100
committerSimon Rettberg2020-01-08 15:05:36 +0100
commit65df5272d9daf0c32622a7f06f82a0aeff983a48 (patch)
tree440410597a8c7ee5c140af960cabc3f2d0830745 /src/main/java/org/openslx
parent[MountVmStore] Add support for dnbd3 with no NFS fallback (diff)
downloadtmlite-bwlp-65df5272d9daf0c32622a7f06f82a0aeff983a48.tar.gz
tmlite-bwlp-65df5272d9daf0c32622a7f06f82a0aeff983a48.tar.xz
tmlite-bwlp-65df5272d9daf0c32622a7f06f82a0aeff983a48.zip
[MakeTarball] Task for creating tar from text/blob data
Diffstat (limited to 'src/main/java/org/openslx')
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/MakeTarball.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/MakeTarball.java b/src/main/java/org/openslx/taskmanager/tasks/MakeTarball.java
new file mode 100644
index 0000000..c8d8bc1
--- /dev/null
+++ b/src/main/java/org/openslx/taskmanager/tasks/MakeTarball.java
@@ -0,0 +1,83 @@
+package org.openslx.taskmanager.tasks;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
+import org.apache.commons.io.FilenameUtils;
+import org.openslx.satserver.util.Archive;
+import org.openslx.satserver.util.Util;
+import org.openslx.taskmanager.api.AbstractTask;
+
+import com.google.gson.annotations.Expose;
+
+public class MakeTarball extends AbstractTask
+{
+
+ @Expose
+ private Map<String, byte[]> files;
+
+ @Expose
+ private String destination;
+
+ private Status status = new Status();
+
+ protected static final String[] ALLOWED_DIRS = { "/tmp/", "/opt/openslx/configs/" };
+
+ @Override
+ protected boolean initTask()
+ {
+ this.setStatusObject( this.status );
+ destination = FilenameUtils.normalize( destination );
+ if ( !Util.startsWith( destination, ALLOWED_DIRS ) ) {
+ status.error = "Illegal target directory " + destination;
+ return false;
+ }
+ if ( files.isEmpty() ) {
+ status.error = "Empty list of files";
+ return false;
+ }
+ for ( Entry<String, byte[]> f : files.entrySet() ) {
+ String fn = f.getKey();
+ if ( fn == null || fn.isEmpty() ) {
+ status.error = "File list contains empty file name";
+ return false;
+ }
+ if ( f.getValue() == null ) {
+ status.error = "File list contains NULL file payload for " + fn;
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @Override
+ protected boolean execute()
+ {
+ TarArchiveOutputStream outArchive = null;
+ try {
+ try {
+ outArchive = Archive.createTarArchive( this.destination );
+ } catch ( IOException e ) {
+ status.error = "Could not create archive at " + this.destination;
+ return false;
+ }
+ for ( Entry<String, byte[]> f : files.entrySet() ) {
+ if ( !Archive.tarCreateFileFromString( outArchive, f.getKey(), f.getValue(), 0644 ) ) {
+ status.error = "Cannot add " + f.getKey() + " to " + this.destination;
+ return false;
+ }
+ }
+ } finally {
+ Util.multiClose( outArchive );
+ }
+ return true;
+ }
+
+ static class Status
+ {
+ private String error;
+ }
+
+}