summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/RecompressArchive.java
blob: 04e7ed6c94096a614941b0eb66ae9498b1cb171f (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package org.openslx.taskmanager.tasks;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.io.FileUtils;
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 RecompressArchive extends AbstractTask
{

	@Expose
	private Map<String, Boolean> inputFiles;
	
	@Expose
	private String outputFile;
	
	@Expose
	private boolean forceRoot;

	/*
	 * Own vars/constants not being deserialized
	 */

	protected static final String[] ALLOWED_DIRS =
	{ "/tmp/", "/opt/openslx/configs/" };

	private Output status = new Output();

	/*
	 * Code
	 */

	@Override
	protected boolean execute()
	{
		String fn = new File( this.outputFile ).getName();
		String tmpFile = "/tmp/bwlp-" + System.nanoTime() + "-" + fn;
		if ( execute2( tmpFile ) ) {
			try {
				FileUtils.deleteQuietly( new File( this.outputFile ) );
				Files.move( Paths.get( tmpFile ), Paths.get( this.outputFile ) );
				return true;
			} catch ( Exception e ) {
				status.error = e.toString();
				status.errorCode = Output.ErrorCode.WRITE_FAILED;
			}
		}
		FileUtils.deleteQuietly( new File( tmpFile ) );
		return false;
	}

	private boolean execute2( String archiveFile )
	{
		// Open output file archive
		TarArchiveOutputStream outArchive = null;
		try {
			try {
				FileUtils.forceMkdir( new File( new File( this.outputFile ).getParent() ) );
				outArchive = Archive.createTarArchive( archiveFile );
			} catch ( IOException e2 ) {
				status.error = e2.toString();
				status.errorCode = Output.ErrorCode.WRITE_FAILED;
				return false;
			}

			// Remember all files added, so we can issue warnings for duplicate entries
			Map<String, List<A>> entries = new HashMap<>();
			// Open input file archives, one by one
			for ( final Entry<String, Boolean> it : this.inputFiles.entrySet() ) {
				final A a = new A( it.getKey(), it.getValue() );
				ArchiveInputStream inArchive = null;
				try {
					try {
						inArchive = Archive.getArchiveInputStream( a.inputFile );
					} catch ( FileNotFoundException e1 ) {
						status.error = "(" + a.inputFile + ") " + e1.getMessage();
						status.errorCode = Output.ErrorCode.NOT_FOUND;
						return false;
					} catch ( IllegalArgumentException e1 ) {
						status.error = "(" + a.inputFile + ") " + e1.getMessage();
						status.errorCode = Output.ErrorCode.UNKNOWN_ERROR;
						return false;
					} catch ( ArchiveException e1 ) {
						status.error = "(" + a.inputFile + ") " + e1.getMessage();
						status.errorCode = Output.ErrorCode.UNKNOWN_FORMAT;
						return false;
					}
					// It's open
					ArchiveEntry inEntry;
					// Iterate over every entry
					while ( ( inEntry = inArchive.getNextEntry() ) != null ) {
						if ( !inArchive.canReadEntryData( inEntry ) )
							continue; // Skip unreadable entries
						if ( inEntry.getName().equals( "/" ) || inEntry.getName().equals( "./" ) )
							continue;
						// Construct TarArchiveEntry - we want unix stuff like uid/gid, links, file/dir mode, so try to get from source archive
						TarArchiveEntry outEntry = Archive.createTarArchiveEntry( inEntry, 0, 0, 0755, 0644, this.forceRoot );
						if ( outEntry == null ) {
							status.addWarning( "Ignoring invalid entry " + inEntry.getName() + " in " + a.inputFile );
							continue;
						}
						// Ask lib if the entry can be written
						if ( !outArchive.canWriteEntryData( outEntry ) ) {
							status.addWarning( "Commons-compress says entry '" + outEntry.getName() + "' cannot be written." );
							continue;
						}
						if ( !outEntry.isDirectory() ) {
							// Dupcheck
							List<A> existing = entries.get( outEntry.getName() );
							if ( existing == null ) {
								entries.put( outEntry.getName(), existing = new ArrayList<A>() );
								existing.add( a );
							} else {
								existing.add( a );
								continue; // Just record, but don't bloat archive
							}
						}
						// Actually add entry now
						try {
							outArchive.putArchiveEntry( outEntry );
						} catch (Exception e) {
							status.error = "(" + a.inputFile + ") Could not putArchiveEntry('" + outEntry.getName() + "'): " + e.getMessage();
							status.errorCode = Output.ErrorCode.TAR_ERROR;
							return false;
						}
						if ( !Util.streamCopy( inArchive, outArchive, outEntry.getSize() ) ) {
							status.error = "(" + a.inputFile + ") Could not copy entry '" + inEntry.getName() + "' to destination archive.";
							status.errorCode = Output.ErrorCode.WRITE_FAILED;
							return false;
						}
						outArchive.closeArchiveEntry();
					} // End entry
				} catch ( IOException e ) {
					return false;
				} finally {
					Util.multiClose( inArchive );
				}
			} // End of loop over input archives
			
			// Check for dups
			boolean hasAnyDup = false;
			for ( Entry<String, List<A>> fit : entries.entrySet() ) {
				if ( fit.getValue().size() < 2 )
					continue;
				boolean hasDup = false;
				for ( A lit : fit.getValue() ) {
					if ( lit.dupCheck ) {
						hasDup = true;
						break;
					}
				}
				if ( !hasDup ) // No file requested to be checked for duplicates
					continue;
				hasAnyDup = true;
				status.addWarning( "Duplicate entry: " + fit.getKey() );
				for ( A lit : fit.getValue() ) {
					status.addWarning( "  Source: " + lit.inputFile );
				}
			}
			if ( hasAnyDup ) {
				status.addWarning( "You have duplicate files in your source archives. It's undefined which one ends up in the final archive." );
			}

			return true;
		} finally {
			Util.multiClose( outArchive );
		}
	}

	@Override
	protected boolean initTask()
	{
		this.setStatusObject( this.status );
		// Input files
		if ( this.inputFiles == null ) {
			status.error = "Input: No archives given";
			status.errorCode = Output.ErrorCode.UNKNOWN_ERROR;
			return false;
		}
		for ( String inputFile : this.inputFiles.keySet() ) {
			if ( inputFile == null || inputFile.length() == 0 || inputFile.charAt( 0 ) != '/' ) {
				status.error = "Input: Need absolute path (got: " + inputFile + ")";
				status.errorCode = Output.ErrorCode.INVALID_DIRECTORY;
				return false;
			}
			inputFile = FilenameUtils.normalize( inputFile );
			if ( inputFile == null || !Util.startsWith( inputFile, ALLOWED_DIRS ) ) {
				status.error = "Input: File not in allowed directory";
				status.errorCode = Output.ErrorCode.INVALID_DIRECTORY;
				return false;
			}
		}
		// Output file
		if ( this.outputFile == null || this.outputFile.length() == 0 || this.outputFile.charAt( 0 ) != '/' ) {
			status.error = "Output: Need absolute path.";
			status.errorCode = Output.ErrorCode.INVALID_DIRECTORY;
			return false;
		}
		this.outputFile = FilenameUtils.normalize( this.outputFile );
		if ( !Util.startsWith( this.outputFile, ALLOWED_DIRS ) ) {
			status.error = "Output: File not in allowed directory";
			status.errorCode = Output.ErrorCode.INVALID_DIRECTORY;
			return false;
		}
		return true;
	}

	@SuppressWarnings( "unused" )
	private static class Output
	{
		protected String error = null;

		protected enum ErrorCode
		{
			NOT_FOUND, UNKNOWN_FORMAT, UNKNOWN_ERROR, INVALID_DIRECTORY, WRITE_FAILED, TAR_ERROR
		};

		protected ErrorCode errorCode = null;
		protected String warnings = null;

		public static class Entry
		{
			protected String name = null;
			protected boolean isdir = false;
			protected long size = -1;
		}
		
		public void addWarning( String txt )
		{
			if (warnings == null) {
				warnings = txt;
			} else {
				warnings += "\n" + txt;
			}
		}
	}
	
	private static class A
	{
		public final String inputFile;
		public final Boolean dupCheck;
		public A( String file, Boolean dupCheck )
		{
			this.inputFile = file;
			this.dupCheck = dupCheck;
		}
	}

}