    001package org.vishia.commander.target;
    002
    003import java.io.Closeable;
    004import java.io.File;
    005import java.io.FileNotFoundException;
    006import java.io.IOException;
    007
    008import org.vishia.util.FileSystem;
    009
    010public class FcmdtCopyCmd implements Closeable
    011{
    012
    013  boolean bRun;
    014
    015  String sSrc, sDst;
    016
    017  String[] param;
    018
    019  Runnable threadRun = new Runnable(){
    020    @Override public void run(){ threadRun(); }
    021  };
    022
    023  Thread threadMng = new Thread(threadRun, "CopyCmd");
    024
    025  FcmdtCopyCmd(){
    026    //threadMng.start();
    027  }
    028
    029  void startCopy(int cmd, String sParam){
    030    String sSrc; String sDst;
    031    String[] srcDst = sParam.split("\n");
    032    synchronized(this){
    033      this.sDst = srcDst[0]; this.sSrc = srcDst[1];
    034      this.param = srcDst;
    035      notify();
    036    }
    037  }
    038
    039
    040  private boolean doCopy(){
    041    boolean bOk = true;
    042    File fileSrc = new File(sSrc);
    043    if(sDst.indexOf('/') <0){
    044      //dst: only the filename is given, use the source directory.
    045      File srcDir = fileSrc.getParentFile();
    046      if(srcDir !=null){
    047        String srcPath = FileSystem.getCanonicalPath(srcDir);
    048        sDst = srcPath + "/" + sDst;
    049      }
    050    }
    051    File fileDst = new File(sDst);
    052    if(fileSrc.exists()){
    053      if(fileSrc.isDirectory()){
    054
    055      } else { //fileSrc is a file
    056        if(fileDst.exists()){
    057          if(!fileDst.canWrite()){
    058            //confirm overwrite
    059            bOk = false;
    060          } else {
    061            bOk = fileDst.delete();
    062          }
    063        }//fileDst exists
    064        if(bOk){
    065          File dstDir = fileDst.getParentFile();
    066          if(!dstDir.exists()){
    067            try{ FileSystem.mkDirPath(fileDst); }
    068            catch(FileNotFoundException exc){
    069              bOk = false;
    070              //confirm faulty dst path
    071            }
    072          }
    073        }
    074        //fileSrc is a file
    075        if(bOk){
    076          int nrofBytesCopied;
    077          try{ nrofBytesCopied = FileSystem.copyFile(fileSrc, fileDst); }
    078          catch(IOException exc){
    079            bOk = false;
    080          }
    081        }
    082      }
    083    } else { //fileSrc doesn't exists
    084      bOk = false;
    085    }
    086    return bOk;
    087  }
    088
    089
    090  void threadRun(){
    091    bRun = true;
    092    while(bRun){
    093      synchronized(this){
    094        if(param == null){
    095          try{wait(); } catch(InterruptedException exc){}
    096        }
    097      }
    098      if(param !=null){
    099        doCopy();
    100        param = null;
    101      }
    102    }
    103  }
    104
    105
    106
    107  @Override public void close()
    108  {
    109    bRun = false;
    110    synchronized(this){ notify(); }
    111  }
    112
    113
    114}