am da068042: Reconcile with jb-mr1-release - do not merge

* commit 'da068042443cf968773bd9c7f2ca8e2ae8e59a05':
  make_ext4fs: fix dentry padding when dentry size is 4088 or 4092
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 be63e27..97cd26f 100644
--- a/ext4_utils/make_ext4fs.c
+++ b/ext4_utils/make_ext4fs.c
@@ -104,18 +104,30 @@
 {
 	int entries = 0;
 	struct dentry *dentries;
-	struct dirent **namelist;
+	struct dirent **namelist = NULL;
 	struct stat stat;
 	int ret;
 	int i;
 	u32 inode;
 	u32 entry_inode;
 	u32 dirs = 0;
+	bool needs_lost_and_found = false;
 
-	entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
-	if (entries < 0) {
-		error_errno("scandir");
-		return EXT4_ALLOCATE_FAILED;
+	if (full_path) {
+		entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
+		if (entries < 0) {
+			error_errno("scandir");
+			return EXT4_ALLOCATE_FAILED;
+		}
+	}
+
+	if (dir_inode == 0) {
+		/* root directory, check if lost+found already exists */
+		for (i = 0; i < entries; i++)
+			if (strcmp(namelist[i]->d_name, "lost+found") == 0)
+				break;
+		if (i == entries)
+			needs_lost_and_found = true;
 	}
 
 	dentries = calloc(entries, sizeof(struct dentry));
@@ -157,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
@@ -195,6 +210,32 @@
 	}
 	free(namelist);
 
+	if (needs_lost_and_found) {
+		/* insert a lost+found directory at the beginning of the dentries */
+		struct dentry *tmp = calloc(entries + 1, sizeof(struct dentry));
+		memset(tmp, 0, sizeof(struct dentry));
+		memcpy(tmp + 1, dentries, entries * sizeof(struct dentry));
+		dentries = tmp;
+
+		dentries[0].filename = strdup("lost+found");
+		asprintf(&dentries[0].path, "%s/lost+found", dir_path);
+		dentries[0].full_path = NULL;
+		dentries[0].size = 0;
+		dentries[0].mode = S_IRWXU;
+		dentries[0].file_type = EXT4_FT_DIR;
+		dentries[0].uid = 0;
+		dentries[0].gid = 0;
+		if (sehnd) {
+			char *sepath = NULL;
+			asprintf(&sepath, "/%s", dentries[0].path);
+			if (selabel_lookup(sehnd, &dentries[0].secon, sepath, dentries[0].mode) < 0)
+				error("cannot lookup security context for %s", dentries[0].path);
+			free(sepath);
+		}
+		entries++;
+		dirs++;
+	}
+
 	inode = make_directory(dir_inode, entries, dentries, dirs);
 
 	for (i = 0; i < entries; i++) {
@@ -323,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;
@@ -427,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