Truncate the filename for overly long class names

This commit is contained in:
Ben Gruver 2013-09-14 11:33:18 -07:00
parent 5ff4ee9a3f
commit d868692e33
2 changed files with 69 additions and 1 deletions

View File

@ -31,6 +31,7 @@ package org.jf.util;
import ds.tree.RadixTree; import ds.tree.RadixTree;
import ds.tree.RadixTreeImpl; import ds.tree.RadixTreeImpl;
import javax.annotation.Nonnull;
import java.io.*; import java.io.*;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -41,6 +42,9 @@ import java.util.regex.Pattern;
* class name to distinguish it from another class with a name that differes only by case. i.e. a.smali and a_2.smali * class name to distinguish it from another class with a name that differes only by case. i.e. a.smali and a_2.smali
*/ */
public class ClassFileNameHandler { public class ClassFileNameHandler {
// we leave an extra 10 characters to allow for a numeric suffix to be added, if it's needed
private static final int MAX_FILENAME_LENGTH = 245;
private PackageNameEntry top; private PackageNameEntry top;
private String fileExtension; private String fileExtension;
private boolean modifyWindowsReservedFilenames; private boolean modifyWindowsReservedFilenames;
@ -83,6 +87,10 @@ public class ClassFileNameHandler {
packageElement += "#"; packageElement += "#";
} }
if (packageElement.length() > MAX_FILENAME_LENGTH) {
packageElement = shortenPathComponent(packageElement, MAX_FILENAME_LENGTH);
}
packageElements[elementIndex++] = packageElement; packageElements[elementIndex++] = packageElement;
elementStart = ++i; elementStart = ++i;
} }
@ -101,11 +109,23 @@ public class ClassFileNameHandler {
packageElement += "#"; packageElement += "#";
} }
if ((packageElement.length() + fileExtension.length()) > MAX_FILENAME_LENGTH) {
packageElement = shortenPathComponent(packageElement, MAX_FILENAME_LENGTH - fileExtension.length());
}
packageElements[elementIndex] = packageElement; packageElements[elementIndex] = packageElement;
return top.addUniqueChild(packageElements, 0); return top.addUniqueChild(packageElements, 0);
} }
@Nonnull
static String shortenPathComponent(@Nonnull String pathComponent, int maxLength) {
int toRemove = pathComponent.length() - maxLength + 1;
int firstIndex = (pathComponent.length()/2) - (toRemove/2);
return pathComponent.substring(0, firstIndex) + "#" + pathComponent.substring(firstIndex+toRemove);
}
private static boolean testForWindowsReservedFileNames(File path) { private static boolean testForWindowsReservedFileNames(File path) {
File f = new File(path, "aux.smali"); File f = new File(path, "aux.smali");
if (f.exists()) { if (f.exists()) {
@ -242,7 +262,6 @@ public class ClassFileNameHandler {
} }
} }
if (pathElementsIndex == pathElements.length - 1) { if (pathElementsIndex == pathElements.length - 1) {
String fileName; String fileName;
if (!isCaseSensitive()) { if (!isCaseSensitive()) {

View File

@ -0,0 +1,49 @@
/*
* Copyright 2013, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.util;
import junit.framework.Assert;
import org.junit.Test;
public class ClassFileNameHandlerTest {
@Test
public void testShortedPathComponent() {
StringBuilder sb = new StringBuilder();
for (int i=0; i<300; i++) {
sb.append((char)i);
}
String result = ClassFileNameHandler.shortenPathComponent(sb.toString(), 255);
Assert.assertEquals(255, result.length());
}
}