Skip to content

Commit 4ddc11c

Browse files
author
Gary Gregory
committed
Use NIO to create sum links but keep OS commands as a back up.
1 parent 32646f3 commit 4ddc11c

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public long length() {
154154
* List files recursively
155155
*/
156156
private static final ListDirectoryWalker LIST_WALKER = new ListDirectoryWalker();
157+
157158
@TempDir
158159
public File temporaryFolder;
159160

@@ -184,16 +185,38 @@ private void consumeRemaining(final Iterator<File> iterator) {
184185
}
185186

186187
private void createCircularSymLink(final File file) throws IOException {
187-
if (!FilenameUtils.isSystemWindows()) {
188-
Runtime.getRuntime()
189-
.exec("ln -s " + file + "/.. " + file + "/cycle");
190-
} else {
188+
assertTrue(file.exists());
189+
final String linkName = file + "/cycle";
190+
final String targetName = file + "/..";
191+
assertTrue(file.exists());
192+
final Path linkPath = Paths.get(linkName);
193+
assertFalse(Files.exists(linkPath));
194+
final Path targetPath = Paths.get(targetName);
195+
assertTrue(Files.exists(targetPath));
196+
try {
197+
// May throw java.nio.file.FileSystemException: C:\Users\...\FileUtilsTestCase\cycle: A required privilege is not held by the client.
198+
// On Windows, you are fine if you run a terminal with admin karma.
199+
Files.createSymbolicLink(linkPath, targetPath);
200+
} catch (final UnsupportedOperationException e) {
201+
e.printStackTrace();
202+
createCircularOsSymLink(linkName, targetName);
203+
}
204+
// Sanity check:
205+
assertTrue(Files.isSymbolicLink(linkPath), () -> "Expected a sym link here: " + linkName);
206+
}
207+
208+
private void createCircularOsSymLink(final String linkName, final String targetName) throws IOException {
209+
if (FilenameUtils.isSystemWindows()) {
210+
// Windows
191211
try {
192-
Runtime.getRuntime()
193-
.exec("mklink /D " + file + "/cycle" + file + "/.. ");
194-
} catch (final IOException ioe) { // So that tests run in FAT filesystems
195-
//don't fail
212+
Runtime.getRuntime().exec("mklink /D " + linkName + " " + targetName);
213+
} catch (final IOException ioe) {
214+
// So that tests run in FAT filesystems don't fail
215+
ioe.printStackTrace();
196216
}
217+
} else {
218+
// Not Windows, assume Linux
219+
Runtime.getRuntime().exec("ln -s " + targetName + " " + linkName);
197220
}
198221
}
199222

@@ -814,10 +837,8 @@ public void testCopyDirectoryToChild() throws Exception {
814837
final File childDir = new File(parentDir, "child");
815838
createFilesForTestCopyDirectory(grandParentDir, parentDir, childDir);
816839

817-
final long expectedCount = LIST_WALKER.list(grandParentDir).size() +
818-
LIST_WALKER.list(parentDir).size();
819-
final long expectedSize = FileUtils.sizeOfDirectory(grandParentDir) +
820-
FileUtils.sizeOfDirectory(parentDir);
840+
final long expectedCount = LIST_WALKER.list(grandParentDir).size() + LIST_WALKER.list(parentDir).size();
841+
final long expectedSize = FileUtils.sizeOfDirectory(grandParentDir) + FileUtils.sizeOfDirectory(parentDir);
821842
FileUtils.copyDirectory(parentDir, childDir);
822843
assertEquals(expectedCount, LIST_WALKER.list(grandParentDir).size());
823844
assertEquals(expectedSize, FileUtils.sizeOfDirectory(grandParentDir));
@@ -2538,7 +2559,7 @@ public void testSizeOfDirectoryAsBigInteger() throws Exception {
25382559
file.delete();
25392560
file.mkdir();
25402561

2541-
this.createCircularSymLink(file);
2562+
createCircularSymLink(file);
25422563

25432564
assertEquals(TEST_DIRECTORY_SIZE_BI, FileUtils.sizeOfDirectoryAsBigInteger(file), "Unexpected directory size");
25442565

0 commit comments

Comments
 (0)