Edit displayed text.
[beagle.git] / tools / ExerciseFileSystem.cs
blobb4d42e97a623c8b115a4565d9d72db7456e91850
1 //
2 // ExerciseFileSystem.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
30 using System.Threading;
32 using CommandLineFu;
33 using Beagle.Util;
35 class ExerciseFileSystemTool {
37 [Option (LongName="root")]
38 static string root = null;
40 [Option (LongName="file-source")]
41 static string file_source = null;
43 [Option (LongName="max-files")]
44 static int max_files = 20;
46 [Option (LongName="max-directories")]
47 static int max_directories = 5;
49 [Option (LongName="depth")]
50 static int max_depth = 4;
52 [Option (LongName="action-count")]
53 static int action_count = 0;
55 [Option (LongName="max-interval")]
56 static int max_interval = 1000;
58 static Random random = new Random ();
60 static ArrayList all_directories = new ArrayList ();
61 static ArrayList all_files = new ArrayList ();
63 static ArrayList available_source_files = null;
64 static string GetRandomSourceFile ()
66 if (file_source == null)
67 return null;
68 if (available_source_files == null) {
69 available_source_files = new ArrayList ();
70 DirectoryInfo dir = new DirectoryInfo (file_source);
71 foreach (FileInfo file in dir.GetFiles ()) {
72 if (file.Name.StartsWith (".")
73 || file.Name.EndsWith ("~")
74 || (file.Name.StartsWith ("#") && file.Name.EndsWith ("#")))
75 continue;
76 available_source_files.Add (file.FullName);
79 if (available_source_files.Count == 0)
80 return null;
82 return (string) available_source_files [random.Next (available_source_files.Count)];
85 static int name_counter = 0;
86 static string GetRandomName ()
88 ++name_counter;
89 return name_counter.ToString ();
92 static string CreateFile (string directory)
94 string source_file = GetRandomSourceFile ();
95 string new_file = Path.Combine (directory, GetRandomName ());
97 if (source_file != null) {
98 new_file += Path.GetExtension (source_file);
99 File.Copy (source_file, new_file);
100 } else {
101 File.Create (new_file).Close ();
104 all_files.Add (new_file);
105 return new_file;
108 static void CreateDirectories (string parent, int depth)
110 string path = Path.Combine (parent, GetRandomName ());
111 Directory.CreateDirectory (path);
113 all_directories.Add (path);
115 int n_files = random.Next (max_files+1);
116 for (int i = 0; i < n_files; ++i)
117 CreateFile (path);
119 if (depth < max_depth) {
120 int n_dirs = random.Next (max_directories+1);
121 if (depth == 0 && n_dirs == 0)
122 n_dirs = 1;
123 for (int i = 0; i < n_dirs; ++i)
124 CreateDirectories (path, depth+1);
128 static void DoSomething ()
130 if (all_directories.Count == 0) {
131 CreateDirectories (root, 0);
132 return;
135 int random_dir_i = random.Next (all_directories.Count);
136 int random_file_i = random.Next (all_files.Count);
138 string random_dir = (string) all_directories [random_dir_i];
139 string random_file = (string) all_files [random_file_i];
141 int action = 0;
142 action = random.Next (4);
143 switch (action) {
145 case 0: // Create new file
146 string new_file = CreateFile (random_dir);
147 Console.WriteLine ("Created {0}", new_file);
148 break;
150 case 1: // Delete a file
151 File.Delete (random_file);
152 all_files.RemoveAt (random_file_i);
153 Console.WriteLine ("Deleted {0}", random_file);
154 break;
156 case 2: // Create new subdirectory
157 string path = Path.Combine (random_dir, GetRandomName ());
158 Directory.CreateDirectory (path);
159 all_directories.Add (path);
160 Console.WriteLine ("Created subdirectory {0}", path);
161 break;
163 case 3: // Recursively delete a subdirectory
164 break;
169 static void Main (string [] args)
171 CommandLine.ProgramName = "beagle-exercise-file-system";
172 CommandLine.ProgramCopyright = "Copyright (C) 2005 Novell, Inc.";
173 args = CommandLine.Process (typeof (ExerciseFileSystemTool), args);
174 if (args == null)
175 return;
177 if (root == null)
178 root = Environment.GetEnvironmentVariable ("PWD");
181 if (Directory.Exists (Path.Combine (root, "1")))
182 Directory.Delete (Path.Combine (root, "1"), true);
183 CreateDirectories (root, 0);
185 Console.WriteLine ("Created {0} files across {1} directories",
186 all_files.Count, all_directories.Count);
188 for (int i = 0; i < action_count; ++i) {
189 DoSomething ();
190 int interval = random.Next (max_interval);
191 if (interval > 0)
192 Thread.Sleep (interval);