Skip to content

Commit

Permalink
8317128: java/nio/file/Files/CopyAndMove.java failed with AccessDenie…
Browse files Browse the repository at this point in the history
…dException

Reviewed-by: alanb, lancea
  • Loading branch information
Brian Burkhalter committed Oct 6, 2023
1 parent b62e774 commit 6c6beba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
28 changes: 16 additions & 12 deletions src/java.base/share/classes/java/nio/file/CopyMoveHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@

package java.nio.file;

import java.nio.file.attribute.*;
import java.io.InputStream;
import java.io.IOException;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.spi.FileSystemProvider;

/**
* Helper class to support copying or moving files when the source and target
Expand Down Expand Up @@ -69,14 +73,6 @@ static CopyOptions parse(CopyOption... options) {
}
return result;
}

CopyOption[] replaceExistingOrEmpty() {
if (replaceExisting) {
return new CopyOption[] { StandardCopyOption.REPLACE_EXISTING };
} else {
return new CopyOption[0];
}
}
}

/**
Expand Down Expand Up @@ -137,14 +133,22 @@ static void copyToForeignTarget(Path source, Path target,
if (sourceAttrs.isSymbolicLink())
throw new IOException("Copying of symbolic links not supported");

// ensure source can be copied
FileSystemProvider provider = source.getFileSystem().provider();
provider.checkAccess(source, AccessMode.READ);

// delete target if it exists and REPLACE_EXISTING is specified
if (opts.replaceExisting)
Files.deleteIfExists(target);
else if (Files.exists(target))
throw new FileAlreadyExistsException(target.toString());

// create directory or copy file
if (sourceAttrs.isDirectory()) {
if (opts.replaceExisting)
Files.deleteIfExists(target);
Files.createDirectory(target);
} else {
try (InputStream in = Files.newInputStream(source)) {
Files.copy(in, target, opts.replaceExistingOrEmpty());
Files.copy(in, target);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, 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
Expand Down Expand Up @@ -31,7 +31,7 @@
import static sun.nio.fs.WindowsConstants.*;

/**
* Internal exception thrown when a Win32 calls fails.
* Internal exception thrown when a Win32 call fails.
*/

class WindowsException extends Exception {
Expand Down

1 comment on commit 6c6beba

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.