DO NOT MERGE Android 8.0 stuff

Change-Id: I8c8a9734adbf36c33463123844fa6e078934ae34
diff --git a/crypto/ext4crypt/Android.mk b/crypto/ext4crypt/Android.mk
index bcbcccf..8b1dcd4 100644
--- a/crypto/ext4crypt/Android.mk
+++ b/crypto/ext4crypt/Android.mk
@@ -8,7 +8,7 @@
 LOCAL_SRC_FILES := Decrypt.cpp Ext4Crypt.cpp Keymaster.cpp KeyStorage.cpp ScryptParameters.cpp Utils.cpp HashPassword.cpp ext4_crypt.cpp
 LOCAL_SHARED_LIBRARIES := libselinux libc libc++ libext4_utils libsoftkeymaster libbase libcrypto libcutils libkeymaster_messages libhardware libprotobuf-cpp-lite
 LOCAL_STATIC_LIBRARIES := libscrypt_static
-LOCAL_C_INCLUDES := system/extras/ext4_utils external/scrypt/lib/crypto system/security/keystore hardware/libhardware/include/hardware system/security/softkeymaster/include/keymaster system/keymaster/include
+LOCAL_C_INCLUDES := system/extras/ext4_utils system/extras/ext4_utils/include/ext4_utils external/scrypt/lib/crypto system/security/keystore hardware/libhardware/include/hardware system/security/softkeymaster/include/keymaster system/keymaster/include
 
 ifneq ($(wildcard hardware/libhardware/include/hardware/keymaster0.h),)
     LOCAL_CFLAGS += -DTW_CRYPTO_HAVE_KEYMASTERX
diff --git a/gpt/Android.mk b/gpt/Android.mk
index 7369339..3c2ec3b 100644
--- a/gpt/Android.mk
+++ b/gpt/Android.mk
@@ -3,7 +3,9 @@
 # Build libgpt_twrp library
 
 include $(CLEAR_VARS)
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 26; echo $$?),0)
 LOCAL_CLANG := false
+endif
 LOCAL_MODULE := libgpt_twrp
 LOCAL_MODULE_TAGS := optional
 
diff --git a/libblkid/lib/fileutils.c b/libblkid/lib/fileutils.c
index c6eb0d6..92a391d 100644
--- a/libblkid/lib/fileutils.c
+++ b/libblkid/lib/fileutils.c
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <sys/time.h>
 #include <sys/resource.h>
+#include <sys/limits.h>
 
 #include "c.h"
 #include "fileutils.h"
@@ -18,6 +19,10 @@
 #define _PATH_TMP   "/tmp/"
 #endif
 
+#ifndef OPEN_MAX
+#define OPEN_MAX 256
+#endif
+
 /* Create open temporary file in safe way.  Please notice that the
  * file permissions are -rw------- by default. */
 int xmkstemp(char **tmpname, char *dir)
diff --git a/libpixelflinger/Android.mk b/libpixelflinger/Android.mk
index 044eea8..1cbde57 100644
--- a/libpixelflinger/Android.mk
+++ b/libpixelflinger/Android.mk
@@ -89,7 +89,9 @@
 #
 
 include $(CLEAR_VARS)
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 26; echo $$?),0)
 LOCAL_CLANG := false
+endif
 LOCAL_MODULE:= libpixelflinger_twrp
 LOCAL_SRC_FILES := $(PIXELFLINGER_SRC_FILES)
 LOCAL_SRC_FILES_arm := $(PIXELFLINGER_SRC_FILES_arm)
diff --git a/mtp/Android.mk b/mtp/Android.mk
index c6a7fb7..32518d3 100644
--- a/mtp/Android.mk
+++ b/mtp/Android.mk
@@ -40,6 +40,9 @@
 ifneq ($(TW_MTP_DEVICE),)
 	LOCAL_CFLAGS += -DUSB_MTP_DEVICE=$(TW_MTP_DEVICE)
 endif
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -gt 25; echo $$?),0)
+    LOCAL_CFLAGS += -DHAS_USBHOST_TIMEOUT
+endif
 
 include $(BUILD_SHARED_LIBRARY)
 
diff --git a/mtp/MtpDataPacket.cpp b/mtp/MtpDataPacket.cpp
index 2c51725..845db96 100644
--- a/mtp/MtpDataPacket.cpp
+++ b/mtp/MtpDataPacket.cpp
@@ -427,7 +427,11 @@
 
 // Wait for result of readDataAsync
 int MtpDataPacket::readDataWait(struct usb_device *device) {
+#ifdef HAS_USBHOST_TIMEOUT
+	struct usb_request *req = usb_request_wait(device, 200);
+#else
 	struct usb_request *req = usb_request_wait(device);
+#endif
 	return (req ? req->actual_length : -1);
 }
 
diff --git a/mtp/MtpDevice.cpp b/mtp/MtpDevice.cpp
index 53f8b2e..089fc22 100644
--- a/mtp/MtpDevice.cpp
+++ b/mtp/MtpDevice.cpp
@@ -50,6 +50,10 @@
 }
 #endif
 
+#ifdef HAS_USBHOST_TIMEOUT
+static const int USB_CONTROL_TRANSFER_TIMEOUT_MS = 200;
+#endif
+
 MtpDevice* MtpDevice::open(const char* deviceName, int fd) {
 	struct usb_device *device = usb_device_new(deviceName, fd);
 	if (!device) {
@@ -70,15 +74,24 @@
 				interface->bInterfaceSubClass == 1 && // Still Image Capture
 				interface->bInterfaceProtocol == 1)	 // Picture Transfer Protocol (PIMA 15470)
 			{
+#ifdef HAS_USBHOST_TIMEOUT
+				char* manufacturerName = usb_device_get_manufacturer_name(device, USB_CONTROL_TRANSFER_TIMEOUT_MS);
+				char* productName = usb_device_get_product_name(device, USB_CONTROL_TRANSFER_TIMEOUT_MS);
+#else
 				char* manufacturerName = usb_device_get_manufacturer_name(device);
 				char* productName = usb_device_get_product_name(device);
+#endif
 				MTPD("Found camera: \"%s\" \"%s\"\n", manufacturerName, productName);
 				free(manufacturerName);
 				free(productName);
 			} else if (interface->bInterfaceClass == 0xFF &&
 					interface->bInterfaceSubClass == 0xFF &&
 					interface->bInterfaceProtocol == 0) {
+#ifdef HAS_USBHOST_TIMEOUT
+				char* interfaceName = usb_device_get_string(device, interface->iInterface, USB_CONTROL_TRANSFER_TIMEOUT_MS);
+#else
 				char* interfaceName = usb_device_get_string(device, interface->iInterface);
+#endif
 				if (!interfaceName) {
 					continue;
 				} else if (strcmp(interfaceName, "MTP")) {
@@ -88,8 +101,13 @@
 				free(interfaceName);
 
 				// Looks like an android style MTP device
+#ifdef HAS_USBHOST_TIMEOUT
+				char* manufacturerName = usb_device_get_manufacturer_name(device, USB_CONTROL_TRANSFER_TIMEOUT_MS);
+				char* productName = usb_device_get_product_name(device, USB_CONTROL_TRANSFER_TIMEOUT_MS);
+#else
 				char* manufacturerName = usb_device_get_manufacturer_name(device);
 				char* productName = usb_device_get_product_name(device);
+#endif
 				MTPI("Found MTP device: \"%s\" \"%s\"\n", manufacturerName, productName);
 				free(manufacturerName);
 				free(productName);
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index 45edd01..b8f4d2e 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -47,14 +47,14 @@
             $(if $(filter $(PLATFORM_SDK_VERSION), 23 24), du)
 
         OUR_TOOLS := \
-            iftop \
-            ioctl \
-            nandread \
+            $(if $(shell test $(PLATFORM_SDK_VERSION) -lt 26; iftop),) \
+            $(if $(shell test $(PLATFORM_SDK_VERSION) -lt 26; ioctl),) \
+            $(if $(shell test $(PLATFORM_SDK_VERSION) -lt 26; nandread),) \
             newfs_msdos \
-            prlimit \
-            sendevent \
-            start \
-            stop \
+            $(if $(shell test $(PLATFORM_SDK_VERSION) -lt 26; prlimit),) \
+            $(if $(shell test $(PLATFORM_SDK_VERSION) -lt 26; sendevent),) \
+            $(if $(shell test $(PLATFORM_SDK_VERSION) -lt 26; start),) \
+            $(if $(shell test $(PLATFORM_SDK_VERSION) -lt 26; stop),) \
 
         ifneq (,$(filter $(PLATFORM_SDK_VERSION), 23))
             BSD_TOOLS += \
diff --git a/toybox/Android.mk b/toybox/Android.mk
index c4f0395..ca8fe46 100644
--- a/toybox/Android.mk
+++ b/toybox/Android.mk
@@ -69,7 +69,6 @@
     toys/android/runcon.c \
     toys/android/setenforce.c \
     toys/android/setprop.c \
-    toys/lsb/dmesg.c \
     toys/lsb/hostname.c \
     toys/lsb/killall.c \
     toys/lsb/md5sum.c \
@@ -213,7 +212,6 @@
     toys/other/xxd.c \
     toys/pending/arp.c \
     toys/pending/diff.c \
-    toys/pending/ftpget.c \
     toys/pending/lsof.c \
     toys/pending/telnet.c \
     toys/pending/test.c \
@@ -222,6 +220,17 @@
     toys/posix/ps.c \
     toys/posix/ulimit.c
 
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -gt 25; echo $$?),0)
+# Android 8.0 had some tools in different paths
+LOCAL_SRC_FILES += \
+    toys/pending/dmesg.c \
+    toys/net/ftpget.c
+else
+LOCAL_SRC_FILES += \
+    toys/lsb/dmesg.c \
+    toys/pending/ftpget.c
+endif
+
 # Account for master branch changes pulld into CM14.1
 ifneq ($(CM_BUILD),)
 LOCAL_SRC_FILES += \
@@ -242,12 +251,22 @@
     toys/posix/file.c
 else
 LOCAL_SRC_FILES += \
+    toys/other/switch_root.c
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -gt 25; echo $$?),0)
+# Android 8.0 had some tools in different paths
+LOCAL_SRC_FILES += \
+    toys/net/ifconfig.c \
+    toys/net/netcat.c \
+    toys/net/netstat.c \
+    toys/net/rfkill.c
+else
+LOCAL_SRC_FILES += \
     toys/other/ifconfig.c \
     toys/other/netcat.c \
     toys/other/rfkill.c \
-    toys/other/switch_root.c \
     toys/pending/netstat.c
 endif
+endif
 else
 LOCAL_SRC_FILES += \
     toys/other/ifconfig.c \