3 using System
.Collections
;
4 using Mono
.Unix
.Native
;
8 static public class TreeBuilder
{
10 static string [] possible_extensions
= new string [] { ".gz", ".bz2", ".tar", ".zip" }
;
12 static public FileObject
NewFile (int n_directories
,
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
)];
33 FileObject gzipped_file
;
34 gzipped_file
= NewFile (n_directories
, n_files
, null, p_archive
, archive_decay
, random
);
35 return new GzipFileObject (gzipped_file
);
38 FileObject bzip2ed_file
;
39 bzip2ed_file
= NewFile (n_directories
, n_files
, null, p_archive
, archive_decay
, random
);
40 return new Bzip2FileObject (bzip2ed_file
);
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
);
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
)
62 foreach (FileSystemObject child
in dir
.Children
)
63 if (child
is DirectoryObject
)
64 GetAllSubdirectories ((DirectoryObject
) child
, target
);
67 static public void Build (DirectoryObject root
,
72 bool build_in_random_order
,
75 //Log.Info ("BUILD {0} {1} {2}", n_directories, n_files, p_archive);
77 random
= new Random ();
79 // First, create the list of all of the directories we could
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.
93 if (build_in_random_order
)
94 create_dir
= (random
.Next (nd
+ nf
) < nd
);
96 create_dir
= (nd
> 0);
101 dir
= new DirectoryObject ();
103 FileSystemObject parent
;
104 parent
= (FileSystemObject
) all_dirs
[random
.Next (all_dirs
.Count
)];
105 parent
.AddChild (dir
, tracker
);
108 //Log.Spew ("dir {0}: {1}", n_directories - nd, dir.FullName);
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
);
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
127 if (random
.Next (5) == 0)
128 Syscall
.chmod (file
.FullName
, (FilePermissions
) 292); // 0444
131 //Log.Spew ("file {0}: {1}", n_files - nf, file.FullName);