Skip to content

Commit f1e0382

Browse files
cushoncopybara-github
authored andcommitted
Automated Code Change
PiperOrigin-RevId: 857854961 Change-Id: Id2d343738e8be18a94fa1103658dbe7f3a968504
1 parent e2320f5 commit f1e0382

4 files changed

Lines changed: 57 additions & 24 deletions

File tree

src/tools/java/com/google/devtools/build/android/AarGeneratorAction.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
import java.nio.file.SimpleFileVisitor;
4141
import java.nio.file.attribute.BasicFileAttributes;
4242
import java.nio.file.attribute.FileTime;
43-
import java.time.Instant;
44-
import java.time.LocalDateTime;
4543
import java.time.ZoneId;
4644
import java.util.ArrayList;
4745
import java.util.Collection;
@@ -69,9 +67,6 @@
6967
* </pre>
7068
*/
7169
public class AarGeneratorAction {
72-
public static final Instant DEFAULT_TIMESTAMP =
73-
LocalDateTime.of(2010, 1, 1, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant();
74-
7570
private static final Logger logger = Logger.getLogger(AarGeneratorAction.class.getName());
7671

7772
/** Flag specifications for this action. */
@@ -217,7 +212,7 @@ static void writeAar(
217212
try (final ZipOutputStream zipOut =
218213
new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(aar)))) {
219214
ZipEntry manifestEntry = new ZipEntry("AndroidManifest.xml");
220-
manifestEntry.setTime(DEFAULT_TIMESTAMP.toEpochMilli());
215+
manifestEntry.setTimeLocal(JarTime.DEFAULT_TIMESTAMP);
221216
zipOut.putNextEntry(manifestEntry);
222217
zipOut.write(Files.readAllBytes(manifest));
223218
zipOut.closeEntry();
@@ -230,14 +225,14 @@ static void writeAar(
230225

231226
ZipEntry aarMetadataEntry =
232227
new ZipEntry("META-INF/com/android/build/gradle/aar-metadata.properties");
233-
aarMetadataEntry.setTime(DEFAULT_TIMESTAMP.toEpochMilli());
228+
aarMetadataEntry.setTimeLocal(JarTime.DEFAULT_TIMESTAMP);
234229
zipOut.putNextEntry(aarMetadataEntry);
235230
zipOut.write(Files.readAllBytes(aarMetadata));
236231
zipOut.closeEntry();
237232
}
238233

239234
ZipEntry classJar = new ZipEntry("classes.jar");
240-
classJar.setTime(DEFAULT_TIMESTAMP.toEpochMilli());
235+
classJar.setTimeLocal(JarTime.DEFAULT_TIMESTAMP);
241236
zipOut.putNextEntry(classJar);
242237
zipOut.write(Files.readAllBytes(classes));
243238
zipOut.closeEntry();
@@ -247,14 +242,14 @@ static void writeAar(
247242
resWriter.writeEntries();
248243

249244
ZipEntry r = new ZipEntry("R.txt");
250-
r.setTime(DEFAULT_TIMESTAMP.toEpochMilli());
245+
r.setTimeLocal(JarTime.DEFAULT_TIMESTAMP);
251246
zipOut.putNextEntry(r);
252247
zipOut.write(Files.readAllBytes(rtxt));
253248
zipOut.closeEntry();
254249

255250
if (!proguardSpecs.isEmpty()) {
256251
ZipEntry proguardTxt = new ZipEntry("proguard.txt");
257-
proguardTxt.setTime(DEFAULT_TIMESTAMP.toEpochMilli());
252+
proguardTxt.setTimeLocal(JarTime.DEFAULT_TIMESTAMP);
258253
zipOut.putNextEntry(proguardTxt);
259254
for (Path proguardSpec : proguardSpecs) {
260255
zipOut.write(Files.readAllBytes(proguardSpec));
@@ -270,7 +265,8 @@ static void writeAar(
270265
assetWriter.writeEntries();
271266
}
272267
}
273-
Files.setLastModifiedTime(aar, FileTime.from(DEFAULT_TIMESTAMP));
268+
Files.setLastModifiedTime(
269+
aar, FileTime.from(JarTime.DEFAULT_TIMESTAMP.atZone(ZoneId.systemDefault()).toInstant()));
274270
}
275271

276272
private static class ZipDirectoryWriter extends SimpleFileVisitor<Path> {
@@ -310,7 +306,7 @@ void writeEntries() throws IOException {
310306

311307
private void writeFileEntry(Path file) throws IOException {
312308
ZipEntry entry = new ZipEntry(new File(dirName, root.relativize(file).toString()).toString());
313-
entry.setTime(DEFAULT_TIMESTAMP.toEpochMilli());
309+
entry.setTimeLocal(JarTime.DEFAULT_TIMESTAMP);
314310
zipOut.putNextEntry(entry);
315311
zipOut.write(Files.readAllBytes(file));
316312
zipOut.closeEntry();
@@ -319,7 +315,7 @@ private void writeFileEntry(Path file) throws IOException {
319315
private void writeDirectoryEntry(Path dir) throws IOException {
320316
ZipEntry entry =
321317
new ZipEntry(new File(dirName, root.relativize(dir).toString()).toString() + "/");
322-
entry.setTime(DEFAULT_TIMESTAMP.toEpochMilli());
318+
entry.setTimeLocal(JarTime.DEFAULT_TIMESTAMP);
323319
zipOut.putNextEntry(entry);
324320
zipOut.closeEntry();
325321
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2015 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.devtools.build.android;
16+
17+
import java.time.LocalDateTime;
18+
19+
/** Jar timestamp normalization. */
20+
public final class JarTime {
21+
/**
22+
* Normalize timestamps to 2010-1-1.
23+
*
24+
* <p>The ZIP format uses MS-DOS timestamps (see <a
25+
* href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">APPNOTE.TXT</a>) which use
26+
* 1980-1-1 as the epoch. To work around this, {@link ZipEntry} uses portability-reducing ZIP
27+
* extensions to store pre-1980 timestamps, which can occasionally <a
28+
* href="https://bugs.openjdk.java.net/browse/JDK-8246129>cause</a> <a
29+
* href="https://openjdk.markmail.org/thread/wzw7zfilk5j7uzqk>issues</a>. For that reason, using a
30+
* fixed post-1980 timestamp is preferred. At Google, the timestamp of 2010-1-1 is used by
31+
* convention in deterministic jar archives.
32+
*/
33+
public static final LocalDateTime DEFAULT_TIMESTAMP = LocalDateTime.of(2010, 1, 1, 0, 0, 0);
34+
35+
private JarTime() {}
36+
}

src/tools/java/com/google/devtools/build/android/idlclass/IdlClass.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.common.collect.Sets;
2525
import com.google.common.hash.Hashing;
2626
import com.google.devtools.build.android.AndroidOptionsUtils;
27+
import com.google.devtools.build.android.JarTime;
2728
import com.google.devtools.build.buildjar.proto.JavaCompilation.CompilationUnit;
2829
import com.google.devtools.build.buildjar.proto.JavaCompilation.Manifest;
2930
import java.io.BufferedOutputStream;
@@ -33,8 +34,6 @@
3334
import java.nio.file.Path;
3435
import java.nio.file.Paths;
3536
import java.nio.file.attribute.FileTime;
36-
import java.time.Instant;
37-
import java.time.LocalDateTime;
3837
import java.time.ZoneId;
3938
import java.util.ArrayList;
4039
import java.util.Enumeration;
@@ -52,10 +51,6 @@
5251
* from idl processing.
5352
*/
5453
public class IdlClass {
55-
// Cribbed from AarGeneratorAction.java
56-
private static final Instant DEFAULT_TIMESTAMP =
57-
LocalDateTime.of(2010, 1, 1, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant();
58-
5954
public static void main(String[] args) throws IOException {
6055
IdlClassOptions idlClassOptions = new IdlClassOptions();
6156
String[] preprocessedArgs = AndroidOptionsUtils.runArgFilePreprocessor(args);
@@ -187,7 +182,7 @@ private static void writeOutputJar(Path outputJar, Path tempDir) throws IOExcept
187182

188183
for (Path path : files) {
189184
ZipEntry entry = new ZipEntry(tempDir.relativize(path).toString());
190-
entry.setTime(DEFAULT_TIMESTAMP.toEpochMilli());
185+
entry.setTimeLocal(JarTime.DEFAULT_TIMESTAMP);
191186
entry.setSize(Files.size(path));
192187
entry.setCompressedSize(Files.size(path));
193188
entry.setCrc(Hashing.crc32().hashBytes(Files.readAllBytes(path)).padToLong());
@@ -199,6 +194,8 @@ private static void writeOutputJar(Path outputJar, Path tempDir) throws IOExcept
199194
} catch (IOException e) {
200195
throw new IOException("Failed to write output jar: " + outputJar, e);
201196
}
202-
Files.setLastModifiedTime(outputJar, FileTime.from(DEFAULT_TIMESTAMP));
197+
Files.setLastModifiedTime(
198+
outputJar,
199+
FileTime.from(JarTime.DEFAULT_TIMESTAMP.atZone(ZoneId.systemDefault()).toInstant()));
203200
}
204201
}

src/tools/javatests/com/google/devtools/build/android/AarGeneratorActionTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.nio.file.Files;
3636
import java.nio.file.Path;
3737
import java.nio.file.attribute.FileTime;
38+
import java.time.ZoneId;
3839
import java.util.HashMap;
3940
import java.util.HashSet;
4041
import java.util.List;
@@ -254,7 +255,9 @@ private void writeClassesJar() throws IOException {
254255

255256
zout.close();
256257

257-
Files.setLastModifiedTime(classes, FileTime.from(AarGeneratorAction.DEFAULT_TIMESTAMP));
258+
Files.setLastModifiedTime(
259+
classes,
260+
FileTime.from(JarTime.DEFAULT_TIMESTAMP.atZone(ZoneId.systemDefault()).toInstant()));
258261
}
259262
}
260263

@@ -569,9 +572,10 @@ public void testExistentAarMetadataPath() throws Exception {
569572
aarData.proguardSpecs);
570573

571574
assertThat(getZipEntryTimestamps(aar))
572-
.containsExactly(AarGeneratorAction.DEFAULT_TIMESTAMP.toEpochMilli());
575+
.containsExactly(
576+
JarTime.DEFAULT_TIMESTAMP.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
573577
assertThat(Files.getLastModifiedTime(aar).toInstant())
574-
.isEqualTo(AarGeneratorAction.DEFAULT_TIMESTAMP);
578+
.isEqualTo(JarTime.DEFAULT_TIMESTAMP.atZone(ZoneId.systemDefault()).toInstant());
575579
}
576580

577581
@Test public void testAssetResourceSubdirs() throws Exception {

0 commit comments

Comments
 (0)