Snap for 5392835 from 9d2a945e9b0e58c602a5b3e29aa6f2e7c1c5cd8e to qt-release

Change-Id: I5b74ee551ca0f6f8b24f67ba69f6a63892c7b315
diff --git a/adb_install.cpp b/adb_install.cpp
index 438da9f..3f2843f 100644
--- a/adb_install.cpp
+++ b/adb_install.cpp
@@ -35,7 +35,6 @@
 #include "ui.h"
 
 int apply_from_adb(bool* wipe_cache) {
-  modified_flash = true;
   // Save the usb state to restore after the sideload operation.
   std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
   // Clean up state and stop adbd.
diff --git a/fuse_sdcard_install.cpp b/fuse_sdcard_install.cpp
index 79ef16b..1f38509 100644
--- a/fuse_sdcard_install.cpp
+++ b/fuse_sdcard_install.cpp
@@ -16,30 +16,191 @@
 
 #include "fuse_sdcard_install.h"
 
+#include <dirent.h>
+#include <signal.h>
 #include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
+#include <algorithm>
 #include <functional>
+#include <memory>
+#include <vector>
 
+#include <android-base/logging.h>
+#include <android-base/strings.h>
+
+#include "bootloader_message/bootloader_message.h"
 #include "fuse_provider.h"
 #include "fuse_sideload.h"
+#include "install.h"
+#include "roots.h"
 
-bool start_sdcard_fuse(const char* path) {
-  FuseFileDataProvider file_data_reader(path, 65536);
+static constexpr const char* SDCARD_ROOT = "/sdcard";
+// How long (in seconds) we wait for the fuse-provided package file to
+// appear, before timing out.
+static constexpr int SDCARD_INSTALL_TIMEOUT = 10;
 
-  if (!file_data_reader) {
-    return false;
+// Set the BCB to reboot back into recovery (it won't resume the install from
+// sdcard though).
+static void SetSdcardUpdateBootloaderMessage() {
+  std::vector<std::string> options;
+  std::string err;
+  if (!update_bootloader_message(options, &err)) {
+    LOG(ERROR) << "Failed to set BCB message: " << err;
+  }
+}
+
+// Returns the selected filename, or an empty string.
+static std::string BrowseDirectory(const std::string& path, Device* device, RecoveryUI* ui) {
+  ensure_path_mounted(path);
+
+  std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path.c_str()), closedir);
+  if (!d) {
+    PLOG(ERROR) << "error opening " << path;
+    return "";
   }
 
-  provider_vtab vtab;
-  vtab.read_block = std::bind(&FuseFileDataProvider::ReadBlockAlignedData, &file_data_reader,
-                              std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
-  vtab.close = [&file_data_reader]() { file_data_reader.Close(); };
+  std::vector<std::string> dirs;
+  std::vector<std::string> entries{ "../" };  // "../" is always the first entry.
+
+  dirent* de;
+  while ((de = readdir(d.get())) != nullptr) {
+    std::string name(de->d_name);
+
+    if (de->d_type == DT_DIR) {
+      // Skip "." and ".." entries.
+      if (name == "." || name == "..") continue;
+      dirs.push_back(name + "/");
+    } else if (de->d_type == DT_REG && android::base::EndsWithIgnoreCase(name, ".zip")) {
+      entries.push_back(name);
+    }
+  }
+
+  std::sort(dirs.begin(), dirs.end());
+  std::sort(entries.begin(), entries.end());
+
+  // Append dirs to the entries list.
+  entries.insert(entries.end(), dirs.begin(), dirs.end());
+
+  std::vector<std::string> headers{ "Choose a package to install:", path };
+
+  size_t chosen_item = 0;
+  while (true) {
+    chosen_item = ui->ShowMenu(
+        headers, entries, chosen_item, true,
+        std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
+
+    // Return if WaitKey() was interrupted.
+    if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
+      return "";
+    }
+
+    const std::string& item = entries[chosen_item];
+    if (chosen_item == 0) {
+      // Go up but continue browsing (if the caller is BrowseDirectory).
+      return "";
+    }
+
+    std::string new_path = path + "/" + item;
+    if (new_path.back() == '/') {
+      // Recurse down into a subdirectory.
+      new_path.pop_back();
+      std::string result = BrowseDirectory(new_path, device, ui);
+      if (!result.empty()) return result;
+    } else {
+      // Selected a zip file: return the path to the caller.
+      return new_path;
+    }
+  }
+
+  // Unreachable.
+}
+
+static bool StartSdcardFuse(const std::string& path) {
+  auto file_data_reader = std::make_unique<FuseFileDataProvider>(path, 65536);
+
+  if (!file_data_reader->Valid()) {
+    return false;
+  }
 
   // The installation process expects to find the sdcard unmounted. Unmount it with MNT_DETACH so
   // that our open file continues to work but new references see it as unmounted.
   umount2("/sdcard", MNT_DETACH);
 
-  return run_fuse_sideload(vtab, file_data_reader.file_size(),
-                           file_data_reader.fuse_block_size()) == 0;
+  return run_fuse_sideload(std::move(file_data_reader)) == 0;
+}
+
+int ApplyFromSdcard(Device* device, bool* wipe_cache, RecoveryUI* ui) {
+  if (ensure_path_mounted(SDCARD_ROOT) != 0) {
+    LOG(ERROR) << "\n-- Couldn't mount " << SDCARD_ROOT << ".\n";
+    return INSTALL_ERROR;
+  }
+
+  std::string path = BrowseDirectory(SDCARD_ROOT, device, ui);
+  if (path.empty()) {
+    LOG(ERROR) << "\n-- No package file selected.\n";
+    ensure_path_unmounted(SDCARD_ROOT);
+    return INSTALL_ERROR;
+  }
+
+  ui->Print("\n-- Install %s ...\n", path.c_str());
+  SetSdcardUpdateBootloaderMessage();
+
+  // We used to use fuse in a thread as opposed to a process. Since accessing
+  // through fuse involves going from kernel to userspace to kernel, it leads
+  // to deadlock when a page fault occurs. (Bug: 26313124)
+  pid_t child;
+  if ((child = fork()) == 0) {
+    bool status = StartSdcardFuse(path);
+
+    _exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
+  }
+
+  // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child
+  // process is ready.
+  int result = INSTALL_ERROR;
+  int status;
+  bool waited = false;
+  for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
+    if (waitpid(child, &status, WNOHANG) == -1) {
+      result = INSTALL_ERROR;
+      waited = true;
+      break;
+    }
+
+    struct stat sb;
+    if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) {
+      if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT - 1) {
+        sleep(1);
+        continue;
+      } else {
+        LOG(ERROR) << "Timed out waiting for the fuse-provided package.";
+        result = INSTALL_ERROR;
+        kill(child, SIGKILL);
+        break;
+      }
+    }
+
+    result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0 /*retry_count*/);
+    break;
+  }
+
+  if (!waited) {
+    // Calling stat() on this magic filename signals the fuse
+    // filesystem to shut down.
+    struct stat sb;
+    stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb);
+
+    waitpid(child, &status, 0);
+  }
+
+  if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+    LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status);
+  }
+
+  ensure_path_unmounted(SDCARD_ROOT);
+  return result;
 }
diff --git a/fuse_sdcard_install.h b/fuse_sdcard_install.h
index 58f84a6..5f0d64a 100644
--- a/fuse_sdcard_install.h
+++ b/fuse_sdcard_install.h
@@ -16,4 +16,7 @@
 
 #pragma once
 
-bool start_sdcard_fuse(const char* path);
+#include "device.h"
+#include "ui.h"
+
+int ApplyFromSdcard(Device* device, bool* wipe_cache, RecoveryUI* ui);
diff --git a/fuse_sideload/fuse_sideload.cpp b/fuse_sideload/fuse_sideload.cpp
index e812486..b5b6ac1 100644
--- a/fuse_sideload/fuse_sideload.cpp
+++ b/fuse_sideload/fuse_sideload.cpp
@@ -76,7 +76,7 @@
 struct fuse_data {
   android::base::unique_fd ffd;  // file descriptor for the fuse socket
 
-  provider_vtab vtab;
+  FuseDataProvider* provider;  // Provider of the source data.
 
   uint64_t file_size;  // bytes
 
@@ -236,7 +236,7 @@
     return 0;
   }
 
-  size_t fetch_size = fd->block_size;
+  uint32_t fetch_size = fd->block_size;
   if (block * fd->block_size + fetch_size > fd->file_size) {
     // If we're reading the last (partial) block of the file, expect a shorter response from the
     // host, and pad the rest of the block with zeroes.
@@ -244,7 +244,7 @@
     memset(fd->block_data + fetch_size, 0, fd->block_size - fetch_size);
   }
 
-  if (!fd->vtab.read_block(block, fd->block_data, fetch_size)) {
+  if (!fd->provider->ReadBlockAlignedData(fd->block_data, fetch_size, block)) {
     return -EIO;
   }
 
@@ -341,12 +341,14 @@
   return NO_STATUS;
 }
 
-int run_fuse_sideload(const provider_vtab& vtab, uint64_t file_size, uint32_t block_size,
-                      const char* mount_point) {
+int run_fuse_sideload(std::unique_ptr<FuseDataProvider>&& provider, const char* mount_point) {
   // If something's already mounted on our mountpoint, try to remove it. (Mostly in case of a
   // previous abnormal exit.)
   umount2(mount_point, MNT_FORCE);
 
+  uint64_t file_size = provider->file_size();
+  uint32_t block_size = provider->fuse_block_size();
+
   // fs/fuse/inode.c in kernel code uses the greater of 4096 and the passed-in max_read.
   if (block_size < 4096) {
     fprintf(stderr, "block size (%u) is too small\n", block_size);
@@ -358,7 +360,7 @@
   }
 
   fuse_data fd = {};
-  fd.vtab = vtab;
+  fd.provider = provider.get();
   fd.file_size = file_size;
   fd.block_size = block_size;
   fd.file_blocks = (file_size == 0) ? 0 : (((file_size - 1) / block_size) + 1);
@@ -480,7 +482,7 @@
   }
 
 done:
-  fd.vtab.close();
+  provider->Close();
 
   if (umount2(mount_point, MNT_DETACH) == -1) {
     fprintf(stderr, "fuse_sideload umount failed: %s\n", strerror(errno));
diff --git a/fuse_sideload/include/fuse_provider.h b/fuse_sideload/include/fuse_provider.h
index 672af05..499d57a 100644
--- a/fuse_sideload/include/fuse_provider.h
+++ b/fuse_sideload/include/fuse_provider.h
@@ -37,7 +37,7 @@
     return fuse_block_size_;
   }
 
-  explicit operator bool() const {
+  bool Valid() const {
     return fd_ != -1;
   }
 
diff --git a/fuse_sideload/include/fuse_sideload.h b/fuse_sideload/include/fuse_sideload.h
index 821c7c8..1b7759a 100644
--- a/fuse_sideload/include/fuse_sideload.h
+++ b/fuse_sideload/include/fuse_sideload.h
@@ -17,7 +17,9 @@
 #ifndef __FUSE_SIDELOAD_H
 #define __FUSE_SIDELOAD_H
 
-#include <functional>
+#include <memory>
+
+#include "fuse_provider.h"
 
 // Define the filenames created by the sideload FUSE filesystem.
 static constexpr const char* FUSE_SIDELOAD_HOST_MOUNTPOINT = "/sideload";
@@ -26,15 +28,7 @@
 static constexpr const char* FUSE_SIDELOAD_HOST_EXIT_FLAG = "exit";
 static constexpr const char* FUSE_SIDELOAD_HOST_EXIT_PATHNAME = "/sideload/exit";
 
-struct provider_vtab {
-  // read a block
-  std::function<bool(uint32_t block, uint8_t* buffer, uint32_t fetch_size)> read_block;
-
-  // close down
-  std::function<void(void)> close;
-};
-
-int run_fuse_sideload(const provider_vtab& vtab, uint64_t file_size, uint32_t block_size,
+int run_fuse_sideload(std::unique_ptr<FuseDataProvider>&& provider,
                       const char* mount_point = FUSE_SIDELOAD_HOST_MOUNTPOINT);
 
 #endif
diff --git a/minadbd/fuse_adb_provider.cpp b/minadbd/fuse_adb_provider.cpp
index cada4bd..9d19a1e 100644
--- a/minadbd/fuse_adb_provider.cpp
+++ b/minadbd/fuse_adb_provider.cpp
@@ -18,14 +18,10 @@
 
 #include <errno.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
-#include <functional>
-
 #include "adb.h"
 #include "adb_io.h"
-#include "fuse_sideload.h"
 
 bool FuseAdbDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size,
                                                uint32_t start_block) const {
@@ -45,14 +41,3 @@
 void FuseAdbDataProvider::Close() {
   WriteFdExactly(fd_, "DONEDONE");
 }
-
-int run_adb_fuse(android::base::unique_fd&& sfd, uint64_t file_size, uint32_t block_size) {
-  FuseAdbDataProvider adb_data_reader(std::move(sfd), file_size, block_size);
-
-  provider_vtab vtab;
-  vtab.read_block = std::bind(&FuseAdbDataProvider::ReadBlockAlignedData, &adb_data_reader,
-                              std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
-  vtab.close = [&adb_data_reader]() { adb_data_reader.Close(); };
-
-  return run_fuse_sideload(vtab, file_size, block_size);
-}
diff --git a/minadbd/fuse_adb_provider.h b/minadbd/fuse_adb_provider.h
index e93aa04..3fb689b 100644
--- a/minadbd/fuse_adb_provider.h
+++ b/minadbd/fuse_adb_provider.h
@@ -35,6 +35,4 @@
   void Close() override;
 };
 
-int run_adb_fuse(android::base::unique_fd&& sfd, uint64_t file_size, uint32_t block_size);
-
 #endif
diff --git a/minadbd/minadbd_services.cpp b/minadbd/minadbd_services.cpp
index 3e11285..6fe5c79 100644
--- a/minadbd/minadbd_services.cpp
+++ b/minadbd/minadbd_services.cpp
@@ -22,6 +22,7 @@
 #include <unistd.h>
 
 #include <functional>
+#include <memory>
 #include <string>
 #include <string_view>
 #include <thread>
@@ -30,6 +31,7 @@
 #include "adb_unique_fd.h"
 #include "fdevent.h"
 #include "fuse_adb_provider.h"
+#include "fuse_sideload.h"
 #include "services.h"
 #include "sysdeps.h"
 
@@ -44,7 +46,9 @@
 
   printf("sideload-host file size %" PRId64 " block size %d\n", file_size, block_size);
 
-  int result = run_adb_fuse(std::move(sfd), file_size, block_size);
+  auto adb_data_reader =
+      std::make_unique<FuseAdbDataProvider>(std::move(sfd), file_size, block_size);
+  int result = run_fuse_sideload(std::move(adb_data_reader));
 
   printf("sideload_host finished\n");
   exit(result == 0 ? 0 : 1);
diff --git a/recovery.cpp b/recovery.cpp
index 63960eb..90ca3f0 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -17,7 +17,6 @@
 #include "recovery.h"
 
 #include <ctype.h>
-#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
@@ -31,7 +30,6 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/wait.h>
 #include <unistd.h>
 
 #include <algorithm>
@@ -57,7 +55,6 @@
 #include "device.h"
 #include "fsck_unshare_blocks.h"
 #include "fuse_sdcard_install.h"
-#include "fuse_sideload.h"
 #include "install.h"
 #include "logging.h"
 #include "otautil/dirutil.h"
@@ -78,7 +75,6 @@
 static constexpr const char* CACHE_ROOT = "/cache";
 static constexpr const char* DATA_ROOT = "/data";
 static constexpr const char* METADATA_ROOT = "/metadata";
-static constexpr const char* SDCARD_ROOT = "/sdcard";
 
 // We define RECOVERY_API_VERSION in Android.mk, which will be picked up by build system and packed
 // into target_files.zip. Assert the version defined in code and in Android.mk are consistent.
@@ -138,16 +134,6 @@
     return android::base::GetBoolProperty("ro.debuggable", false);
 }
 
-// Set the BCB to reboot back into recovery (it won't resume the install from
-// sdcard though).
-static void set_sdcard_update_bootloader_message() {
-  std::vector<std::string> options;
-  std::string err;
-  if (!update_bootloader_message(options, &err)) {
-    LOG(ERROR) << "Failed to set BCB message: " << err;
-  }
-}
-
 // Clear the recovery command and prepare to boot a (hopefully working) system,
 // copy our log file to cache as well (for the system to read). This function is
 // idempotent: call it as many times as you like.
@@ -293,72 +279,6 @@
   return android::base::WaitForProperty("sys.usb.state", state);
 }
 
-// Returns the selected filename, or an empty string.
-static std::string browse_directory(const std::string& path, Device* device) {
-  ensure_path_mounted(path);
-
-  std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path.c_str()), closedir);
-  if (!d) {
-    PLOG(ERROR) << "error opening " << path;
-    return "";
-  }
-
-  std::vector<std::string> dirs;
-  std::vector<std::string> entries{ "../" };  // "../" is always the first entry.
-
-  dirent* de;
-  while ((de = readdir(d.get())) != nullptr) {
-    std::string name(de->d_name);
-
-    if (de->d_type == DT_DIR) {
-      // Skip "." and ".." entries.
-      if (name == "." || name == "..") continue;
-      dirs.push_back(name + "/");
-    } else if (de->d_type == DT_REG && android::base::EndsWithIgnoreCase(name, ".zip")) {
-      entries.push_back(name);
-    }
-  }
-
-  std::sort(dirs.begin(), dirs.end());
-  std::sort(entries.begin(), entries.end());
-
-  // Append dirs to the entries list.
-  entries.insert(entries.end(), dirs.begin(), dirs.end());
-
-  std::vector<std::string> headers{ "Choose a package to install:", path };
-
-  size_t chosen_item = 0;
-  while (true) {
-    chosen_item = ui->ShowMenu(
-        headers, entries, chosen_item, true,
-        std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
-
-    // Return if WaitKey() was interrupted.
-    if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
-      return "";
-    }
-
-    const std::string& item = entries[chosen_item];
-    if (chosen_item == 0) {
-      // Go up but continue browsing (if the caller is browse_directory).
-      return "";
-    }
-
-    std::string new_path = path + "/" + item;
-    if (new_path.back() == '/') {
-      // Recurse down into a subdirectory.
-      new_path.pop_back();
-      std::string result = browse_directory(new_path, device);
-      if (!result.empty()) return result;
-    } else {
-      // Selected a zip file: return the path to the caller.
-      return new_path;
-    }
-  }
-
-  // Unreachable.
-}
-
 static bool yes_no(Device* device, const char* question1, const char* question2) {
   std::vector<std::string> headers{ question1, question2 };
   std::vector<std::string> items{ " No", " Yes" };
@@ -709,85 +629,6 @@
   ui->ShowText(true);
 }
 
-// TODO(xunchang) move apply_from_sdcard() to fuse_sdcard_install.cpp
-// How long (in seconds) we wait for the fuse-provided package file to
-// appear, before timing out.
-#define SDCARD_INSTALL_TIMEOUT 10
-
-static int apply_from_sdcard(Device* device, bool* wipe_cache) {
-    modified_flash = true;
-
-    if (ensure_path_mounted(SDCARD_ROOT) != 0) {
-        ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT);
-        return INSTALL_ERROR;
-    }
-
-    std::string path = browse_directory(SDCARD_ROOT, device);
-    if (path.empty()) {
-        ui->Print("\n-- No package file selected.\n");
-        ensure_path_unmounted(SDCARD_ROOT);
-        return INSTALL_ERROR;
-    }
-
-    ui->Print("\n-- Install %s ...\n", path.c_str());
-    set_sdcard_update_bootloader_message();
-
-    // We used to use fuse in a thread as opposed to a process. Since accessing
-    // through fuse involves going from kernel to userspace to kernel, it leads
-    // to deadlock when a page fault occurs. (Bug: 26313124)
-    pid_t child;
-    if ((child = fork()) == 0) {
-        bool status = start_sdcard_fuse(path.c_str());
-
-        _exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
-    }
-
-    // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child
-    // process is ready.
-    int result = INSTALL_ERROR;
-    int status;
-    bool waited = false;
-    for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
-        if (waitpid(child, &status, WNOHANG) == -1) {
-            result = INSTALL_ERROR;
-            waited = true;
-            break;
-        }
-
-        struct stat sb;
-        if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) {
-            if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) {
-                sleep(1);
-                continue;
-            } else {
-                LOG(ERROR) << "Timed out waiting for the fuse-provided package.";
-                result = INSTALL_ERROR;
-                kill(child, SIGKILL);
-                break;
-            }
-        }
-
-        result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0 /*retry_count*/);
-        break;
-    }
-
-    if (!waited) {
-        // Calling stat() on this magic filename signals the fuse
-        // filesystem to shut down.
-        struct stat sb;
-        stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb);
-
-        waitpid(child, &status, 0);
-    }
-
-    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
-        LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status);
-    }
-
-    ensure_path_unmounted(SDCARD_ROOT);
-    return result;
-}
-
 // Returns REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION means to take the default,
 // which is to reboot or shutdown depending on if the --shutdown_after flag was passed to recovery.
 static Device::BuiltinAction prompt_and_wait(Device* device, int status) {
@@ -849,32 +690,32 @@
         break;
 
       case Device::APPLY_ADB_SIDELOAD:
-      case Device::APPLY_SDCARD:
-        {
-          bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD);
-          if (adb) {
-            status = apply_from_adb(&should_wipe_cache);
-          } else {
-            status = apply_from_sdcard(device, &should_wipe_cache);
-          }
+      case Device::APPLY_SDCARD: {
+        modified_flash = true;
+        bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD);
+        if (adb) {
+          status = apply_from_adb(&should_wipe_cache);
+        } else {
+          status = ApplyFromSdcard(device, &should_wipe_cache, ui);
+        }
 
-          if (status == INSTALL_SUCCESS && should_wipe_cache) {
-            if (!wipe_cache(false, device)) {
-              status = INSTALL_ERROR;
-            }
-          }
-
-          if (status != INSTALL_SUCCESS) {
-            ui->SetBackground(RecoveryUI::ERROR);
-            ui->Print("Installation aborted.\n");
-            copy_logs(modified_flash, has_cache);
-          } else if (!ui->IsTextVisible()) {
-            return Device::NO_ACTION;  // reboot if logs aren't visible
-          } else {
-            ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card");
+        if (status == INSTALL_SUCCESS && should_wipe_cache) {
+          if (!wipe_cache(false, device)) {
+            status = INSTALL_ERROR;
           }
         }
+
+        if (status != INSTALL_SUCCESS) {
+          ui->SetBackground(RecoveryUI::ERROR);
+          ui->Print("Installation aborted.\n");
+          copy_logs(modified_flash, has_cache);
+        } else if (!ui->IsTextVisible()) {
+          return Device::NO_ACTION;  // reboot if logs aren't visible
+        } else {
+          ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card");
+        }
         break;
+      }
 
       case Device::VIEW_RECOVERY_LOGS:
         choose_recovery_file(device);
diff --git a/tests/component/sideload_test.cpp b/tests/component/sideload_test.cpp
index 8d290db..f5981ac 100644
--- a/tests/component/sideload_test.cpp
+++ b/tests/component/sideload_test.cpp
@@ -16,13 +16,16 @@
 
 #include <unistd.h>
 
+#include <memory>
 #include <string>
 #include <vector>
 
 #include <android-base/file.h>
 #include <android-base/strings.h>
+#include <android-base/unique_fd.h>
 #include <gtest/gtest.h>
 
+#include "fuse_provider.h"
 #include "fuse_sideload.h"
 
 TEST(SideloadTest, fuse_device) {
@@ -30,14 +33,17 @@
 }
 
 TEST(SideloadTest, run_fuse_sideload_wrong_parameters) {
-  provider_vtab vtab;
-  vtab.close = [](void) {};
+  auto provider_small_block =
+      std::make_unique<FuseFileDataProvider>(android::base::unique_fd(), 4096, 4095);
+  ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_small_block)));
 
-  ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, 4095));
-  ASSERT_EQ(-1, run_fuse_sideload(vtab, 4096, (1 << 22) + 1));
+  auto provider_large_block =
+      std::make_unique<FuseFileDataProvider>(android::base::unique_fd(), 4096, (1 << 22) + 1);
+  ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_large_block)));
 
-  // Too many blocks.
-  ASSERT_EQ(-1, run_fuse_sideload(vtab, ((1 << 18) + 1) * 4096, 4096));
+  auto provider_too_many_blocks = std::make_unique<FuseFileDataProvider>(
+      android::base::unique_fd(), ((1 << 18) + 1) * 4096, 4096);
+  ASSERT_EQ(-1, run_fuse_sideload(std::move(provider_too_many_blocks)));
 }
 
 TEST(SideloadTest, run_fuse_sideload) {
@@ -50,18 +56,15 @@
   const std::string content = android::base::Join(blocks, "");
   ASSERT_EQ(16384U, content.size());
 
-  provider_vtab vtab;
-  vtab.close = [](void) {};
-  vtab.read_block = [&blocks](uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
-    if (block >= 4) return false;
-    blocks[block].copy(reinterpret_cast<char*>(buffer), fetch_size);
-    return true;
-  };
+  TemporaryFile temp_file;
+  ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
 
+  auto provider = std::make_unique<FuseFileDataProvider>(temp_file.path, 4096);
+  ASSERT_TRUE(provider->Valid());
   TemporaryDir mount_point;
   pid_t pid = fork();
   if (pid == 0) {
-    ASSERT_EQ(0, run_fuse_sideload(vtab, 16384, 4096, mount_point.path));
+    ASSERT_EQ(0, run_fuse_sideload(std::move(provider), mount_point.path));
     _exit(EXIT_SUCCESS);
   }