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

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

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 DeleteFile extends AbstractTask
{

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

	@Expose
	private String file = null;

	private Output status = new Output();

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

	@Override
	protected boolean execute()
	{
		try {
			Files.deleteIfExists( Paths.get( this.file ) );
		} catch ( IOException 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;
	}

}