Package org.jboss.fresh.shell.commands

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

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.io.BufferWriter;
import org.jboss.fresh.shell.AbstractExecutable;

import java.io.PrintWriter;

// EX OK

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

  public static final String directParam = "-d";

  //  mkdir [PATHS]
  //  every path must have existing parent!
  public void process(String exename, String[] params) throws Exception {
    log.debug("entered");

    if (helpRequested()) {
      PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
      out.print("Usage: ln [--help] [-ex] TARGET LINK [-d]\n");
      out.print("    -d : direct\n");
      out.print("    --help : this help\n");
      out.close();
      log.debug("done");
      return;
    }


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

    // List paths = new LinkedList();
    boolean direct = false;


    if ((params.length != 2) && (params.length != 3)) {
      if (canThrowEx()) {
        throw new Exception("Usage: ln TARGET LINK [-d]");
      } else {
        out.println("Usage: ln TARGET LINK [-d]");
      }
    } else {
      FileName target = new FileName(params[0]);
      FileName link = new FileName(params[1]);

      if ((params.length > 2) && (params[2].equals(directParam)))
        direct = true;

      if (target.isRelative())
        target = pwd.absolutize(target);
      target = vfs.resolve(shell.getUserCtx(), target, direct);
      if (link.isRelative())
        link = pwd.absolutize(link);
//      link = vfs.resolve(shell.getUserCtx(), link, true);

      boolean targetXsts, linkXsts;
      targetXsts = vfs.exists(shell.getUserCtx(), target, true);
      linkXsts = vfs.exists(shell.getUserCtx(), link, true);

      if (targetXsts && !linkXsts) {
        FileInfo linkInfo = new FileInfo(link, FileInfo.TYPE_LINK);
        linkInfo.setTarget(target);
        vfs.createFile(shell.getUserCtx(), linkInfo);

      } else {
        if (!targetXsts) {
          if (canThrowEx()) {
            throw new Exception(target + ": Target must exist! Cannot create a dead link!");
          } else {
            out.println(target + ": Target must exist! Cannot create a dead link!");
          }
        }

        if (linkXsts) {
          if (canThrowEx()) {
            throw new Exception(link + ": Link path must not exist! Cannot overwrite existing path!");
          } else {
            out.println(link + ": Link path must not exist! Cannot overwrite existing path!");
          }
        }
      }
    }

    out.close();

    log.debug("done");
  }
}
TOP

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

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.