optionally use a canned fs_config when building ext4 fs

In order to accurately re-create old images after changes to
android_filesystem_config.h, we need make_ext4fs and mkuserimg.sh to
be able to take a file containing the desired
uid/gid/mode/capabilities for each path in the filesystem, and use
that instead of the fs_config function that is built in to the binary.

The target_files already contains this file, which is created at the
time of the original build by running the binary fs_config for each
path in the build.

Change-Id: I501dc9544de37a592c1e831b6bf785c086cb6912
diff --git a/ext4_utils/Android.mk b/ext4_utils/Android.mk
index 8cb04eb..c5684f9 100644
--- a/ext4_utils/Android.mk
+++ b/ext4_utils/Android.mk
@@ -33,7 +33,7 @@
 
 
 include $(CLEAR_VARS)
-LOCAL_SRC_FILES := make_ext4fs_main.c
+LOCAL_SRC_FILES := make_ext4fs_main.c canned_fs_config.c
 LOCAL_MODULE := make_ext4fs
 LOCAL_STATIC_LIBRARIES += \
     libext4_utils_host \
@@ -74,7 +74,7 @@
 
 
 include $(CLEAR_VARS)
-LOCAL_SRC_FILES := make_ext4fs_main.c
+LOCAL_SRC_FILES := make_ext4fs_main.c canned_fs_config.c
 LOCAL_MODULE := make_ext4fs
 LOCAL_SHARED_LIBRARIES := \
     libext4_utils \
diff --git a/ext4_utils/canned_fs_config.c b/ext4_utils/canned_fs_config.c
new file mode 100644
index 0000000..3a155fc
--- /dev/null
+++ b/ext4_utils/canned_fs_config.c
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+
+#include "private/android_filesystem_config.h"
+
+#include "canned_fs_config.h"
+
+typedef struct {
+	const char* path;
+	unsigned uid;
+	unsigned gid;
+	unsigned mode;
+	uint64_t capabilities;
+} Path;
+
+static Path* canned_data = NULL;
+static int canned_alloc = 0;
+static int canned_used = 0;
+
+static int path_compare(const void* a, const void* b) {
+	return strcmp(((Path*)a)->path, ((Path*)b)->path);
+}
+
+int load_canned_fs_config(const char* fn) {
+	FILE* f = fopen(fn, "r");
+	if (f == NULL) {
+		fprintf(stderr, "failed to open %s: %s\n", fn, strerror(errno));
+		return -1;
+	}
+
+	char line[PATH_MAX + 200];
+	while (fgets(line, sizeof(line), f)) {
+		while (canned_used >= canned_alloc) {
+			canned_alloc = (canned_alloc+1) * 2;
+			canned_data = (Path*) realloc(canned_data, canned_alloc * sizeof(Path));
+		}
+		Path* p = canned_data + canned_used;
+		p->path = strdup(strtok(line, " "));
+		p->uid = atoi(strtok(NULL, " "));
+		p->gid = atoi(strtok(NULL, " "));
+		p->mode = strtol(strtok(NULL, " "), NULL, 8);   // mode is in octal
+		p->capabilities = 0;
+
+		char* token = NULL;
+		do {
+			token = strtok(NULL, " ");
+			if (token && strncmp(token, "capabilities=", 13) == 0) {
+				p->capabilities = strtoll(token+13, NULL, 0);
+				break;
+			}
+		} while (token);
+
+		canned_used++;
+	}
+
+	fclose(f);
+
+	qsort(canned_data, canned_used, sizeof(Path), path_compare);
+	printf("sorted %d canned fs_configs\n", canned_used);
+
+	return 0;
+}
+
+void canned_fs_config(const char* path, int dir,
+					  unsigned* uid, unsigned* gid, unsigned* mode, uint64_t* capabilities) {
+	Path key;
+	key.path = path+1;   // canned paths lack the leading '/'
+	Path* p = (Path*) bsearch(&key, canned_data, canned_used, sizeof(Path), path_compare);
+	if (p == NULL) {
+		fprintf(stderr, "failed to find [%s] in canned fs_config\n", path);
+		exit(1);
+	}
+	*uid = p->uid;
+	*gid = p->gid;
+	*mode = p->mode;
+	*capabilities = p->capabilities;
+
+#if 0
+	// for debugging, run the built-in fs_config and compare the results.
+
+	unsigned c_uid, c_gid, c_mode;
+	uint64_t c_capabilities;
+	fs_config(path, dir, &c_uid, &c_gid, &c_mode, &c_capabilities);
+
+	if (c_uid != *uid) printf("%s uid %d %d\n", path, *uid, c_uid);
+	if (c_gid != *gid) printf("%s gid %d %d\n", path, *gid, c_gid);
+	if (c_mode != *mode) printf("%s mode 0%o 0%o\n", path, *mode, c_mode);
+	if (c_capabilities != *capabilities) printf("%s capabilities %llx %llx\n", path, *capabilities, c_capabilities);
+#endif
+}
diff --git a/ext4_utils/canned_fs_config.h b/ext4_utils/canned_fs_config.h
new file mode 100644
index 0000000..aec923b
--- /dev/null
+++ b/ext4_utils/canned_fs_config.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _CANNED_FS_CONFIG_H
+#define _CANNED_FS_CONFIG_H
+
+#include <inttypes.h>
+
+int load_canned_fs_config(const char* fn);
+void canned_fs_config(const char* path, int dir,
+                      unsigned* uid, unsigned* gid, unsigned* mode, uint64_t* capabilities);
+
+#endif
diff --git a/ext4_utils/make_ext4fs_main.c b/ext4_utils/make_ext4fs_main.c
index 9163506..87eaecd 100644
--- a/ext4_utils/make_ext4fs_main.c
+++ b/ext4_utils/make_ext4fs_main.c
@@ -39,6 +39,7 @@
 
 #include "make_ext4fs.h"
 #include "ext4_utils.h"
+#include "canned_fs_config.h"
 
 #ifndef USE_MINGW /* O_BINARY is windows-specific flag */
 #define O_BINARY 0
@@ -52,7 +53,7 @@
 	fprintf(stderr, "%s [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]\n", basename(path));
 	fprintf(stderr, "    [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]\n");
 	fprintf(stderr, "    [ -L <label> ] [ -f ] [ -a <android mountpoint> ]\n");
-	fprintf(stderr, "    [ -S file_contexts ] [ -T timestamp ]\n");
+	fprintf(stderr, "    [ -S file_contexts ] [ -C fs_config ] [ -T timestamp ]\n");
 	fprintf(stderr, "    [ -z | -s ] [ -w ] [ -c ] [ -J ] [ -v ]\n");
 	fprintf(stderr, "    <filename> [<directory>]\n");
 }
@@ -64,6 +65,7 @@
 	const char *directory = NULL;
 	char *mountpoint = NULL;
 	fs_config_func_t fs_config_func = NULL;
+	const char *fs_config_file = NULL;
 	int gzip = 0;
 	int sparse = 0;
 	int crc = 0;
@@ -77,7 +79,7 @@
 	struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, "" } };
 #endif
 
-	while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:S:T:fwzJsctv")) != -1) {
+	while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:S:T:C:fwzJsctv")) != -1) {
 		switch (opt) {
 		case 'l':
 			info.len = parse_num(optarg);
@@ -105,7 +107,6 @@
 			break;
 		case 'a':
 #ifdef ANDROID
-			fs_config_func = fs_config;
 			mountpoint = optarg;
 #else
 			fprintf(stderr, "can't set android permissions - built without android support\n");
@@ -147,6 +148,9 @@
 		case 'T':
 			fixed_time = strtoll(optarg, NULL, 0);
 			break;
+		case 'C':
+			fs_config_file = optarg;
+			break;
 		default: /* '?' */
 			usage(argv[0]);
 			exit(EXIT_FAILURE);
@@ -165,6 +169,16 @@
 	}
 #endif
 
+	if (fs_config_file) {
+		if (load_canned_fs_config(fs_config_file) < 0) {
+			fprintf(stderr, "failed to load %s\n", fs_config_file);
+			exit(EXIT_FAILURE);
+		}
+		fs_config_func = canned_fs_config;
+	} else if (mountpoint) {
+		fs_config_func = fs_config;
+	}
+
 	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 6ef0294..de0914f 100755
--- a/ext4_utils/mkuserimg.sh
+++ b/ext4_utils/mkuserimg.sh
@@ -5,7 +5,7 @@
 function usage() {
 cat<<EOT
 Usage:
-mkuserimg.sh [-s] SRC_DIR OUTPUT_FILE EXT_VARIANT MOUNT_POINT SIZE [-T TIMESTAMP] [FILE_CONTEXTS]
+mkuserimg.sh [-s] SRC_DIR OUTPUT_FILE EXT_VARIANT MOUNT_POINT SIZE [-T TIMESTAMP] [-C FS_CONFIG] [FILE_CONTEXTS]
 EOT
 }
 
@@ -17,7 +17,7 @@
   shift
 fi
 
-if [ $# -lt 5 -o $# -gt 8 ]; then
+if [ $# -lt 5 ]; then
   usage
   exit 1
 fi
@@ -39,6 +39,13 @@
   TIMESTAMP=$2
   shift; shift
 fi
+
+FS_CONFIG=
+if [[ "$1" == "-C" ]]; then
+  FS_CONFIG=$2
+  shift; shift
+fi
+
 FC=$1
 
 case $EXT_VARIANT in
@@ -56,11 +63,15 @@
   exit 2
 fi
 
+OPT=""
 if [ -n "$FC" ]; then
-    FCOPT="-S $FC"
+  OPT="$OPT -S $FC"
+fi
+if [ -n "$FS_CONFIG" ]; then
+  OPT="$OPT -C $FS_CONFIG"
 fi
 
-MAKE_EXT4FS_CMD="make_ext4fs $ENABLE_SPARSE_IMAGE -T $TIMESTAMP $FCOPT -l $SIZE -a $MOUNT_POINT $OUTPUT_FILE $SRC_DIR"
+MAKE_EXT4FS_CMD="make_ext4fs $ENABLE_SPARSE_IMAGE -T $TIMESTAMP $OPT -l $SIZE -a $MOUNT_POINT $OUTPUT_FILE $SRC_DIR"
 echo $MAKE_EXT4FS_CMD
 $MAKE_EXT4FS_CMD
 if [ $? -ne 0 ]; then