Skip to content

Commit 299a8ee

Browse files
author
Brian Burkhalter
committed
8325302: Files.move(REPLACE_EXISTING) throws NoSuchFileException on deleted target
Reviewed-by: alanb
1 parent 3a1f4d0 commit 299a8ee

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2024, 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
@@ -930,12 +930,14 @@ void move(UnixPath source, UnixPath target, CopyOption... options)
930930
} catch (UnixException x) {
931931
// target is non-empty directory that can't be replaced.
932932
if (targetAttrs.isDirectory() &&
933-
(x.errno() == EEXIST || x.errno() == ENOTEMPTY))
934-
{
933+
(x.errno() == EEXIST || x.errno() == ENOTEMPTY)) {
935934
throw new DirectoryNotEmptyException(
936935
target.getPathForExceptionMessage());
937936
}
938-
x.rethrowAsIOException(target);
937+
// ignore file not found otherwise rethrow
938+
if (x.errno() != ENOENT) {
939+
x.rethrowAsIOException(target);
940+
}
939941
}
940942
}
941943

@@ -1061,12 +1063,14 @@ void copy(final UnixPath source,
10611063
} catch (UnixException x) {
10621064
// target is non-empty directory that can't be replaced.
10631065
if (targetAttrs.isDirectory() &&
1064-
(x.errno() == EEXIST || x.errno() == ENOTEMPTY))
1065-
{
1066+
(x.errno() == EEXIST || x.errno() == ENOTEMPTY)) {
10661067
throw new DirectoryNotEmptyException(
10671068
target.getPathForExceptionMessage());
10681069
}
1069-
x.rethrowAsIOException(target);
1070+
// ignore file not found otherwise rethrow
1071+
if (x.errno() != ENOENT) {
1072+
x.rethrowAsIOException(target);
1073+
}
10701074
}
10711075
}
10721076

src/java.base/windows/classes/sun/nio/fs/WindowsFileCopy.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2024, 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
@@ -177,7 +177,11 @@ static void copy(final WindowsPath source,
177177
target.getPathForExceptionMessage());
178178
}
179179
}
180-
x.rethrowAsIOException(target);
180+
// ignore file not found otherwise rethrow
181+
if (x.lastError() != ERROR_FILE_NOT_FOUND &&
182+
x.lastError() != ERROR_PATH_NOT_FOUND) {
183+
x.rethrowAsIOException(target);
184+
}
181185
}
182186
}
183187

@@ -400,7 +404,11 @@ static void move(WindowsPath source, WindowsPath target, CopyOption... options)
400404
target.getPathForExceptionMessage());
401405
}
402406
}
403-
x.rethrowAsIOException(target);
407+
// ignore file not found otherwise rethrow
408+
if (x.lastError() != ERROR_FILE_NOT_FOUND &&
409+
x.lastError() != ERROR_PATH_NOT_FOUND) {
410+
x.rethrowAsIOException(target);
411+
}
404412
}
405413
}
406414

0 commit comments

Comments
 (0)