diff options
| author | Jeff Sharkey <jsharkey@android.com> | 2015-06-11 19:13:37 -0700 |
|---|---|---|
| committer | Jeff Sharkey <jsharkey@android.com> | 2015-06-11 19:16:27 -0700 |
| commit | 4f5e8b3ca489245005b76176ac6d28f5f184f3fe (patch) | |
| tree | 2b7975f534d07dfc7759cd5461630457b31759f5 /core/java/android/os/FileUtils.java | |
| parent | 47b872d9c36347382dd24ad5c3f70490d8fcbb23 (diff) | |
Valid filenames have length limits!
ext4 filenames are at most 255 bytes. vfat filenames are bit more
lax, but we're often saving them on ext4 through a FUSE daemon, so
limit them the same way.
Since package names are used as directory names, verify that they're
valid filenames.
Tests to verify behavior.
Bug: 18689171
Change-Id: If7df4c40d352954510b71de4ff05d78259c721ed
Diffstat (limited to 'core/java/android/os/FileUtils.java')
| -rw-r--r-- | core/java/android/os/FileUtils.java | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/core/java/android/os/FileUtils.java b/core/java/android/os/FileUtils.java index 917271d6e670..864225a0df12 100644 --- a/core/java/android/os/FileUtils.java +++ b/core/java/android/os/FileUtils.java @@ -24,6 +24,8 @@ import android.util.Log; import android.util.Slog; import android.webkit.MimeTypeMap; +import com.android.internal.annotations.VisibleForTesting; + import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -34,6 +36,7 @@ import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Comparator; import java.util.Objects; @@ -456,6 +459,7 @@ public class FileUtils { res.append('_'); } } + trimFilename(res, 255); return res.toString(); } @@ -504,9 +508,31 @@ public class FileUtils { res.append('_'); } } + // Even though vfat allows 255 UCS-2 chars, we might eventually write to + // ext4 through a FUSE layer, so use that limit. + trimFilename(res, 255); + return res.toString(); + } + + @VisibleForTesting + public static String trimFilename(String str, int maxBytes) { + final StringBuilder res = new StringBuilder(str); + trimFilename(res, maxBytes); return res.toString(); } + private static void trimFilename(StringBuilder res, int maxBytes) { + byte[] raw = res.toString().getBytes(StandardCharsets.UTF_8); + if (raw.length > maxBytes) { + maxBytes -= 3; + while (raw.length > maxBytes) { + res.deleteCharAt(res.length() / 2); + raw = res.toString().getBytes(StandardCharsets.UTF_8); + } + res.insert(res.length() / 2, "..."); + } + } + public static String rewriteAfterRename(File beforeDir, File afterDir, String path) { if (path == null) return null; final File result = rewriteAfterRename(beforeDir, afterDir, new File(path)); |
