forked from codeicon/disParity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
211 lines (187 loc) · 6.33 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Diagnostics;
using disParity;
namespace disParity.CmdLine
{
public enum Command
{
None,
Update,
Recover,
Test,
Verify,
List,
Stats,
HashCheck,
Monitor,
Undelete
}
class Program
{
public static bool ignoreHidden = false;
static Int32 driveNum = -1;
static string shutdownMsg;
static Config config;
static void Main(string[] args)
{
string recoverDir = "";
Command cmd = Command.None;
bool verbose = false;
if (args.Length > 0) {
if (args[0].ToLower() == "update")
cmd = Command.Update;
else if (args[0].ToLower() == "verify") {
cmd = Command.Verify;
}
else if ((args.Length == 3) && args[0].ToLower() == "recover") {
cmd = Command.Recover;
if (!ReadDriveNum(args))
return;
recoverDir = args[2];
}
else if ((args.Length == 2) && args[0].ToLower() == "test") {
cmd = Command.Test;
if (!ReadDriveNum(args))
return;
}
else if (args[0].ToLower() == "list") {
cmd = Command.List;
if (args.Length == 2) {
if (!ReadDriveNum(args))
return;
}
}
else if (args[0].ToLower() == "stats")
cmd = Command.Stats;
else if (args[0].ToLower() == "hashcheck") {
cmd = Command.HashCheck;
if (args.Length == 2) {
if (!ReadDriveNum(args))
return;
}
}
else if (args[0].ToLower() == "monitor") {
verbose = true;
cmd = Command.Monitor;
}
else if (args[0].ToLower() == "undelete") {
cmd = Command.Undelete;
if (!ReadDriveNum(args))
return;
}
}
if (args.Length > 1 && (cmd == Command.Update || cmd == Command.Verify)) {
if (args[1].ToLower() == "-v")
verbose = true;
else {
PrintUsage();
return;
}
}
if (cmd == Command.None) {
PrintUsage();
return;
}
if (SingleInstance.AlreadyRunning()) {
Console.WriteLine("Another instance of disParity is currently running.");
return;
}
disParity.Version.DoUpgradeCheck(HandleNewVersionAvailable);
string appDataPath = Utils.AppDataFolder;
if (!Directory.Exists(appDataPath))
Directory.CreateDirectory(appDataPath);
string logPath = Path.Combine(appDataPath, "logs");
if (!Directory.Exists(logPath))
Directory.CreateDirectory(logPath);
string logFileName = Path.Combine(logPath, "disParity " + DateTime.Now.ToString("yy-MM-dd HH.mm.ss"));
LogFile.Open(logFileName, verbose);
LogFile.Write("Beginning \"{0}\" command at {1} on {2}\r\n", args[0].ToLower(),
DateTime.Now.ToShortTimeString(), DateTime.Today.ToLongDateString());
string ConfigPath = Path.Combine(appDataPath, "Config.xml");
try {
config = new Config(ConfigPath);
config.Load();
}
catch (Exception e) {
LogFile.Write("Could not load Config file: " + e.Message);
return;
}
try {
ParitySet set = new ParitySet(config, null); // <--- FIX ME, need to pass actual environment here
set.ReloadDrives();
switch (cmd) {
case Command.Update:
set.Update(true);
break;
case Command.Recover:
int successes;
int failures;
set.Recover(set.Drives[driveNum], recoverDir, out successes, out failures);
break;
case Command.HashCheck:
if (driveNum != -1)
set.HashCheck(set.Drives[driveNum]);
//else
// set.HashCheck();
break;
}
}
catch (Exception e) {
LogFile.Log("Fatal error encountered during {0}: {1}",
args[0].ToLower(), e.Message);
LogFile.Log("Stack trace: {0}", e.StackTrace);
}
finally {
if (!String.IsNullOrEmpty(shutdownMsg))
LogFile.Write(shutdownMsg);
LogFile.Close();
}
}
private static void HandleNewVersionAvailable(string newVersion)
{
shutdownMsg = "Note: Version " + newVersion +
" of disParity is now available for download from www.vilett.com/disParity/\r\n";
}
static bool ReadDriveNum(string[] args)
{
if (args.Length < 2) {
PrintUsage();
return false;
}
try {
driveNum = Convert.ToInt32(args[1]) - 1;
return true;
}
catch {
PrintUsage();
return false;
}
}
static void PrintUsage()
{
Console.WriteLine("disParity Snapshot Parity Utility Version " + Version.VersionString +
"\r\n\r\n" +
"Usage:\r\n\r\n" +
" disparity update [-v] Create or update parity to reflect latest file data\r\n" +
" since the last snapshot\r\n " +
" disparity recover [num] [dir] Recover drive [num] to directory [dir]\r\n" +
" disparity test [num] Simulate a recovery of drive [num]\r\n" +
" disparity verify [-v] Verify that all file data matches the\r\n" +
" parity data in the current snapshot\r\n" +
" disparity list [num] Output a list of all files currently\r\n" +
" protected. Specify optional drive [num]\r\n" +
" to restrict output to a single drive.\r\n" +
" disparity stats List file counts and total data size\r\n" +
" disparity hashcheck [num] Check hash of every file on drive [num]\r\n" +
" disparity undelete [num] Restores any deleted files on drive [num]\r\n" +
"\r\nSpecify optional -v to enable verbose logging.");
}
}
}