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

import java.io.File;
import java.io.IOException;

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

import com.google.gson.annotations.Expose;

public class CopyDirectory extends AbstractTask
{
	private static final Logger LOG = Logger.getLogger( CopyDirectory.class );

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

	@Expose
	private String source = null;
	@Expose
	private String destination = null;

	private Output status = new Output();

	@Override
	protected boolean initTask()
	{
		this.source = FilenameUtils.normalize( this.source );
		if ( this.source == null || !Util.startsWith( this.source, ALLOWED_DIRS ) || this.source.endsWith( "/" ) ) {
			status.error = "Source not in allowed directory";
			return false;
		}
		this.destination = FilenameUtils.normalize( this.destination );
		if ( this.destination == null || !Util.startsWith( this.destination, ALLOWED_DIRS ) || this.destination.endsWith( "/" ) ) {
			status.error = "Destination not in allowed directory";
			return false;
		}
		return true;
	}

	@Override
	protected boolean execute()
	{
		File src = new File( this.source );
		File dst = new File( this.destination );
		if ( !src.exists() || !src.isDirectory() ) {
			status.error = this.source + " is not a directory!";
			return false;
		}
		if ( !src.canRead() ) {
			status.error = "Cannot read " + this.source;
			return false;
		}
		try {
			if ( dst.exists() && dst.isDirectory() )
				FileUtils.deleteDirectory( dst );
			FileUtils.copyDirectory( src, dst );
		} catch ( IOException e ) {
			status.error = e.toString();
			return false;
		}
		return true;
	}

	private class Output
	{
		public String error = null;
	}

}