Package org.jboss.fresh.shell.commands

Source Code of org.jboss.fresh.shell.commands.MvExe

package org.jboss.fresh.shell.commands;

import org.jboss.fresh.vfs.FileInfo;
import org.jboss.fresh.vfs.FileName;
import org.jboss.fresh.vfs.VFS;
import org.jboss.fresh.vfs.VFSException;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.shell.AbstractExecutable;

import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;

// EX OK

public class MvExe extends AbstractExecutable {
  private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(MvExe.class);

  /**
   Moves the specified files to the specified directory, or the specified file into a new file.
   */

  private static final String FORCE = "-f";

  public void process(String exename, String[] params) throws Exception {
    log.debug("entered");

    if (helpRequested()) {
      PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
      out.print("  Usage:\n");
      out.print("     mv [--help] [-ex] FILES DEST_DIR\n");
      out.print("         FILES : files to be moved.\n");
      out.print("      DEST_DIR : the directory for the specified files to be moved to.\n");
      out.print("    mv [--help] [-ex] FILE DEST_FILE\n");
      out.print("           FILE : file to be moved.\n");
      out.print("      DEST_FILE : the file path where the specified file is going to be moved to.\n");
      out.print("  -f force command, if one of the specified files does not exist then continue with the copying anyway.\n");
      out.close();
      log.debug("done");
      return;
    }

    try {
      PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
      VFS vfs = shell.getVFS();
      FileName pwd = new FileName(shell.getEnvProperty("PWD"));

      LinkedList paths = new LinkedList();
      boolean force = false;
      boolean errors = false;

      for (int i = 0; i < params.length; i++) {
        String param = params[i];
        log.debug("param:" + param);
        if ((i == 0) && param.equals(FORCE))
          force = true;
        else {
          FileName path = new FileName(param);
          if (path.isRelative())
            path = pwd.absolutize(path);

          //path = vfs.resolve(shell.getUserCtx(), path, true);

          if (!vfs.exists(shell.getUserCtx(), path, true) && i != params.length - 1) {
            if (canThrowEx()) {
              throw new RuntimeException(param + ": Path already exists!");
            } else {
              out.println(param + ": Path does not exists!");
              errors = true;
            }
            break;
          } else
            paths.add(path);
        }
      }

      if ((paths.size() <= 1) || (errors && !force)) {
        out.println("Usage: mv FILES DEST_DIR");
        out.println("       mv FILE DEST_FILE");

      } else {
        FileName lastPath = (FileName) paths.removeLast();
        boolean lastPathXsists = vfs.exists(shell.getUserCtx(), lastPath, true);

        if (lastPathXsists) {
          FileInfo lastInfo = vfs.getFileInfo(shell.getUserCtx(), lastPath, false);

          if (lastInfo.isDirectory() && (force || !errors)) {
            log.debug("1.Moving multiple files to dir.");
            Iterator it = paths.iterator();
            while (it.hasNext()) {
              FileName path = (FileName) it.next();
              FileInfo info = vfs.getFileInfo(shell.getUserCtx(), path, true);
              if (move(info, lastPath.absolutize(path.getName()), force, vfs, out) && !force)
                return;
            }
          } else if ((paths.size() == 1) && force) {
            log.debug("1.Moving file (overwrite).");
            FileName path = (FileName) paths.getFirst();
            FileInfo info = vfs.getFileInfo(shell.getUserCtx(), path, true);
            if (move(info, lastPath, force, vfs, out) && !force)
              return;

          } else {
            if (canThrowEx()) {
              throw new RuntimeException(lastPath + ": Destination path is not a directory!");
            } else {
              out.println(lastPath + ": Destination path is not a directory!");
            }
          }

        } else if (paths.size() == 1) {
          log.debug("1.Moving file.");
          FileName path = (FileName) paths.getFirst();
          FileInfo info = vfs.getFileInfo(shell.getUserCtx(), path, true);
          if (move(info, lastPath, force, vfs, out) && !force)
            return;

        } else {
          if (canThrowEx()) {
            throw new RuntimeException("Usage: mv FILES DEST_DIR\n       mv FILE DEST_FILE");
          } else {
            out.println("Usage: mv FILES DEST_DIR");
            out.println("       mv FILE DEST_FILE");
          }
        }
      }

      out.close();
      log.debug("entered");

    } catch (VFSException e) {
      //  e.getUnderlyingThrowable().printStackTrace();
      throw e;
    }

  }

  private boolean move(FileInfo sourceInfo, FileName dest, boolean force, VFS vfs, PrintWriter out) throws Exception {
    vfs.move(shell.getUserCtx(), sourceInfo.getFileName(), dest, true);
    out.println(sourceInfo.getFileName() + ": File or directory on all his children successfully moved.");
    return false;
  }

/*
  // returns true if errors occured
  private boolean move(FileInfo sourceInfo, FileName dest, boolean force, VFS vfs, PrintWriter out) throws Exception {
    FileInfo copy = sourceInfo;
    FileName file = sourceInfo.getFileName();
System.out.println("SOURCE = " +file);
System.out.println("DEST   = " +dest);

    if (dest.isParent(file)) {
      out.println(file+ ": Contains destination path! Operation canceled");
      if (!force)
        return true;

    } else if (copy.isDirectory()) {
System.out.println("2.Moving dir.");
      boolean destXsists = vfs.exists(shell.getUserCtx(), dest, true);

      if (force && destXsists)
        vfs.remove(shell.getUserCtx(), dest, true, false); // children stay intact

      else if (destXsists) {
        out.println(dest+ ": Destination path already exsists! Use -f to override.");
        if (!force)
          return true;
      }

      copy.setFileName(dest);
      vfs.createFile(shell.getUserCtx(), copy);
      out.println(file+ ": Path copied to " +dest);

      List fileInfos = vfs.list(shell.getUserCtx(), file, true);
      Iterator it = fileInfos.iterator();

      while (it.hasNext()) {
        FileInfo srcInfo = (FileInfo)it.next();
        boolean errors;
        errors = move(srcInfo, dest.absolutize(srcInfo.getFileName().getName()), force, vfs, out);
        if (errors && !force)
          return true;
      }
      vfs.remove(shell.getUserCtx(), file, true, true);


    } else {
System.out.println("2.Copying file.");
      boolean destXsists = vfs.exists(shell.getUserCtx(), dest, true);

      if (force && destXsists)
        vfs.remove(shell.getUserCtx(), dest, true, true);
      else if (destXsists) {
        out.println(dest+ ": Destination path already exsists! Use -f to override.");
        if (!force)
          return true;
      }

      String sourceTag = copy.getTag();
      copy.setFileName(dest);
      String targetTag = vfs.createFile(shell.getUserCtx(), copy);

      if (vfs.hasContent(shell.getUserCtx(), file, true)) {
        FileReadInfo read;
        FileOpInfo readOp, writeOp;
        readOp = new FileOpInfo();
        readOp.filename = file;
        readOp.tag = sourceTag;
        writeOp = new FileOpInfo();
        writeOp.filename = dest;
        writeOp.tag = targetTag;

        do {
          read = vfs.read(shell.getUserCtx(), readOp);
          writeOp.append = true;
          writeOp.buf = read.buf;
          writeOp.complete = !read.more;
          FileWriteInfo write = vfs.write(shell.getUserCtx(), writeOp);
          writeOp.tag = write.tag;
          readOp.offset += read.buf.length;

        } while (read.more);
      }

      vfs.remove(shell.getUserCtx(), file, true, true);
      out.println(file.toString()+ ": Path moved to " +dest);
    }

    return false;
  }
*/
}
TOP

Related Classes of org.jboss.fresh.shell.commands.MvExe

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.