summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/Symlink.java
blob: 44919fc0568142cc905444a500b0de78a7524869 (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
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()
	{
		deleteRecursiveIfExists( 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;
	}

	/**
	 * Recursively deletes `item`, which may be a directory.
	 * Symbolic links will be deleted instead of their referents.
	 * Returns a boolean indicating whether `item` still exists.
	 * http://stackoverflow.com/questions/8666420
	 */
	public static boolean deleteRecursiveIfExists( File item )
	{
		if ( !item.exists() )
			return true;
		boolean ret = true;
		if ( !Files.isSymbolicLink( item.toPath() ) && item.isDirectory() ) {
			File[] subitems = item.listFiles();
			for ( File subitem : subitems ) {
				if ( !deleteRecursiveIfExists( subitem ) ) {
					ret = false;
				}
			}
		}
		return item.delete() && ret;
	}

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

}