-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Expand file tree
/
Copy pathexport_output_file.cpp
More file actions
140 lines (123 loc) · 3.15 KB
/
Copy pathexport_output_file.cpp
File metadata and controls
140 lines (123 loc) · 3.15 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
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "export/output/export_output_file.h"
#include "export/output/export_output_result.h"
#include "export/output/export_output_stats.h"
#include "base/qt/qt_string_view.h"
#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <gsl/util>
namespace Export {
namespace Output {
File::File(const QString &path, Stats *stats) : _path(path), _stats(stats) {
}
int64 File::size() const {
return _offset;
}
bool File::empty() const {
return !_offset;
}
Result File::writeBlock(const QByteArray &block) {
const auto result = writeBlockAttempt(block);
if (!result) {
_file.reset();
}
return result;
}
Result File::writeBlockAttempt(const QByteArray &block) {
if (_stats && !_inStats) {
_inStats = true;
_stats->incrementFiles();
}
if (const auto result = reopen(); !result) {
return result;
}
const auto size = block.size();
if (!size) {
return Result::Success();
}
if (_file->write(block) == size && _file->flush()) {
_offset += size;
if (_stats) {
_stats->incrementBytes(size);
}
return Result::Success();
}
return error();
}
Result File::reopen() {
if (_file && _file->isOpen()) {
return Result::Success();
}
_file.emplace(_path);
if (_file->exists()) {
if (_file->size() < _offset) {
return fatalError();
} else if (!_file->resize(_offset)) {
return error();
}
} else if (_offset > 0) {
return fatalError();
}
if (_file->open(QIODevice::Append)) {
return Result::Success();
}
const auto info = QFileInfo(_path);
const auto dir = info.absoluteDir();
return (!dir.exists()
&& dir.mkpath(dir.absolutePath())
&& _file->open(QIODevice::Append))
? Result::Success()
: error();
}
Result File::error() const {
return Result(Result::Type::Error, _path);
}
Result File::fatalError() const {
return Result(Result::Type::FatalError, _path);
}
QString File::PrepareRelativePath(
const QString &folder,
const QString &suggested) {
if (!QFile::exists(folder + suggested)) {
return suggested;
}
// Not lastIndexOf('.') so that "file.tar.xz" won't be messed up.
const auto position = suggested.indexOf('.');
const auto base = suggested.mid(0, position);
const auto extension = (position >= 0)
? base::StringViewMid(suggested, position)
: QStringView();
const auto relativePart = [&](int attempt) {
auto result = base + QString(" (%1)").arg(attempt);
result.append(extension);
return result;
};
auto attempt = 0;
while (true) {
const auto relativePath = relativePart(++attempt);
if (!QFile::exists(folder + relativePath)) {
return relativePath;
}
}
}
Result File::Copy(
const QString &source,
const QString &path,
Stats *stats) {
QFile f(source);
if (!f.exists() || !f.open(QIODevice::ReadOnly)) {
return Result(Result::Type::FatalError, source);
}
const auto bytes = f.readAll();
if (bytes.size() != f.size()) {
return Result(Result::Type::FatalError, source);
}
return File(path, stats).writeBlock(bytes);
}
} // namespace Output
} // namespace File