{"thread":{"id":"33030","subject":"[PATCH] archive-zip: fix compressed size for stored export-subst files","startedAt":"2013-02-27T10:20:21Z","lastAt":"2013-02-27T10:20:21Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"210388","messageId":"512DDDE5.5070707@lsrfire.ath.cx","threadId":"33030","inReplyTo":null,"subject":"[PATCH] archive-zip: fix compressed size for stored export-subst files","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2013-02-27T10:20:21Z","receivedAt":"2013-02-27T10:20:21Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Currently ZIP archive entries of files with export-subst attribute are\nbroken if they are stored uncompressed.\n\nWe get the size of a file from sha1_object_info(), but this number is\nlikely wrong for files whose contents are changed due to export-subst\nplaceholder expansion.  We use sha1_file_to_archive() to get the\nexpanded file contents and size in that case.  We proceed to use that\nsize for the uncompressed size field (good), but the compressed size\nfield is set based on the size from sha1_object_info() (bad).\n\nThis matters only for uncompressed files because for deflated files\nwe use the correct value after compression is done.  And for files\nwithout export-subst expansion the sizes from sha1_object_info() and\nsha1_file_to_archive() are the same, so they are unaffected as well.\n\nThis patch fixes the issue by setting the compressed size based on the\nuncompressed size only after we actually know the latter.\n\nAlso make use of the test file substfile1 to check for the breakage;\nit was only stored verbatim so far.  For that purpose, set the\nattribute export-subst and replace its contents with the expected\nexpansion after committing.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\nThis bug was present even before 5ea2c847 (archive-zip: write\nuncompressed size into header even with streaming).\n\n archive-zip.c          | 2 +-\n t/t5003-archive-zip.sh | 6 ++++++\n 2 files changed, 7 insertions(+), 1 deletion(-)\n\ndiff --git a/archive-zip.c b/archive-zip.c\nindex d3aef53..a8d1193 100644\n--- a/archive-zip.c\n+++ b/archive-zip.c\n@@ -240,7 +240,6 @@ static int write_zip_entry(struct archiver_args *args,\n \t\t\t(mode & 0111) ? ((mode) << 16) : 0;\n \t\tif (S_ISREG(mode) && args->compression_level != 0 && size > 0)\n \t\t\tmethod = 8;\n-\t\tcompressed_size = (method == 0) ? size : 0;\n \n \t\tif (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&\n \t\t    size > big_file_threshold) {\n@@ -259,6 +258,7 @@ static int write_zip_entry(struct archiver_args *args,\n \t\t\tcrc = crc32(crc, buffer, size);\n \t\t\tout = buffer;\n \t\t}\n+\t\tcompressed_size = (method == 0) ? size : 0;\n \t} else {\n \t\treturn error(\"unsupported file mode: 0%o (SHA1: %s)\", mode,\n \t\t\t\tsha1_to_hex(sha1));\ndiff --git a/t/t5003-archive-zip.sh b/t/t5003-archive-zip.sh\nindex 7cfe9ca..6a33606 100755\n--- a/t/t5003-archive-zip.sh\n+++ b/t/t5003-archive-zip.sh\n@@ -76,6 +76,12 @@ test_expect_success \\\n      git update-ref HEAD $(TZ=GMT GIT_COMMITTER_DATE=\"2005-05-27 22:00:00\" \\\n      git commit-tree $treeid </dev/null)'\n \n+test_expect_success 'setup export-subst' '\n+\techo \"substfile?\" export-subst >>.git/info/attributes &&\n+\tgit log --max-count=1 \"--pretty=format:A${SUBSTFORMAT}O\" HEAD \\\n+\t\t>a/substfile1\n+'\n+\n test_expect_success \\\n     'create bare clone' \\\n     'git clone --bare . bare.git &&\n-- \n1.8.0\n"}]}