Skip to content

Commit 13596cd

Browse files
author
Brian Burkhalter
committed
8287097: (fs) Files::copy requires an undocumented permission when copying from the default file system to a non-default file system
Reviewed-by: chegar, alanb
1 parent 49e24f0 commit 13596cd

File tree

3 files changed

+82
-8
lines changed

3 files changed

+82
-8
lines changed

src/java.base/share/classes/java/nio/file/CopyMoveHelper.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,21 @@ static void copyToForeignTarget(Path source, Path target,
110110
Files.getFileAttributeView(source, PosixFileAttributeView.class);
111111

112112
// attributes of source file
113-
BasicFileAttributes sourceAttrs = sourcePosixView != null ?
114-
Files.readAttributes(source,
115-
PosixFileAttributes.class,
116-
linkOptions) :
117-
Files.readAttributes(source,
118-
BasicFileAttributes.class,
119-
linkOptions);
113+
BasicFileAttributes sourceAttrs = null;
114+
if (sourcePosixView != null) {
115+
try {
116+
sourceAttrs = Files.readAttributes(source,
117+
PosixFileAttributes.class,
118+
linkOptions);
119+
} catch (SecurityException ignored) {
120+
// okay to continue if RuntimePermission("accessUserInformation") not granted
121+
}
122+
}
123+
if (sourceAttrs == null)
124+
sourceAttrs = Files.readAttributes(source,
125+
BasicFileAttributes.class,
126+
linkOptions);
127+
assert sourceAttrs != null;
120128

121129
if (sourceAttrs.isSymbolicLink())
122130
throw new IOException("Copying of symbolic links not supported");
@@ -157,7 +165,11 @@ static void copyToForeignTarget(Path source, Path target,
157165

158166
if (sourceAttrs instanceof PosixFileAttributes sourcePosixAttrs &&
159167
targetView instanceof PosixFileAttributeView targetPosixView) {
160-
targetPosixView.setPermissions(sourcePosixAttrs.permissions());
168+
try {
169+
targetPosixView.setPermissions(sourcePosixAttrs.permissions());
170+
} catch (SecurityException ignored) {
171+
// okay to continue if RuntimePermission("accessUserInformation") not granted
172+
}
161173
}
162174
} catch (Throwable x) {
163175
// rollback
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. 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+
* @test
26+
* @bug 8245194
27+
* @run main/othervm/java.security.policy=copy.policy CopyToNonDefaultFS
28+
* @summary Test for exception copying from default to non-default file system
29+
*/
30+
31+
import java.io.*;
32+
import java.nio.ByteBuffer;
33+
import java.nio.channels.*;
34+
import java.nio.file.*;
35+
import java.util.*;
36+
37+
import static java.nio.file.StandardOpenOption.*;
38+
39+
public class CopyToNonDefaultFS {
40+
public static void main(String... args) throws IOException {
41+
Path source = Files.createTempFile(Path.of("."), "tmp", ".dat");
42+
try (FileChannel fc = FileChannel.open(source, CREATE, WRITE)) {
43+
fc.position(8191);
44+
fc.write(ByteBuffer.wrap(new byte[] {27}));
45+
}
46+
47+
Path zip = Path.of("out.zip");
48+
zip.toFile().deleteOnExit();
49+
Map<String,String> env =
50+
Map.of("create", String.valueOf(Files.notExists(zip)));
51+
52+
ClassLoader cl = CopyToNonDefaultFS.class.getClassLoader();
53+
try (FileSystem fileSystem = FileSystems.newFileSystem(zip, env, cl)) {
54+
Path p = fileSystem.getPath(source.getFileName().toString());
55+
Files.copy(source, p);
56+
}
57+
}
58+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
grant {
2+
permission java.io.FilePermission "<<ALL FILES>>", "read,write";
3+
permission java.io.FilePermission "out.zip", "delete";
4+
};

0 commit comments

Comments
 (0)