Skip to content

Commit 97041a7

Browse files
committed
Move vendored code to its own module and add shims
1 parent 73b533b commit 97041a7

33 files changed

+775
-747
lines changed

build.mill

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,11 @@ trait OsModule extends OsLibModule { outer =>
166166
}
167167
}
168168

169-
object os extends Module with ApacheAntZipVendor {
169+
object os extends Module {
170170

171171
object jvm extends Cross[OsJvmModule](scalaVersions)
172172
trait OsJvmModule extends OsModule with MiMaChecks {
173+
def moduleDeps = super.moduleDeps ++ Seq(os.zip.jvm())
173174

174175
object test extends ScalaTests with OsLibTestModule {
175176
override def ivyDeps = T { super.ivyDeps() ++ Agg(Deps.expecty) }
@@ -196,6 +197,48 @@ object os extends Module with ApacheAntZipVendor {
196197
object nohometest extends ScalaTests with OsLibTestModule
197198
}
198199

200+
object zip extends Module {
201+
object jvm extends Cross[OsZipJvmModule](scalaVersions)
202+
trait OsZipJvmModule extends OsLibModule
203+
204+
val pkg = "os.shaded_org_apache_tools_zip"
205+
val zipSrc = millSourcePath / "src" / "os/shaded_org_apache_tools_zip"
206+
207+
def apacheAntZipOriginalSource: T[PathRef] = Task(persistent = true) {
208+
if (!_root_.os.exists(Task.dest / "unzipped")) {
209+
val antVersion = Deps.ant.version
210+
_root_.os.unzip.stream(
211+
requests.get.stream(
212+
s"https://repo1.maven.org/maven2/org/apache/ant/ant/$antVersion/ant-$antVersion-sources.jar"
213+
),
214+
Task.dest / "unzipped"
215+
)
216+
}
217+
218+
PathRef(Task.dest / "unzipped" / "org/apache/tools/zip")
219+
}
220+
221+
def apacheAntZipSource: T[PathRef] = Task {
222+
_root_.os.walk.stream(zipSrc)
223+
.filter(_.ext == "java")
224+
.filter(_.last != "PermissionUtils.java")
225+
.foreach(_root_.os.remove(_))
226+
227+
// Move from "package org.apache.tools.zip" to "package os.shaded_org_apache_tools_zip"
228+
// Make all classes package private (private [os]) by removing any `public` access modifier
229+
_root_.os.walk.stream(apacheAntZipOriginalSource().path)
230+
.filter(_.ext == "java")
231+
.foreach { p =>
232+
val content = _root_.os.read(p)
233+
.replaceAll("org.apache.tools.zip", pkg)
234+
.replaceAll("(?m)^public ", "")
235+
_root_.os.write(zipSrc / p.last, content)
236+
}
237+
238+
PathRef(zipSrc)
239+
}
240+
}
241+
199242
/*object native extends Cross[OsNativeModule](scalaVersions)
200243
trait OsNativeModule extends OsModule with ScalaNativeModule {
201244
def scalaNativeVersion = "0.5.2"
@@ -218,45 +261,3 @@ object os extends Module with ApacheAntZipVendor {
218261
}
219262
}
220263
}
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-
}

os/src/ZipOps.scala

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package os
22

3+
import os.shaded_org_apache_tools_zip.{shim => apache}
4+
35
import java.net.URI
46
import java.nio.file.{FileSystem, FileSystems, Files}
57
import java.nio.file.attribute.{BasicFileAttributeView, FileTime, PosixFilePermissions}
@@ -115,7 +117,7 @@ object zip {
115117
compressionLevel: Int,
116118
out: java.io.OutputStream
117119
): Unit = {
118-
val zipOut = new _ApacheZipOutputStream(out)
120+
val zipOut = new apache.ZipOutputStream(out)
119121
zipOut.setLevel(compressionLevel)
120122

121123
try {
@@ -144,25 +146,21 @@ object zip {
144146
!isExcluded && isIncluded
145147
}
146148

147-
private def toFileType(file: os.Path): PermissionUtils.FileType = {
148-
if (os.isLink(file)) PermissionUtils.FileType.SYMLINK
149-
else if (os.isFile(file)) PermissionUtils.FileType.REGULAR_FILE
150-
else if (os.isDir(file)) PermissionUtils.FileType.DIR
151-
else PermissionUtils.FileType.OTHER
152-
}
153-
154149
private def makeZipEntry(
155150
file: os.Path,
156151
sub: os.SubPath,
157152
preserveMtimes: Boolean,
158-
zipOut: _ApacheZipOutputStream
153+
zipOut: apache.ZipOutputStream
159154
) = {
160-
val zipEntry = new _ApacheZipEntry(sub.toString)
155+
val zipEntry = new apache.ZipEntry(sub.toString)
161156

162157
val mtime = if (preserveMtimes) os.mtime(file) else 0
163158
zipEntry.setTime(mtime)
164159

165-
val mode = PermissionUtils.modeFromPermissions(os.perms(file).toSet(), toFileType(file))
160+
val mode = apache.PermissionUtils.modeFromPermissions(
161+
os.perms(file).toSet(),
162+
apache.PermissionUtils.FileType.of(file.toNIO)
163+
)
166164
zipEntry.setUnixMode(mode)
167165

168166
val fis =
@@ -247,7 +245,7 @@ object unzip {
247245

248246
private lazy val S_IFMT: Int = java.lang.Integer.parseInt("0170000", 8)
249247
private def isSymLink(mode: Int): Boolean =
250-
(mode & S_IFMT) == _ApacheUnixStat.LINK_FLAG
248+
(mode & S_IFMT) == apache.UnixStat.LINK_FLAG
251249

252250
/**
253251
* Extract the given zip file into the destination directory
@@ -264,7 +262,7 @@ object unzip {
264262
): os.Path = {
265263
checker.value.onWrite(dest)
266264

267-
val zipFile = new _ApacheZipFile(source.toIO)
265+
val zipFile = new apache.ZipFile(source.toIO)
268266
val zipEntryInputStreams = zipFile.getEntries.asScala
269267
.filter(ze => os.zip.shouldInclude(ze.getName, excludePatterns, includePatterns))
270268
.map(ze => (ze, zipFile.getInputStream(ze)))
@@ -274,7 +272,7 @@ object unzip {
274272
val newFile = dest / os.SubPath(zipEntry.getName)
275273
val mode = zipEntry.getUnixMode
276274
val perms = if (mode > 0) {
277-
os.PermSet.fromSet(PermissionUtils.permissionsFromMode(mode))
275+
os.PermSet.fromSet(apache.PermissionUtils.permissionsFromMode(mode))
278276
} else null
279277

280278
if (zipEntry.isDirectory) {

os/src/zip/_ApacheAbstractUnicodeExtraField.java renamed to os/zip/src/os/shaded_org_apache_tools_zip/AbstractUnicodeExtraField.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
package os;
19+
package os.shaded_org_apache_tools_zip;
2020

2121
import java.nio.charset.StandardCharsets;
2222
import java.util.zip.CRC32;
@@ -25,12 +25,12 @@
2525
/**
2626
* A common base class for Unicode extra information extra fields.
2727
*/
28-
public abstract class _ApacheAbstractUnicodeExtraField implements _ApacheZipExtraField {
28+
abstract class AbstractUnicodeExtraField implements ZipExtraField {
2929
private long nameCRC32;
3030
private byte[] unicodeName;
3131
private byte[] data;
3232

33-
protected _ApacheAbstractUnicodeExtraField() {
33+
protected AbstractUnicodeExtraField() {
3434
}
3535

3636
/**
@@ -45,7 +45,7 @@ protected _ApacheAbstractUnicodeExtraField() {
4545
* @param len The length of the encoded filename or comment in
4646
* <code>bytes</code>.
4747
*/
48-
protected _ApacheAbstractUnicodeExtraField(final String text, final byte[] bytes, final int off,
48+
protected AbstractUnicodeExtraField(final String text, final byte[] bytes, final int off,
4949
final int len) {
5050
final CRC32 crc32 = new CRC32();
5151
crc32.update(bytes, off, len);
@@ -62,7 +62,7 @@ protected _ApacheAbstractUnicodeExtraField(final String text, final byte[] bytes
6262
* @param bytes The encoded of the filename or comment in the zip
6363
* file.
6464
*/
65-
protected _ApacheAbstractUnicodeExtraField(final String text, final byte[] bytes) {
65+
protected AbstractUnicodeExtraField(final String text, final byte[] bytes) {
6666

6767
this(text, bytes, 0, bytes.length);
6868
}
@@ -75,7 +75,7 @@ private void assembleData() {
7575
data = new byte[5 + unicodeName.length];
7676
// version 1
7777
data[0] = 0x01;
78-
System.arraycopy(_ApacheZipLong.getBytes(nameCRC32), 0, data, 1, 4);
78+
System.arraycopy(ZipLong.getBytes(nameCRC32), 0, data, 1, 4);
7979
System.arraycopy(unicodeName, 0, data, 5, unicodeName.length);
8080
}
8181

@@ -136,11 +136,11 @@ public byte[] getCentralDirectoryData() {
136136
}
137137

138138
/** {@inheritDoc} */
139-
public _ApacheZipShort getCentralDirectoryLength() {
139+
public ZipShort getCentralDirectoryLength() {
140140
if (data == null) {
141141
assembleData();
142142
}
143-
return new _ApacheZipShort(data.length);
143+
return new ZipShort(data.length);
144144
}
145145

146146
/** {@inheritDoc} */
@@ -149,7 +149,7 @@ public byte[] getLocalFileDataData() {
149149
}
150150

151151
/** {@inheritDoc} */
152-
public _ApacheZipShort getLocalFileDataLength() {
152+
public ZipShort getLocalFileDataLength() {
153153
return getCentralDirectoryLength();
154154
}
155155

@@ -169,7 +169,7 @@ public void parseFromLocalFileData(final byte[] buffer, final int offset, final
169169
+ "] for UniCode path extra data.");
170170
}
171171

172-
nameCRC32 = _ApacheZipLong.getValue(buffer, offset + 1);
172+
nameCRC32 = ZipLong.getValue(buffer, offset + 1);
173173
unicodeName = new byte[length - 5];
174174
System.arraycopy(buffer, offset + 5, unicodeName, 0, length - 5);
175175
data = null;

os/src/zip/_ApacheAsiExtraField.java renamed to os/zip/src/os/shaded_org_apache_tools_zip/AsiExtraField.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
package os;
19+
package os.shaded_org_apache_tools_zip;
2020

2121
import java.util.zip.CRC32;
2222
import java.util.zip.ZipException;
@@ -49,9 +49,9 @@
4949
* the character encoding of the file name at all, it is assumed that
5050
* it uses the current platform's default encoding.</p>
5151
*/
52-
public class _ApacheAsiExtraField implements _ApacheZipExtraField, _ApacheUnixStat, Cloneable {
52+
class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
5353

54-
private static final _ApacheZipShort HEADER_ID = new _ApacheZipShort(0x756E);
54+
private static final ZipShort HEADER_ID = new ZipShort(0x756E);
5555
private static final int WORD = 4;
5656
/**
5757
* Standard Unix stat(2) file mode.
@@ -93,27 +93,27 @@ public class _ApacheAsiExtraField implements _ApacheZipExtraField, _ApacheUnixSt
9393
*/
9494
private CRC32 crc = new CRC32();
9595

96-
/** Constructor for _ApacheAsiExtraField. */
97-
public _ApacheAsiExtraField() {
96+
/** Constructor for AsiExtraField. */
97+
public AsiExtraField() {
9898
}
9999

100100
/**
101101
* The Header-ID.
102102
* @return the value for the header id for this extrafield
103103
* @since 1.1
104104
*/
105-
public _ApacheZipShort getHeaderId() {
105+
public ZipShort getHeaderId() {
106106
return HEADER_ID;
107107
}
108108

109109
/**
110110
* Length of the extra field in the local file data - without
111111
* Header-ID or length specifier.
112-
* @return a <code>_ApacheZipShort</code> for the length of the data of this extra field
112+
* @return a <code>ZipShort</code> for the length of the data of this extra field
113113
* @since 1.1
114114
*/
115-
public _ApacheZipShort getLocalFileDataLength() {
116-
return new _ApacheZipShort(WORD // CRC
115+
public ZipShort getLocalFileDataLength() {
116+
return new ZipShort(WORD // CRC
117117
+ 2 // Mode
118118
+ WORD // SizDev
119119
+ 2 // UID
@@ -127,7 +127,7 @@ public _ApacheZipShort getLocalFileDataLength() {
127127
* @return the centralDirectory length
128128
* @since 1.1
129129
*/
130-
public _ApacheZipShort getCentralDirectoryLength() {
130+
public ZipShort getCentralDirectoryLength() {
131131
return getLocalFileDataLength();
132132
}
133133

@@ -140,16 +140,16 @@ public _ApacheZipShort getCentralDirectoryLength() {
140140
public byte[] getLocalFileDataData() {
141141
// CRC will be added later
142142
byte[] data = new byte[getLocalFileDataLength().getValue() - WORD];
143-
System.arraycopy(_ApacheZipShort.getBytes(getMode()), 0, data, 0, 2);
143+
System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
144144

145145
byte[] linkArray = getLinkedFile().getBytes(); // Uses default charset - see class Javadoc
146146
// CheckStyle:MagicNumber OFF
147-
System.arraycopy(_ApacheZipLong.getBytes(linkArray.length),
147+
System.arraycopy(ZipLong.getBytes(linkArray.length),
148148
0, data, 2, WORD);
149149

150-
System.arraycopy(_ApacheZipShort.getBytes(getUserId()),
150+
System.arraycopy(ZipShort.getBytes(getUserId()),
151151
0, data, 6, 2);
152-
System.arraycopy(_ApacheZipShort.getBytes(getGroupId()),
152+
System.arraycopy(ZipShort.getBytes(getGroupId()),
153153
0, data, 8, 2);
154154

155155
System.arraycopy(linkArray, 0, data, 10, linkArray.length);
@@ -160,7 +160,7 @@ public byte[] getLocalFileDataData() {
160160
long checksum = crc.getValue();
161161

162162
byte[] result = new byte[data.length + WORD];
163-
System.arraycopy(_ApacheZipLong.getBytes(checksum), 0, result, 0, WORD);
163+
System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD);
164164
System.arraycopy(data, 0, result, WORD, data.length);
165165
return result;
166166
}
@@ -292,7 +292,7 @@ public boolean isDirectory() {
292292
public void parseFromLocalFileData(byte[] data, int offset, int length)
293293
throws ZipException {
294294

295-
long givenChecksum = _ApacheZipLong.getValue(data, offset);
295+
long givenChecksum = ZipLong.getValue(data, offset);
296296
byte[] tmp = new byte[length - WORD];
297297
System.arraycopy(data, offset + WORD, tmp, 0, length - WORD);
298298
crc.reset();
@@ -305,15 +305,15 @@ public void parseFromLocalFileData(byte[] data, int offset, int length)
305305
+ Long.toHexString(realChecksum));
306306
}
307307

308-
int newMode = _ApacheZipShort.getValue(tmp, 0);
308+
int newMode = ZipShort.getValue(tmp, 0);
309309
// CheckStyle:MagicNumber OFF
310-
final int linkArrayLength = (int) _ApacheZipLong.getValue(tmp, 2);
310+
final int linkArrayLength = (int) ZipLong.getValue(tmp, 2);
311311
if (linkArrayLength < 0 || linkArrayLength > tmp.length - 10) {
312312
throw new ZipException("Bad symbolic link name length " + linkArrayLength
313313
+ " in ASI extra field");
314314
}
315-
uid = _ApacheZipShort.getValue(tmp, 6);
316-
gid = _ApacheZipShort.getValue(tmp, 8);
315+
uid = ZipShort.getValue(tmp, 6);
316+
gid = ZipShort.getValue(tmp, 8);
317317
if (linkArrayLength == 0) {
318318
link = "";
319319
} else {
@@ -345,7 +345,7 @@ protected int getMode(int mode) {
345345
@Override
346346
public Object clone() {
347347
try {
348-
_ApacheAsiExtraField cloned = (_ApacheAsiExtraField) super.clone();
348+
AsiExtraField cloned = (AsiExtraField) super.clone();
349349
cloned.crc = new CRC32();
350350
return cloned;
351351
} catch (CloneNotSupportedException cnfe) {

0 commit comments

Comments
 (0)