Skip to content

Commit fdce97d

Browse files
author
Brian Burkhalter
committed
8267820: (fs) Files.copy should attempt to copy POSIX attributes when target file in custom file system
Reviewed-by: lancea, alanb
1 parent e8a1ce0 commit fdce97d

File tree

5 files changed

+54
-27
lines changed

5 files changed

+54
-27
lines changed

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

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -105,11 +105,20 @@ static void copyToForeignTarget(Path source, Path target,
105105
LinkOption[] linkOptions = (opts.followLinks) ? new LinkOption[0] :
106106
new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
107107

108+
// retrieve source posix view, null if unsupported
109+
final PosixFileAttributeView sourcePosixView =
110+
Files.getFileAttributeView(source, PosixFileAttributeView.class);
111+
108112
// attributes of source file
109-
BasicFileAttributes attrs = Files.readAttributes(source,
110-
BasicFileAttributes.class,
111-
linkOptions);
112-
if (attrs.isSymbolicLink())
113+
BasicFileAttributes sourceAttrs = sourcePosixView != null ?
114+
Files.readAttributes(source,
115+
PosixFileAttributes.class,
116+
linkOptions) :
117+
Files.readAttributes(source,
118+
BasicFileAttributes.class,
119+
linkOptions);
120+
121+
if (sourceAttrs.isSymbolicLink())
113122
throw new IOException("Copying of symbolic links not supported");
114123

115124
// delete target if it exists and REPLACE_EXISTING is specified
@@ -119,22 +128,37 @@ static void copyToForeignTarget(Path source, Path target,
119128
throw new FileAlreadyExistsException(target.toString());
120129

121130
// create directory or copy file
122-
if (attrs.isDirectory()) {
131+
if (sourceAttrs.isDirectory()) {
123132
Files.createDirectory(target);
124133
} else {
125134
try (InputStream in = Files.newInputStream(source)) {
126135
Files.copy(in, target);
127136
}
128137
}
129138

130-
// copy basic attributes to target
139+
// copy basic and, if supported, POSIX attributes to target
131140
if (opts.copyAttributes) {
132-
BasicFileAttributeView view =
133-
Files.getFileAttributeView(target, BasicFileAttributeView.class);
141+
BasicFileAttributeView targetView = null;
142+
if (sourcePosixView != null) {
143+
targetView = Files.getFileAttributeView(target,
144+
PosixFileAttributeView.class);
145+
}
146+
147+
// target might not support posix even if source does
148+
if (targetView == null) {
149+
targetView = Files.getFileAttributeView(target,
150+
BasicFileAttributeView.class);
151+
}
152+
134153
try {
135-
view.setTimes(attrs.lastModifiedTime(),
136-
attrs.lastAccessTime(),
137-
attrs.creationTime());
154+
targetView.setTimes(sourceAttrs.lastModifiedTime(),
155+
sourceAttrs.lastAccessTime(),
156+
sourceAttrs.creationTime());
157+
158+
if (sourceAttrs instanceof PosixFileAttributes sourcePosixAttrs &&
159+
targetView instanceof PosixFileAttributeView targetPosixView) {
160+
targetPosixView.setPermissions(sourcePosixAttrs.permissions());
161+
}
138162
} catch (Throwable x) {
139163
// rollback
140164
try {

test/jdk/java/nio/file/Files/CopyAndMove.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
2222
*/
2323

2424
/* @test
25-
* @bug 4313887 6838333 6917021 7006126 6950237 8006645 8201407
25+
* @bug 4313887 6838333 6917021 7006126 6950237 8006645 8201407 8267820
2626
* @summary Unit test for java.nio.file.Files copy and move methods (use -Dseed=X to set PRNG seed)
2727
* @library .. /test/lib
2828
* @build jdk.test.lib.Platform jdk.test.lib.RandomFactory
@@ -672,16 +672,15 @@ static void copyAndVerify(Path source, Path target, CopyOption... options)
672672
checkBasicAttributes(basicAttributes,
673673
readAttributes(source, BasicFileAttributes.class, linkOptions));
674674

675+
// check POSIX attributes are copied
676+
if (!Platform.isWindows() && testPosixAttributes) {
677+
checkPosixAttributes(
678+
readAttributes(source, PosixFileAttributes.class, linkOptions),
679+
readAttributes(target, PosixFileAttributes.class, linkOptions));
680+
}
681+
675682
// verify other attributes when same provider
676683
if (source.getFileSystem().provider() == target.getFileSystem().provider()) {
677-
678-
// check POSIX attributes are copied
679-
if (!Platform.isWindows() && testPosixAttributes) {
680-
checkPosixAttributes(
681-
readAttributes(source, PosixFileAttributes.class, linkOptions),
682-
readAttributes(target, PosixFileAttributes.class, linkOptions));
683-
}
684-
685684
// check DOS attributes are copied
686685
if (Platform.isWindows()) {
687686
checkDosAttributes(

test/jdk/jdk/nio/zipfs/TestPosix.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2021, SAP SE. All rights reserved.
2+
* Copyright (c) 2019, 2022, SAP SE. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -69,11 +69,11 @@
6969
/**
7070
* @test
7171
* @bug 8213031 8273935
72+
* @summary Test POSIX ZIP file operations.
7273
* @modules jdk.zipfs
7374
* jdk.jartool
7475
* @run testng TestPosix
7576
* @run testng/othervm/java.security.policy=test.policy.posix TestPosix
76-
* @summary Test POSIX zip file operations.
7777
*/
7878
public class TestPosix {
7979
private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
@@ -528,9 +528,11 @@ public void testUnzipDefault() throws IOException {
528528
}
529529

530530
// check entries on copied zipfs - no permission data should exist
531-
try (FileSystem zip = FileSystems.newFileSystem(ZIP_FILE_COPY, ENV_DEFAULT)) {
532-
checkEntries(zip, checkExpects.noPermDataInZip);
533-
}
531+
if (System.getProperty("os.name").toLowerCase().contains("windows"))
532+
try (FileSystem zip = FileSystems.newFileSystem(ZIP_FILE_COPY,
533+
ENV_DEFAULT)) {
534+
checkEntries(zip, checkExpects.noPermDataInZip);
535+
}
534536
}
535537

536538
/**

test/jdk/jdk/nio/zipfs/test.policy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ grant {
33
permission java.util.PropertyPermission "test.jdk","read";
44
permission java.util.PropertyPermission "test.src","read";
55
permission java.util.PropertyPermission "user.dir","read";
6+
permission java.lang.RuntimePermission "accessUserInformation";
67
};

test/jdk/jdk/nio/zipfs/test.policy.posix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ grant {
55
permission java.util.PropertyPermission "test.src","read";
66
permission java.util.PropertyPermission "user.dir","read";
77
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.module";
8+
permission java.lang.RuntimePermission "accessUserInformation";
89
};

0 commit comments

Comments
 (0)