archive-zip: use enum for compression method
Add an enumeration to assign names to the magic values that determine the ZIP compression method to use. Use those names to improve code readability. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Dec 7, 2019 at 13:20 UTC
e05e8cf074562dca433c3f984f9fc249fc73b056
1 file changed
+14
-9
archive-zip.c
+14
-9
@@ -24,6 +24,11 @@ static unsigned int max_creator_version;
24
#define ZIP_STREAM (1 << 3)
25
#define ZIP_UTF8 (1 << 11)
26
27
+enum zip_method {
28
+ ZIP_METHOD_STORE = 0,
29
+ ZIP_METHOD_DEFLATE = 8
30
+};
31
+
32
struct zip_local_header {
33
unsigned char magic[4];
34
unsigned char version[2];
@@ -291,7 +296,7 @@ static int write_zip_entry(struct archiver_args *args,
296
unsigned long attr2;
297
unsigned long compressed_size;
298
unsigned long crc;
294
- int method;
299
+ enum zip_method method;
300
unsigned char *out;
301
void *deflated = NULL;
302
void *buffer;
@@ -320,7 +325,7 @@ static int write_zip_entry(struct archiver_args *args,
325
}
326
327
if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
323
- method = 0;
328
+ method = ZIP_METHOD_STORE;
329
attr2 = 16;
330
out = NULL;
331
size = 0;
@@ -330,13 +335,13 @@ static int write_zip_entry(struct archiver_args *args,
335
enum object_type type = oid_object_info(args->repo, oid,
336
&size);
337
333
- method = 0;
338
+ method = ZIP_METHOD_STORE;
339
attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
340
(mode & 0111) ? ((mode) << 16) : 0;
341
if (S_ISLNK(mode) || (mode & 0111))
342
creator_version = 0x0317;
343
if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
339
- method = 8;
344
+ method = ZIP_METHOD_DEFLATE;
345
346
if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
347
size > big_file_threshold) {
@@ -358,7 +363,7 @@ static int write_zip_entry(struct archiver_args *args,
363
buffer, size);
364
out = buffer;
365
}
361
- compressed_size = (method == 0) ? size : 0;
366
+ compressed_size = (method == ZIP_METHOD_STORE) ? size : 0;
367
} else {
368
return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode,
369
oid_to_hex(oid));
@@ -367,13 +372,13 @@ static int write_zip_entry(struct archiver_args *args,
372
if (creator_version > max_creator_version)
373
max_creator_version = creator_version;
374
370
- if (buffer && method == 8) {
375
+ if (buffer && method == ZIP_METHOD_DEFLATE) {
376
out = deflated = zlib_deflate_raw(buffer, size,
377
args->compression_level,
378
&compressed_size);
379
if (!out || compressed_size >= size) {
380
out = buffer;
376
- method = 0;
381
+ method = ZIP_METHOD_STORE;
382
compressed_size = size;
383
}
384
}
@@ -420,7 +425,7 @@ static int write_zip_entry(struct archiver_args *args,
425
zip_offset += ZIP64_EXTRA_SIZE;
426
}
427
423
- if (stream && method == 0) {
428
+ if (stream && method == ZIP_METHOD_STORE) {
429
unsigned char buf[STREAM_BUFFER_SIZE];
430
ssize_t readlen;
431
@@ -443,7 +448,7 @@ static int write_zip_entry(struct archiver_args *args,
448
zip_offset += compressed_size;
449
450
write_zip_data_desc(size, compressed_size, crc);
446
- } else if (stream && method == 8) {
451
+ } else if (stream && method == ZIP_METHOD_DEFLATE) {
452
unsigned char buf[STREAM_BUFFER_SIZE];
453
ssize_t readlen;
454
git_zstream zstream;