<commit_before>/*
* JBoss, Home of Professional Open Source
* Copyright 2006, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This 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 software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.virtual.plugins.copy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;

import org.jboss.logging.Logger;
import org.jboss.util.id.GUID;
import org.jboss.virtual.VirtualFile;
import org.jboss.virtual.plugins.context.file.FileSystemContext;
import org.jboss.virtual.plugins.context.DelegatingHandler;
import org.jboss.virtual.spi.VirtualFileHandler;

/**
 * Copy mechanism to be used in VFSUtils.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public abstract class AbstractCopyMechanism implements CopyMechanism
{
   private static Logger log = Logger.getLogger(CopyMechanism.class);

   private static File tempDir;

   private static class GetTempDir implements PrivilegedAction<File>
   {
      public File run()
      {
         String tempDirKey = System.getProperty("vfs.temp.dir", "jboss.server.temp.dir");
         return new File(System.getProperty(tempDirKey, System.getProperty("java.io.tmpdir")));
      }
   }

   /**
    * Get temp directory.
    *
    * @return the temp directory
    */
   public synchronized static File getTempDirectory()
   {
      if (tempDir == null)
      {
         tempDir = AccessController.doPrivileged(new GetTempDir());
         log.info("VFS temp dir: " + tempDir);
      }
      return tempDir;
   }

   /**
    * Get mechanism type.
    *
    * @return the type
    */
   protected abstract String getType();

   /**
    * Is handler already modified.
    *
    * @param handler the handler
    * @return true if already modified
    * @throws IOException for any error
    */
   protected abstract boolean isAlreadyModified(VirtualFileHandler handler) throws IOException;

   /**
    * Should we replace old handler with new.
    *
    * @param parent the parent handler
    * @param oldHandler the old handler
    * @param newHandler the new handler
    * @return true if needs replacement
    * @throws IOException for any error
    */
   protected abstract boolean replaceOldHandler(VirtualFileHandler parent, VirtualFileHandler oldHandler, VirtualFileHandler newHandler) throws IOException;

   /**
    * Unwrap the handler from possible delegate handler.
    *
    * @param handler the handler to unwrap
    * @return unwrapped handler
    */
   protected VirtualFileHandler unwrap(VirtualFileHandler handler)
   {
      if (handler instanceof DelegatingHandler)
         handler = ((DelegatingHandler)handler).getDelegate();
      return handler;
   }

   public VirtualFile copy(VirtualFile file, VirtualFileHandler handler) throws IOException, URISyntaxException
   {
      VirtualFileHandler unwrapped = unwrap(handler);
      // check modification on unwrapped
      if (isAlreadyModified(unwrapped))
      {
         if (log.isTraceEnabled())
            log.trace("Should already be " + getType() + ": " + unwrapped);
         return file;
      }

      //create guid dir
      File guidDir = createTempDirectory(getTempDirectory(), GUID.asString());
      // unpack handler
      File copy = copy(guidDir, handler);
      // create new handler
      FileSystemContext fileSystemContext = new FileSystemContext(copy);
      VirtualFileHandler newHandler = fileSystemContext.getRoot();
      VirtualFileHandler parent = handler.getParent();
      if (parent != null && replaceOldHandler(parent, handler, newHandler))
         parent.replaceChild(handler, newHandler);

      return newHandler.getVirtualFile();
   }

   /**
    * Copy handler.
    *
    * @param guidDir the guid directory
    * @param handler the handler to copy
    * @return handler's copy as file
    * @throws IOException for any error
    */
   protected File copy(File guidDir, VirtualFileHandler handler) throws IOException
   {
      File copy = createTempDirectory(guidDir, handler.getName());
      unpack(handler, copy, false);
      return copy;
   }

   /**
    * Create the temp directory.
    *
    * @param parent the parent<commit_msg>Copy options when copying file.<commit_after>/*
* JBoss, Home of Professional Open Source
* Copyright 2006, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This 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 software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.virtual.plugins.copy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;
import java.util.Map;

import org.jboss.logging.Logger;
import org.jboss.util.id.GUID;
import org.jboss.virtual.VirtualFile;
import org.jboss.virtual.plugins.context.DelegatingHandler;
import org.jboss.virtual.plugins.context.file.FileSystemContext;
import org.jboss.virtual.spi.VFSContext;
import org.jboss.virtual.spi.VirtualFileHandler;

/**
 * Copy mechanism to be used in VFSUtils.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public abstract class AbstractCopyMechanism implements CopyMechanism
{
   private static Logger log = Logger.getLogger(CopyMechanism.class);

   private static File tempDir;

   private static class GetTempDir implements PrivilegedAction<File>
   {
      public File run()
      {
         String tempDirKey = System.getProperty("vfs.temp.dir", "jboss.server.temp.dir");
         return new File(System.getProperty(tempDirKey, System.getProperty("java.io.tmpdir")));
      }
   }

   /**
    * Get temp directory.
    *
    * @return the temp directory
    */
   public synchronized static File getTempDirectory()
   {
      if (tempDir == null)
      {
         tempDir = AccessController.doPrivileged(new GetTempDir());
         log.info("VFS temp dir: " + tempDir);
      }
      return tempDir;
   }

   /**
    * Get mechanism type.
    *
    * @return the type
    */
   protected abstract String getType();

   /**
    * Is handler already modified.
    *
    * @param handler the handler
    * @return true if already modified
    * @throws IOException for any error
    */
   protected abstract boolean isAlreadyModified(VirtualFileHandler handler) throws IOException;

   /**
    * Should we replace old handler with new.
    *
    * @param parent the parent handler
    * @param oldHandler the old handler
    * @param newHandler the new handler
    * @return true if needs replacement
    * @throws IOException for any error
    */
   protected abstract boolean replaceOldHandler(VirtualFileHandler parent, VirtualFileHandler oldHandler, VirtualFileHandler newHandler) throws IOException;

   /**
    * Unwrap the handler from possible delegate handler.
    *
    * @param handler the handler to unwrap
    * @return unwrapped handler
    */
   protected VirtualFileHandler unwrap(VirtualFileHandler handler)
   {
      if (handler instanceof DelegatingHandler)
         handler = ((DelegatingHandler)handler).getDelegate();
      return handler;
   }

   public VirtualFile copy(VirtualFile file, VirtualFileHandler handler) throws IOException, URISyntaxException
   {
      VirtualFileHandler unwrapped = unwrap(handler);
      // check modification on unwrapped
      if (isAlreadyModified(unwrapped))
      {
         if (log.isTraceEnabled())
            log.trace("Should already be " + getType() + ": " + unwrapped);
         return file;
      }

      //create guid dir
      File guidDir = createTempDirectory(getTempDirectory(), GUID.asString());
      // unpack handler
      File copy = copy(guidDir, handler);
      // create new handler
      FileSystemContext fileSystemContext = new FileSystemContext(copy);

      // merge old options
      VFSContext oldVFSContext = handler.getVFSContext();
      Map<String, String> newOptions = fileSystemContext.getOptions();
      Map<String, String> oldOptions = oldVFSContext.getOptions();
      if (newOptions != null && oldOptions != null)
         newOptions.putAll(oldOptions);

      VirtualFileHandler newHandler = fileSystemContext.getRoot();
      VirtualFileHandler parent = handler.getParent();
      if (parent != null && replaceOldHandler(parent, handler, newHandler))
         parent.replaceChild(handler, newHandler);

      return newHandler.getVirtualFile();
   }

   /**
    * Copy handler.
    *
    * @param guidDir the guid directory
    * @param handler the handler to copy
    * @return handler's copy as file
    * @throws IOException for any error
    */
   protected File copy(File guidDir, VirtualFileHandler handler) throws IOException
   {
      File copy = createTempDirectory(guidDir, handler.getName());
      unpack(handler, copy, false);
      return copy;
   }

   /**
    * Create the temp directory.
    *
    * @param parent the parent
{'fraction_non_alphanumeric': 0.08781127129750983, 'fraction_numerical': 0.003182924545965175, 'mean_word_length': 3.3146203554119547, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1220562', 'n_tokens_mistral': 2893, 'n_tokens_neox': 2814, 'n_words': 965}