All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.UnixUtil Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util;

import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;

//------------------------------------------------------------------------------
/**
 * Unix utilities.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class UnixUtil
{
   //--------------------------------------------------------------------------
   /**
    Function to create a symbolic link.
    @param inFrom the file being linked to.
    @param inLink the file that will become the symbolic link.
    @throws IOException
    */
   public static void symlink(File inFrom, File inLink)
      throws IOException
   {
      boolean createLink = true;

      if (isSymlink(inLink))
      {
         if (readlink(inLink).equals(inFrom.getPath()))
         {
            // The requested link already exists.
            createLink = false;
         }
      }
      else if (inLink.exists())
      {
         throw new IOException("Cannot replace regular file " + StringUtil.singleQuote(inFrom.getPath())
               + " with a symlink!");
      }

      if (createLink)
      {
         String parent = inLink.getParent();
         if (! StringUtil.isSet(parent)) parent = ".";

         Executor exe = new Executor();
         exe.setCommand("cd " + StringUtil.singleQuote(parent) + "; ln -s "
               + StringUtil.singleQuote(inFrom.getPath()) + " " + StringUtil.singleQuote(inLink.getName()));

         if (exe.exec() != 0)
         {
            throw new IOException("Error executing \"" + exe.getCommand() + "\": " + exe.getSTDERR());
         }
      }
   }

   //--------------------------------------------------------------------------
   /**
    Uses the 'stat' command to test if the specified file is a symbolic link.
    */
   public static boolean isSymlink(File inFile)
   {
      boolean result = false;

      Executor exe = new Executor();

      if (OS.value() == OS.Mac)
      {
         exe.setCommand("stat -f '%HT' " + StringUtil.singleQuote(inFile.getPath()));
         if (exe.exec() != 0
               && exe.getSTDERR().indexOf("No such file") < 0)
         {
            throw new RuntimeException("Error executing \"" + exe.getCommand() + "\": " + exe.getSTDERR());
         }
         else if (exe.getSTDOUT().trim().equals("Symbolic Link"))
         {
            result = true;
         }
      }
      else
      {
         exe.setCommand("stat " + StringUtil.singleQuote(inFile.getPath()));
         if (exe.exec() != 0
               && exe.getSTDERR().indexOf("No such file") < 0)
         {
            throw new RuntimeException("Error executing \"" + exe.getCommand() + "\": " + exe.getSTDERR());
         }
         else if (exe.getSTDOUT().contains("symbolic link"))
         {
            result = true;
         }
      }

      return result;
   }

   //--------------------------------------------------------------------------
   /**
    Uses the 'readlink' command to read the contents of a symbolic link.
    */
   public static String readlink(File inFile)
         throws IOException
   {
      if (! isSymlink(inFile))
      {
         throw new IOException(StringUtil.singleQuote(inFile.getPath()) + " is not a symlink!");
      }

      Executor exe = new Executor();
      exe.setCommand("readlink " + StringUtil.singleQuote(inFile.getPath()));

      if (exe.exec() != 0)
      {
         throw new IOException("Error executing \"" + exe.getCommand() + "\": " + exe.getSTDERR());
      }

      return exe.getSTDOUT().trim();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy