Skip to content

Commit c70080d

Browse files
SapMachine #2218: Add method to check if a path in zipfs is a symbolic link
1 parent 7a0b213 commit c70080d

8 files changed

Lines changed: 192 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2026 SAP SE. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package jdk.internal.access;
27+
28+
import java.nio.file.Path;
29+
30+
/**
31+
* SharedSecrets interface used for the access from jdk.nio.zipfs
32+
*/
33+
34+
public interface JdkNioZipfsAccess {
35+
36+
public boolean isSymbolicLink(Path path);
37+
}

src/java.base/share/classes/jdk/internal/access/SharedSecrets.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public class SharedSecrets {
109109
@Stable private static JavaxCryptoSealedObjectAccess javaxCryptoSealedObjectAccess;
110110
@Stable private static JavaxCryptoSpecAccess javaxCryptoSpecAccess;
111111
@Stable private static JavaxSecurityAccess javaxSecurityAccess;
112+
// SapMachine 2026-04-14: Support for symlink detection in zipfs.
113+
@Stable private static JdkNioZipfsAccess jdkNioZipfsAccess;
112114

113115
public static void setJavaUtilCollectionAccess(JavaUtilCollectionAccess juca) {
114116
javaUtilCollectionAccess = juca;
@@ -537,4 +539,14 @@ private static void ensureClassInitialized(Class<?> c) {
537539
MethodHandles.lookup().ensureInitialized(c);
538540
} catch (IllegalAccessException e) {}
539541
}
542+
543+
// SapMachine 2026-04-14
544+
public static void setJdkNioZipfsAccess(JdkNioZipfsAccess access) {
545+
jdkNioZipfsAccess = access;
546+
}
547+
548+
// SapMachine 2026-04-14
549+
public static JdkNioZipfsAccess getJdkNioZipfsAccess() {
550+
return jdkNioZipfsAccess;
551+
}
540552
}

src/java.base/share/classes/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@
167167
jdk.net,
168168
// SapMachine 2024-06-12: process group extension
169169
jdk.sapext,
170+
// SapMachine 2026-04-13: symlink support for zipfs entries in sapext.
171+
jdk.zipfs,
170172
jdk.sctp,
171173
jdk.crypto.cryptoki;
172174
exports jdk.internal.classfile.components to
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2026 SAP SE. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.sap.jdk.ext.util;
26+
27+
import java.nio.file.Path;
28+
29+
import jdk.internal.access.JdkNioZipfsAccess;
30+
import jdk.internal.access.SharedSecrets;
31+
32+
public class ZipfsUtils {
33+
34+
// Silence warning about implicit ctor.
35+
private ZipfsUtils() {
36+
}
37+
38+
/**
39+
* Returns <code>true</code> if the given path is a <code>jdk.nio.zipfs.ZipPath</code> which
40+
* represents a symbolic link.
41+
*
42+
* @param path The path in the zipfs.
43+
* @return <code>true</code> if the path represents a symbolic link.
44+
*/
45+
public static boolean isSymbolicLink(Path path) {
46+
JdkNioZipfsAccess access = SharedSecrets.getJdkNioZipfsAccess();
47+
48+
if (access == null) {
49+
return false;
50+
}
51+
52+
return access.isSymbolicLink(path);
53+
}
54+
}

src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3649,4 +3649,17 @@ public boolean equals(Object other) {
36493649
oname, 0, oname.length);
36503650
}
36513651
}
3652+
3653+
// SapMachine 2026-04-14: Returns true if the resolved path points to a symnbolic link.
3654+
boolean isSymlink(byte[] path) {
3655+
IndexNode inode = getInode(path);
3656+
3657+
if ((inode != null) && (inode.pos != -1)) {
3658+
long attrEx = ZipConstants.CENATX(cen, inode.pos);
3659+
3660+
return (attrEx & 0xF0000000L) == 0xA0000000L;
3661+
}
3662+
3663+
return false;
3664+
}
36523665
}

src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ final class ZipPath implements Path {
6262
private volatile int[] offsets;
6363
private int hashcode = 0; // cached hashcode (created lazily)
6464

65+
// SapMachine 2026-04-14: Support for symlink detection.
66+
static class JdkNioZipfsAccessImpl implements jdk.internal.access.JdkNioZipfsAccess {
67+
public boolean isSymbolicLink(Path path) {
68+
if (!(path instanceof ZipPath)) {
69+
return false;
70+
}
71+
72+
ZipPath zipPath = (ZipPath) path;
73+
byte[] resolvedPath = zipPath.getResolvedPath();
74+
75+
return zipPath.zfs.isSymlink(resolvedPath);
76+
}
77+
}
78+
79+
// SapMachine 2026-04-14: Support for symlink detection.
80+
static {
81+
jdk.internal.access.SharedSecrets.setJdkNioZipfsAccess(new JdkNioZipfsAccessImpl());
82+
}
83+
6584
ZipPath(ZipFileSystem zfs, byte[] path) {
6685
this(zfs, path, false);
6786
}

test/jdk/sap/ZipfsUtilsTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2026 SAP SE. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
/**
26+
* @test
27+
* @summary Runs the test for com.sap.jdk.ext.util.ZipfsUtils.
28+
*
29+
* @run junit ZipfsUtilsTest
30+
*/
31+
32+
import java.io.File;
33+
import java.net.URI;
34+
import java.nio.file.FileSystem;
35+
import java.nio.file.FileSystems;
36+
import java.util.Map;
37+
38+
import com.sap.jdk.ext.util.ZipfsUtils;
39+
40+
import org.junit.jupiter.api.Test;
41+
42+
import static org.junit.jupiter.api.Assertions.assertFalse;
43+
import static org.junit.jupiter.api.Assertions.assertTrue;
44+
45+
public class ZipfsUtilsTest {
46+
47+
@Test
48+
public void basicTest() throws Exception {
49+
File file = new File(System.getProperty("test.src", "."), "zip-with-symlink.zip");
50+
URI uri = new URI("jar", file.toURI().toString(), null);
51+
FileSystem fs = FileSystems.newFileSystem(uri, Map.of());
52+
assertFalse(ZipfsUtils.isSymbolicLink(fs.getPath("file")));
53+
assertTrue(ZipfsUtils.isSymbolicLink(fs.getPath("symlink")));
54+
}
55+
}

test/jdk/sap/zip-with-symlink.zip

306 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)