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 24 import java.nio.file.*; 25 import java.io.IOException; 26 import java.util.*; 27 28 /** 29 * Creates a file tree with possible cycles caused by symbolic links 30 * to ancestor directories. 31 */ 32 33 public class CreateFileTree { 34 35 static final Random rand = new Random(); 36 37 public static Path createTemporaryDirectory() throws IOException { 38 Path tmpdir = Paths.get(System.getProperty("java.io.tmpdir")); 39 Path dir; 40 do { 41 dir = tmpdir.resolve("name" + rand.nextInt()); 42 } while (dir.exists()); 43 dir.createDirectory(); 44 return dir; 45 } 46 47 public static void main(String[] args) throws IOException { 48 Path top = createTemporaryDirectory(); 49 if (!top.isAbsolute()) 50 top = top.toAbsolutePath(); 51 52 List<Path> dirs = new ArrayList<Path>(); 53 54 // create tree 55 Queue<Path> queue = new ArrayDeque<Path>(); 56 queue.add(top); 57 int total = 1 + rand.nextInt(20); 58 int n = 0; 59 Path dir; 60 while (((dir = queue.poll()) != null) && (n < total)) { 61 int r = Math.min((total-n), (1+rand.nextInt(3))); 62 for (int i=0; i<r; i++) { 63 String name = "dir" + (++n); 64 Path subdir = dir.resolve(name).createDirectory(); 65 queue.offer(subdir); 66 dirs.add(subdir); 67 } 68 } 69 assert dirs.size() >= 2; 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 dirs.get(x).resolve(name).createFile(); 77 } 78 79 // create a few sym links in the file tree so as to create cycles 80 int links = 1 + rand.nextInt(5); 81 for (int i=0; i<links; i++) { 82 int x = rand.nextInt(dirs.size()); 83 int y; 84 do { 85 y = rand.nextInt(dirs.size()); 86 } while (y != x); 87 String name = "link" + (i+1); 88 Path link = dirs.get(x).resolve(name); 89 Path target = dirs.get(y); 90 link.createSymbolicLink(target); 91 } 92 93 // done 94 System.out.println(top); 95 } 96 } | 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 24 import java.nio.file.*; 25 import java.io.IOException; 26 import java.util.*; 27 28 /** 29 * Creates a file tree with possible cycles caused by symbolic links 30 * to ancestor directories. 31 */ 32 33 public class CreateFileTree { 34 35 static final Random rand = new Random(); 36 37 public static void main(String[] args) throws IOException { 38 Path top = Files.createTempDirectory("tree"); 39 List<Path> dirs = new ArrayList<Path>(); 40 41 // create tree 42 Queue<Path> queue = new ArrayDeque<Path>(); 43 queue.add(top); 44 int total = 1 + rand.nextInt(20); 45 int n = 0; 46 Path dir; 47 while (((dir = queue.poll()) != null) && (n < total)) { 48 int r = Math.min((total-n), (1+rand.nextInt(3))); 49 for (int i=0; i<r; i++) { 50 String name = "dir" + (++n); 51 Path subdir = Files.createDirectory(dir.resolve(name)); 52 queue.offer(subdir); 53 dirs.add(subdir); 54 } 55 } 56 assert dirs.size() >= 2; 57 58 // create a few regular files in the file tree 59 int files = dirs.size() * 3; 60 for (int i=0; i<files; i++) { 61 String name = "file" + (i+1); 62 int x = rand.nextInt(dirs.size()); 63 Files.createFile(dirs.get(x).resolve(name)); 64 } 65 66 // create a few sym links in the file tree so as to create cycles 67 int links = 1 + rand.nextInt(5); 68 for (int i=0; i<links; i++) { 69 int x = rand.nextInt(dirs.size()); 70 int y; 71 do { 72 y = rand.nextInt(dirs.size()); 73 } while (y != x); 74 String name = "link" + (i+1); 75 Path link = dirs.get(x).resolve(name); 76 Path target = dirs.get(y); 77 Files.createSymbolicLink(link, target); 78 } 79 80 // done 81 System.out.println(top); 82 } 83 } |