8201407: Files.move throws DirectoryNonEmptyException when moving directory across file system

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2018-06-07 07:43:29 -07:00
parent 9f39d8c408
commit e9f3b0e527
4 changed files with 41 additions and 7 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2018, 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
@ -373,6 +373,22 @@ class UnixCopyFile {
}
}
// throw a DirectoryNotEmpty exception if appropriate
static void ensureEmptyDir(UnixPath dir) throws IOException {
try {
long ptr = opendir(dir);
try (UnixDirectoryStream stream =
new UnixDirectoryStream(dir, ptr, e -> true)) {
if (stream.iterator().hasNext()) {
throw new DirectoryNotEmptyException(
dir.getPathForExceptionMessage());
}
}
} catch (UnixException e) {
e.rethrowAsIOException(dir);
}
}
// move file from source to target
static void move(UnixPath source, UnixPath target, CopyOption... options)
throws IOException
@ -465,6 +481,7 @@ class UnixCopyFile {
// copy source to target
if (sourceAttrs.isDirectory()) {
ensureEmptyDir(source);
copyDirectory(source, sourceAttrs, target, flags);
} else {
if (sourceAttrs.isSymbolicLink()) {