From 63898ce467281085b639e45645f18175ffe046ed Mon Sep 17 00:00:00 2001 From: Raymond Lai Date: Sat, 21 Oct 2023 15:51:19 +0800 Subject: [PATCH] Fix ls lines for (even more) legacy devices For GPD XD Gen 1 running LegacyROM (4.4.4), where its ls command is not returning any number for directory size (usually 1 or 4096) --- .../filesystem/files/FileUtils.java | 14 ++++++++++-- .../filesystem/files/FileUtilsTest.kt | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/amaze/filemanager/filesystem/files/FileUtils.java b/app/src/main/java/com/amaze/filemanager/filesystem/files/FileUtils.java index cf8a2cd612..f3af5ce731 100644 --- a/app/src/main/java/com/amaze/filemanager/filesystem/files/FileUtils.java +++ b/app/src/main/java/com/amaze/filemanager/filesystem/files/FileUtils.java @@ -30,6 +30,7 @@ import java.util.Date; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicLong; @@ -941,10 +942,19 @@ public static HybridFileParcelable parseName(String line, boolean isStat) { } link = new StringBuilder(link.toString().trim()); } - long Size = (size == null || size.trim().length() == 0) ? -1 : Long.parseLong(size); + long Size; + if (size == null || size.trim().length() == 0) { + Size = -1; + } else { + try { + Size = Long.parseLong(size); + } catch (NumberFormatException ifItIsNotANumber) { + Size = -1; + } + } if (date.trim().length() > 0 && !isStat) { ParsePosition pos = new ParsePosition(0); - SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd | HH:mm"); + SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd | HH:mm", Locale.US); Date stringDate = simpledateformat.parse(date, pos); if (stringDate == null) { LOG.warn("parseName: unable to parse datetime string [" + date + "]"); diff --git a/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt b/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt index 5b1ba3133c..7b8af9aafd 100644 --- a/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt +++ b/app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt @@ -27,7 +27,9 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import com.amaze.filemanager.filesystem.files.FileUtils.getPathsInPath import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith import org.robolectric.annotation.Config @@ -364,6 +366,26 @@ class FileUtilsTest { } } + /** + * Test [FileUtils.parseName] for special cases + */ + @Test + fun testParseStringForSpecialCases() { + // Found on TranceLove's GPD XD Gen 1 running LegacyROM (4.4.4) that dirs doesn't even + // report default folder node size = 4096 or anything + val lsLine = "drwxr-xr-x root root 2023-10-21 13:57 acct" + + val systemTz = TimeZone.getDefault() + TimeZone.setDefault(TimeZone.getTimeZone("UTC")) + + val result = FileUtils.parseName(lsLine, false) + assertNotNull(result) + assertEquals("acct", result.name) + assertEquals("drwxr-xr-x", result.permission) + assertTrue(result.isDirectory) + TimeZone.setDefault(systemTz) + } + /** * Test [FileUtils.parseName] */