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; } }