Skip to content

Commit

Permalink
zipcmp: add -T for comparing time stamps
Browse files Browse the repository at this point in the history
  • Loading branch information
0-wiz-0 committed Mar 15, 2024
1 parent 5ef08f2 commit 1400a74
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 235 deletions.
8 changes: 5 additions & 3 deletions man/zipcmp.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions man/zipcmp.man
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" Automatically generated from an mdoc input file. Do not edit.
.\" zipcmp.mdoc -- compare zip archives
.\" Copyright (C) 2003-2022 Dieter Baron and Thomas Klausner
.\" Copyright (C) 2003-2024 Dieter Baron and Thomas Klausner
.\"
.\" This file is part of libzip, a library to manipulate ZIP archives.
.\" The authors can be contacted at <[email protected]>
Expand Down Expand Up @@ -30,7 +30,7 @@
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.TH "ZIPCMP" "1" "March 19, 2022" "NiH" "General Commands Manual"
.TH "ZIPCMP" "1" "March 15, 2024" "NiH" "General Commands Manual"
.nh
.if n .ad l
.SH "NAME"
Expand All @@ -39,7 +39,7 @@
.SH "SYNOPSIS"
.HP 7n
\fBzipcmp\fR
[\fB\-ChipqstVv\fR]
[\fB\-ChipqsTtVv\fR]
\fIarchive1\ archive2\fR
.SH "DESCRIPTION"
\fBzipcmp\fR
Expand Down Expand Up @@ -78,6 +78,9 @@ Compare
\fB\-s\fR
Print a summary of how many files where added and removed.
.TP 5n
\fB\-T\fR
Additionally compare the time stamps of the entries.
.TP 5n
\fB\-t\fR
Test zip files by comparing the contents to their checksums.
.TP 5n
Expand Down
8 changes: 5 additions & 3 deletions man/zipcmp.mdoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" zipcmp.mdoc -- compare zip archives
.\" Copyright (C) 2003-2022 Dieter Baron and Thomas Klausner
.\" Copyright (C) 2003-2024 Dieter Baron and Thomas Klausner
.\"
.\" This file is part of libzip, a library to manipulate ZIP archives.
.\" The authors can be contacted at <[email protected]>
Expand Down Expand Up @@ -29,15 +29,15 @@
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd March 19, 2022
.Dd March 15, 2024
.Dt ZIPCMP 1
.Os
.Sh NAME
.Nm zipcmp
.Nd compare contents of zip archives
.Sh SYNOPSIS
.Nm
.Op Fl ChipqstVv
.Op Fl ChipqsTtVv
.Ar archive1 archive2
.Sh DESCRIPTION
.Nm
Expand Down Expand Up @@ -70,6 +70,8 @@ Compare
.Fl v .
.It Fl s
Print a summary of how many files where added and removed.
.It Fl T
Additionally compare the time stamps of the entries.
.It Fl t
Test zip files by comparing the contents to their checksums.
.It Fl V
Expand Down
2 changes: 1 addition & 1 deletion regress/zipcmp_zip_dir.test
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ stdout
--- zipcmp_zip_dir.zip
+++ a
- directory '00-empty-dir/'
- file 'dir-with-file/a', size 1, crc e8b7be43
- file 'dir-with-file/a', size 1, crc e8b7be43, mtime 0
+ directory 'empty-dir-in-dir/'
- directory 'empty-dir/'
end-of-inline-data
41 changes: 25 additions & 16 deletions src/diff_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

#include "compat.h"

static void ensure_header(diff_output_t *output) {
static void
ensure_header(diff_output_t *output) {
if (output->archive_names[0] != NULL) {
printf("--- %s\n", output->archive_names[0]);
printf("+++ %s\n", output->archive_names[1]);
Expand All @@ -16,63 +17,71 @@ static void ensure_header(diff_output_t *output) {
}
}

void diff_output_init(diff_output_t *output, int verbose, char *const archive_names[]) {
void
diff_output_init(diff_output_t *output, int verbose, char *const archive_names[]) {
output->archive_names[0] = archive_names[0];
output->archive_names[1] = archive_names[1];
output->verbose = verbose;
output->file_name = NULL;
output->file_size = 0;
output->file_crc = 0;
output->file_mtime = 0;
}

void diff_output_start_file(diff_output_t *output, const char *name, zip_uint64_t size, zip_uint32_t crc) {
void
diff_output_start_file(diff_output_t *output, const char *name, zip_uint64_t size, zip_uint32_t crc, zip_uint64_t mtime) {
output->file_name = name;
output->file_size = size;
output->file_crc = crc;
output->file_mtime = mtime;
}

void diff_output_end_file(diff_output_t *output) {
void
diff_output_end_file(diff_output_t *output) {
output->file_name = NULL;
}

void diff_output(diff_output_t *output, int side, const char *fmt, ...) {
void
diff_output(diff_output_t *output, int side, const char *fmt, ...) {
va_list ap;

if (!output->verbose) {
return;
}

ensure_header(output);

if (output->file_name != NULL) {
diff_output_file(output, ' ', output->file_name, output->file_size, output->file_crc);
diff_output_file(output, ' ', output->file_name, output->file_size, output->file_crc, output->file_mtime);
output->file_name = NULL;
}

printf("%c ", side);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}

void diff_output_file(diff_output_t *output, char side, const char *name, zip_uint64_t size, zip_uint32_t crc) {
void
diff_output_file(diff_output_t *output, char side, const char *name, zip_uint64_t size, zip_uint32_t crc, zip_uint64_t mtime) {
if (!output->verbose) {
return;
}

ensure_header(output);

if (size == 0 && crc == 0 && name[0] != '\0' && name[strlen(name) - 1] == '/') {
printf("%c directory '%s'\n", side, name);
}
else {
printf("%c file '%s', size %" PRIu64 ", crc %08x\n", side, name, size, crc);
printf("%c file '%s', size %" PRIu64 ", crc %08x, mtime %" PRIu64 "\n", side, name, size, crc, mtime);
}
}

#define MAX_BYTES 64
void diff_output_data(diff_output_t *output, int side, const zip_uint8_t *data, zip_uint64_t data_length, const char *fmt, ...) {
void
diff_output_data(diff_output_t *output, int side, const zip_uint8_t *data, zip_uint64_t data_length, const char *fmt, ...) {
char prefix[1024];
char hexdata[MAX_BYTES * 3 + 6];
size_t i, offset;
Expand All @@ -81,7 +90,7 @@ void diff_output_data(diff_output_t *output, int side, const zip_uint8_t *data,
if (!output->verbose) {
return;
}

offset = 0;
for (i = 0; i < data_length; i++) {
hexdata[offset++] = (i == 0 ? '<' : ' ');
Expand All @@ -96,11 +105,11 @@ void diff_output_data(diff_output_t *output, int side, const zip_uint8_t *data,

hexdata[offset++] = '>';
hexdata[offset] = '\0';

va_start(ap, fmt);
vsnprintf(prefix, sizeof(prefix), fmt, ap);
va_end(ap);
prefix[sizeof(prefix) - 1] = '\0';

diff_output(output, side, "%s, length %" PRIu64 ", data %s", prefix, data_length, hexdata);
}
5 changes: 3 additions & 2 deletions src/diff_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef struct {
const char *file_name;
zip_uint64_t file_size;
zip_uint32_t file_crc;
zip_uint64_t file_mtime;
int verbose;
} diff_output_t;

Expand All @@ -18,11 +19,11 @@ typedef struct {
#endif

void diff_output_init(diff_output_t *output, int verbose, char *const archive_names[]);
void diff_output_start_file(diff_output_t *output, const char *name, zip_uint64_t size, zip_uint32_t crc);
void diff_output_start_file(diff_output_t *output, const char *name, zip_uint64_t size, zip_uint32_t crc, zip_uint64_t mtime);
void diff_output_end_file(diff_output_t *output);

void diff_output(diff_output_t *output, int side, const char *fmt, ...) PRINTF_LIKE(3, 4);
void diff_output_data(diff_output_t *output, int side, const zip_uint8_t *data, zip_uint64_t data_length, const char *fmt, ...) PRINTF_LIKE(5, 6);
void diff_output_file(diff_output_t *output, char side, const char *name, zip_uint64_t size, zip_uint32_t crc);
void diff_output_file(diff_output_t *output, char side, const char *name, zip_uint64_t size, zip_uint32_t crc, zip_uint64_t mtime);

#endif /* HAD_DIFF_OUTPUT_H */
Loading

0 comments on commit 1400a74

Please sign in to comment.