Skip to content

Commit

Permalink
Use a different date time when normalizing zip entries
Browse files Browse the repository at this point in the history
See also 584f9df.

PiperOrigin-RevId: 212574463
  • Loading branch information
cushon authored and Copybara-Service committed Sep 12, 2018
1 parent d22eddc commit 475f40a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/tools/singlejar/combiners.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void *Concatenator::OutputEntry(bool compress) {
lh->signature();
lh->version(20);
lh->bit_flag(0x0);
lh->last_mod_file_time(1); // 00:00:01
lh->last_mod_file_date(33); // 1980-01-01
lh->last_mod_file_time(1); // 00:00:01
lh->last_mod_file_date(30 << 9 | 1 << 5 | 1); // 2010-01-01
lh->crc32(0x12345678);
lh->compressed_file_size32(0);
lh->file_name(filename_.c_str(), filename_.size());
Expand Down
17 changes: 10 additions & 7 deletions src/tools/singlejar/output_jar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ bool OutputJar::Open() {
return true;
}

// January 1, 2010 as a DOS date
static const uint16_t kDefaultDate = 30 << 9 | 1 << 5 | 1;

bool OutputJar::AddJar(int jar_path_index) {
const std::string &input_jar_path =
options_->input_jars[jar_path_index].first;
Expand Down Expand Up @@ -450,7 +453,7 @@ bool OutputJar::AddJar(int jar_path_index) {
off64_t local_header_offset = Position();

// When normalize_timestamps is set, entry's timestamp is to be set to
// 01/01/1980 00:00:00 (or to 01/01/1980 00:00:02, if an entry is a .class
// 01/01/2010 00:00:00 (or to 01/01/2010 00:00:02, if an entry is a .class
// file). This is somewhat expensive because we have to copy the local
// header to memory as input jar is memory mapped as read-only. Try to copy
// as little as possible.
Expand All @@ -462,7 +465,7 @@ bool OutputJar::AddJar(int jar_path_index) {
normalized_time = 1;
}
lh_field_to_remove = lh->unix_time_extra_field();
fix_timestamp = jar_entry->last_mod_file_date() != 33 ||
fix_timestamp = jar_entry->last_mod_file_date() != kDefaultDate ||
jar_entry->last_mod_file_time() != normalized_time ||
lh_field_to_remove != nullptr;
}
Expand All @@ -489,7 +492,7 @@ bool OutputJar::AddJar(int jar_path_index) {
} else {
memcpy(lh_new, lh, lh_size);
}
lh_new->last_mod_file_date(33);
lh_new->last_mod_file_date(kDefaultDate);
lh_new->last_mod_file_time(normalized_time);
// Now write these few bytes and adjust read/write positions accordingly.
if (!WriteBytes(lh_new, lh_new->size())) {
Expand Down Expand Up @@ -549,9 +552,9 @@ void OutputJar::WriteEntry(void *buffer) {
// https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
// ("32-Bit Windows Time/Date Formats")
if (options_->normalize_timestamps) {
// Regular "normalized" timestamp is 01/01/1980 00:00:00, while for the
// .class file it is 01/01/1980 00:00:02
entry->last_mod_file_date(33);
// Regular "normalized" timestamp is 01/01/2010 00:00:00, while for the
// .class file it is 01/01/2010 00:00:02
entry->last_mod_file_date(kDefaultDate);
entry->last_mod_file_time(
ends_with(entry->file_name(), entry->file_name_length(), ".class") ? 1
: 0);
Expand Down Expand Up @@ -757,7 +760,7 @@ void OutputJar::AppendToDirectoryBuffer(const CDH *cdh, off64_t lh_pos,
out_cdh->local_header_offset32(lh_pos_needs64 ? 0xFFFFFFFF : lh_pos);
if (fix_timestamp) {
out_cdh->last_mod_file_time(normalized_time);
out_cdh->last_mod_file_date(33);
out_cdh->last_mod_file_date(kDefaultDate);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/tools/singlejar/output_jar_simple_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ TEST_F(OutputJarSimpleTest, Normalize) {
"--classpath_resources", cp_resource_path});

// Scan all entries, verify that *.class entries have timestamp
// 01/01/1980 00:00:02 and the rest have the timestamp of 01/01/1980 00:00:00.
// 01/01/2010 00:00:02 and the rest have the timestamp of 01/01/2010 00:00:00.
InputJar input_jar;
ASSERT_TRUE(input_jar.Open(out_path));
const LH *lh;
Expand All @@ -515,8 +515,8 @@ TEST_F(OutputJarSimpleTest, Normalize) {
<< entry_name << " modification date";
EXPECT_EQ(lh->last_mod_file_time(), cdh->last_mod_file_time())
<< entry_name << " modification time";
EXPECT_EQ(33, cdh->last_mod_file_date())
<< entry_name << " modification date should be 01/01/1980";
EXPECT_EQ(15393, cdh->last_mod_file_date())
<< entry_name << " modification date should be 01/01/2010";
auto n = entry_name.size() - strlen(".class");
if (0 == strcmp(entry_name.c_str() + n, ".class")) {
EXPECT_EQ(1, cdh->last_mod_file_time())
Expand Down

0 comments on commit 475f40a

Please sign in to comment.