am 07c8a6d2: Reconcile with jb-mr1-factory-release jb-mr1-release - do not merge

* commit '07c8a6d238ddf855d5d6b4421d2527c580a374b6':
diff --git a/ext4_utils/Android.mk b/ext4_utils/Android.mk
index 99145a4..1fef735 100644
--- a/ext4_utils/Android.mk
+++ b/ext4_utils/Android.mk
@@ -21,13 +21,12 @@
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES := $(libext4_utils_src_files)
 LOCAL_MODULE := libext4_utils_host
-LOCAL_C_INCLUDES += external/zlib
-LOCAL_STATIC_LIBRARIES += libsparse_host
-ifeq ($(HAVE_SELINUX), true)
-  LOCAL_C_INCLUDES += external/libselinux/include
+LOCAL_STATIC_LIBRARIES := \
+    libsparse_host \
+    libz
+ifneq ($(HOST_OS),windows)
   LOCAL_STATIC_LIBRARIES += libselinux
-  LOCAL_CFLAGS += -DHAVE_SELINUX
-endif # HAVE_SELINUX
+endif
 include $(BUILD_HOST_STATIC_LIBRARY)
 
 
@@ -41,11 +40,8 @@
 ifeq ($(HOST_OS),windows)
   LOCAL_LDLIBS += -lws2_32
 else
-  ifeq ($(HAVE_SELINUX), true)
-    LOCAL_C_INCLUDES += external/libselinux/include
-    LOCAL_STATIC_LIBRARIES += libselinux
-    LOCAL_CFLAGS += -DHAVE_SELINUX
-  endif # HAVE_SELINUX
+  LOCAL_STATIC_LIBRARIES += libselinux
+  LOCAL_CFLAGS := -DHOST
 endif
 include $(BUILD_HOST_EXECUTABLE)
 
@@ -59,41 +55,29 @@
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES := $(libext4_utils_src_files)
 LOCAL_MODULE := libext4_utils
-LOCAL_C_INCLUDES += external/zlib
 LOCAL_SHARED_LIBRARIES := \
+    libselinux \
     libsparse \
     libz
-ifeq ($(HAVE_SELINUX), true)
-  LOCAL_C_INCLUDES += external/libselinux/include
-  LOCAL_SHARED_LIBRARIES += libselinux
-  LOCAL_CFLAGS += -DHAVE_SELINUX
-endif # HAVE_SELINUX
 include $(BUILD_SHARED_LIBRARY)
 
 
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES := $(libext4_utils_src_files)
 LOCAL_MODULE := libext4_utils_static
-LOCAL_C_INCLUDES += external/zlib
 LOCAL_STATIC_LIBRARIES += \
+    libselinux \
     libsparse_static
-ifeq ($(HAVE_SELINUX), true)
-  LOCAL_C_INCLUDES += external/libselinux/include
-  LOCAL_STATIC_LIBRARIES += libselinux
-  LOCAL_CFLAGS += -DHAVE_SELINUX
-endif # HAVE_SELINUX
 include $(BUILD_STATIC_LIBRARY)
 
 
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES := make_ext4fs_main.c
 LOCAL_MODULE := make_ext4fs
-LOCAL_SHARED_LIBRARIES += libext4_utils libz
-ifeq ($(HAVE_SELINUX), true)
-  LOCAL_C_INCLUDES += external/libselinux/include
-  LOCAL_SHARED_LIBRARIES += libselinux
-  LOCAL_CFLAGS += -DHAVE_SELINUX
-endif # HAVE_SELINUX
+LOCAL_SHARED_LIBRARIES := \
+    libext4_utils \
+    libselinux \
+    libz
 include $(BUILD_EXECUTABLE)
 
 
@@ -102,13 +86,9 @@
 LOCAL_MODULE := ext2simg
 LOCAL_SHARED_LIBRARIES += \
     libext4_utils \
+    libselinux \
     libsparse \
     libz
-ifeq ($(HAVE_SELINUX), true)
-  LOCAL_C_INCLUDES += external/libselinux/include
-  LOCAL_SHARED_LIBRARIES += libselinux
-  LOCAL_CFLAGS += -DHAVE_SELINUX
-endif # HAVE_SELINUX
 include $(BUILD_EXECUTABLE)
 
 
@@ -117,13 +97,9 @@
 LOCAL_MODULE := ext2simg
 LOCAL_STATIC_LIBRARIES += \
     libext4_utils_host \
+    libselinux \
     libsparse_host \
     libz
-ifeq ($(HAVE_SELINUX), true)
-  LOCAL_C_INCLUDES += external/libselinux/include
-  LOCAL_STATIC_LIBRARIES += libselinux
-  LOCAL_CFLAGS += -DHAVE_SELINUX
-endif # HAVE_SELINUX
 include $(BUILD_HOST_EXECUTABLE)
 
 
diff --git a/ext4_utils/contents.c b/ext4_utils/contents.c
index 345ab1e..8d20eb2 100644
--- a/ext4_utils/contents.c
+++ b/ext4_utils/contents.c
@@ -250,7 +250,6 @@
 	return 0;
 }
 
-#ifdef HAVE_SELINUX
 #define XATTR_SELINUX_SUFFIX "selinux"
 
 /* XXX */
@@ -292,9 +291,3 @@
 
 	return 0;
 }
-#else
-int inode_set_selinux(u32 inode_num, const char *secon)
-{
-	return 0;
-}
-#endif
diff --git a/ext4_utils/make_ext4fs.c b/ext4_utils/make_ext4fs.c
index be63e27..ca81398 100644
--- a/ext4_utils/make_ext4fs.c
+++ b/ext4_utils/make_ext4fs.c
@@ -157,15 +157,18 @@
 			error("can't set android permissions - built without android support");
 #endif
 		}
-#ifdef HAVE_SELINUX
+#ifndef USE_MINGW
 		if (sehnd) {
 			char *sepath = NULL;
 			asprintf(&sepath, "/%s", dentries[i].path);
 			if (selabel_lookup(sehnd, &dentries[i].secon, sepath, stat.st_mode) < 0) {
 				error("cannot lookup security context for %s", sepath);
 			}
+#if 0
+			// TODO make this a debug flag
 			if (dentries[i].secon)
 				printf("Labeling %s as %s\n", sepath, dentries[i].secon);
+#endif
 			free(sepath);
 		}
 #endif
@@ -323,8 +326,9 @@
 }
 
 int make_ext4fs_internal(int fd, const char *directory,
-                         char *mountpoint, fs_config_func_t fs_config_func, int gzip, int sparse,
-                         int crc, int wipe, int init_itabs, struct selabel_handle *sehnd)
+                         const char *mountpoint, fs_config_func_t fs_config_func, int gzip,
+                         int sparse, int crc, int wipe, int init_itabs,
+                         struct selabel_handle *sehnd)
 {
 	u32 root_inode_num;
 	u16 root_mode;
@@ -427,7 +431,7 @@
 	root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
 	inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
 
-#ifdef HAVE_SELINUX
+#ifndef USE_MINGW
 	if (sehnd) {
 		char *sepath = NULL;
 		char *secontext = NULL;
diff --git a/ext4_utils/make_ext4fs.h b/ext4_utils/make_ext4fs.h
index c217c3d..e80e636 100644
--- a/ext4_utils/make_ext4fs.h
+++ b/ext4_utils/make_ext4fs.h
@@ -24,9 +24,10 @@
 extern "C" {
 #endif
 
-#ifdef HAVE_SELINUX
+#ifndef USE_MINGW
 #include <selinux/selinux.h>
 #include <selinux/label.h>
+#include <selinux/android.h>
 #else
 struct selabel_handle;
 #endif
@@ -38,8 +39,9 @@
 int make_ext4fs(const char *filename, s64 len,
                 const char *mountpoint, struct selabel_handle *sehnd);
 int make_ext4fs_internal(int fd, const char *directory,
-                         char *mountpoint, fs_config_func_t fs_config_func, int gzip, int sparse,
-                         int crc, int wipe, int init_itabs, struct selabel_handle *sehnd);
+                         const char *mountpoint, fs_config_func_t fs_config_func, int gzip,
+                         int sparse, int crc, int wipe, int init_itabs,
+                         struct selabel_handle *sehnd);
 
 #ifdef __cplusplus
 }
diff --git a/ext4_utils/make_ext4fs_main.c b/ext4_utils/make_ext4fs_main.c
index f7beeb5..000b857 100644
--- a/ext4_utils/make_ext4fs_main.c
+++ b/ext4_utils/make_ext4fs_main.c
@@ -62,7 +62,7 @@
 	int fd;
 	int exitcode;
 	struct selabel_handle *sehnd = NULL;
-#ifdef HAVE_SELINUX
+#ifndef USE_MINGW
 	struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, "" } };
 #endif
 
@@ -121,7 +121,7 @@
 			init_itabs = 1;
 			break;
 		case 'S':
-#ifdef HAVE_SELINUX
+#ifndef USE_MINGW
 			seopts[0].value = optarg;
 			sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
 			if (!sehnd) {
@@ -129,13 +129,25 @@
 				exit(EXIT_FAILURE);
 			}
 #endif
-			   break;
+			break;
 		default: /* '?' */
 			usage(argv[0]);
 			exit(EXIT_FAILURE);
 		}
 	}
 
+#if !defined(HOST)
+	// Use only if -S option not requested
+	if (!sehnd && mountpoint[0] != '\0') {
+		sehnd = selinux_android_file_context_handle();
+
+		if (!sehnd) {
+			perror(optarg);
+			exit(EXIT_FAILURE);
+		}
+	}
+#endif
+
 	if (wipe && sparse) {
 		fprintf(stderr, "Cannot specifiy both wipe and sparse\n");
 		usage(argv[0]);