use fs_config and file_contexts from target_files

When building images, we want to use the file_contexts and fs_config
data contained in the target_files zip, rather than whatever happens
to be in the current client.

Change-Id: I13df2405898039f5a9b4bb4837147e76b31b068a
diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py
index 0bbdddc..6b593e3 100755
--- a/tools/releasetools/build_image.py
+++ b/tools/releasetools/build_image.py
@@ -207,13 +207,20 @@
   shutil.rmtree(tempdir_name, ignore_errors=True)
   return True
 
-def BuildImage(in_dir, prop_dict, out_file):
+def BuildImage(in_dir, prop_dict, out_file,
+               fs_config=None,
+               fc_config=None):
   """Build an image to out_file from in_dir with property prop_dict.
 
   Args:
     in_dir: path of input directory.
     prop_dict: property dictionary.
     out_file: path of the output image file.
+    fs_config: path to the fs_config file (typically
+      META/filesystem_config.txt).  If None then the configuration in
+      the local client will be used.
+    fc_config: path to the SELinux file_contexts file.  If None then
+      the value from prop_dict['selinux_fc'] will be used.
 
   Returns:
     True iff the image is built successfully.
@@ -243,7 +250,11 @@
     build_command.append(prop_dict["partition_size"])
     if "timestamp" in prop_dict:
       build_command.extend(["-T", str(prop_dict["timestamp"])])
-    if "selinux_fc" in prop_dict:
+    if fs_config is not None:
+      build_command.extend(["-C", fs_config])
+    if fc_config is not None:
+      build_command.append(fc_config)
+    elif "selinux_fc" in prop_dict:
       build_command.append(prop_dict["selinux_fc"])
   else:
     build_command = ["mkyaffs2image", "-f"]
diff --git a/tools/releasetools/img_from_target_files.py b/tools/releasetools/img_from_target_files.py
index 6be5a52..2ca9316 100755
--- a/tools/releasetools/img_from_target_files.py
+++ b/tools/releasetools/img_from_target_files.py
@@ -83,8 +83,17 @@
   fstab = info_dict["fstab"]
   if fstab:
     image_props["fs_type" ] = fstab["/system"].fs_type
+
+  fs_config = os.path.join(input_dir, "META/filesystem_config.txt")
+  if not os.path.exists(fs_config): fs_config = None
+
+  fc_config = os.path.join(input_dir, "BOOT/RAMDISK/file_contexts")
+  if not os.path.exists(fc_config): fc_config = None
+
   succ = build_image.BuildImage(os.path.join(input_dir, "system"),
-                                image_props, img.name)
+                                image_props, img.name,
+                                fs_config=fs_config,
+                                fc_config=fc_config)
   assert succ, "build system.img image failed"
 
   mapdata = None