|
19 | 19 |
|
20 | 20 | import org.apache.lucene.mockfile.FilterFileSystemProvider; |
21 | 21 | import org.apache.lucene.mockfile.FilterPath; |
| 22 | +import org.apache.lucene.util.Constants; |
22 | 23 | import org.elasticsearch.common.CheckedConsumer; |
23 | 24 | import org.elasticsearch.common.io.PathUtils; |
24 | 25 | import org.elasticsearch.test.ESTestCase; |
|
27 | 28 | import java.io.IOException; |
28 | 29 | import java.io.OutputStream; |
29 | 30 | import java.net.URI; |
| 31 | +import java.nio.channels.FileChannel; |
30 | 32 | import java.nio.charset.StandardCharsets; |
31 | 33 | import java.nio.file.AccessDeniedException; |
32 | 34 | import java.nio.file.FileSystem; |
33 | 35 | import java.nio.file.Files; |
| 36 | +import java.nio.file.NoSuchFileException; |
| 37 | +import java.nio.file.OpenOption; |
34 | 38 | import java.nio.file.Path; |
| 39 | +import java.nio.file.attribute.FileAttribute; |
35 | 40 | import java.util.ArrayList; |
36 | 41 | import java.util.Arrays; |
37 | 42 | import java.util.List; |
| 43 | +import java.util.Objects; |
| 44 | +import java.util.Set; |
38 | 45 | import java.util.function.Function; |
39 | 46 |
|
40 | 47 | import static org.hamcrest.Matchers.arrayWithSize; |
@@ -214,6 +221,43 @@ public void testFsyncDirectory() throws Exception { |
214 | 221 | // no exception |
215 | 222 | } |
216 | 223 |
|
| 224 | + private static final class AccessDeniedWhileOpeningDirectoryFileSystem extends FilterFileSystemProvider { |
| 225 | + |
| 226 | + AccessDeniedWhileOpeningDirectoryFileSystem(final FileSystem delegate) { |
| 227 | + super("access_denied://", Objects.requireNonNull(delegate)); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public FileChannel newFileChannel( |
| 232 | + final Path path, |
| 233 | + final Set<? extends OpenOption> options, |
| 234 | + final FileAttribute<?>... attrs) throws IOException { |
| 235 | + if (Files.isDirectory(path)) { |
| 236 | + throw new AccessDeniedException(path.toString()); |
| 237 | + } |
| 238 | + return delegate.newFileChannel(path, options, attrs); |
| 239 | + } |
| 240 | + |
| 241 | + } |
| 242 | + |
| 243 | + public void testFsyncAccessDeniedOpeningDirectory() throws Exception { |
| 244 | + final Path path = createTempDir().toRealPath(); |
| 245 | + final FileSystem fs = new AccessDeniedWhileOpeningDirectoryFileSystem(path.getFileSystem()).getFileSystem(URI.create("file:///")); |
| 246 | + final Path wrapped = new FilterPath(path, fs); |
| 247 | + if (Constants.WINDOWS) { |
| 248 | + // no exception, we early return and do not even try to open the directory |
| 249 | + IOUtils.fsync(wrapped, true); |
| 250 | + } else { |
| 251 | + expectThrows(AccessDeniedException.class, () -> IOUtils.fsync(wrapped, true)); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + public void testFsyncNonExistentDirectory() throws Exception { |
| 256 | + final Path dir = FilterPath.unwrap(createTempDir()).toRealPath(); |
| 257 | + final Path nonExistentDir = dir.resolve("non-existent"); |
| 258 | + expectThrows(NoSuchFileException.class, () -> IOUtils.fsync(nonExistentDir, true)); |
| 259 | + } |
| 260 | + |
217 | 261 | public void testFsyncFile() throws IOException { |
218 | 262 | final Path path = createTempDir().toRealPath(); |
219 | 263 | final Path subPath = path.resolve(randomAlphaOfLength(8)); |
|
0 commit comments