Bye-bye googledriver.
[beagle.git] / bludgeon / TreeBuilder.cs
blob4463b8463a31bc948d881559b73a9192fd44139d
2 using System;
3 using System.Collections;
4 using Mono.Unix.Native;
6 namespace Bludgeon {
8 static public class TreeBuilder {
10 static string [] possible_extensions = new string [] { ".gz", ".bz2", ".tar", ".zip" };
12 static public FileObject NewFile (int n_directories,
13 int n_files,
14 string extension,
15 double p_archive,
16 double archive_decay,
17 Random random)
19 if (random == null)
20 random = new Random ();
22 n_directories = (int) Math.Floor (n_directories * archive_decay);
23 n_files = (int) Math.Floor (n_files * archive_decay);
25 if (n_files == 0 || extension == ".txt" || (extension == null && random.NextDouble () > p_archive))
26 return new TextFileObject ();
28 if (extension == null)
29 extension = possible_extensions [random.Next (possible_extensions.Length)];
31 switch (extension) {
32 case ".gz":
33 FileObject gzipped_file;
34 gzipped_file = NewFile (n_directories, n_files, null, p_archive, archive_decay, random);
35 return new GzipFileObject (gzipped_file);
37 case ".bz2":
38 FileObject bzip2ed_file;
39 bzip2ed_file = NewFile (n_directories, n_files, null, p_archive, archive_decay, random);
40 return new Bzip2FileObject (bzip2ed_file);
42 case ".tar":
43 DirectoryObject tar_root;
44 tar_root = new DirectoryObject ();
45 Build (tar_root, n_directories, n_files, p_archive, archive_decay, false, null);
46 return new TarFileObject (tar_root);
48 case ".zip":
49 DirectoryObject zip_root;
50 zip_root = new DirectoryObject ();
51 Build (zip_root, n_directories, n_files, p_archive, archive_decay, false, null);
52 return new ZipFileObject (zip_root);
56 throw new Exception ("Something terrible happened!");
59 static public void GetAllSubdirectories (DirectoryObject dir, ArrayList target)
61 target.Add (dir);
62 foreach (FileSystemObject child in dir.Children)
63 if (child is DirectoryObject)
64 GetAllSubdirectories ((DirectoryObject) child, target);
67 static public void Build (DirectoryObject root,
68 int n_directories,
69 int n_files,
70 double p_archive,
71 double archive_decay,
72 bool build_in_random_order,
73 EventTracker tracker)
75 //Log.Info ("BUILD {0} {1} {2}", n_directories, n_files, p_archive);
76 Random random;
77 random = new Random ();
79 // First, create the list of all of the directories we could
80 // put things in.
81 ArrayList all_dirs;
82 all_dirs = new ArrayList ();
83 GetAllSubdirectories (root, all_dirs);
85 int nd = n_directories, nf = n_files;
87 // Next, we construct the directories and files.
88 while (nd > 0 || nf > 0) {
90 // If we are not building in a random order,
91 // we create all of the directories first.
92 bool create_dir;
93 if (build_in_random_order)
94 create_dir = (random.Next (nd + nf) < nd);
95 else
96 create_dir = (nd > 0);
98 if (create_dir) {
100 DirectoryObject dir;
101 dir = new DirectoryObject ();
103 FileSystemObject parent;
104 parent = (FileSystemObject) all_dirs [random.Next (all_dirs.Count)];
105 parent.AddChild (dir, tracker);
106 all_dirs.Add (dir);
108 //Log.Spew ("dir {0}: {1}", n_directories - nd, dir.FullName);
109 --nd;
111 } else {
114 FileObject file;
115 file = NewFile (n_directories, n_files, null, p_archive, archive_decay, random);
117 FileSystemObject parent;
118 parent = (FileSystemObject) all_dirs [random.Next (all_dirs.Count)];
119 parent.AddChild (file, tracker);
121 #if false
122 // Commented out because it breaks queries
124 // 20% of the time make the file unwritable, which prevents us from
125 // being able to set extended attributes and makes us fall back to
126 // our sqlite store.
127 if (random.Next (5) == 0)
128 Syscall.chmod (file.FullName, (FilePermissions) 292); // 0444
129 #endif
131 //Log.Spew ("file {0}: {1}", n_files - nf, file.FullName);
132 --nf;