From 299a8ee68d2dc433fa4b7ad14e2518aebab4cfac Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Wed, 7 Feb 2024 19:02:30 +0000 Subject: [PATCH] 8325302: Files.move(REPLACE_EXISTING) throws NoSuchFileException on deleted target Reviewed-by: alanb --- .../classes/sun/nio/fs/UnixFileSystem.java | 18 +++++++++++------- .../classes/sun/nio/fs/WindowsFileCopy.java | 14 +++++++++++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java b/src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java index 5a7b5d8591a..a9e10a94be3 100644 --- a/src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java +++ b/src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2024, 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 @@ -930,12 +930,14 @@ abstract class UnixFileSystem } catch (UnixException x) { // target is non-empty directory that can't be replaced. if (targetAttrs.isDirectory() && - (x.errno() == EEXIST || x.errno() == ENOTEMPTY)) - { + (x.errno() == EEXIST || x.errno() == ENOTEMPTY)) { throw new DirectoryNotEmptyException( target.getPathForExceptionMessage()); } - x.rethrowAsIOException(target); + // ignore file not found otherwise rethrow + if (x.errno() != ENOENT) { + x.rethrowAsIOException(target); + } } } @@ -1061,12 +1063,14 @@ abstract class UnixFileSystem } catch (UnixException x) { // target is non-empty directory that can't be replaced. if (targetAttrs.isDirectory() && - (x.errno() == EEXIST || x.errno() == ENOTEMPTY)) - { + (x.errno() == EEXIST || x.errno() == ENOTEMPTY)) { throw new DirectoryNotEmptyException( target.getPathForExceptionMessage()); } - x.rethrowAsIOException(target); + // ignore file not found otherwise rethrow + if (x.errno() != ENOENT) { + x.rethrowAsIOException(target); + } } } diff --git a/src/java.base/windows/classes/sun/nio/fs/WindowsFileCopy.java b/src/java.base/windows/classes/sun/nio/fs/WindowsFileCopy.java index 11fd13c1b8e..8ad69662822 100644 --- a/src/java.base/windows/classes/sun/nio/fs/WindowsFileCopy.java +++ b/src/java.base/windows/classes/sun/nio/fs/WindowsFileCopy.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2024, 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 @@ -177,7 +177,11 @@ class WindowsFileCopy { target.getPathForExceptionMessage()); } } - x.rethrowAsIOException(target); + // ignore file not found otherwise rethrow + if (x.lastError() != ERROR_FILE_NOT_FOUND && + x.lastError() != ERROR_PATH_NOT_FOUND) { + x.rethrowAsIOException(target); + } } } @@ -400,7 +404,11 @@ class WindowsFileCopy { target.getPathForExceptionMessage()); } } - x.rethrowAsIOException(target); + // ignore file not found otherwise rethrow + if (x.lastError() != ERROR_FILE_NOT_FOUND && + x.lastError() != ERROR_PATH_NOT_FOUND) { + x.rethrowAsIOException(target); + } } }