Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

Startup & Branding: a failed picker import no longer deletes a picture of the same name

מקס חיים778a6ba129b5
diff --git a/addons/startup-branding/app/src/main/java/io/github/maxhayim/startupbranding/Images.java b/addons/startup-branding/app/src/main/java/io/github/maxhayim/startupbranding/Images.java
index 2b92d5a8d..f6b8221e7 100644
--- a/addons/startup-branding/app/src/main/java/io/github/maxhayim/startupbranding/Images.java
+++ b/addons/startup-branding/app/src/main/java/io/github/maxhayim/startupbranding/Images.java
@@ -19,6 +19,7 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.function.Predicate;
/**
* The pictures: PNG, JPEG or WebP files in the app's own folder on the TV, put there from a computer
@@ -114,24 +115,35 @@ final class Images {
// A provider without names: the made-up one will do.
}
if (Look.fileName(name).isEmpty()) name = "picked-" + System.currentTimeMillis();
- File out = new File(dir(c), name);
- long total = 0;
- try (InputStream in = c.getContentResolver().openInputStream(uri); OutputStream o = new FileOutputStream(out)) {
- if (in == null) return null;
- byte[] buf = new byte[64 * 1024];
- for (int n; (n = in.read(buf)) > 0; ) {
- total += n;
- if (total > MAX_BYTES) throw new IOException("too big");
- o.write(buf, 0, n);
- }
+ try (InputStream in = c.getContentResolver().openInputStream(uri)) {
+ return in != null && replace(in, dir(c), name, Images::usable) ? name : null;
} catch (IOException | SecurityException e) {
- out.delete();
return null;
}
- if (!usable(out)) {
- out.delete();
- return null;
+ }
+
+ /**
+ * Copies into a hidden file next to the picture and renames it over the picture only once it's whole and
+ * usable, so a failed import never touches a picture of the same name. The hidden file never shows in
+ * {@link #list} ({@link Look#fileName} refuses names starting with a dot).
+ */
+ static boolean replace(InputStream in, File dir, String name, Predicate<File> usable) throws IOException {
+ File part = File.createTempFile(".import-", ".part", dir);
+ boolean done = false;
+ try {
+ long total = 0;
+ try (OutputStream o = new FileOutputStream(part)) {
+ byte[] buf = new byte[64 * 1024];
+ for (int n; (n = in.read(buf)) > 0; ) {
+ total += n;
+ if (total > MAX_BYTES) return false;
+ o.write(buf, 0, n);
+ }
+ }
+ done = usable.test(part) && part.renameTo(new File(dir, name));
+ return done;
+ } finally {
+ if (!done) part.delete();
}
- return name;
}
}
diff --git a/addons/startup-branding/app/src/test/java/io/github/maxhayim/startupbranding/StartupBrandingTest.java b/addons/startup-branding/app/src/test/java/io/github/maxhayim/startupbranding/StartupBrandingTest.java
index 24f71de9a..13c7efd91 100644
--- a/addons/startup-branding/app/src/test/java/io/github/maxhayim/startupbranding/StartupBrandingTest.java
+++ b/addons/startup-branding/app/src/test/java/io/github/maxhayim/startupbranding/StartupBrandingTest.java
@@ -8,6 +8,11 @@ import static org.junit.Assert.assertTrue;
import org.junit.Test;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -181,4 +186,72 @@ public class StartupBrandingTest {
assertEquals("12L", Compat.release(32));
assertEquals("API 40", Compat.release(40));
}
+
+ // A failed import must leave a picture of the same name alone (picked files keep their provider's name).
+ private static File folderWithLogo() throws IOException {
+ File dir = Files.createTempDirectory("pictures").toFile();
+ Files.write(new File(dir, "logo.png").toPath(), "old".getBytes());
+ return dir;
+ }
+
+ private static String logo(File dir) throws IOException {
+ return new String(Files.readAllBytes(new File(dir, "logo.png").toPath()));
+ }
+
+ private static void onlyLogo(File dir) {
+ assertArrayEquals(new String[]{"logo.png"}, dir.list());
+ }
+
+ @Test
+ public void unusableImportKeepsThePicture() throws IOException {
+ File dir = folderWithLogo();
+ assertFalse(Images.replace(new ByteArrayInputStream("new".getBytes()), dir, "logo.png", f -> false));
+ assertEquals("old", logo(dir));
+ onlyLogo(dir);
+ }
+
+ @Test
+ public void oversizedImportKeepsThePicture() throws IOException {
+ File dir = folderWithLogo();
+ InputStream endless = new InputStream() {
+ @Override
+ public int read() {
+ return 1;
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) {
+ return len;
+ }
+ };
+ assertFalse(Images.replace(endless, dir, "logo.png", f -> true));
+ assertEquals("old", logo(dir));
+ onlyLogo(dir);
+ }
+
+ @Test
+ public void failedReadKeepsThePicture() throws IOException {
+ File dir = folderWithLogo();
+ InputStream broken = new InputStream() {
+ @Override
+ public int read() throws IOException {
+ throw new IOException("provider went away");
+ }
+ };
+ try {
+ Images.replace(broken, dir, "logo.png", f -> true);
+ } catch (IOException expected) {
+ // what copyIn turns into null
+ }
+ assertEquals("old", logo(dir));
+ onlyLogo(dir);
+ }
+
+ @Test
+ public void usableImportReplacesThePicture() throws IOException {
+ File dir = folderWithLogo();
+ assertTrue(Images.replace(new ByteArrayInputStream("new".getBytes()), dir, "logo.png", f -> f.length() == 3));
+ assertEquals("new", logo(dir));
+ onlyLogo(dir);
+ }
}