summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/Symlink.java
blob: 7a6a3bd8d61b808bd5cd671ff943b1270c3bd5c6 (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.File;
import java.nio.file.Files;
import java.nio.file.Paths;

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

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

	@Expose
	private String target = null;
	@Expose
	private String linkname = null;

	private Output status = new Output();

	@Override
	protected boolean initTask()
	{
		if ( Util.isEmpty( this.linkname ) ) {
			status.error = "Link name empty";
			return false;
		}
		if ( this.linkname.endsWith( "/" ) ) {
			status.error = "Link name ends with slash";
			return false;
		}
		this.setStatusObject( status );
		if ( Util.isEmpty( this.target ) ) {
			status.error = "Target empty";
			return false;
		}
		this.linkname = FilenameUtils.normalize( this.linkname );
		if ( !Util.startsWith( this.linkname, ALLOWED_DIRS ) ) {
			status.error = "Link name not in allowed directory";
			return false;
		}
		if ( !this.target.startsWith( "/" ) ) {
			this.target = new File( this.linkname ).getParent() + "/" + this.target;
		}
		this.target = FilenameUtils.normalize( this.target );
		if ( !Util.startsWith( this.target, ALLOWED_DIRS ) ) {
			status.error = "Target not in allowed directory";
			return false;
		}
		return true;
	}

	@Override
	protected boolean execute()
	{
		FileUtils.deleteQuietly( new File( linkname ) );
		if ( this.target != null ) {
   		try {
   			Files.createSymbolicLink( Paths.get( this.linkname ), Paths.get( this.target ) );
   		} catch ( Exception e ) {
   			status.error = e.toString();
   			return false;
   		}
		}
		return true;
	}

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

}