-
Notifications
You must be signed in to change notification settings - Fork 1
/
parselist.cpp
executable file
·178 lines (154 loc) · 6.46 KB
/
parselist.cpp
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
#include <QDateTime>
#include <QStringList>
#include <QLocale>
#include "parselist.h"
void _q_fixupDateTime(QDateTime *dateTime)
{
// Adjust for future tolerance.
const int futureTolerance = 86400;
if (dateTime->secsTo(QDateTime::currentDateTime()) < -futureTolerance) {
QDate d = dateTime->date();
d.setDate(d.year() - 1, d.month(), d.day());
dateTime->setDate(d);
}
}
void _q_paseUnixDir(const QStringList &tokens, const QString &userName, QUrlInfo *info)
{
// Unix style, 7 + 1 entries
// -rw-r--r-- 1 ftp ftp 17358091 Aug 10 2004 qt-x11-free-3.3.3.tar.gz
// drwxr-xr-x 3 ftp ftp 4096 Apr 14 2000 compiled-examples
// lrwxrwxrwx 1 ftp ftp 9 Oct 29 2005 qtscape -> qtmozilla
if (tokens.size() != 8)
return;
char first = tokens.at(1).at(0).toLatin1();
if (first == 'd') {
info->setDir(true);
info->setFile(false);
info->setSymLink(false);
} else if (first == '-') {
info->setDir(false);
info->setFile(true);
info->setSymLink(false);
} else if (first == 'l') {
info->setDir(true);
info->setFile(false);
info->setSymLink(true);
}
// Resolve filename
QString name = tokens.at(7);
if (info->isSymLink()) {
int linkPos = name.indexOf(QLatin1String(" ->"));
if (linkPos != -1)
name.resize(linkPos);
}
info->setName(name);
// Resolve owner & group
info->setOwner(tokens.at(3));
info->setGroup(tokens.at(4));
// Resolve size
info->setSize(tokens.at(5).toLongLong());
QStringList formats;
formats << QLatin1String("MMM dd yyyy") << QLatin1String("MMM dd hh:mm") << QLatin1String("MMM d yyyy")
<< QLatin1String("MMM d hh:mm") << QLatin1String("MMM d yyyy") << QLatin1String("MMM dd yyyy");
QString dateString = tokens.at(6);
dateString[0] = dateString[0].toUpper();
// Resolve the modification date by parsing all possible formats
QDateTime dateTime;
int n = 0;
#ifndef QT_NO_DATESTRING
do {
dateTime = QLocale::c().toDateTime(dateString, formats.at(n++));
} while (n < formats.size() && (!dateTime.isValid()));
#endif
if (n == 2 || n == 4) {
// Guess the year.
dateTime.setDate(QDate(QDate::currentDate().year(),
dateTime.date().month(),
dateTime.date().day()));
_q_fixupDateTime(&dateTime);
}
if (dateTime.isValid())
info->setLastModified(dateTime);
// Resolve permissions
int permissions = 0;
QString p = tokens.at(2);
permissions |= (p[0] == QLatin1Char('r') ? QUrlInfo::ReadOwner : 0);
permissions |= (p[1] == QLatin1Char('w') ? QUrlInfo::WriteOwner : 0);
permissions |= (p[2] == QLatin1Char('x') ? QUrlInfo::ExeOwner : 0);
permissions |= (p[3] == QLatin1Char('r') ? QUrlInfo::ReadGroup : 0);
permissions |= (p[4] == QLatin1Char('w') ? QUrlInfo::WriteGroup : 0);
permissions |= (p[5] == QLatin1Char('x') ? QUrlInfo::ExeGroup : 0);
permissions |= (p[6] == QLatin1Char('r') ? QUrlInfo::ReadOther : 0);
permissions |= (p[7] == QLatin1Char('w') ? QUrlInfo::WriteOther : 0);
permissions |= (p[8] == QLatin1Char('x') ? QUrlInfo::ExeOther : 0);
info->setPermissions(permissions);
bool isOwner = info->owner() == userName;
info->setReadable((permissions & QUrlInfo::ReadOther) || ((permissions & QUrlInfo::ReadOwner) && isOwner));
info->setWritable((permissions & QUrlInfo::WriteOther) || ((permissions & QUrlInfo::WriteOwner) && isOwner));
}
void _q_parseDosDir(const QStringList &tokens, const QString &userName, QUrlInfo *info)
{
// DOS style, 3 + 1 entries
// 01-16-02 11:14AM <DIR> epsgroup
// 06-05-03 03:19PM 1973 readme.txt
if (tokens.size() != 4)
return;
Q_UNUSED(userName);
QString name = tokens.at(3);
info->setName(name);
info->setSymLink(name.toLower().endsWith(QLatin1String(".lnk")));
if (tokens.at(2) == QLatin1String("<DIR>")) {
info->setFile(false);
info->setDir(true);
} else {
info->setFile(true);
info->setDir(false);
info->setSize(tokens.at(2).toLongLong());
}
// Note: We cannot use QFileInfo; permissions are for the server-side
// machine, and QFileInfo's behavior depends on the local platform.
int permissions = QUrlInfo::ReadOwner | QUrlInfo::WriteOwner
| QUrlInfo::ReadGroup | QUrlInfo::WriteGroup
| QUrlInfo::ReadOther | QUrlInfo::WriteOther;
QString ext;
int extIndex = name.lastIndexOf(QLatin1Char('.'));
if (extIndex != -1)
ext = name.mid(extIndex + 1);
if (ext == QLatin1String("exe") || ext == QLatin1String("bat") || ext == QLatin1String("com"))
permissions |= QUrlInfo::ExeOwner | QUrlInfo::ExeGroup | QUrlInfo::ExeOther;
info->setPermissions(permissions);
info->setReadable(true);
info->setWritable(info->isFile());
QDateTime dateTime;
#ifndef QT_NO_DATESTRING
dateTime = QLocale::c().toDateTime(tokens.at(1), QLatin1String("MM-dd-yy hh:mmAP"));
if (dateTime.date().year() < 1971) {
dateTime.setDate(QDate(dateTime.date().year() + 100,
dateTime.date().month(),
dateTime.date().day()));
}
#endif
info->setLastModified(dateTime);
}
bool parseDir(const QByteArray &buffer, const QString &userName, QUrlInfo *info)
{
if (buffer.isEmpty())
return false;
QString bufferStr = QString::fromLatin1(buffer).trimmed();
// Unix style FTP servers
QRegExp unixPattern(QLatin1String("^([\\-dl])([a-zA-Z\\-]{9,9})\\s+\\d+\\s+(\\S*)\\s+"
"(\\S*)\\s+(\\d+)\\s+(\\S+\\s+\\S+\\s+\\S+)\\s+(\\S.*)"));
if (unixPattern.indexIn(bufferStr) == 0) {
_q_parseUnixDir(unixPattern.capturedTexts(), userName, info);
return true;
}
// DOS style FTP servers
QRegExp dosPattern(QLatin1String("^(\\d\\d-\\d\\d-\\d\\d\\ \\ \\d\\d:\\d\\d[AP]M)\\s+"
"(<DIR>|\\d+)\\s+(\\S.*)$"));
if (dosPattern.indexIn(bufferStr) == 0) {
_q_parseDosDir(dosPattern.capturedTexts(), userName, info);
return true;
}
// Unsupported
return false;
}