Reorder task shouldn't leave corrupted files behind

This commit is contained in:
Roman Shevchenko
2012-07-10 20:19:40 +02:00
parent 1e251bd40a
commit 1cfdae8be5
2 changed files with 76 additions and 45 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,11 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* User: anna
* Date: 23-Apr-2009
*/
package com.intellij.util.io.zip;
import com.intellij.openapi.util.io.FileUtil;
@@ -30,25 +25,26 @@ import java.io.File;
import java.io.IOException;
import java.util.*;
/**
* @author anna
* @since 23-Apr-2009
*/
@SuppressWarnings("CallToPrintStackTrace")
public class ReorderJarsMain {
private ReorderJarsMain() {
}
private ReorderJarsMain() { }
public static void main(String[] args) {
final String orderTxtPath = args[0];
final String jarsPath = args[1];
final String destinationHomePath = args[2];
final String libPath = args.length > 3 ? args[3] : null;
try {
final Map<String, List<String>> toReorder = getOrder(new File(orderTxtPath));
final String orderTxtPath = args[0];
final String jarsPath = args[1];
final String destinationPath = args[2];
final String libPath = args.length > 3 ? args[3] : null;
final Map<String, List<String>> toReorder = getOrder(new File(orderTxtPath));
final Set<String> ignoredJars = libPath == null ? Collections.<String>emptySet() : loadIgnoredJars(libPath);
for (String jarUrl : toReorder.keySet()) {
if (ignoredJars.contains(StringUtil.trimStart(jarUrl, "/lib/"))) continue;
if (jarUrl.startsWith("/lib/ant")) continue;
final File jarFile = new File(jarsPath, jarUrl);
@@ -64,16 +60,16 @@ public class ReorderJarsMain {
if (orderedEntries.contains(o1.getName())) {
return orderedEntries.contains(o2.getName()) ? orderedEntries.indexOf(o1.getName()) - orderedEntries.indexOf(o2.getName()) : -1;
}
if (orderedEntries.contains(o2.getName())) return 1;
return 0;
else {
return orderedEntries.contains(o2.getName()) ? 1 : 0;
}
}
});
final File tempJarFile = FileUtil.createTempFile("__reorder__", "__reorder__");
final JBZipFile file = new JBZipFile(tempJarFile);
JBZipEntry sizeEntry = file.getOrCreateEntry(JarMemoryLoader.SIZE_ENTRY);
final JBZipEntry sizeEntry = file.getOrCreateEntry(JarMemoryLoader.SIZE_ENTRY);
sizeEntry.setData(ZipShort.getBytes(orderedEntries.size()));
for (JBZipEntry entry : entries) {
@@ -82,14 +78,24 @@ public class ReorderJarsMain {
}
file.close();
final File resultJarFile = new File(destinationHomePath, jarUrl);
resultJarFile.getParentFile().mkdirs();
FileUtil.rename(tempJarFile, resultJarFile);
final File resultJarFile = new File(destinationPath, jarUrl);
final File resultDir = resultJarFile.getParentFile();
if (!resultDir.isDirectory() && !resultDir.mkdirs()) {
throw new IOException("Cannot create: " + resultDir);
}
try {
FileUtil.rename(tempJarFile, resultJarFile);
}
catch (Exception e) {
FileUtil.delete(resultJarFile);
throw e;
}
FileUtil.delete(tempJarFile);
}
}
catch (IOException e) {
e.printStackTrace();
catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
@@ -119,5 +125,4 @@ public class ReorderJarsMain {
}
return entriesOrder;
}
}
@@ -1,9 +1,26 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.util.io.zip;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.lang.JarMemoryLoader;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import sun.misc.Resource;
import java.io.File;
@@ -11,16 +28,33 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
/**
* @author Dmitry Avdeev
* Date: 7/12/11
* @since 7/12/11
*/
public class ReorderJarsTest extends TestCase {
public class ReorderJarsTest {
private File myTempDirectory;
public void testReordering() throws IOException {
@Before
public void setUp() throws Exception {
myTempDirectory = FileUtil.createTempDirectory("__", "__");
}
@After
public void tearDown() {
FileUtil.delete(myTempDirectory);
}
private static String getTestDataPath() {
String homePath = PathManager.getHomePath().replace(File.separatorChar, '/');
if (new File(homePath + "/community/java/java-tests/testData").exists()) return homePath + "/community/java/java-tests/testData";
return homePath + "/java/java-tests/testData";
}
@Test
public void testReordering() throws IOException {
String path = getTestDataPath() + "/ide/plugins/reorderJars";
JBZipFile zipFile = null;
try {
@@ -34,8 +68,10 @@ public class ReorderJarsTest extends TestCase {
}
}
ReorderJarsMain.main(new String[] { path + "/order.txt", path, myTempDirectory.getPath() } );
ReorderJarsMain.main(new String[]{path + "/order.txt", path, myTempDirectory.getPath()});
File[] files = myTempDirectory.listFiles();
assertNotNull(files);
assertEquals(1, files.length);
File file = files[0];
assertEquals("annotations.jar", file.getName());
@@ -65,17 +101,14 @@ public class ReorderJarsTest extends TestCase {
assertTrue(Arrays.equals(data, bytes));
}
public static String getTestDataPath() {
String homePath = PathManager.getHomePath().replace(File.separatorChar, '/');
if (new File(homePath + "/community/java/java-tests/testData").exists()) return homePath + "/community/java/java-tests/testData";
return homePath + "/java/java-tests/testData";
}
@Test
public void testPluginXml() throws Exception {
String path = getTestDataPath() + "/ide/plugins/reorderJars";
ReorderJarsMain.main(new String[] { path + "/zkmOrder.txt", path, myTempDirectory.getPath() } );
File[] files = myTempDirectory.listFiles();
assertNotNull(files);
File file = files[0];
assertEquals("zkm.jar", file.getName());
@@ -89,12 +122,5 @@ public class ReorderJarsTest extends TestCase {
finally {
zipFile.close();
}
}
@Override
protected void setUp() throws Exception {
super.setUp();
myTempDirectory = FileUtil.createTempDirectory("__", "__");
}
}