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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
#define ENABLE_IMPLICIT_FILEPATH_CONVERSIONS
#include "queueloader.h"
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <libgen.h>
#include <unistd.h>
#include "config.h"
#include "configcontainer.h"
#include "logger.h"
#include "strprintf.h"
#include "utils.h"
using namespace newsboat;
namespace podboat {
QueueLoader::QueueLoader(const Filepath& filepath,
const ConfigContainer& cfg_,
std::function<void()> cb_require_view_update_)
: queuefile(filepath)
, cfg(cfg_)
, cb_require_view_update(cb_require_view_update_)
{
}
void QueueLoader::reload(std::vector<Download>& downloads,
bool also_remove_finished) const
{
CategorizedDownloads categorized_downloads;
const auto res = categorize_downloads(downloads, also_remove_finished);
if (!res.has_value()) {
return;
}
categorized_downloads = res.value();
update_from_queue_file(categorized_downloads);
write_queue_file(categorized_downloads);
if (cfg.get_configvalue_as_bool("delete-played-files")) {
delete_played_files(categorized_downloads);
}
downloads = std::move(categorized_downloads.to_keep);
}
nonstd::optional<QueueLoader::CategorizedDownloads> QueueLoader::categorize_downloads(
const std::vector<Download>& downloads,
bool also_remove_finished)
{
CategorizedDownloads result;
for (const auto& dl : downloads) {
// we are not allowed to reload if a download is in progress!
if (dl.status() == DlStatus::DOWNLOADING) {
LOG(Level::INFO,
"QueueLoader::reload: aborting reload due to "
"DlStatus::DOWNLOADING status");
return nonstd::nullopt;
}
bool keep_entry = false;
switch (dl.status()) {
case DlStatus::QUEUED:
case DlStatus::CANCELLED:
case DlStatus::FAILED:
case DlStatus::MISSING:
case DlStatus::READY:
case DlStatus::PLAYED:
case DlStatus::RENAME_FAILED:
LOG(Level::DEBUG,
"QueueLoader::reload: storing %s to new vector",
dl.url());
keep_entry = true;
break;
case DlStatus::FINISHED:
if (!also_remove_finished) {
LOG(Level::DEBUG,
"QueueLoader::reload: storing %s to new vector",
dl.url());
keep_entry = true;
}
break;
case DlStatus::DELETED:
keep_entry = false;
break;
case DlStatus::DOWNLOADING:
assert(!"Can't be reached because of the `if` above");
break;
}
if (keep_entry) {
result.to_keep.push_back(dl);
} else {
result.to_delete.push_back(dl);
}
}
return result;
}
void QueueLoader::update_from_queue_file(CategorizedDownloads& downloads) const
{
std::fstream f(queuefile, std::fstream::in);
if (!f.is_open()) {
return;
}
bool comments_ignored = false;
for (std::string line; std::getline(f, line); ) {
if (line.empty()) {
continue;
}
LOG(Level::DEBUG,
"QueueLoader::reload: loaded `%s' from queue file",
line);
const std::vector<std::string> fields = utils::tokenize_quoted(line);
bool url_found = false;
if (fields.empty()) {
if (!comments_ignored) {
std::cout << strprintf::fmt(
_("WARNING: Comment found "
"in %s. The queue file is regenerated "
"when Podboat exits and comments will "
"be deleted. Press Enter to continue or "
"Ctrl+C to abort"),
queuefile)
<< std::endl;
std::cin.ignore();
comments_ignored = true;
}
continue;
}
for (const auto& dl : downloads.to_keep) {
if (fields[0] == dl.url()) {
LOG(Level::INFO,
"QueueLoader::reload: found `%s' in old vector",
fields[0]);
url_found = true;
break;
}
}
for (const auto& dl : downloads.to_delete) {
if (fields[0] == dl.url()) {
LOG(Level::INFO,
"QueueLoader::reload: found `%s' in scheduled for deletion vector",
fields[0]);
url_found = true;
break;
}
}
if (url_found) {
continue;
}
LOG(Level::INFO,
"QueueLoader::reload: found `%s' nowhere -> storing to new vector",
line);
Download d(cb_require_view_update);
std::string fn;
if (fields.size() == 1) {
fn = get_filename(fields[0]);
} else {
fn = fields[1];
}
d.set_filename(fn);
if (fields.size() >= 3) {
if (fields[2] == "missing") {
d.set_status(DlStatus::MISSING);
}
if (fields[2] == "downloaded") {
d.set_status(DlStatus::READY);
}
if (fields[2] == "played") {
d.set_status(DlStatus::PLAYED);
}
if (fields[2] == "finished") {
d.set_status(DlStatus::FINISHED);
}
}
if (access(fn.c_str(), F_OK) == 0) {
LOG(Level::INFO,
"QueueLoader::reload: found `%s' on file system -> mark as already downloaded",
fn);
if (d.status() == DlStatus::QUEUED || d.status() == DlStatus::MISSING) {
d.set_status(DlStatus::READY);
}
} else if (access((fn + ConfigContainer::PARTIAL_FILE_SUFFIX).c_str(),
F_OK) == 0) {
LOG(Level::INFO,
"QueueLoader::reload: found `%s' on file system -> mark as partially downloaded",
fn + ConfigContainer::PARTIAL_FILE_SUFFIX);
d.set_status(DlStatus::FAILED);
} else {
if (d.status() != DlStatus::QUEUED) {
d.set_status(DlStatus::MISSING);
}
}
d.set_url(fields[0]);
downloads.to_keep.push_back(d);
}
}
void QueueLoader::write_queue_file(const CategorizedDownloads& downloads) const
{
std::fstream f(queuefile, std::fstream::out);
if (!f.is_open()) {
return;
}
for (const auto& dl : downloads.to_keep) {
f << dl.url() << " " << utils::quote(dl.filename());
switch (dl.status()) {
case DlStatus::READY:
f << " downloaded";
break;
case DlStatus::PLAYED:
f << " played";
break;
case DlStatus::FINISHED:
f << " finished";
break;
case DlStatus::MISSING:
f << " missing";
break;
// The following statuses have no marks in the queue file.
case DlStatus::QUEUED:
case DlStatus::DOWNLOADING:
case DlStatus::CANCELLED:
case DlStatus::DELETED:
case DlStatus::FAILED:
case DlStatus::RENAME_FAILED:
break;
}
f << std::endl;
}
}
void QueueLoader::delete_played_files(const CategorizedDownloads& downloads)
const
{
for (const auto& dl : downloads.to_delete) {
const Filepath filename = dl.filename();
LOG(Level::INFO, "Deleting file %s", filename);
if (std::remove(filename.to_locale_string().c_str()) != 0) {
if (errno != ENOENT) {
LOG(Level::ERROR,
"Failed to delete file %s, error code: %d (%s)",
filename, errno, strerror(errno));
}
}
}
}
newsboat::Filepath QueueLoader::get_filename(const std::string& str) const
{
std::string fn = cfg.get_configvalue("download-path");
if (fn[fn.length() - 1] != NEWSBEUTER_PATH_SEP) {
fn.push_back(NEWSBEUTER_PATH_SEP);
}
char buf[1024];
snprintf(buf, sizeof(buf), "%s", str.c_str());
char* base = basename(buf);
if (!base || strlen(base) == 0) {
time_t t = time(nullptr);
fn.append(utils::mt_strf_localtime("%Y-%b-%d-%H%M%S.unknown", t));
} else {
fn.append(utils::replace_all(base, "'", "%27"));
}
return fn;
}
} // namespace podboat
|