8280881: (fs) UnixNativeDispatcher.close0 may throw UnixException

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2022-03-10 16:38:44 +00:00
parent 1668c02ee8
commit e8a1ce00b2
10 changed files with 67 additions and 45 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -182,7 +182,7 @@ class LinuxDosFileAttributeView
x.rethrowAsIOException(file); x.rethrowAsIOException(file);
return null; // keep compiler happy return null; // keep compiler happy
} finally { } finally {
close(fd); close(fd, e -> null);
} }
} }
@ -277,7 +277,7 @@ class LinuxDosFileAttributeView
} catch (UnixException x) { } catch (UnixException x) {
x.rethrowAsIOException(file); x.rethrowAsIOException(file);
} finally { } finally {
close(fd); close(fd, e -> null);
} }
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -70,7 +70,7 @@ class LinuxWatchService
socketpair(sp); socketpair(sp);
configureBlocking(sp[0], false); configureBlocking(sp[0], false);
} catch (UnixException x) { } catch (UnixException x) {
UnixNativeDispatcher.close(ifd); UnixNativeDispatcher.close(ifd, e -> null);
throw new IOException(x.errorString()); throw new IOException(x.errorString());
} }
@ -296,9 +296,9 @@ class LinuxWatchService
// free resources // free resources
unsafe.freeMemory(address); unsafe.freeMemory(address);
UnixNativeDispatcher.close(socketpair[0]); UnixNativeDispatcher.close(socketpair[0], e -> null);
UnixNativeDispatcher.close(socketpair[1]); UnixNativeDispatcher.close(socketpair[1], e -> null);
UnixNativeDispatcher.close(ifd); UnixNativeDispatcher.close(ifd, e -> null);
} }
/** /**

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -186,7 +186,7 @@ class UnixCopyFile {
} }
if (sfd >= 0) { if (sfd >= 0) {
source.getFileSystem().copyNonPosixAttributes(sfd, dfd); source.getFileSystem().copyNonPosixAttributes(sfd, dfd);
close(sfd); close(sfd, e -> null);
} }
} }
// copy time stamps last // copy time stamps last
@ -210,7 +210,7 @@ class UnixCopyFile {
done = true; done = true;
} finally { } finally {
if (dfd >= 0) if (dfd >= 0)
close(dfd); close(dfd, e -> null);
if (!done) { if (!done) {
// rollback // rollback
try { rmdir(target); } catch (UnixException ignore) { } try { rmdir(target); } catch (UnixException ignore) { }
@ -288,7 +288,7 @@ class UnixCopyFile {
} }
complete = true; complete = true;
} finally { } finally {
close(fo); close(fo, e -> null);
// copy of file or attributes failed so rollback // copy of file or attributes failed so rollback
if (!complete) { if (!complete) {
@ -298,7 +298,7 @@ class UnixCopyFile {
} }
} }
} finally { } finally {
close(fi); close(fi, e -> null);
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -67,7 +67,7 @@ class UnixDirectoryStream
this.filter = filter; this.filter = filter;
} }
protected final UnixPath directory() { final UnixPath directory() {
return dir; return dir;
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -160,7 +160,7 @@ class UnixFileAttributeViews {
} }
} }
} finally { } finally {
close(fd); close(fd, e -> null);
} }
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -197,7 +197,7 @@ abstract class UnixFileStore
if (e.errno() == UnixConstants.XATTR_NOT_FOUND) if (e.errno() == UnixConstants.XATTR_NOT_FOUND)
return true; return true;
} finally { } finally {
UnixNativeDispatcher.close(fd); UnixNativeDispatcher.close(fd, e -> null);
} }
return false; return false;
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -431,13 +431,14 @@ public abstract class UnixFileSystemProvider
dfd2 = dup(dfd1); dfd2 = dup(dfd1);
dp = fdopendir(dfd1); dp = fdopendir(dfd1);
} catch (UnixException x) { } catch (UnixException x) {
IOException ioe = x.errno() == UnixConstants.ENOTDIR ?
new NotDirectoryException(dir.getPathForExceptionMessage()) :
x.asIOException(dir);
if (dfd1 != -1) if (dfd1 != -1)
UnixNativeDispatcher.close(dfd1); UnixNativeDispatcher.close(dfd1, e -> null);
if (dfd2 != -1) if (dfd2 != -1)
UnixNativeDispatcher.close(dfd2); UnixNativeDispatcher.close(dfd2, e -> null);
if (x.errno() == UnixConstants.ENOTDIR) throw ioe;
throw new NotDirectoryException(dir.getPathForExceptionMessage());
x.rethrowAsIOException(dir);
} }
return new UnixSecureDirectoryStream(dir, dp, dfd2, filter); return new UnixSecureDirectoryStream(dir, dp, dfd2, filter);
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,8 @@
package sun.nio.fs; package sun.nio.fs;
import java.util.function.Function;
/** /**
* Unix system and library calls. * Unix system and library calls.
*/ */
@ -90,12 +92,30 @@ class UnixNativeDispatcher {
/** /**
* close(int filedes). If fd is -1 this is a no-op. * close(int filedes). If fd is -1 this is a no-op.
*/ */
static void close(int fd) { static void close(int fd) throws UnixException {
if (fd != -1) { if (fd != -1) {
close0(fd); close0(fd);
} }
} }
private static native void close0(int fd); private static native void close0(int fd) throws UnixException;
/**
* close(fd). If close fails then the given exception supplier function is
* invoked to produce an exception to throw. If the function returns null
* then no exception is thrown. If close fails and the exception supplier
* function is null, then no exception is thrown.
*/
static <X extends Throwable>
void close(int fd, Function<UnixException, X> mapper) throws X {
try {
close(fd);
} catch (UnixException e) {
if (mapper != null) {
X ex = mapper.apply(e);
if (ex != null) throw ex;
}
}
}
/** /**
* void rewind(FILE* stream); * void rewind(FILE* stream);

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -61,7 +61,7 @@ class UnixSecureDirectoryStream
ds.writeLock().lock(); ds.writeLock().lock();
try { try {
if (ds.closeImpl()) { if (ds.closeImpl()) {
UnixNativeDispatcher.close(dfd); UnixNativeDispatcher.close(dfd, e -> e.asIOException(ds.directory()));
} }
} finally { } finally {
ds.writeLock().unlock(); ds.writeLock().unlock();
@ -117,13 +117,14 @@ class UnixSecureDirectoryStream
newdfd2 = dup(newdfd1); newdfd2 = dup(newdfd1);
ptr = fdopendir(newdfd1); ptr = fdopendir(newdfd1);
} catch (UnixException x) { } catch (UnixException x) {
IOException ioe = x.errno() == UnixConstants.ENOTDIR ?
new NotDirectoryException(file.toString()) :
x.asIOException(file);
if (newdfd1 != -1) if (newdfd1 != -1)
UnixNativeDispatcher.close(newdfd1); UnixNativeDispatcher.close(newdfd1, e -> null);
if (newdfd2 != -1) if (newdfd2 != -1)
UnixNativeDispatcher.close(newdfd2); UnixNativeDispatcher.close(newdfd1, e -> null);
if (x.errno() == UnixConstants.ENOTDIR) throw ioe;
throw new NotDirectoryException(file.toString());
x.rethrowAsIOException(file);
} }
return new UnixSecureDirectoryStream(child, ptr, newdfd2, null); return new UnixSecureDirectoryStream(child, ptr, newdfd2, null);
} finally { } finally {
@ -422,7 +423,7 @@ class UnixSecureDirectoryStream
} }
} finally { } finally {
if (file != null) if (file != null)
UnixNativeDispatcher.close(fd); UnixNativeDispatcher.close(fd, e-> null);
} }
} finally { } finally {
ds.readLock().unlock(); ds.readLock().unlock();
@ -504,7 +505,7 @@ class UnixSecureDirectoryStream
x.rethrowAsIOException(file); x.rethrowAsIOException(file);
} finally { } finally {
if (file != null && fd >= 0) if (file != null && fd >= 0)
UnixNativeDispatcher.close(fd); UnixNativeDispatcher.close(fd, e-> null);
} }
} finally { } finally {
ds.readLock().unlock(); ds.readLock().unlock();
@ -527,7 +528,7 @@ class UnixSecureDirectoryStream
x.rethrowAsIOException(file); x.rethrowAsIOException(file);
} finally { } finally {
if (file != null && fd >= 0) if (file != null && fd >= 0)
UnixNativeDispatcher.close(fd); UnixNativeDispatcher.close(fd, e-> null);
} }
} finally { } finally {
ds.readLock().unlock(); ds.readLock().unlock();

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -133,7 +133,7 @@ abstract class UnixUserDefinedFileAttributeView
null, "Unable to get list of extended attributes: " + null, "Unable to get list of extended attributes: " +
x.getMessage()); x.getMessage());
} finally { } finally {
close(fd); close(fd, e -> null);
} }
} }
@ -157,7 +157,7 @@ abstract class UnixUserDefinedFileAttributeView
null, "Unable to get size of extended attribute '" + name + null, "Unable to get size of extended attribute '" + name +
"': " + x.getMessage()); "': " + x.getMessage());
} finally { } finally {
close(fd); close(fd, e -> null);
} }
} }
@ -221,7 +221,7 @@ abstract class UnixUserDefinedFileAttributeView
throw new FileSystemException(file.getPathForExceptionMessage(), throw new FileSystemException(file.getPathForExceptionMessage(),
null, "Error reading extended attribute '" + name + "': " + msg); null, "Error reading extended attribute '" + name + "': " + msg);
} finally { } finally {
close(fd); close(fd, e -> null);
} }
} }
@ -283,7 +283,7 @@ abstract class UnixUserDefinedFileAttributeView
null, "Error writing extended attribute '" + name + "': " + null, "Error writing extended attribute '" + name + "': " +
x.getMessage()); x.getMessage());
} finally { } finally {
close(fd); close(fd, e -> null);
} }
} }
@ -305,7 +305,7 @@ abstract class UnixUserDefinedFileAttributeView
throw new FileSystemException(file.getPathForExceptionMessage(), throw new FileSystemException(file.getPathForExceptionMessage(),
null, "Unable to delete extended attribute '" + name + "': " + x.getMessage()); null, "Unable to delete extended attribute '" + name + "': " + x.getMessage());
} finally { } finally {
close(fd); close(fd, e -> null);
} }
} }
@ -346,4 +346,4 @@ abstract class UnixUserDefinedFileAttributeView
buffer.release(); buffer.release();
} }
} }
} }