This repository has been archived by the owner on Nov 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Options.cs
158 lines (129 loc) · 4.49 KB
/
Options.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
using System;
using System.Collections.Generic;
using System.IO;
using Mono.Options;
using System.Linq;
using RawDiskLib;
namespace NtfsDir
{
public class Options
{
public string ErrorDetails { get; set; }
public ActionType ActionType { get; set; }
public SizeUnit SizeUnit { get; set; }
public bool ShowFileIds { get; set; }
public bool ShowAllNames { get; set; }
public bool ShowAllStreams { get; set; }
public char Drive { get; set; }
public string PathArgument { get; set; }
public PathType PathType { get; set; }
private OptionSet _options;
public Options()
{
_options = new OptionSet();
_options.Add("dir=", "Work with a directory", s =>
{
PathType = PathType.Directory;
PathArgument = s;
});
_options.Add("fileids", "Show MFT File Ids", s => ShowFileIds = true);
_options.Add("allnames", "Show all alternate names, such as 8.3 names", s => ShowAllNames = true);
_options.Add("streams", "Show all streams", s => ShowAllStreams = true);
_options.Add("bytes", "Print all sizes in Bytes", s => { SizeUnit = SizeUnit.Bytes; });
_options.Add("sectors", "Print all sizes in Sectors", s => { SizeUnit = SizeUnit.Sectors; });
_options.Add("clusters", "Print all sizes in Clusters", s => { SizeUnit = SizeUnit.Clusters; });
_options.Add("full", "Display full details", s => ActionType = ActionType.ShowFull);
_options.Add("help", "Display a help page", s => ActionType = ActionType.ShowHelp);
ErrorDetails = null;
}
public bool Parse(string[] args)
{
if (args == null || args.Length == 0)
{
ActionType = ActionType.ShowHelp;
return true;
}
List<string> extras;
try
{
extras = _options.Parse(args);
}
catch (Exception ex)
{
ErrorDetails = ex.Message;
return false;
}
if (ActionType == ActionType.ShowHelp)
return true;
// Validation
if (PathType == PathType.Unknown)
{
if (!extras.Any())
{
ErrorDetails = "Must specify a directory";
return false;
}
PathArgument = extras.First();
extras.RemoveAt(0);
PathType = PathType.Directory;
}
if (ActionType == ActionType.Unknown)
{
ActionType = ActionType.ShowFull;
}
if (ShowAllNames && ShowAllStreams)
{
ErrorDetails = "--allnames and --streams cannot be used together.";
return false;
}
// Cleaning
if (PathType == PathType.Directory)
{
Console.WriteLine(PathArgument);
if ((PathArgument[0] == '"' || PathArgument[0] == '\'') && PathArgument[0] == PathArgument.Last())
{
// Strip quotes
PathArgument = PathArgument.Substring(1, PathArgument.Length - 2);
}
// Fixup
if (!Path.IsPathRooted(PathArgument))
{
PathArgument = Path.Combine(Environment.CurrentDirectory, PathArgument);
}
}
if (!char.IsLetter(Drive))
{
Drive = PathArgument[0];
}
char[] volumes = Utils.GetAllAvailableVolumes().ToArray();
if (!volumes.Contains(Drive))
{
ErrorDetails = "The volume " + Drive + " does not exist.";
return false;
}
return true;
}
public void DisplayHelp()
{
Console.WriteLine("NtfsDir Help");
_options.WriteOptionDescriptions(Console.Out);
}
}
public enum ActionType
{
Unknown,
ShowHelp,
ShowFull
}
public enum PathType
{
Unknown,
Directory
}
public enum SizeUnit
{
Clusters,
Sectors,
Bytes
}
}