-
Notifications
You must be signed in to change notification settings - Fork 18
/
FtpDirectoryListEntry.cs
187 lines (172 loc) · 5.83 KB
/
FtpDirectoryListEntry.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace WebOne
{
/// <summary>
/// FTP directory list entry (an line of LIST command output)
/// </summary>
internal class FtpDirectoryListEntry
{
/// <summary>
/// Access control (e.g. "drwxr-xr-x")
/// </summary>
public string Permissions { get; private set; }
/// <summary>
/// Inode number
/// </summary>
public int Inode { get; private set; }
/// <summary>
/// File or directory owner (user name)
/// </summary>
public string OwnerUser { get; private set; }
/// <summary>
/// File or directory owner (group name)
/// </summary>
public string OwnerGroup { get; private set; }
/// <summary>
/// File size
/// </summary>
public long Size { get; private set; }
/// <summary>
/// File or directory change date
/// </summary>
public string Date { get; private set; }
/// <summary>
/// File or directory name
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Is the item a directory (true) or a file (false)
/// </summary>
public bool Directory { get; private set; }
/// <summary>
/// Is the item a symbolic link
/// </summary>
public bool SymLink { get; private set; }
public new string ToString()
{
if (Directory) return Name + " <DIR>";
return Name;
}
/// <summary>
/// Mask for LIST entries in UNIX format
/// </summary>
private const string UnixPattern =
@"^([\w-]+)\s+(\d+)\s+(\w+)\s+(\w+)\s+(\d+)\s+" +
@"(\w+\s+\d+\s+\d+|\w+\s+\d+\s+\d+:\d+)\s+(.+)$";
/// <summary>
/// Mask for LIST entries in DOS/WinNT format
/// </summary>
private const string DosPattern =
@"^(\d+-\d+-\d+\s+\d+:\d+(?:AM|PM))\s+(<DIR>|\d+)\s+(.+)$";
/// <summary>
/// Decode a LIST result line to <see cref="FtpDirectoryListEntry"/>
/// </summary>
/// <param name="Line">Raw line value</param>
/// <param name="Type">Line syntax (kind of FTP server)</param>
/// <returns></returns>
public static FtpDirectoryListEntry ParseLine(string Line, LineType Type)
{
switch (Type)
{
case LineType.UNIX:
return ParseUNIX(Line);
case LineType.DOS:
return ParseDOS(Line);
default:
throw new ArgumentOutOfRangeException(nameof(Type), "Unknown LIST result line type");
}
}
/// <summary>
/// Decode a UNIX-formatted line to <see cref="FtpDirectoryListEntry"/>
/// </summary>
/// <param name="Line">Raw line value (e.g. "drwxr-xr-x 3 oscollect 1011 4096 Nov 25 07:55 200 Best Games")</param>
private static FtpDirectoryListEntry ParseUNIX(string Line)
{
//thanks: https://stackoverflow.com/a/40045894/7600726
FtpDirectoryListEntry entry = new();
Regex regex = new (UnixPattern);
Match match = regex.Match(Line);
if (!match.Success) throw new ArgumentException("The LIST result line is not in UNIX format", nameof(Line));
entry.Permissions = match.Groups[1].Value;
entry.Inode = int.Parse(match.Groups[2].Value);
entry.OwnerUser = match.Groups[3].Value;
entry.OwnerGroup = match.Groups[4].Value;
entry.Size = long.Parse(match.Groups[5].Value);
entry.Date = match.Groups[6].Value;
entry.Name = match.Groups[7].Value;
entry.Directory = entry.Permissions.StartsWith("d");
entry.SymLink = entry.Permissions.StartsWith("l");
return entry;
}
/// <summary>
/// Decode a DOS-formatted line to <see cref="FtpDirectoryListEntry"/>
/// </summary>
/// <param name="Line">Raw line value (e.g. "06-25-09 02:41PM 144700153 image34.gif")</param>
private static FtpDirectoryListEntry ParseDOS(string Line)
{
//thanks: https://stackoverflow.com/a/39771146/7600726
FtpDirectoryListEntry entry = new();
Regex regex = new(DosPattern);
Match match = regex.Match(Line);
if (!match.Success) throw new ArgumentException("The LIST result line is not in DOS/WINNT format", nameof(Line));
entry.Date = match.Groups[1].Value;
entry.Directory = match.Groups[2].Value == "<DIR>";
if (!entry.Directory) entry.Size = long.Parse(match.Groups[2].Value);
else entry.Size = -1;
entry.Name = match.Groups[3].Value;
entry.SymLink = false; //DOS don't support symbolic links
return entry;
}
/// <summary>
/// Test if the line is in UNIX format
/// </summary>
/// <param name="Line">Raw line value (e.g. "drwxr-xr-x 3 oscollect 1011 4096 Nov 25 07:55 200 Best Games")</param>
/// <returns>true if it's UNIX, false if not</returns>
public static bool IsUnixLine(string Line)
{
Regex regex = new Regex(UnixPattern);
Match match = regex.Match(Line);
return match.Success;
}
/// <summary>
/// Test if the line is in DOS format
/// </summary>
/// <param name="Line">Raw line value (e.g. "drwxr-xr-x 3 oscollect 1011 4096 Nov 25 07:55 200 Best Games")</param>
/// <returns>true if it's DOS, false if not</returns>
public static bool IsDosLine(string Line)
{
Regex regex = new Regex(DosPattern);
Match match = regex.Match(Line);
return match.Success;
}
/// <summary>
/// Kinds of FTP LIST result line syntaxes
/// </summary>
public enum LineType
{
/// <summary>
/// <!--Auto-detect-->
/// </summary>
Unknown,
/// <summary>
/// d--x--x--x 2 ftp ftp 4096 Mar 07 2002 bin<br/>
/// -rw-r--r-- 1 ftp ftp 659450 Jun 15 05:07 TEST.TXT<br/>
/// -rw-r--r-- 1 ftp ftp 101786380 Sep 08 2008 TEST03-05.TXT<br/>
/// drwxrwxr-x 2 ftp ftp 4096 May 06 12:24 dropoff<br/>
/// </summary>
UNIX,
/// <summary>
/// 08-10-11 12:02PM <DIR> Version2<br/>
/// 06-25-09 02:41PM 144700153 image34.gif<br/>
/// 06-25-09 02:51PM 144700153 updates.txt<br/>
/// 11-04-10 02:45PM 144700214 digger.tif<br/>
/// </summary>
DOS
}
}
}