Skip to content

Commit 4fd5a28

Browse files
committed
Check vendored Apache Ant zip source code to git
to help with IDE and code review
1 parent 4ddd8e9 commit 4fd5a28

30 files changed

+7748
-36
lines changed

build.mill

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ object Deps {
2323
val acyclic = ivy"com.lihaoyi:::acyclic:0.3.18"
2424
val jna = ivy"net.java.dev.jna:jna:5.15.0"
2525
val geny = ivy"com.lihaoyi::geny::1.1.1"
26-
// val compress = ivy"org.apache.ant:ant:1.10.15"
26+
val ant = ivy"org.apache.ant:ant:1.10.15"
2727
val sourcecode = ivy"com.lihaoyi::sourcecode::0.4.2"
2828
val utest = ivy"com.lihaoyi::utest::0.8.4"
2929
val expecty = ivy"com.eed3si9n.expecty::expecty::0.16.0"
@@ -114,7 +114,6 @@ trait OsLibModule
114114

115115
trait OsModule extends OsLibModule { outer =>
116116
def ivyDeps = Agg(Deps.geny)
117-
// def ivyDeps = Agg(Deps.geny, Deps.compress)
118117
override def compileIvyDeps = T {
119118
val scalaReflectOpt = Option.when(!ZincWorkerUtil.isDottyOrScala3(scalaVersion()))(
120119
Deps.scalaReflect(scalaVersion())
@@ -140,38 +139,6 @@ trait OsModule extends OsLibModule { outer =>
140139

141140
def scalaDocOptions = super.scalaDocOptions() ++ conditionalScalaDocOptions()
142141

143-
def apacheAntZipSources: T[PathRef] = Task(persistent = true) {
144-
if (!_root_.os.exists(Task.dest / "src")) {
145-
_root_.os.remove.all(Task.dest / "unzipped")
146-
_root_.os.unzip.stream(
147-
requests.get.stream("https://repo1.maven.org/maven2/org/apache/ant/ant/1.10.15/ant-1.10.15-sources.jar"),
148-
Task.dest / "unzipped"
149-
)
150-
_root_.os.makeDir.all(Task.dest / "src/os")
151-
152-
val classes = _root_.os.walk.stream(Task.dest / "unzipped" / "org/apache/tools/zip")
153-
.map(_.baseName)
154-
val classRegex = classes.mkString("|")
155-
val prefix = "_Apache"
156-
157-
// Add "_Apache" prefix to all classes
158-
// Move from "package org.apache.tools.zip" to "package os"
159-
// Make all classes package private (private [os])
160-
_root_.os.walk.stream(Task.dest / "unzipped" / "org/apache/tools/zip")
161-
.filter(_.ext == "java")
162-
.foreach { p =>
163-
val content = _root_.os.read(p)
164-
.replaceAll(s"(?<![_a-zA-Z\\.])(${classRegex})(?![_a-zA-Z0-9])", prefix + "$1")
165-
.replaceAll(s"(?<=org.apache.tools.zip.)($classRegex)(?![_a-zA-Z0-9])", prefix + "$1")
166-
.replaceAll("org.apache.tools.zip", "os")
167-
.replaceAll("^public ", "")
168-
_root_.os.write(Task.dest / "src/os" / s"$prefix${p.last}", content)
169-
}
170-
}
171-
172-
PathRef(Task.dest / "src")
173-
}
174-
175142
def generatedSources = T {
176143
val conversions = for (i <- Range.inclusive(2, 22)) yield {
177144
val ts = Range.inclusive(1, i).map(n => s"T$n").mkString(", ")
@@ -195,14 +162,15 @@ trait OsModule extends OsLibModule { outer =>
195162
|""".stripMargin,
196163
createFolders = true
197164
)
198-
Seq(PathRef(T.dest), apacheAntZipSources())
165+
Seq(PathRef(T.dest))
199166
}
200167
}
201168

202-
object os extends Module {
169+
object os extends Module with ApacheAntZipVendor {
203170

204171
object jvm extends Cross[OsJvmModule](scalaVersions)
205172
trait OsJvmModule extends OsModule with MiMaChecks {
173+
206174
object test extends ScalaTests with OsLibTestModule {
207175
override def ivyDeps = T { super.ivyDeps() ++ Agg(Deps.expecty) }
208176

@@ -250,3 +218,45 @@ object os extends Module {
250218
}
251219
}
252220
}
221+
222+
trait ApacheAntZipVendor extends Module {
223+
def apacheAntZipOriginalSource: T[PathRef] = Task(persistent = true) {
224+
if (!_root_.os.exists(Task.dest / "unzipped")) {
225+
val antVersion = Deps.ant.version
226+
_root_.os.unzip.stream(
227+
requests.get.stream(
228+
s"https://repo1.maven.org/maven2/org/apache/ant/ant/$antVersion/ant-$antVersion-sources.jar"
229+
),
230+
Task.dest / "unzipped"
231+
)
232+
}
233+
234+
PathRef(Task.dest / "unzipped" / "org/apache/tools/zip")
235+
}
236+
237+
def apacheAntZipSource: T[PathRef] = Task {
238+
val zipSrc = millSourcePath / "src/zip"
239+
_root_.os.remove.all(zipSrc)
240+
_root_.os.makeDir.all(zipSrc)
241+
242+
val classes = _root_.os.walk.stream(apacheAntZipOriginalSource().path)
243+
.map(_.baseName)
244+
val classRegex = classes.mkString("|")
245+
val prefix = "_Apache"
246+
247+
// Add "_Apache" prefix to all classes
248+
// Move from "package org.apache.tools.zip" to "package os"
249+
// Make all classes package private (private [os])
250+
_root_.os.walk.stream(apacheAntZipOriginalSource().path)
251+
.filter(_.ext == "java")
252+
.foreach { p =>
253+
val content = _root_.os.read(p)
254+
.replaceAll(s"(?<![_a-zA-Z\\.])($classRegex)(?![_a-zA-Z0-9])", prefix + "$1")
255+
.replaceAll(s"(?<=org.apache.tools.zip.)($classRegex)(?![_a-zA-Z0-9])", prefix + "$1")
256+
.replaceAll("org.apache.tools.zip", "os")
257+
.replaceAll("^public ", "")
258+
_root_.os.write(zipSrc / s"$prefix${p.last}", content)
259+
}
260+
PathRef(zipSrc)
261+
}
262+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package os;
20+
21+
import java.nio.charset.StandardCharsets;
22+
import java.util.zip.CRC32;
23+
import java.util.zip.ZipException;
24+
25+
/**
26+
* A common base class for Unicode extra information extra fields.
27+
*/
28+
public abstract class _ApacheAbstractUnicodeExtraField implements _ApacheZipExtraField {
29+
private long nameCRC32;
30+
private byte[] unicodeName;
31+
private byte[] data;
32+
33+
protected _ApacheAbstractUnicodeExtraField() {
34+
}
35+
36+
/**
37+
* Assemble as unicode extension from the name/comment and
38+
* encoding of the original zip entry.
39+
*
40+
* @param text The file name or comment.
41+
* @param bytes The encoded of the filename or comment in the zip
42+
* file.
43+
* @param off The offset of the encoded filename or comment in
44+
* <code>bytes</code>.
45+
* @param len The length of the encoded filename or comment in
46+
* <code>bytes</code>.
47+
*/
48+
protected _ApacheAbstractUnicodeExtraField(final String text, final byte[] bytes, final int off,
49+
final int len) {
50+
final CRC32 crc32 = new CRC32();
51+
crc32.update(bytes, off, len);
52+
nameCRC32 = crc32.getValue();
53+
54+
unicodeName = text.getBytes(StandardCharsets.UTF_8);
55+
}
56+
57+
/**
58+
* Assemble as unicode extension from the name/comment and
59+
* encoding of the original zip entry.
60+
*
61+
* @param text The file name or comment.
62+
* @param bytes The encoded of the filename or comment in the zip
63+
* file.
64+
*/
65+
protected _ApacheAbstractUnicodeExtraField(final String text, final byte[] bytes) {
66+
67+
this(text, bytes, 0, bytes.length);
68+
}
69+
70+
private void assembleData() {
71+
if (unicodeName == null) {
72+
return;
73+
}
74+
75+
data = new byte[5 + unicodeName.length];
76+
// version 1
77+
data[0] = 0x01;
78+
System.arraycopy(_ApacheZipLong.getBytes(nameCRC32), 0, data, 1, 4);
79+
System.arraycopy(unicodeName, 0, data, 5, unicodeName.length);
80+
}
81+
82+
/**
83+
* @return The CRC32 checksum of the filename or comment as
84+
* encoded in the central directory of the zip file.
85+
*/
86+
public long getNameCRC32() {
87+
return nameCRC32;
88+
}
89+
90+
/**
91+
* @param nameCRC32 The CRC32 checksum of the filename as encoded
92+
* in the central directory of the zip file to set.
93+
*/
94+
public void setNameCRC32(final long nameCRC32) {
95+
this.nameCRC32 = nameCRC32;
96+
data = null;
97+
}
98+
99+
/**
100+
* @return The utf-8 encoded name.
101+
*/
102+
public byte[] getUnicodeName() {
103+
byte[] b = null;
104+
if (unicodeName != null) {
105+
b = new byte[unicodeName.length];
106+
System.arraycopy(unicodeName, 0, b, 0, b.length);
107+
}
108+
return b;
109+
}
110+
111+
/**
112+
* @param unicodeName The utf-8 encoded name to set.
113+
*/
114+
public void setUnicodeName(final byte[] unicodeName) {
115+
if (unicodeName != null) {
116+
this.unicodeName = new byte[unicodeName.length];
117+
System.arraycopy(unicodeName, 0, this.unicodeName, 0,
118+
unicodeName.length);
119+
} else {
120+
this.unicodeName = null;
121+
}
122+
data = null;
123+
}
124+
125+
/** {@inheritDoc} */
126+
public byte[] getCentralDirectoryData() {
127+
if (data == null) {
128+
this.assembleData();
129+
}
130+
byte[] b = null;
131+
if (data != null) {
132+
b = new byte[data.length];
133+
System.arraycopy(data, 0, b, 0, b.length);
134+
}
135+
return b;
136+
}
137+
138+
/** {@inheritDoc} */
139+
public _ApacheZipShort getCentralDirectoryLength() {
140+
if (data == null) {
141+
assembleData();
142+
}
143+
return new _ApacheZipShort(data.length);
144+
}
145+
146+
/** {@inheritDoc} */
147+
public byte[] getLocalFileDataData() {
148+
return getCentralDirectoryData();
149+
}
150+
151+
/** {@inheritDoc} */
152+
public _ApacheZipShort getLocalFileDataLength() {
153+
return getCentralDirectoryLength();
154+
}
155+
156+
/** {@inheritDoc} */
157+
public void parseFromLocalFileData(final byte[] buffer, final int offset, final int length)
158+
throws ZipException {
159+
160+
if (length < 5) {
161+
throw new ZipException("UniCode path extra data must have at least"
162+
+ " 5 bytes.");
163+
}
164+
165+
final int version = buffer[offset];
166+
167+
if (version != 0x01) {
168+
throw new ZipException("Unsupported version [" + version
169+
+ "] for UniCode path extra data.");
170+
}
171+
172+
nameCRC32 = _ApacheZipLong.getValue(buffer, offset + 1);
173+
unicodeName = new byte[length - 5];
174+
System.arraycopy(buffer, offset + 5, unicodeName, 0, length - 5);
175+
data = null;
176+
}
177+
178+
}

0 commit comments

Comments
 (0)