File: debdelta.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 (89 lines) | stat: -rw-r--r-- 3,005 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
/**************************************************************************
*   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 <unistd.h>

#include <cupt/common.hpp>
#include <cupt/download/uri.hpp>
#include <cupt/download/method.hpp>
#include <cupt/download/methodfactory.hpp>

namespace cupt {

class DebdeltaMethod: public download::Method
{
	string perform(const Config& config, const download::Uri& uri,
			const string& targetPath, const std::function< void (const vector< string >&) >& callback)
	{
		auto deltaCallback = [callback](const vector< string >& params)
		{
			if (!params.empty() && params[0] == "expected-size")
			{
				auto uiSizeParams = params;
				uiSizeParams[0] = "ui-size";
				callback(uiSizeParams);
			}
			else
			{
				callback(params);
			}
		};

		// download delta file
		auto deltaUri = uri.getOpaque();
		auto deltaDownloadPath = targetPath + ".delta";

		download::MethodFactory methodFactory(config);
		auto method = methodFactory.getDownloadMethodForUri(deltaUri);
		auto deltaDownloadResult = method->perform(config, deltaUri, deltaDownloadPath, deltaCallback);
		delete method;
		if (!deltaDownloadResult.empty())
		{
			return format2(__("delta download failed: %s"), deltaDownloadResult);
		}

		// invoking a deb patcher
		auto patchCommand = format2("debpatch --accept-unsigned %s / %s >/dev/null",
				deltaDownloadPath, targetPath);
		auto patchResult = ::system(patchCommand.c_str());

		// remove delta anyway
		if (unlink(deltaDownloadPath.c_str()) == -1)
		{
			warn2e(__("unable to remove the file '%s'"), deltaDownloadPath);
		}

		if (patchResult != 0)
		{
			return format2(__("debpatch returned error code %d"), patchResult);
		}

		// all went ok
		return string();
	}
};

}

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