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.
*
* 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] :
new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
// retrieve source posix view, null if unsupported
final PosixFileAttributeView sourcePosixView =
Files.getFileAttributeView(source, PosixFileAttributeView.class);
// attributes of source file
BasicFileAttributes attrs = Files.readAttributes(source,
BasicFileAttributes.class,
linkOptions);
if (attrs.isSymbolicLink())
BasicFileAttributes sourceAttrs = sourcePosixView != null ?
Files.readAttributes(source,
PosixFileAttributes.class,
linkOptions) :
Files.readAttributes(source,
BasicFileAttributes.class,
linkOptions);
if (sourceAttrs.isSymbolicLink())
throw new IOException("Copying of symbolic links not supported");
// delete target if it exists and REPLACE_EXISTING is specified
@ -119,7 +128,7 @@ class CopyMoveHelper {
throw new FileAlreadyExistsException(target.toString());
// create directory or copy file
if (attrs.isDirectory()) {
if (sourceAttrs.isDirectory()) {
Files.createDirectory(target);
} else {
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) {
BasicFileAttributeView view =
Files.getFileAttributeView(target, BasicFileAttributeView.class);
BasicFileAttributeView targetView = null;
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 {
view.setTimes(attrs.lastModifiedTime(),
attrs.lastAccessTime(),
attrs.creationTime());
targetView.setTimes(sourceAttrs.lastModifiedTime(),
sourceAttrs.lastAccessTime(),
sourceAttrs.creationTime());
if (sourceAttrs instanceof PosixFileAttributes sourcePosixAttrs &&
targetView instanceof PosixFileAttributeView targetPosixView) {
targetPosixView.setPermissions(sourcePosixAttrs.permissions());
}
} catch (Throwable x) {
// rollback
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.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,7 +22,7 @@
*/
/* @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)
* @library .. /test/lib
* @build jdk.test.lib.Platform jdk.test.lib.RandomFactory
@ -672,16 +672,15 @@ public class CopyAndMove {
checkBasicAttributes(basicAttributes,
readAttributes(source, BasicFileAttributes.class, linkOptions));
// check POSIX attributes are copied
if (!Platform.isWindows() && testPosixAttributes) {
checkPosixAttributes(
readAttributes(source, PosixFileAttributes.class, linkOptions),
readAttributes(target, PosixFileAttributes.class, linkOptions));
}
// verify other attributes when same provider
if (source.getFileSystem().provider() == target.getFileSystem().provider()) {
// check POSIX attributes are copied
if (!Platform.isWindows() && testPosixAttributes) {
checkPosixAttributes(
readAttributes(source, PosixFileAttributes.class, linkOptions),
readAttributes(target, PosixFileAttributes.class, linkOptions));
}
// check DOS attributes are copied
if (Platform.isWindows()) {
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.
*
* This code is free software; you can redistribute it and/or modify it
@ -69,11 +69,11 @@ import static org.testng.Assert.fail;
/**
* @test
* @bug 8213031 8273935
* @summary Test POSIX ZIP file operations.
* @modules jdk.zipfs
* jdk.jartool
* @run testng TestPosix
* @run testng/othervm/java.security.policy=test.policy.posix TestPosix
* @summary Test POSIX zip file operations.
*/
public class TestPosix {
private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
@ -528,9 +528,11 @@ public class TestPosix {
}
// check entries on copied zipfs - no permission data should exist
try (FileSystem zip = FileSystems.newFileSystem(ZIP_FILE_COPY, ENV_DEFAULT)) {
checkEntries(zip, checkExpects.noPermDataInZip);
}
if (System.getProperty("os.name").toLowerCase().contains("windows"))
try (FileSystem zip = FileSystems.newFileSystem(ZIP_FILE_COPY,
ENV_DEFAULT)) {
checkEntries(zip, checkExpects.noPermDataInZip);
}
}
/**

View File

@ -3,4 +3,5 @@ grant {
permission java.util.PropertyPermission "test.jdk","read";
permission java.util.PropertyPermission "test.src","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 "user.dir","read";
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.module";
permission java.lang.RuntimePermission "accessUserInformation";
};