Skip to content

Commit dd5d7d0

Browse files
author
Brian Burkhalter
committedApr 2, 2024
8327002: (fs) java/nio/file/Files/CopyMoveVariations.java should be able to test across file systems
Reviewed-by: alanb
1 parent 6ae1cf1 commit dd5d7d0

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed
 

‎test/jdk/java/nio/file/Files/CopyMoveVariations.java

+39-12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.nio.file.CopyOption;
3535
import java.nio.file.FileAlreadyExistsException;
3636
import java.nio.file.Files;
37+
import java.nio.file.FileStore;
3738
import java.nio.file.LinkOption;
3839
import java.nio.file.Path;
3940
import java.nio.file.attribute.PosixFilePermission;
@@ -68,19 +69,21 @@ enum PathType {
6869
}
6970

7071
private static final boolean SUPPORTS_POSIX_PERMISSIONS;
72+
private static final String TMP_DIR =
73+
System.getProperty("copymove.tmp.dir", ".");
7174

7275
static {
73-
Path currentDir = null;
76+
Path tempFile = null;
7477
try {
75-
currentDir = Files.createTempFile(Path.of("."), "this", "that");
78+
tempFile = Files.createTempFile(Path.of(TMP_DIR), "this", "that");
7679
SUPPORTS_POSIX_PERMISSIONS =
77-
Files.getFileStore(currentDir).supportsFileAttributeView("posix");
80+
Files.getFileStore(tempFile).supportsFileAttributeView("posix");
7881
} catch (IOException cause) {
7982
throw new UncheckedIOException(cause);
8083
} finally {
81-
if (currentDir != null) {
84+
if (tempFile != null) {
8285
try {
83-
Files.delete(currentDir);
86+
Files.delete(tempFile);
8487
} catch (IOException ignore) {
8588
}
8689
}
@@ -91,6 +94,13 @@ private static boolean supportsPosixPermissions() {
9194
return SUPPORTS_POSIX_PERMISSIONS;
9295
}
9396

97+
private static boolean isSameFileStore(Path p1, Path p2)
98+
throws IOException {
99+
FileStore fs1 = p1.getFileSystem().provider().getFileStore(p1);
100+
FileStore fs2 = p2.getFileSystem().provider().getFileStore(p2);
101+
return fs1.equals(fs2);
102+
}
103+
94104
private static Stream<Arguments> params() {
95105
List<Arguments> list = new ArrayList<Arguments>();
96106

@@ -142,15 +152,15 @@ void op(OpType op, PathType type, String mode, boolean replaceExisting,
142152
Path source = null;
143153
Path target = null;
144154
Path linkTarget = null;
145-
Path currentDir = Path.of(".");
155+
Path tmpDir = Path.of(TMP_DIR);
146156
try {
147157
switch (type) {
148158
case FILE ->
149-
source = Files.createTempFile(currentDir, "file", "dat");
159+
source = Files.createTempFile(tmpDir, "file", "dat");
150160
case DIR ->
151-
source = Files.createTempDirectory(currentDir, "dir");
161+
source = Files.createTempDirectory(tmpDir, "dir");
152162
case LINK -> {
153-
linkTarget = Files.createTempFile(currentDir, "link", "target");
163+
linkTarget = Files.createTempFile(tmpDir, "link", "target");
154164
Path link = Path.of("link");
155165
source = Files.createSymbolicLink(link, linkTarget);
156166
}
@@ -164,7 +174,7 @@ void op(OpType op, PathType type, String mode, boolean replaceExisting,
164174
Files.setPosixFilePermissions(source, perms);
165175

166176
if (targetExists)
167-
target = Files.createTempFile(currentDir, "file", "target");
177+
target = Files.createTempFile(tmpDir, "file", "target");
168178
else
169179
target = Path.of("target");
170180

@@ -193,7 +203,11 @@ void op(OpType op, PathType type, String mode, boolean replaceExisting,
193203
assertThrows(FileAlreadyExistsException.class,
194204
() -> Files.move(src, dst, options));
195205
} else {
196-
Files.move(source, target, options);
206+
try {
207+
Files.move(source, target, options);
208+
} catch (AccessDeniedException ade) {
209+
assertTrue(mode.charAt(0) != 'r');
210+
}
197211
assert Files.exists(target);
198212
}
199213
} else if (type == PathType.DIR) {
@@ -213,7 +227,20 @@ void op(OpType op, PathType type, String mode, boolean replaceExisting,
213227
Files.move(source, target, options);
214228
assert Files.exists(target);
215229
} catch (AccessDeniedException ade) {
216-
assertTrue(mode.charAt(1) != 'w');
230+
Path other = target.getParent();
231+
if (other == null)
232+
other = Path.of(System.getProperty("user.dir"));
233+
if (isSameFileStore(source, other)) {
234+
// directories on same store should be renamed
235+
assertTrue(mode.charAt(1) != 'w');
236+
} else {
237+
// directories on different stores will likely be
238+
// moved by a copy which requires read permission
239+
if (mode.charAt(1) == 'w')
240+
assertTrue(mode.charAt(0) != 'r');
241+
else
242+
assertTrue(mode.charAt(1) != 'w');
243+
}
217244
} catch (FileAlreadyExistsException faee) {
218245
assertTrue(targetExists && !replaceExisting);
219246
}

0 commit comments

Comments
 (0)
Please sign in to comment.