summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/DeleteDirectory.java
blob: 1a0de0567d7fb9b53106624e134728b0fe351cbd (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
package org.openslx.taskmanager.tasks;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.openslx.satserver.util.Util;
import org.openslx.taskmanager.api.AbstractTask;

import com.google.gson.annotations.Expose;

public class DeleteDirectory extends AbstractTask
{

	protected static final String[] ALLOWED_DIRS =
	{ "/tmp/", "/srv/openslx/www/boot/" };

	@Expose
	private String path = null;
	@Expose
	private boolean recursive = false;

	private Output status = new Output();

	@Override
	protected boolean initTask()
	{
		this.setStatusObject( status );
		this.path = FilenameUtils.normalize( this.path );
		if ( this.path == null || !Util.startsWith( this.path, ALLOWED_DIRS )
				|| Arrays.asList( ALLOWED_DIRS ).contains( this.path ) ) {
			status.error = "Directory not in allowed directory";
			return false;
		}
		return true;
	}

	@Override
	protected boolean execute()
	{
		try {
			FileUtils.deleteDirectory( new File( this.path ) );
		} catch ( Exception e1 ) {
			status.error = e1.toString();
			return false;
		}
		return true;
	}

	/**
	 * Output - contains additional status data of this task
	 */
	@SuppressWarnings( "unused" )
	private static class Output
	{
		protected String error = null;
	}

}