android/filesystems: Fix a bug in ramdisk_extractor.cpp

This patch fixes a bug that prevented proper extraction of ramdisk
files when the target file appears after another file with the exact
same name length in the cpio stream.

+ Add regression test that updates the test ramdisk image.

Change-Id: I7e03c8acf725fd15a94c97bd2592690297d59ab3
Signed-off-by: Jason Hu <jia-cheng.hu@intel.com>
diff --git a/android/filesystems/ramdisk_extractor.cpp b/android/filesystems/ramdisk_extractor.cpp
index 62e4445..4c56e42 100644
--- a/android/filesystems/ramdisk_extractor.cpp
+++ b/android/filesystems/ramdisk_extractor.cpp
@@ -258,7 +258,7 @@
 
         // The file data is 4-byte padded with NUL bytes too.
         size_t skipFile = (entrySize + 3) & ~3;
-        size_t skipCount = 0;
+        size_t skipCount = skipName + skipFile;
 
         // Last record is named 'TRAILER!!!' and indicates end of archive.
         static const char kTrailer[] = "TRAILER!!!";
@@ -267,7 +267,6 @@
         if ((entrySize == 0 || nameSize != fileNameLen + 1U) &&
             nameSize != kTrailerSize + 1U) {
             D("---- %d Skipping\n", __LINE__);
-            skipCount = skipName + skipFile;
         } else {
             // Read the name and compare it.
             nameSize -= 1U;
diff --git a/android/filesystems/ramdisk_extractor_unittest.cpp b/android/filesystems/ramdisk_extractor_unittest.cpp
index 2462b5d..b87f888 100644
--- a/android/filesystems/ramdisk_extractor_unittest.cpp
+++ b/android/filesystems/ramdisk_extractor_unittest.cpp
@@ -80,6 +80,20 @@
     free(out);
 }
 
+TEST_F(RamdiskExtractorTest, FindZoo) {
+    static const char kExpected[] = "Meow!!\n";
+    static const size_t kExpectedSize = sizeof(kExpected) - 1U;
+    char* out = NULL;
+    size_t outSize = 0;
+
+    EXPECT_TRUE(fillData(kTestRamdiskImage, kTestRamdiskImageSize));
+    EXPECT_TRUE(android_extractRamdiskFile(path(), "zoo", &out, &outSize));
+    EXPECT_EQ(kExpectedSize, outSize);
+    EXPECT_TRUE(out);
+    EXPECT_TRUE(!memcmp(out, kExpected, outSize));
+    free(out);
+}
+
 TEST_F(RamdiskExtractorTest, MissingFile) {
     char* out = NULL;
     size_t outSize = 0;
diff --git a/android/filesystems/testing/TestRamdiskImage.h b/android/filesystems/testing/TestRamdiskImage.h
index baec302..7430d0d 100644
--- a/android/filesystems/testing/TestRamdiskImage.h
+++ b/android/filesystems/testing/TestRamdiskImage.h
@@ -12,24 +12,26 @@
 #ifndef ANDROID_FILESYSTEMS_TESTING_TEST_RAMDISK_IMAGE_H
 #define ANDROID_FILESYSTEMS_TESTING_TEST_RAMDISK_IMAGE_H
 
-/* Auto-generated by create_ramdisk_test_data.sh on 2014-06-23 - DO NOT EDIT!! */
+/* Auto-generated by create_ramdisk_test_data.sh on 2014-07-15 - DO NOT EDIT!! */
 
 static const unsigned char kTestRamdiskImage[] = {
-  0x1f, 0x8b, 0x08, 0x00, 0x6d, 0x1a, 0xa8, 0x53, 0x02, 0x03, 0xbd, 0x8e,
-  0x41, 0x0a, 0x83, 0x30, 0x10, 0x45, 0xb3, 0xee, 0x29, 0x26, 0x37, 0x98,
-  0xc4, 0x46, 0xd3, 0x65, 0xac, 0x86, 0x16, 0x5c, 0x49, 0xa1, 0x6b, 0x4b,
-  0x63, 0x11, 0x06, 0x43, 0xad, 0x7a, 0xfe, 0x2a, 0x41, 0x90, 0x2e, 0x8a,
-  0xdd, 0xf4, 0x6d, 0xfe, 0x1f, 0x66, 0x60, 0x1e, 0x26, 0x98, 0xa0, 0x40,
-  0x14, 0xb9, 0xd4, 0x07, 0x8b, 0x13, 0x5a, 0x98, 0x39, 0x30, 0xb6, 0x26,
-  0x9d, 0x53, 0x44, 0x5a, 0x63, 0x40, 0xa8, 0xc8, 0x4c, 0xeb, 0x38, 0x0b,
-  0xa3, 0x54, 0x21, 0xed, 0x11, 0xbf, 0xa3, 0x96, 0x72, 0xab, 0x3a, 0xc9,
-  0x58, 0x51, 0xc1, 0xd8, 0x38, 0x70, 0xaf, 0x1e, 0x86, 0x16, 0xc8, 0xb7,
-  0x0f, 0xa8, 0xc9, 0x0d, 0xa3, 0x83, 0xbe, 0xab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b, 0x4a, 0x6b, 0xe5, 0x10, 0x42, 0xe0, 0xbd, 0xcd, 0x85, 0x70, 0xdd,
+  0x36, 0xe0, 0x0b, 0xb7, 0xf9, 0x53, 0x79, 0x58, 0xdb, 0x3b, 0xf7, 0xca,
+  0xde, 0x19, 0x73, 0xe6, 0x6e, 0x94, 0x62, 0xa5, 0x7b, 0xb3, 0xd6, 0x5d,
+  0xbe, 0xdc, 0x5b, 0xa5, 0xb5, 0xa5, 0x47, 0x5b, 0xeb, 0x33, 0x9b, 0xdf,
+  0xbf, 0x88, 0xa5, 0xff, 0x3e, 0x89, 0xa7, 0x72, 0xc8, 0xc5, 0x2e, 0x93,
+  0x39, 0x63, 0x8c, 0xfc, 0xf9, 0x19, 0x9e, 0x05, 0x57, 0xf3, 0x6c, 0x00,
+  0x04, 0x00, 0x00
 };
 
 static const size_t kTestRamdiskImageSize = sizeof(kTestRamdiskImage);
diff --git a/android/filesystems/testing/create_ramdisk_test_data.sh b/android/filesystems/testing/create_ramdisk_test_data.sh
index 1bd3e42..39ce3dd 100755
--- a/android/filesystems/testing/create_ramdisk_test_data.sh
+++ b/android/filesystems/testing/create_ramdisk_test_data.sh
@@ -27,7 +27,9 @@
 cat > bar2 <<EOF
 La vie est un long fleuve tranquille
 EOF
-
+cat > zoo <<EOF
+Meow!!
+EOF
 echo "/* Auto-generated by $PROGNAME on $DATE - DO NOT EDIT!! */"
 echo ""
 echo "static const unsigned char kTestRamdiskImage[] = {"