/* * Copyright 2008 Wishnu Prasetya. * * This file is part of T2. * T2 is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License (GPL) as published by the * Free Software Foundation; either version 3 of the License, or any * later version. * * T2 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 General Public License * for more details. * * A copy of the GNU General Public License can be found in T2 distribution. * If it is missing, see http://www.gnu.org/licenses. */ package t2ext.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * Makes backups of files and restores them after the T2Extension process. * * TODO: handle files in a .jar? and generally classes in packages * * @author Maaike Gerritsen */ public class FileCopy { public static final String TEMP_DIR = "t2temp" + File.separator; private String _oldDirectoryName; private String _jarDir; private String _jarName; private String _homeDir; private String _className; private InfoFile _infoFile; public FileCopy(String className, String jarDir, InfoFile infoFile) { _className = className; File file1 = new File(jarDir); File file2 = new File(file1.getAbsolutePath()); _jarDir = file2.getAbsolutePath(); _homeDir = file2.getParent(); _jarName = _jarDir.substring(_homeDir.length()); if(!_homeDir.endsWith("/") && !_homeDir.endsWith("\\")) _homeDir = _homeDir+"/"; if(_jarName.startsWith("/") || _jarName.startsWith("\\")) _jarName = _jarName.substring(1); _infoFile = infoFile; String newClassName = className.replace(".", ":"); String[] split = newClassName.split(":"); if (split.length > 1) _oldDirectoryName = split[0]; else _oldDirectoryName = className + ".class"; } public void copyDirectory() { copyDirectory(false); } /** * Copies the original class file (or its whole directory) to a _copy * @param setToOld If setToOld is true it will set the _copy files back * to their originals instead. */ // FIXME this fails when the className contains a package private void copyDirectory(boolean setToOld) { String original = (new File(_oldDirectoryName)).getAbsolutePath(); String copyFile = (new File(_oldDirectoryName + "_copy")) .getAbsolutePath(); File source = new File(original); File target = new File(copyFile); if (!setToOld) copyFiles(source, target); else copyFiles(target, source); } private void copyClassFiles(File sourceLocation) { File directory = sourceLocation.getParentFile(); if (directory.isDirectory()) { String[] children = directory.list(); for (int i = 0; i < children.length; i++) { String child = children[i]; if (child.endsWith(".class")) { String fullOldName = (new File(child)).getAbsolutePath(); child = child + "_copy"; String fullNewName = (new File(child)).getAbsolutePath(); File original = new File(fullOldName); File newFile = new File(fullNewName); copyFile(original, newFile); } } } } private void copyFiles(File sourceLocation, File targetLocation) { if (sourceLocation.getName().endsWith(".class")) copyClassFiles(sourceLocation); else copyCompleteDirectory(sourceLocation, targetLocation); } private void copyCompleteDirectory(File sourceLocation, File targetLocation) { if (sourceLocation.isDirectory()) { if (!targetLocation.exists()) { System.out.println("Creating directory: "+targetLocation); targetLocation.mkdir(); } String[] children = sourceLocation.list(); for (int i = 0; i < children.length; i++) { copyCompleteDirectory( new File(sourceLocation, children[i]), new File( targetLocation, children[i])); } } else { copyFile(sourceLocation, targetLocation); } } /** * Restores the files to their original state. */ // TODO paths? public void setDirectoryToOld() { copyDirectory(true); _infoFile = InfoFile.load(); String copyFile = (new File(_oldDirectoryName + "_copy")) .getAbsolutePath(); File file = new File(copyFile); deleteDirectory(file); String t2ext = (new File("t2ext")).getAbsolutePath(); File file1 = new File(t2ext); deleteDirectory(file1); String here = (new File("")).getAbsolutePath(); //File file2 = new File(here + "/build.xml"); File file2 = new File(TEMP_DIR+"build.xml"); System.out.println("Deleting "+file2); file2.delete(); File file3 = new File(here + "/classInfo.xml"); file3.delete(); if (!_infoFile.saveTraces) { String fileName = _className + ".tr"; if (!_infoFile.saveFile.equals("")) fileName = _infoFile.saveFile; File file4 = new File(here + "/" + fileName); file4.delete(); } // File file5 = new File(here+"/paths.xml"); // file5.delete(); File file6 = new File(here + "/T2ECopy2.jar"); file6.delete(); File file7 = new File(here + "/PathTree.tr"); file7.delete(); File file8 = new File(here + "/infoFile.info"); file8.delete(); String copyString = _jarDir.replace(_jarName, "T2ECopy.jar"); File file9 = new File(copyString); file9.delete(); } public void deleteOriginal() { String clean = new File(_oldDirectoryName).getAbsolutePath(); File file = new File(clean); System.out.println("Deleting directory "+file); deleteDirectory(file); } // TODO where and why is this used? private void deleteDirectory(File path) { if (path.exists() && path.listFiles() != null) { File[] files = path.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { System.out.println("Deleting directory "+files[i]); deleteDirectory(files[i]); } else { System.out.println("Deleting file "+files[i]); files[i].delete(); } } } if (path.getName().endsWith(".class_copy")) { String completeName = path.getAbsolutePath(); File file = new File(completeName); String[] children = file.getParentFile().list(); if (children != null) { for (int i = 0; i < children.length; i++) { String child = children[i]; if (child.endsWith(".class_copy")) { File childFile = new File(child); System.out.println("Deleting file "+childFile); childFile.delete(); } } } else path.delete(); } else path.delete(); } /** * Recursively deletes a directory and all of its files. * @param dir * @return Returns true on success, false otherwise. */ public static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * Copies the CUT to the t2temp directory for safekeeping. */ public void backupCUT() { File original = Util.getClassLocation(_className); File newFile = new File(TEMP_DIR+original.getName()); copyFile(original, newFile); } }