8267820: (fs) Files.copy should attempt to copy POSIX attributes when target file in custom file system

Reviewed-by: lancea, alanb
This commit is contained in:
Brian Burkhalter 2022-03-10 17:36:51 +00:00
parent e8a1ce00b2
commit fdce97df5f
5 changed files with 54 additions and 27 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -105,11 +105,20 @@ class CopyMoveHelper {
LinkOption[] linkOptions = (opts.followLinks) ? new LinkOption[0] : LinkOption[] linkOptions = (opts.followLinks) ? new LinkOption[0] :
new LinkOption[] { LinkOption.NOFOLLOW_LINKS }; new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
// retrieve source posix view, null if unsupported
final PosixFileAttributeView sourcePosixView =
Files.getFileAttributeView(source, PosixFileAttributeView.class);
// attributes of source file // attributes of source file
BasicFileAttributes attrs = Files.readAttributes(source, BasicFileAttributes sourceAttrs = sourcePosixView != null ?
Files.readAttributes(source,
PosixFileAttributes.class,
linkOptions) :
Files.readAttributes(source,
BasicFileAttributes.class, BasicFileAttributes.class,
linkOptions); linkOptions);
if (attrs.isSymbolicLink())
if (sourceAttrs.isSymbolicLink())
throw new IOException("Copying of symbolic links not supported"); throw new IOException("Copying of symbolic links not supported");
// delete target if it exists and REPLACE_EXISTING is specified // delete target if it exists and REPLACE_EXISTING is specified
@ -119,7 +128,7 @@ class CopyMoveHelper {
throw new FileAlreadyExistsException(target.toString()); throw new FileAlreadyExistsException(target.toString());
// create directory or copy file // create directory or copy file
if (attrs.isDirectory()) { if (sourceAttrs.isDirectory()) {
Files.createDirectory(target); Files.createDirectory(target);
} else { } else {
try (InputStream in = Files.newInputStream(source)) { try (InputStream in = Files.newInputStream(source)) {
@ -127,14 +136,29 @@ class CopyMoveHelper {
} }
} }
// copy basic attributes to target // copy basic and, if supported, POSIX attributes to target
if (opts.copyAttributes) { if (opts.copyAttributes) {
BasicFileAttributeView view = BasicFileAttributeView targetView = null;
Files.getFileAttributeView(target, BasicFileAttributeView.class); if (sourcePosixView != null) {
targetView = Files.getFileAttributeView(target,
PosixFileAttributeView.class);
}
// target might not support posix even if source does
if (targetView == null) {
targetView = Files.getFileAttributeView(target,
BasicFileAttributeView.class);
}
try { try {
view.setTimes(attrs.lastModifiedTime(), targetView.setTimes(sourceAttrs.lastModifiedTime(),
attrs.lastAccessTime(), sourceAttrs.lastAccessTime(),
attrs.creationTime()); sourceAttrs.creationTime());
if (sourceAttrs instanceof PosixFileAttributes sourcePosixAttrs &&
targetView instanceof PosixFileAttributeView targetPosixView) {
targetPosixView.setPermissions(sourcePosixAttrs.permissions());
}
} catch (Throwable x) { } catch (Throwable x) {
// rollback // rollback
try { try {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,7 +22,7 @@
*/ */
/* @test /* @test
* @bug 4313887 6838333 6917021 7006126 6950237 8006645 8201407 * @bug 4313887 6838333 6917021 7006126 6950237 8006645 8201407 8267820
* @summary Unit test for java.nio.file.Files copy and move methods (use -Dseed=X to set PRNG seed) * @summary Unit test for java.nio.file.Files copy and move methods (use -Dseed=X to set PRNG seed)
* @library .. /test/lib * @library .. /test/lib
* @build jdk.test.lib.Platform jdk.test.lib.RandomFactory * @build jdk.test.lib.Platform jdk.test.lib.RandomFactory
@ -672,9 +672,6 @@ public class CopyAndMove {
checkBasicAttributes(basicAttributes, checkBasicAttributes(basicAttributes,
readAttributes(source, BasicFileAttributes.class, linkOptions)); readAttributes(source, BasicFileAttributes.class, linkOptions));
// verify other attributes when same provider
if (source.getFileSystem().provider() == target.getFileSystem().provider()) {
// check POSIX attributes are copied // check POSIX attributes are copied
if (!Platform.isWindows() && testPosixAttributes) { if (!Platform.isWindows() && testPosixAttributes) {
checkPosixAttributes( checkPosixAttributes(
@ -682,6 +679,8 @@ public class CopyAndMove {
readAttributes(target, PosixFileAttributes.class, linkOptions)); readAttributes(target, PosixFileAttributes.class, linkOptions));
} }
// verify other attributes when same provider
if (source.getFileSystem().provider() == target.getFileSystem().provider()) {
// check DOS attributes are copied // check DOS attributes are copied
if (Platform.isWindows()) { if (Platform.isWindows()) {
checkDosAttributes( checkDosAttributes(

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, 2021, SAP SE. All rights reserved. * Copyright (c) 2019, 2022, SAP SE. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -69,11 +69,11 @@ import static org.testng.Assert.fail;
/** /**
* @test * @test
* @bug 8213031 8273935 * @bug 8213031 8273935
* @summary Test POSIX ZIP file operations.
* @modules jdk.zipfs * @modules jdk.zipfs
* jdk.jartool * jdk.jartool
* @run testng TestPosix * @run testng TestPosix
* @run testng/othervm/java.security.policy=test.policy.posix TestPosix * @run testng/othervm/java.security.policy=test.policy.posix TestPosix
* @summary Test POSIX zip file operations.
*/ */
public class TestPosix { public class TestPosix {
private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar") private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
@ -528,7 +528,9 @@ public class TestPosix {
} }
// check entries on copied zipfs - no permission data should exist // check entries on copied zipfs - no permission data should exist
try (FileSystem zip = FileSystems.newFileSystem(ZIP_FILE_COPY, ENV_DEFAULT)) { if (System.getProperty("os.name").toLowerCase().contains("windows"))
try (FileSystem zip = FileSystems.newFileSystem(ZIP_FILE_COPY,
ENV_DEFAULT)) {
checkEntries(zip, checkExpects.noPermDataInZip); checkEntries(zip, checkExpects.noPermDataInZip);
} }
} }

View File

@ -3,4 +3,5 @@ grant {
permission java.util.PropertyPermission "test.jdk","read"; permission java.util.PropertyPermission "test.jdk","read";
permission java.util.PropertyPermission "test.src","read"; permission java.util.PropertyPermission "test.src","read";
permission java.util.PropertyPermission "user.dir","read"; permission java.util.PropertyPermission "user.dir","read";
permission java.lang.RuntimePermission "accessUserInformation";
}; };

View File

@ -5,4 +5,5 @@ grant {
permission java.util.PropertyPermission "test.src","read"; permission java.util.PropertyPermission "test.src","read";
permission java.util.PropertyPermission "user.dir","read"; permission java.util.PropertyPermission "user.dir","read";
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.module"; permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.module";
permission java.lang.RuntimePermission "accessUserInformation";
}; };