summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/MakeTarball.java
blob: c8d8bc13455bac1540fd82f10e5b21b537622992 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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;
	}

}