|
16 | 16 |
|
17 | 17 | package com.google.common.io; |
18 | 18 |
|
| 19 | +import static com.google.common.io.Files.deleteDirectoryContents; |
| 20 | +import static com.google.common.io.Files.deleteRecursively; |
| 21 | +import static com.google.common.io.Files.touch; |
19 | 22 | import static com.google.common.truth.Truth.assertThat; |
20 | 23 | import static java.nio.charset.StandardCharsets.US_ASCII; |
21 | 24 | import static java.nio.charset.StandardCharsets.UTF_16LE; |
|
36 | 39 | import java.nio.ByteBuffer; |
37 | 40 | import java.nio.MappedByteBuffer; |
38 | 41 | import java.nio.channels.FileChannel.MapMode; |
| 42 | +import java.nio.file.Path; |
| 43 | +import java.nio.file.attribute.PosixFilePermission; |
39 | 44 | import java.util.ArrayList; |
| 45 | +import java.util.EnumSet; |
40 | 46 | import java.util.List; |
41 | 47 | import java.util.Random; |
| 48 | +import java.util.Set; |
42 | 49 | import junit.framework.TestSuite; |
43 | 50 | import org.jspecify.annotations.NullUnmarked; |
44 | 51 |
|
@@ -407,6 +414,27 @@ public boolean delete() { |
407 | 414 | private static final long serialVersionUID = 0; |
408 | 415 | } |
409 | 416 |
|
| 417 | + public void testDeleteRealUnreadableDirectory() throws Exception { |
| 418 | + File tempDir = createTempDir(); |
| 419 | + File foo = file(tempDir, "foo"); |
| 420 | + touch(foo); |
| 421 | + Path tempDirPath = tempDir.toPath(); |
| 422 | + Set<PosixFilePermission> origPermissions = |
| 423 | + java.nio.file.Files.getPosixFilePermissions(tempDirPath); |
| 424 | + java.nio.file.Files.setPosixFilePermissions( |
| 425 | + tempDirPath, EnumSet.of(PosixFilePermission.OWNER_WRITE)); |
| 426 | + |
| 427 | + assertThrows(IOException.class, () -> deleteDirectoryContents(tempDir)); |
| 428 | + |
| 429 | + assertThrows(IOException.class, () -> deleteRecursively(tempDir)); |
| 430 | + |
| 431 | + java.nio.file.Files.setPosixFilePermissions(tempDirPath, origPermissions); |
| 432 | + assertTrue(foo.exists()); |
| 433 | + |
| 434 | + // Clean it up |
| 435 | + deleteRecursively(tempDir); |
| 436 | + } |
| 437 | + |
410 | 438 | public void testLineReading() throws IOException { |
411 | 439 | File temp = createTempFile(); |
412 | 440 | assertThat(Files.readFirstLine(temp, UTF_8)).isNull(); |
|
0 commit comments