Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.1.0] Fix up permissions error in getInputStream, like we already do for getOutputStream. #21087

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ protected InputStream getInputStream(PathFragment path) throws IOException {
if (e.getMessage().endsWith("(Interrupted system call)")) {
continue;
} else {
// FileInputStream throws FileNotFoundException if opening fails for any reason,
// including permissions. Fix it up here.
// TODO(tjgq): Migrate to java.nio.
if (e.getMessage().equals(path + ERR_PERMISSION_DENIED)) {
throw new FileAccessException(e.getMessage());
}
throw e;
}
}
Expand Down Expand Up @@ -158,9 +164,9 @@ protected OutputStream getOutputStream(PathFragment path, boolean append, boolea
try {
return createFileOutputStream(path, append, internal);
} catch (FileNotFoundException e) {
// Why does it throw a *FileNotFoundException* if it can't write?
// That does not make any sense! And its in a completely different
// format than in other situations, no less!
// FileOutputStream throws FileNotFoundException if opening fails for any reason,
// including permissions. Fix it up here.
// TODO(tjgq): Migrate to java.nio.
if (e.getMessage().equals(path + ERR_PERMISSION_DENIED)) {
throw new FileAccessException(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,20 @@ public void testInputAndOutputStream() throws Exception {
}
}

@Test
public void testInputStreamPermissionError() throws Exception {
assertThat(xFile.exists()).isTrue();
xFile.setReadable(false);
assertThrows(FileAccessException.class, () -> xFile.getInputStream());
}

@Test
public void testOutputStreamPermissionError() throws Exception {
assertThat(xFile.exists()).isTrue();
xFile.setWritable(false);
assertThrows(FileAccessException.class, () -> xFile.getOutputStream());
}

@Test
public void testFileChannel() throws Exception {
byte[] bytes = "abcdefghijklmnoprstuvwxyz".getBytes(StandardCharsets.ISO_8859_1);
Expand Down
Loading