Merge "Move the png open and destroy functions into a class"
diff --git a/applypatch/Android.mk b/applypatch/Android.mk
index 59aa0ce..f5dda2b 100644
--- a/applypatch/Android.mk
+++ b/applypatch/Android.mk
@@ -112,11 +112,13 @@
 LOCAL_STATIC_LIBRARIES := \
     libapplypatch_modes \
     libapplypatch \
-    libbase \
     libedify \
     libotafault \
-    libcrypto \
     libbspatch \
+    libbase \
+    libziparchive \
+    liblog \
+    libcrypto \
     libbz
 LOCAL_SHARED_LIBRARIES := \
     libbase \
diff --git a/otafault/Android.mk b/otafault/Android.mk
index 4784d56..3e14f77 100644
--- a/otafault/Android.mk
+++ b/otafault/Android.mk
@@ -18,8 +18,6 @@
 
 otafault_static_libs := \
     libziparchive \
-    libz \
-    libselinux \
     libbase \
     liblog
 
@@ -35,7 +33,7 @@
 LOCAL_MODULE := libotafault
 LOCAL_C_INCLUDES := bootable/recovery
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)
-LOCAL_WHOLE_STATIC_LIBRARIES := $(otafault_static_libs)
+LOCAL_STATIC_LIBRARIES := $(otafault_static_libs)
 
 include $(BUILD_STATIC_LIBRARY)
 
diff --git a/otafault/config.cpp b/otafault/config.cpp
index b94e429..c11f774 100644
--- a/otafault/config.cpp
+++ b/otafault/config.cpp
@@ -14,16 +14,14 @@
  * limitations under the License.
  */
 
+#include "config.h"
+
 #include <map>
 #include <string>
 
-#include <stdio.h>
-#include <unistd.h>
-
 #include <android-base/stringprintf.h>
 #include <ziparchive/zip_archive.h>
 
-#include "config.h"
 #include "ota_io.h"
 
 #define OTAIO_MAX_FNAME_SIZE 128
diff --git a/otafault/config.h b/otafault/config.h
index 4adbdd1..cc4bfd2 100644
--- a/otafault/config.h
+++ b/otafault/config.h
@@ -15,13 +15,13 @@
  */
 
 /*
- * Read configuration files in the OTA package to determine which files, if any, will trigger errors.
+ * Read configuration files in the OTA package to determine which files, if any, will trigger
+ * errors.
  *
- * OTA packages can be modified to trigger errors by adding a top-level
- * directory called .libotafault, which may optionally contain up to three
- * files called READ, WRITE, and FSYNC. Each one of these optional files
- * contains the name of a single file on the device disk which will cause
- * an IO error on the first call of the appropriate I/O action to that file.
+ * OTA packages can be modified to trigger errors by adding a top-level directory called
+ * .libotafault, which may optionally contain up to three files called READ, WRITE, and FSYNC.
+ * Each one of these optional files contains the name of a single file on the device disk which
+ * will cause an IO error on the first call of the appropriate I/O action to that file.
  *
  * Example:
  * ota.zip
@@ -29,9 +29,9 @@
  *   .libotafault
  *     WRITE
  *
- * If the contents of the file WRITE were /system/build.prop, the first write
- * action to /system/build.prop would fail with EIO. Note that READ and
- * FSYNC files are absent, so these actions will not cause an error.
+ * If the contents of the file WRITE were /system/build.prop, the first write action to
+ * /system/build.prop would fail with EIO. Note that READ and FSYNC files are absent, so these
+ * actions will not cause an error.
  */
 
 #ifndef _UPDATER_OTA_IO_CFG_H_
@@ -39,8 +39,6 @@
 
 #include <string>
 
-#include <stdbool.h>
-
 #include <ziparchive/zip_archive.h>
 
 #define OTAIO_BASE_DIR ".libotafault"
diff --git a/otafault/ota_io.cpp b/otafault/ota_io.cpp
index faae527..a82a7ee 100644
--- a/otafault/ota_io.cpp
+++ b/otafault/ota_io.cpp
@@ -18,15 +18,17 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
 
 #include <map>
-#include <memory>
 #include <mutex>
 
 #include <android-base/thread_annotations.h>
+
 #include "config.h"
 
 static std::mutex filename_mutex;
diff --git a/otafault/ota_io.h b/otafault/ota_io.h
index 9428f1b..45e481a 100644
--- a/otafault/ota_io.h
+++ b/otafault/ota_io.h
@@ -23,8 +23,9 @@
 #ifndef _UPDATER_OTA_IO_H_
 #define _UPDATER_OTA_IO_H_
 
+#include <stddef.h>
 #include <stdio.h>
-#include <sys/stat.h>
+#include <sys/stat.h>  // mode_t
 
 #include <memory>
 
diff --git a/otafault/test.cpp b/otafault/test.cpp
index 6514782..60c40e0 100644
--- a/otafault/test.cpp
+++ b/otafault/test.cpp
@@ -14,9 +14,10 @@
  * limitations under the License.
  */
 
-#include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
 
 #include "ota_io.h"
diff --git a/roots.cpp b/roots.cpp
index 835a1dd..7d7d1bd 100644
--- a/roots.cpp
+++ b/roots.cpp
@@ -18,6 +18,7 @@
 
 #include <ctype.h>
 #include <fcntl.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
@@ -197,16 +198,22 @@
   return WEXITSTATUS(status);
 }
 
-static ssize_t get_file_size(int fd, uint64_t reserve_len) {
+static int64_t get_file_size(int fd, uint64_t reserve_len) {
   struct stat buf;
   int ret = fstat(fd, &buf);
   if (ret) return 0;
 
-  ssize_t computed_size;
+  int64_t computed_size;
   if (S_ISREG(buf.st_mode)) {
     computed_size = buf.st_size - reserve_len;
   } else if (S_ISBLK(buf.st_mode)) {
-    computed_size = get_block_device_size(fd) - reserve_len;
+    uint64_t block_device_size = get_block_device_size(fd);
+    if (block_device_size < reserve_len ||
+        block_device_size > std::numeric_limits<int64_t>::max()) {
+      computed_size = 0;
+    } else {
+      computed_size = block_device_size - reserve_len;
+    }
   } else {
     computed_size = 0;
   }
@@ -250,13 +257,13 @@
     close(fd);
   }
 
-  ssize_t length = 0;
+  int64_t length = 0;
   if (v->length != 0) {
     length = v->length;
   } else if (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0) {
     android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
     if (fd == -1) {
-      PLOG(ERROR) << "get_file_size: failed to open " << v->blk_device;
+      PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
       return -1;
     }
     length = get_file_size(fd.get(), CRYPT_FOOTER_OFFSET);
diff --git a/tests/Android.mk b/tests/Android.mk
index 748d9c8..31c7de1 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -143,7 +143,6 @@
     libdivsufsort \
     libdivsufsort64 \
     libfs_mgr \
-    liblog \
     libvintf_recovery \
     libvintf \
     libtinyxml2 \
@@ -154,6 +153,7 @@
     libcrypto \
     libbz \
     libziparchive \
+    liblog \
     libutils \
     libz \
     libbase \