resolved conflicts for merge of 917e120f to jb-mr1-dev-plus-aosp

Change-Id: I5f3176d4f4859e7cf70899173f28944fc3a678b4
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 aeed31e..6300680 100644
--- a/ext4_utils/contents.c
+++ b/ext4_utils/contents.c
@@ -242,7 +242,6 @@
 	return 0;
 }
 
-#ifdef HAVE_SELINUX
 #define XATTR_SELINUX_SUFFIX "selinux"
 
 /* XXX */
@@ -284,9 +283,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 db6fbe6..97cd26f 100644
--- a/ext4_utils/make_ext4fs.c
+++ b/ext4_utils/make_ext4fs.c
@@ -169,15 +169,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
@@ -222,7 +225,6 @@
 		dentries[0].file_type = EXT4_FT_DIR;
 		dentries[0].uid = 0;
 		dentries[0].gid = 0;
-#ifdef HAVE_SELINUX
 		if (sehnd) {
 			char *sepath = NULL;
 			asprintf(&sepath, "/%s", dentries[0].path);
@@ -230,7 +232,6 @@
 				error("cannot lookup security context for %s", dentries[0].path);
 			free(sepath);
 		}
-#endif
 		entries++;
 		dirs++;
 	}
@@ -363,8 +364,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;
@@ -467,7 +469,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]);
diff --git a/ext4_utils/mkuserimg.sh b/ext4_utils/mkuserimg.sh
index 1136a9e..c44129e 100755
--- a/ext4_utils/mkuserimg.sh
+++ b/ext4_utils/mkuserimg.sh
@@ -17,7 +17,7 @@
   shift
 fi
 
-if [ $# -ne 4 -a $# -ne 5 -a $# -ne 6 ]; then
+if [ $# -ne 5 -a $# -ne 6 ]; then
   usage
   exit 1
 fi
@@ -45,7 +45,8 @@
 fi
 
 if [ -z $SIZE ]; then
-    SIZE=128M
+  echo "Need size of filesystem"
+  exit 2
 fi
 
 if [ -n "$FC" ]; then