File: wget.cpp

package info (click to toggle)
cupt 2.10.4%2Bnmu2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,144 kB
  • sloc: cpp: 23,642; perl: 1,599; sh: 40; makefile: 19
file content (183 lines) | stat: -rw-r--r-- 5,484 bytes parent folder | download | duplicates (5)
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
/**************************************************************************
*   Copyright (C) 2010 by Eugene V. Lyubimkin                             *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License                  *
*   (version 3 or above) as published by the Free Software Foundation.    *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU GPL                        *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA               *
**************************************************************************/
#include <sys/stat.h>

#include <thread>
#include <atomic>
#include <chrono>
#include <mutex>
#include <condition_variable>

using std::to_string;

#include <cupt/config.hpp>
#include <cupt/download/method.hpp>
#include <cupt/download/uri.hpp>
#include <cupt/file.hpp>

namespace cupt {

// returns true if succeeded
static bool __get_file_size(const string& path, ssize_t* result)
{
	struct stat st;
	if (lstat(path.c_str(), &st) == -1)
	{
		if (errno != ENOENT)
		{
			fatal2e(__("%s() failed: '%s'"), "lstat", path);
		}
		return false;
	}
	else
	{
		*result = st.st_size;
		return true;
	}
}

class WgetMethod: public cupt::download::Method
{
	vector< string > generateWgetParametersVector(const Config& config,
			const download::Uri& uri, const string& targetPath)
	{
		vector< string > p;

		p.push_back("env");
		auto proxy = getAcquireSuboptionForUri(config, uri, "proxy");
		if (!proxy.empty() && proxy != "DIRECT")
		{
			p.push_back(uri.getProtocol() + "_proxy=" + proxy);
		}
		p.push_back("wget"); // passed as a binary name, not parameter
		p.push_back("--continue");
		p.push_back(string("--tries=") + to_string(config.getInteger("acquire::retries")+1));
		auto maxSpeedLimit = getIntegerAcquireSuboptionForUri(config, uri, "dl-limit");
		if (maxSpeedLimit)
		{
			p.push_back(string("--limit-rate=") + to_string(maxSpeedLimit) + "k");
		}
		if (proxy == "DIRECT")
		{
			p.push_back("--no-proxy");
		}
		if (uri.getProtocol() != "http" || !config.getBool("acquire::http::allowredirect"))
		{
			p.push_back("--max-redirect=0");
		}
		auto timeout = getIntegerAcquireSuboptionForUri(config, uri, "timeout");
		if (timeout)
		{
			p.push_back(string("--timeout=") + to_string(timeout));
		}
		p.push_back(string(uri));
		p.push_back(string("--output-document=") + targetPath);
		p.push_back(format2("--user-agent=\"Wget (libcupt/%s)\"", cupt::libraryVersion));
		p.push_back("2>&1");

		return p;
	}

	string perform(const Config& config, const download::Uri& uri,
			const string& targetPath, const std::function< void (const vector< string >&) >& callback)
	{
		bool wgetProcessFinished = false;
		std::condition_variable wgetProcessFinishedCV;
		std::mutex wgetProcessFinishedMutex;

		try
		{
			ssize_t totalBytes = 0;
			if (__get_file_size(targetPath, &totalBytes))
			{
				callback({ "downloading", to_string(totalBytes), to_string(0)});
			}

			// wget executor
			std::thread downloadingStatsThread([&targetPath, &totalBytes, &callback,
					&wgetProcessFinishedMutex, &wgetProcessFinishedCV, &wgetProcessFinished]()
			{
				std::unique_lock< std::mutex > conditionMutexLock(wgetProcessFinishedMutex);
				while (!wgetProcessFinishedCV.wait_for(conditionMutexLock, std::chrono::milliseconds(100),
						[&wgetProcessFinished](){ return wgetProcessFinished; }))
				{
					decltype(totalBytes) newTotalBytes;
					if (__get_file_size(targetPath, &newTotalBytes))
					{
						if (newTotalBytes != totalBytes)
						{
							callback({ "downloading", to_string(newTotalBytes), to_string(newTotalBytes - totalBytes) });
							totalBytes = newTotalBytes;
						}
					}
				}
			});

			string errorString;
			auto oldMessageFd = cupt::messageFd; // disable printing errors
			cupt::messageFd = -1;
			try
			{
				auto wgetParameters = generateWgetParametersVector(config, uri, targetPath);

				string openError;
				File wgetOutputFile(join(" ", wgetParameters), "pr", openError);
				if (!openError.empty())
				{
					fatal2(__("unable to launch a wget process: %s"), openError);
				}

				string line;
				while (wgetOutputFile.getLine(line), !wgetOutputFile.eof())
				{
					errorString += line;
					errorString += '\n';
				}

				{
					std::lock_guard< std::mutex > guard(wgetProcessFinishedMutex);
					wgetProcessFinished = true;
				}
				wgetProcessFinishedCV.notify_all();
				downloadingStatsThread.join();
			}
			catch (Exception&)
			{
				cupt::messageFd = oldMessageFd;
				return errorString;
			}

			cupt::messageFd = oldMessageFd;
			return "";
		}
		catch (Exception& e)
		{
			return format2("download method error: %s", e.what());
		}
	}
};

}

extern "C"
{
	cupt::download::Method* construct()
	{
		return new cupt::WgetMethod();
	}
}