Subject: Re: [PATCH] archive-zip: add --text parameter

From: René Scharfe <[email-masked]>
Date: Thu, 05 Mar 2015 16:27:48 +0100

Am 04.03.2015 um 22:13 schrieb René Scharfe:
> diff --git a/archive-zip.c b/archive-zip.c
> index 4bde019..3767940 100644
> --- a/archive-zip.c
> +++ b/archive-zip.c
> @@ -5,6 +5,7 @@
>   #include "archive.h"
>   #include "streaming.h"
>   #include "utf8.h"
> +#include "xdiff-interface.h"
>
>   static int zip_date;
>   static int zip_time;
> @@ -210,6 +211,7 @@ static int write_zip_entry(struct archiver_args *args,
>   	struct git_istream *stream = NULL;
>   	unsigned long flags = 0;
>   	unsigned long size;
> +	int is_binary = -1;
>
>   	crc = crc32(0, NULL, 0);
>
> @@ -238,8 +240,14 @@ static int write_zip_entry(struct archiver_args *args,
>   		method = 0;
>   		attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
>   			(mode & 0111) ? ((mode) << 16) : 0;
> -		if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
> -			method = 8;
> +		if (S_ISREG(mode)) {
> +			if (args->compression_level != 0 && size > 0)
> +				method = 8;
> +			if (args->text == ARCHIVE_TEXT_ALL)
> +				is_binary = 0;
> +			else if (args->text == ARCHIVE_TEXT_NONE)
> +				is_binary = 1;
> +		}
>
>   		if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
>   		    size > big_file_threshold) {
> @@ -256,6 +264,8 @@ static int write_zip_entry(struct archiver_args *args,
>   				return error("cannot read %s",
>   					     sha1_to_hex(sha1));
>   			crc = crc32(crc, buffer, size);
> +			if (is_binary < 0)
> +				is_binary = buffer_is_binary(buffer, size);
>   			out = buffer;
>   		}
>   		compressed_size = (method == 0) ? size : 0;
> @@ -300,7 +310,6 @@ static int write_zip_entry(struct archiver_args *args,
>   	copy_le16(dirent.extra_length, ZIP_EXTRA_MTIME_SIZE);
>   	copy_le16(dirent.comment_length, 0);
>   	copy_le16(dirent.disk, 0);
> -	copy_le16(dirent.attr1, 0);
>   	copy_le32(dirent.attr2, attr2);
>   	copy_le32(dirent.offset, zip_offset);
>
> @@ -328,6 +337,8 @@ static int write_zip_entry(struct archiver_args *args,
>   			if (readlen <= 0)
>   				break;
>   			crc = crc32(crc, buf, readlen);
> +			if (is_binary < 0)
> +				is_binary = buffer_is_binary(buffer, size);

buffer is NULL here, so this crashes.  buf and readlen have to be used 
instead.  This code path is only used for entries that are too big to be 
compressed in one go.

>   			write_or_die(1, buf, readlen);
>   		}
>   		close_istream(stream);
> @@ -361,6 +372,8 @@ static int write_zip_entry(struct archiver_args *args,
>   			if (readlen <= 0)
>   				break;
>   			crc = crc32(crc, buf, readlen);
> +			if (is_binary < 0)
> +				is_binary = buffer_is_binary(buffer, size);

Same here.

>   			zstream.next_in = buf;
>   			zstream.avail_in = readlen;
> @@ -405,6 +418,8 @@ static int write_zip_entry(struct archiver_args *args,
>   	free(deflated);
>   	free(buffer);
>
> +	copy_le16(dirent.attr1, !is_binary);
> +
>   	memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
>   	zip_dir_offset += ZIP_DIR_HEADER_SIZE;
>   	memcpy(zip_dir + zip_dir_offset, path, pathlen);
> @@ -466,7 +481,7 @@ static int write_zip_archive(const struct archiver *ar,
>   static struct archiver zip_archiver = {
>   	"zip",
>   	write_zip_archive,
> -	ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE
> +	ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE|ARCHIVER_TEXT_ATTRIBUTE
>   };
>
>   void init_zip_archiver(void)