Skip to content

Commit 6c6beba

Browse files
author
Brian Burkhalter
committedOct 6, 2023
8317128: java/nio/file/Files/CopyAndMove.java failed with AccessDeniedException
Reviewed-by: alanb, lancea
1 parent b62e774 commit 6c6beba

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed
 

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

+16-12
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525

2626
package java.nio.file;
2727

28-
import java.nio.file.attribute.*;
2928
import java.io.InputStream;
3029
import java.io.IOException;
30+
import java.nio.file.attribute.BasicFileAttributes;
31+
import java.nio.file.attribute.BasicFileAttributeView;
32+
import java.nio.file.attribute.PosixFileAttributes;
33+
import java.nio.file.attribute.PosixFileAttributeView;
34+
import java.nio.file.spi.FileSystemProvider;
3135

3236
/**
3337
* Helper class to support copying or moving files when the source and target
@@ -69,14 +73,6 @@ static CopyOptions parse(CopyOption... options) {
6973
}
7074
return result;
7175
}
72-
73-
CopyOption[] replaceExistingOrEmpty() {
74-
if (replaceExisting) {
75-
return new CopyOption[] { StandardCopyOption.REPLACE_EXISTING };
76-
} else {
77-
return new CopyOption[0];
78-
}
79-
}
8076
}
8177

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

136+
// ensure source can be copied
137+
FileSystemProvider provider = source.getFileSystem().provider();
138+
provider.checkAccess(source, AccessMode.READ);
139+
140+
// delete target if it exists and REPLACE_EXISTING is specified
141+
if (opts.replaceExisting)
142+
Files.deleteIfExists(target);
143+
else if (Files.exists(target))
144+
throw new FileAlreadyExistsException(target.toString());
145+
140146
// create directory or copy file
141147
if (sourceAttrs.isDirectory()) {
142-
if (opts.replaceExisting)
143-
Files.deleteIfExists(target);
144148
Files.createDirectory(target);
145149
} else {
146150
try (InputStream in = Files.newInputStream(source)) {
147-
Files.copy(in, target, opts.replaceExistingOrEmpty());
151+
Files.copy(in, target);
148152
}
149153
}
150154

‎src/java.base/windows/classes/sun/nio/fs/WindowsException.java

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

3333
/**
34-
* Internal exception thrown when a Win32 calls fails.
34+
* Internal exception thrown when a Win32 call fails.
3535
*/
3636

3737
class WindowsException extends Exception {

0 commit comments

Comments
 (0)
Please sign in to comment.