-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
164 lines (146 loc) · 4.41 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace FileMeddler
{
class Program
{
private const int kRetryMilliseconds = 50;
private const int kLockMilliseconds = 200;
private const int kGiveUpMilliseconds = 5000;
private static readonly HashSet<string> s_extensionsToIgnore = new HashSet<string> { ".exe", ".dll", ".ini", ".pdb" };
// private static ConcurrentDictionary<string,int> _lockedFiles = new ConcurrentDictionary<string, byte>();
private static readonly HashSet<string> s_filesInProcess = new HashSet<string>();
private static string s_root = "";
private static void Main(string[] args)
{
var consoleColor = Console.ForegroundColor;
var filter = "*.*";
if (args.Length == 1)
filter = args[0];
s_root = Directory.GetCurrentDirectory();
var watcher = new FileSystemWatcher()
{
Path = s_root,
Filter = filter
};
watcher.Created += SomethingHappened;
watcher.Changed += SomethingHappened;
watcher.Renamed += SomethingHappened;
watcher.Deleted += SomethingHappened;
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Ready to meddle. Press Enter to stop.");
Console.ReadLine();
Console.ForegroundColor = consoleColor; // return text to the original color
}
private static void SomethingHappened(object sender, FileSystemEventArgs e)
{
// if a new directory was created or renamed, camp on all of its files
if (Directory.Exists(e.FullPath))
{
try
{
foreach (var f in Directory.GetFiles(e.FullPath))
{
var x = new Thread(() => CampOnFile(f, e.ChangeType));
x.Start();
}
}
catch (Exception err)
{
Print(ConsoleColor.Red, " Error trying to camp on all files in: " + e.FullPath);
Print(ConsoleColor.Red, " " + err.Message);
}
return;
}
//the FileWatcher won't give us a new one until
//we return. Since timing is the whole point here,
//we spawn a thread to try and grab that file and return quickly.
var t = new Thread(() => CampOnFile(e.FullPath, e.ChangeType));
t.Start();
}
private static void CampOnFile(string fullPath, WatcherChangeTypes changeType)
{
var filename = Path.GetFileName(fullPath);
var extension = Path.GetExtension(filename);
if (s_extensionsToIgnore.Contains(extension.ToLowerInvariant()))
return;
if (s_filesInProcess.Contains(fullPath))
{
//Print(ConsoleColor.Gray, " Already processing: " + filename);
return;
}
else
{
s_filesInProcess.Add(fullPath);
}
var startTime = DateTime.Now.AddMilliseconds(kGiveUpMilliseconds);
var reportedWaiting = false;
var relativePath = fullPath.Replace(s_root, "") + " ";
switch (changeType)
{
case WatcherChangeTypes.Created:
Print(ConsoleColor.DarkMagenta, "Creation: " + relativePath);
break;
case WatcherChangeTypes.Deleted:
Print(ConsoleColor.DarkRed, "Deletion: " + relativePath);
s_filesInProcess.Remove(fullPath);
return;
case WatcherChangeTypes.Changed:
Print(ConsoleColor.Cyan, "Modified: " + relativePath);
break;
case WatcherChangeTypes.Renamed:
Print(ConsoleColor.DarkCyan, "Renamed: " + relativePath);
break;
default:
throw new ArgumentOutOfRangeException();
}
do
{
try
{
using(File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.None))
{
Print(ConsoleColor.Yellow, " Locking: " + filename);
Thread.Sleep(kLockMilliseconds);
}
Print(ConsoleColor.Green, " Released: " + filename);
s_filesInProcess.Remove(fullPath);
return;
}
catch(FileNotFoundException)
{
Print(ConsoleColor.DarkGreen, " File gone: " + filename);
s_filesInProcess.Remove(fullPath);
return;
}
catch(Exception error)
{
if(DateTime.Now > startTime)
{
Print(ConsoleColor.Red, " Giving up waiting for: " + filename);
Print(ConsoleColor.Red, error.Message);
s_filesInProcess.Remove(fullPath);
return;
}
if(!reportedWaiting)
{
Print(ConsoleColor.Magenta, " Waiting to acquire: " + filename);
}
reportedWaiting = true;
Thread.Sleep(kRetryMilliseconds);
}
} while(true);
}
private static void Print(ConsoleColor color, string message)
{
lock(Console.Out)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
}
}
}
}