Path: blob/master/test/jdk/java/nio/file/Files/walkFileTree/CreateFileTree.java
41155 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.file.*;24import java.io.IOException;25import java.util.*;2627/**28* Creates a file tree with possible cycles caused by symbolic links29* to ancestor directories.30*/3132public class CreateFileTree {3334private static final Random rand = new Random();3536private static boolean supportsLinks(Path dir) {37Path link = dir.resolve("testlink");38Path target = dir.resolve("testtarget");39try {40Files.createSymbolicLink(link, target);41Files.delete(link);42return true;43} catch (UnsupportedOperationException x) {44return false;45} catch (IOException x) {46return false;47}48}4950static Path create() throws IOException {51Path top = Files.createTempDirectory("tree");52List<Path> dirs = new ArrayList<Path>();5354// create tree55Queue<Path> queue = new ArrayDeque<Path>();56queue.add(top);57int total = 1 + rand.nextInt(20);58int n = 0;59Path dir;60while (((dir = queue.poll()) != null) && (n < total)) {61int r = Math.min((total-n), (1+rand.nextInt(3)));62for (int i=0; i<r; i++) {63String name = "dir" + (++n);64Path subdir = Files.createDirectory(dir.resolve(name));65queue.offer(subdir);66dirs.add(subdir);67}68}6970// create a few regular files in the file tree71int files = dirs.size() * 3;72for (int i=0; i<files; i++) {73String name = "file" + (i+1);74int x = rand.nextInt(dirs.size());75Files.createFile(dirs.get(x).resolve(name));76}7778// create a few sym links in the file tree so as to create cycles79if (supportsLinks(top)) {80int links = 1 + rand.nextInt(5);81for (int i=0; i<links; i++) {82int x = rand.nextInt(dirs.size());83int y;84do {85y = rand.nextInt(dirs.size());86} while (y != x);87String name = "link" + (i+1);88Path link = dirs.get(x).resolve(name);89Path target = dirs.get(y);90Files.createSymbolicLink(link, target);91}92}9394return top;95}9697public static void main(String[] args) throws IOException {98Path top = create();99System.out.println(top);100}101}102103104