From cbba66783b85c29261d5eb181413c965a9d9ce39 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 11 Dec 2019 18:14:48 +0100 Subject: [CopyDirectory] New task for new minilinux slx-admin module --- .../openslx/taskmanager/tasks/CopyDirectory.java | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/main/java/org/openslx/taskmanager/tasks/CopyDirectory.java diff --git a/src/main/java/org/openslx/taskmanager/tasks/CopyDirectory.java b/src/main/java/org/openslx/taskmanager/tasks/CopyDirectory.java new file mode 100644 index 0000000..041c681 --- /dev/null +++ b/src/main/java/org/openslx/taskmanager/tasks/CopyDirectory.java @@ -0,0 +1,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; + } + +} -- cgit v1.2.3-55-g7522