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