Skip to content

Commit e05e8cf

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 75b2f01 commit e05e8cf

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

archive-zip.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ static unsigned int max_creator_version;
2424
#define ZIP_STREAM (1 << 3)
2525
#define ZIP_UTF8 (1 << 11)
2626

27+
enum zip_method {
28+
ZIP_METHOD_STORE = 0,
29+
ZIP_METHOD_DEFLATE = 8
30+
};
31+
2732
struct zip_local_header {
2833
unsigned char magic[4];
2934
unsigned char version[2];
@@ -291,7 +296,7 @@ static int write_zip_entry(struct archiver_args *args,
291296
unsigned long attr2;
292297
unsigned long compressed_size;
293298
unsigned long crc;
294-
int method;
299+
enum zip_method method;
295300
unsigned char *out;
296301
void *deflated = NULL;
297302
void *buffer;
@@ -320,7 +325,7 @@ static int write_zip_entry(struct archiver_args *args,
320325
}
321326

322327
if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
323-
method = 0;
328+
method = ZIP_METHOD_STORE;
324329
attr2 = 16;
325330
out = NULL;
326331
size = 0;
@@ -330,13 +335,13 @@ static int write_zip_entry(struct archiver_args *args,
330335
enum object_type type = oid_object_info(args->repo, oid,
331336
&size);
332337

333-
method = 0;
338+
method = ZIP_METHOD_STORE;
334339
attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
335340
(mode & 0111) ? ((mode) << 16) : 0;
336341
if (S_ISLNK(mode) || (mode & 0111))
337342
creator_version = 0x0317;
338343
if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
339-
method = 8;
344+
method = ZIP_METHOD_DEFLATE;
340345

341346
if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
342347
size > big_file_threshold) {
@@ -358,7 +363,7 @@ static int write_zip_entry(struct archiver_args *args,
358363
buffer, size);
359364
out = buffer;
360365
}
361-
compressed_size = (method == 0) ? size : 0;
366+
compressed_size = (method == ZIP_METHOD_STORE) ? size : 0;
362367
} else {
363368
return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode,
364369
oid_to_hex(oid));
@@ -367,13 +372,13 @@ static int write_zip_entry(struct archiver_args *args,
367372
if (creator_version > max_creator_version)
368373
max_creator_version = creator_version;
369374

370-
if (buffer && method == 8) {
375+
if (buffer && method == ZIP_METHOD_DEFLATE) {
371376
out = deflated = zlib_deflate_raw(buffer, size,
372377
args->compression_level,
373378
&compressed_size);
374379
if (!out || compressed_size >= size) {
375380
out = buffer;
376-
method = 0;
381+
method = ZIP_METHOD_STORE;
377382
compressed_size = size;
378383
}
379384
}
@@ -420,7 +425,7 @@ static int write_zip_entry(struct archiver_args *args,
420425
zip_offset += ZIP64_EXTRA_SIZE;
421426
}
422427

423-
if (stream && method == 0) {
428+
if (stream && method == ZIP_METHOD_STORE) {
424429
unsigned char buf[STREAM_BUFFER_SIZE];
425430
ssize_t readlen;
426431

@@ -443,7 +448,7 @@ static int write_zip_entry(struct archiver_args *args,
443448
zip_offset += compressed_size;
444449

445450
write_zip_data_desc(size, compressed_size, crc);
446-
} else if (stream && method == 8) {
451+
} else if (stream && method == ZIP_METHOD_DEFLATE) {
447452
unsigned char buf[STREAM_BUFFER_SIZE];
448453
ssize_t readlen;
449454
git_zstream zstream;

0 commit comments

Comments
 (0)