Repository navigation
Expand file tree
/
Copy pathfileinfo.cpp
More file actions
261 lines (236 loc) · 6.96 KB
/
Copy pathfileinfo.cpp
File metadata and controls
261 lines (236 loc) · 6.96 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "fileinfo.h"
#include <QDateTime>
#include <QRegularExpression>
#include <QStringList>
namespace {
// Qt 5.14 起 SplitBehavior 从 QString 移到了 Qt 命名空间。
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
const Qt::SplitBehavior kSkipEmptyParts = Qt::SkipEmptyParts;
#else
const QString::SplitBehavior kSkipEmptyParts = QString::SkipEmptyParts;
#endif
// POSIX 文件类型位。这里不直接依赖 libssh2/sys/stat,方便单元测试。
const unsigned long kTypeMask = 0170000UL;
const unsigned long kTypeDirectory = 0040000UL;
const unsigned long kTypeRegular = 0100000UL;
const unsigned long kTypeSymlink = 0120000UL;
const unsigned long kTypeFifo = 0010000UL;
const unsigned long kTypeSocket = 0140000UL;
const unsigned long kTypeCharDevice = 0020000UL;
const unsigned long kTypeBlockDevice = 0060000UL;
char typeCharForMode(unsigned long mode) {
switch (mode & kTypeMask) {
case kTypeDirectory:
return 'd';
case kTypeRegular:
return '-';
case kTypeSymlink:
return 'l';
case kTypeFifo:
return 'p';
case kTypeSocket:
return 's';
case kTypeCharDevice:
return 'c';
case kTypeBlockDevice:
return 'b';
default:
return '?';
}
}
int fileTypeForChar(QChar type) {
switch (type.toLatin1()) {
case 'd':
return RemoteFileDirectory;
case '-':
return RemoteFileRegular;
case 'l':
return RemoteFileSymlink;
case 'p':
return RemoteFileFifo;
case 's':
return RemoteFileSocket;
case 'c':
return RemoteFileCharDevice;
case 'b':
return RemoteFileBlockDevice;
default:
return RemoteFileUnknown;
}
}
bool isPermissionToken(const QString &token) {
if (token.size() < 10) {
return false;
}
static const QString types = QStringLiteral("d-lpscb");
static const QString flags = QStringLiteral("-rwxstST");
if (!types.contains(token.at(0))) {
return false;
}
for (int i = 1; i < 10; ++i) {
if (!flags.contains(token.at(i))) {
return false;
}
}
return true;
}
} // namespace
int fileTypeFromMode(unsigned long mode) {
switch (mode & kTypeMask) {
case kTypeDirectory:
return RemoteFileDirectory;
case kTypeRegular:
return RemoteFileRegular;
case kTypeSymlink:
return RemoteFileSymlink;
case kTypeFifo:
return RemoteFileFifo;
case kTypeSocket:
return RemoteFileSocket;
case kTypeCharDevice:
return RemoteFileCharDevice;
case kTypeBlockDevice:
return RemoteFileBlockDevice;
default:
return RemoteFileUnknown;
}
}
QString formatPermissions(unsigned long mode) {
QString result(10, QLatin1Char('-'));
result[0] = QLatin1Char(typeCharForMode(mode));
static const char rwx[] = "rwx";
for (int i = 0; i < 9; ++i) {
if (mode & (1UL << (8 - i))) {
result[i + 1] = QLatin1Char(rwx[i % 3]);
}
}
// setuid / setgid / sticky
if (mode & 04000UL) {
result[3] = QLatin1Char((mode & 0100UL) ? 's' : 'S');
}
if (mode & 02000UL) {
result[6] = QLatin1Char((mode & 0010UL) ? 's' : 'S');
}
if (mode & 01000UL) {
result[9] = QLatin1Char((mode & 0001UL) ? 't' : 'T');
}
return result;
}
QString formatFileSize(qint64 bytes) {
if (bytes < 0) {
return QStringLiteral("-");
}
static const char *units[] = {"B", "KB", "MB", "GB", "TB", "PB", "EB"};
double value = static_cast<double>(bytes);
int unit = 0;
while (value >= 1024.0 && unit < 6) {
value /= 1024.0;
++unit;
}
int precision = 0;
if (unit > 0) {
precision = value < 10.0 ? 2 : (value < 100.0 ? 1 : 0);
}
return QString::number(value, 'f', precision) + QLatin1Char(' ') +
QLatin1String(units[unit]);
}
QString normalizeRemotePath(const QString &path) {
const QString trimmed = path.trimmed();
if (trimmed.isEmpty()) {
return QStringLiteral("/");
}
const QStringList parts = trimmed.split(QLatin1Char('/'), kSkipEmptyParts);
QStringList normalized;
normalized.reserve(parts.size());
for (const QString &part : parts) {
if (part == QLatin1String(".")) {
continue;
}
if (part == QLatin1String("..")) {
if (!normalized.isEmpty()) {
normalized.removeLast();
}
continue;
}
normalized.append(part);
}
return QLatin1Char('/') + normalized.join(QLatin1Char('/'));
}
QString parentRemotePath(const QString &path) {
const QString normalized = normalizeRemotePath(path);
if (normalized == QLatin1String("/")) {
return normalized;
}
const int index = normalized.lastIndexOf(QLatin1Char('/'));
return index <= 0 ? QStringLiteral("/") : normalized.left(index);
}
QString joinRemotePath(const QString &dirPath, const QString &name) {
if (name.startsWith(QLatin1Char('/'))) {
return normalizeRemotePath(name);
}
const QString dir = normalizeRemotePath(dirPath);
if (dir == QLatin1String("/")) {
return QLatin1Char('/') + name;
}
return dir + QLatin1Char('/') + name;
}
bool parseLongEntry(const QString &longEntry, FileInfo_S *info) {
if (!info) {
return false;
}
const QString trimmed = longEntry.trimmed();
if (trimmed.isEmpty()) {
return false;
}
// 长格式形如:
// -rw-r--r-- 1 user group 1234 Jan 1 12:00 file.txt
// 只需要前四个字段;文件名可能包含空格,不能按下标取文件名。
const QStringList fields =
trimmed.split(QRegularExpression(QStringLiteral("\\s+")),
kSkipEmptyParts);
if (fields.size() < 5 || !isPermissionToken(fields.at(0))) {
return false;
}
const QString permission = fields.at(0).left(10);
const int type = fileTypeForChar(permission.at(0));
if (type == RemoteFileUnknown) {
return false;
}
info->fileType = type;
info->permission = permission;
info->fileNum = fields.at(1);
info->userOrGroup = fields.at(2) + QLatin1Char('/') + fields.at(3);
return true;
}
FileInfo_S makeFileInfo(const QString &dirPath, const QString &fileName,
qint64 size, qint64 mtimeSecs, unsigned long mode,
unsigned long uid, unsigned long gid,
const QString &longEntry) {
FileInfo_S info;
info.fileName = fileName;
info.filePath = joinRemotePath(dirPath, fileName);
info.fileSize = size;
info.updateTime =
mtimeSecs > 0
? QDateTime::fromSecsSinceEpoch(mtimeSecs)
.toString(QStringLiteral("yyyy-MM-dd hh:mm:ss"))
: QStringLiteral("-");
FileInfo_S parsed;
const bool hasLongEntry = parseLongEntry(longEntry, &parsed);
if (hasLongEntry) {
info.fileNum = parsed.fileNum;
info.userOrGroup = parsed.userOrGroup;
} else {
info.userOrGroup =
QString::number(uid) + QLatin1Char('/') + QString::number(gid);
}
// attrs 是权威数据;服务器没有提供权限位时才使用 longname。
if (mode != 0) {
info.fileType = fileTypeFromMode(mode);
info.permission = formatPermissions(mode);
} else if (hasLongEntry) {
info.fileType = parsed.fileType;
info.permission = parsed.permission;
}
return info;
}