CreateFileTree.java revision 9330:8b1f1c2a400f
1/* 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.nio.file.*; 25import java.io.IOException; 26import java.util.*; 27 28/** 29 * Creates a file tree with possible cycles caused by symbolic links 30 * to ancestor directories. 31 */ 32 33public class CreateFileTree { 34 35 private static final Random rand = new Random(); 36 37 private static boolean supportsLinks(Path dir) { 38 Path link = dir.resolve("testlink"); 39 Path target = dir.resolve("testtarget"); 40 try { 41 Files.createSymbolicLink(link, target); 42 Files.delete(link); 43 return true; 44 } catch (UnsupportedOperationException x) { 45 return false; 46 } catch (IOException x) { 47 return false; 48 } 49 } 50 51 static Path create() throws IOException { 52 Path top = Files.createTempDirectory("tree"); 53 List<Path> dirs = new ArrayList<Path>(); 54 55 // create tree 56 Queue<Path> queue = new ArrayDeque<Path>(); 57 queue.add(top); 58 int total = 1 + rand.nextInt(20); 59 int n = 0; 60 Path dir; 61 while (((dir = queue.poll()) != null) && (n < total)) { 62 int r = Math.min((total-n), (1+rand.nextInt(3))); 63 for (int i=0; i<r; i++) { 64 String name = "dir" + (++n); 65 Path subdir = Files.createDirectory(dir.resolve(name)); 66 queue.offer(subdir); 67 dirs.add(subdir); 68 } 69 } 70 71 // create a few regular files in the file tree 72 int files = dirs.size() * 3; 73 for (int i=0; i<files; i++) { 74 String name = "file" + (i+1); 75 int x = rand.nextInt(dirs.size()); 76 Files.createFile(dirs.get(x).resolve(name)); 77 } 78 79 // create a few sym links in the file tree so as to create cycles 80 if (supportsLinks(top)) { 81 int links = 1 + rand.nextInt(5); 82 for (int i=0; i<links; i++) { 83 int x = rand.nextInt(dirs.size()); 84 int y; 85 do { 86 y = rand.nextInt(dirs.size()); 87 } while (y != x); 88 String name = "link" + (i+1); 89 Path link = dirs.get(x).resolve(name); 90 Path target = dirs.get(y); 91 Files.createSymbolicLink(link, target); 92 } 93 } 94 95 return top; 96 } 97 98 public static void main(String[] args) throws IOException { 99 Path top = create(); 100 System.out.println(top); 101 } 102} 103