blob: f1b5db2f1b01ea223d261f8357b43bc364972919 (
plain) (
tree)
|
|
package org.openslx.taskmanager.tasks;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.LinkOption;
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 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 ( !Files.exists( item.toPath(), LinkOption.NOFOLLOW_LINKS ) )
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;
}
}
|