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.
*
* This code is free software; you can redistribute it and/or modify it
@ -182,7 +182,7 @@ class LinuxDosFileAttributeView
x.rethrowAsIOException(file);
return null; // keep compiler happy
} finally {
close(fd);
close(fd, e -> null);
}
}
@ -277,7 +277,7 @@ class LinuxDosFileAttributeView
} catch (UnixException x) {
x.rethrowAsIOException(file);
} 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.
*
* This code is free software; you can redistribute it and/or modify it
@ -70,7 +70,7 @@ class LinuxWatchService
socketpair(sp);
configureBlocking(sp[0], false);
} catch (UnixException x) {
UnixNativeDispatcher.close(ifd);
UnixNativeDispatcher.close(ifd, e -> null);
throw new IOException(x.errorString());
}
@ -296,9 +296,9 @@ class LinuxWatchService
// free resources
unsafe.freeMemory(address);
UnixNativeDispatcher.close(socketpair[0]);
UnixNativeDispatcher.close(socketpair[1]);
UnixNativeDispatcher.close(ifd);
UnixNativeDispatcher.close(socketpair[0], e -> null);
UnixNativeDispatcher.close(socketpair[1], e -> null);
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.
*
* This code is free software; you can redistribute it and/or modify it
@ -186,7 +186,7 @@ class UnixCopyFile {
}
if (sfd >= 0) {
source.getFileSystem().copyNonPosixAttributes(sfd, dfd);
close(sfd);
close(sfd, e -> null);
}
}
// copy time stamps last
@ -210,7 +210,7 @@ class UnixCopyFile {
done = true;
} finally {
if (dfd >= 0)
close(dfd);
close(dfd, e -> null);
if (!done) {
// rollback
try { rmdir(target); } catch (UnixException ignore) { }
@ -288,7 +288,7 @@ class UnixCopyFile {
}
complete = true;
} finally {
close(fo);
close(fo, e -> null);
// copy of file or attributes failed so rollback
if (!complete) {
@ -298,7 +298,7 @@ class UnixCopyFile {
}
}
} 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.
*
* This code is free software; you can redistribute it and/or modify it
@ -67,7 +67,7 @@ class UnixDirectoryStream
this.filter = filter;
}
protected final UnixPath directory() {
final UnixPath directory() {
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.
*
* This code is free software; you can redistribute it and/or modify it
@ -160,7 +160,7 @@ class UnixFileAttributeViews {
}
}
} 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.
*
* 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)
return true;
} finally {
UnixNativeDispatcher.close(fd);
UnixNativeDispatcher.close(fd, e -> null);
}
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.
*
* This code is free software; you can redistribute it and/or modify it
@ -431,13 +431,14 @@ public abstract class UnixFileSystemProvider
dfd2 = dup(dfd1);
dp = fdopendir(dfd1);
} catch (UnixException x) {
IOException ioe = x.errno() == UnixConstants.ENOTDIR ?
new NotDirectoryException(dir.getPathForExceptionMessage()) :
x.asIOException(dir);
if (dfd1 != -1)
UnixNativeDispatcher.close(dfd1);
UnixNativeDispatcher.close(dfd1, e -> null);
if (dfd2 != -1)
UnixNativeDispatcher.close(dfd2);
if (x.errno() == UnixConstants.ENOTDIR)
throw new NotDirectoryException(dir.getPathForExceptionMessage());
x.rethrowAsIOException(dir);
UnixNativeDispatcher.close(dfd2, e -> null);
throw ioe;
}
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.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,8 @@
package sun.nio.fs;
import java.util.function.Function;
/**
* Unix system and library calls.
*/
@ -90,12 +92,30 @@ class UnixNativeDispatcher {
/**
* 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) {
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);

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.
*
* This code is free software; you can redistribute it and/or modify it
@ -61,7 +61,7 @@ class UnixSecureDirectoryStream
ds.writeLock().lock();
try {
if (ds.closeImpl()) {
UnixNativeDispatcher.close(dfd);
UnixNativeDispatcher.close(dfd, e -> e.asIOException(ds.directory()));
}
} finally {
ds.writeLock().unlock();
@ -117,13 +117,14 @@ class UnixSecureDirectoryStream
newdfd2 = dup(newdfd1);
ptr = fdopendir(newdfd1);
} catch (UnixException x) {
IOException ioe = x.errno() == UnixConstants.ENOTDIR ?
new NotDirectoryException(file.toString()) :
x.asIOException(file);
if (newdfd1 != -1)
UnixNativeDispatcher.close(newdfd1);
UnixNativeDispatcher.close(newdfd1, e -> null);
if (newdfd2 != -1)
UnixNativeDispatcher.close(newdfd2);
if (x.errno() == UnixConstants.ENOTDIR)
throw new NotDirectoryException(file.toString());
x.rethrowAsIOException(file);
UnixNativeDispatcher.close(newdfd1, e -> null);
throw ioe;
}
return new UnixSecureDirectoryStream(child, ptr, newdfd2, null);
} finally {
@ -422,7 +423,7 @@ class UnixSecureDirectoryStream
}
} finally {
if (file != null)
UnixNativeDispatcher.close(fd);
UnixNativeDispatcher.close(fd, e-> null);
}
} finally {
ds.readLock().unlock();
@ -504,7 +505,7 @@ class UnixSecureDirectoryStream
x.rethrowAsIOException(file);
} finally {
if (file != null && fd >= 0)
UnixNativeDispatcher.close(fd);
UnixNativeDispatcher.close(fd, e-> null);
}
} finally {
ds.readLock().unlock();
@ -527,7 +528,7 @@ class UnixSecureDirectoryStream
x.rethrowAsIOException(file);
} finally {
if (file != null && fd >= 0)
UnixNativeDispatcher.close(fd);
UnixNativeDispatcher.close(fd, e-> null);
}
} finally {
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.
*
* 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: " +
x.getMessage());
} finally {
close(fd);
close(fd, e -> null);
}
}
@ -157,7 +157,7 @@ abstract class UnixUserDefinedFileAttributeView
null, "Unable to get size of extended attribute '" + name +
"': " + x.getMessage());
} finally {
close(fd);
close(fd, e -> null);
}
}
@ -221,7 +221,7 @@ abstract class UnixUserDefinedFileAttributeView
throw new FileSystemException(file.getPathForExceptionMessage(),
null, "Error reading extended attribute '" + name + "': " + msg);
} finally {
close(fd);
close(fd, e -> null);
}
}
@ -283,7 +283,7 @@ abstract class UnixUserDefinedFileAttributeView
null, "Error writing extended attribute '" + name + "': " +
x.getMessage());
} finally {
close(fd);
close(fd, e -> null);
}
}
@ -305,7 +305,7 @@ abstract class UnixUserDefinedFileAttributeView
throw new FileSystemException(file.getPathForExceptionMessage(),
null, "Unable to delete extended attribute '" + name + "': " + x.getMessage());
} finally {
close(fd);
close(fd, e -> null);
}
}
@ -346,4 +346,4 @@ abstract class UnixUserDefinedFileAttributeView
buffer.release();
}
}
}
}