make_factory_package: Remove race condition.

Remove race condition caused by unneeded temporary file with fixed name.

BUG=chrome-os-partner:12999
TEST=Run two scripts simataneously with same source images, and then netboot
     two devices with two miniomaha servers on different ports.

Change-Id: I3d20b77fc658c26366d7ce0ad2fcaa379e9c2843
Reviewed-on: https://gerrit.chromium.org/gerrit/31110
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
Commit-Ready: Shik Chen <shik@chromium.org>
Tested-by: Shik Chen <shik@chromium.org>
diff --git a/factory_setup/make_factory_package.sh b/factory_setup/make_factory_package.sh
index 4784dbb..f53a9e4 100755
--- a/factory_setup/make_factory_package.sh
+++ b/factory_setup/make_factory_package.sh
@@ -372,14 +372,15 @@
 }
 
 # Compresses kernel and rootfs of an imge file, and output its hash.
-# Usage:compress_and_hash_memento_image kernel rootfs
+# Usage:compress_and_hash_memento_image kernel rootfs output_file
 # Please see "mk_memento_images --help" for detail of parameter syntax
 compress_and_hash_memento_image() {
-  local kern="$1"
+  local kernel="$1"
   local rootfs="$2"
-  [ "$#" = "2" ] || die "Internal error: compress_and_hash_memento_image $*"
+  local output_file="$3"
+  [ "$#" = "3" ] || die "Internal error: compress_and_hash_memento_image $*"
 
-  "${SCRIPT_DIR}/mk_memento_images.sh" "$kern" "$rootfs" "." |
+  "${SCRIPT_DIR}/mk_memento_images.sh" "$kernel" "$rootfs" "$output_file" |
     grep hash |
     awk '{print $4}'
 }
@@ -563,60 +564,45 @@
   echo "Output omaha image to ${OMAHA_DATA_DIR}"
   echo "Output omaha config to ${OMAHA_CONF}"
 
-  # Get the release image.
-  # TODO(hungte) deprecate pushd and use temporary folders
-  pushd "${RELEASE_DIR}" >/dev/null
-  prepare_dir "."
-
-  kernel="${RELEASE_KERNEL:-${RELEASE_IMAGE}:2}"
-  rootfs="${RELEASE_IMAGE}:3"
-  release_hash="$(compress_and_hash_memento_image "$kernel" "$rootfs")"
-  mv ./update.gz "${OMAHA_DATA_DIR}/rootfs-release.gz"
+  kernel="${RELEASE_KERNEL:-${FLAGS_release}:2}"
+  rootfs="${FLAGS_release}:3"
+  release_hash="$(compress_and_hash_memento_image "$kernel" "$rootfs" \
+                  "${OMAHA_DATA_DIR}/rootfs-release.gz")"
   echo "release: ${release_hash}"
 
-  oem_hash="$(compress_and_hash_partition "${RELEASE_IMAGE}" 8 "oem.gz")"
-  mv oem.gz "${OMAHA_DATA_DIR}"
+  oem_hash="$(compress_and_hash_partition "${FLAGS_release}" 8 \
+              "${OMAHA_DATA_DIR}/oem.gz")"
   echo "oem: ${oem_hash}"
 
-  popd >/dev/null
-
-  # Go to retrieve the factory test image.
-  pushd "${FACTORY_DIR}" >/dev/null
-  prepare_dir "."
-
-  kernel="${FACTORY_IMAGE}:2"
-  rootfs="${FACTORY_IMAGE}:3"
-  test_hash="$(compress_and_hash_memento_image "$kernel" "$rootfs")"
-  mv ./update.gz "${OMAHA_DATA_DIR}/rootfs-test.gz"
+  kernel="${FLAGS_factory}:2"
+  rootfs="${FLAGS_factory}:3"
+  test_hash="$(compress_and_hash_memento_image "$kernel" "$rootfs" \
+               "${OMAHA_DATA_DIR}/rootfs-test.gz")"
   echo "test: ${test_hash}"
 
-  state_hash="$(compress_and_hash_partition "${FACTORY_IMAGE}" 1 "state.gz")"
-  mv state.gz "${OMAHA_DATA_DIR}"
+  state_hash="$(compress_and_hash_partition "${FLAGS_factory}" 1 \
+                "${OMAHA_DATA_DIR}/state.gz")"
   echo "state: ${state_hash}"
 
-  efi_hash="$(compress_and_hash_partition "${FACTORY_IMAGE}" 12 "efi.gz")"
-  mv efi.gz "${OMAHA_DATA_DIR}"
+  efi_hash="$(compress_and_hash_partition "${FLAGS_factory}" 12 \
+              "${OMAHA_DATA_DIR}/efi.gz")"
   echo "efi: ${efi_hash}"
 
-  popd >/dev/null
-
   if [ -n "${FLAGS_firmware_updater}" ]; then
     firmware_hash="$(compress_and_hash_file "${FLAGS_firmware_updater}" \
-                     "firmware.gz")"
-    mv firmware.gz "${OMAHA_DATA_DIR}"
+                     "${OMAHA_DATA_DIR}/firmware.gz")"
     echo "firmware: ${firmware_hash}"
   fi
 
   if [ -n "${FLAGS_hwid_updater}" ]; then
-    hwid_hash="$(compress_and_hash_file "${FLAGS_hwid_updater}" "hwid.gz")"
-    mv hwid.gz "${OMAHA_DATA_DIR}"
+    hwid_hash="$(compress_and_hash_file "${FLAGS_hwid_updater}" \
+                 "${OMAHA_DATA_DIR}/hwid.gz")"
     echo "hwid: ${hwid_hash}"
   fi
 
   if [ -n "${FLAGS_complete_script}" ]; then
     complete_hash="$(compress_and_hash_file "${FLAGS_complete_script}" \
-                     "complete.gz")"
-    mv complete.gz "${OMAHA_DATA_DIR}"
+                     "${OMAHA_DATA_DIR}/complete.gz")"
     echo "complete: ${complete_hash}"
   fi
 
diff --git a/factory_setup/mk_memento_images.sh b/factory_setup/mk_memento_images.sh
index 3cb6f40..ab28bb3 100755
--- a/factory_setup/mk_memento_images.sh
+++ b/factory_setup/mk_memento_images.sh
@@ -14,13 +14,14 @@
 # We need 2-3 non-zero parameters.
 if [ "$#" -lt 2 ] || [ "$#" -gt 3 ] || [ -z "$1" ] || [ -z "$2" ]; then
   echo "
-Usage: $0 kernel_partition_img[:index] rootfs_partition_img[:index] [output_dir]
+Usage: $0 kernel_partition_img[:index] rootfs_partition_img[:index] \
+          [output_file]
 
           Input parameters may be either a simple partition image file, or a
           disk image file name followed by ':' and target partition index number
 
-          If output_dir is omitted, the folder of kernel_partition_img will be
-          use.
+          If output_file is omitted, the folder of kernel_partition_img will be
+          used, and the output file will be named as 'update.gz'.
 
 Examples:
        $0 part_2 part_3
@@ -94,11 +95,10 @@
 fi
 
 if [ "$#" = "3" ]; then
-  FINAL_OUT_DIR="$(readlink -f $3)"
+  FINAL_OUT_FILE="$(readlink -f $3)"
 else
-  FINAL_OUT_DIR="$(dirname "$(readlink -f $1)")"
+  FINAL_OUT_FILE="$(dirname "$(readlink -f $1)")/update.gz"
 fi
-FINAL_OUT_FILE="$FINAL_OUT_DIR/update.gz"
 echo "Output: $FINAL_OUT_FILE"
 
 # Update payload format: