xref: /haiku/src/bin/pkgman/PackageManager.cpp (revision bd6068614473f87449dfa2eaa67fad1527c61e11)
1 /*
2  * Copyright 2013-2015, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler <axeld@pinc-software.de>
7  *		Rene Gollent <rene@gollent.com>
8  *		Ingo Weinhold <ingo_weinhold@gmx.de>
9  */
10 
11 
12 #include <StringForSize.h>
13 #include <StringForRate.h>
14 	// Must be first, or the BPrivate namespaces are confused
15 
16 #include "PackageManager.h"
17 
18 #include <InterfaceDefs.h>
19 
20 #include <sys/ioctl.h>
21 #include <unistd.h>
22 
23 #include <package/CommitTransactionResult.h>
24 #include <package/DownloadFileRequest.h>
25 #include <package/RefreshRepositoryRequest.h>
26 #include <package/solver/SolverPackage.h>
27 #include <package/solver/SolverProblem.h>
28 #include <package/solver/SolverProblemSolution.h>
29 
30 #include "pkgman.h"
31 
32 
33 using namespace BPackageKit::BPrivate;
34 
35 
36 PackageManager::PackageManager(BPackageInstallationLocation location,
37 	bool interactive)
38 	:
39 	BPackageManager(location, &fClientInstallationInterface, this),
40 	BPackageManager::UserInteractionHandler(),
41 	fDecisionProvider(interactive),
42 	fClientInstallationInterface(),
43 	fInteractive(interactive)
44 {
45 }
46 
47 
48 PackageManager::~PackageManager()
49 {
50 }
51 
52 
53 void
54 PackageManager::SetInteractive(bool interactive)
55 {
56 	fInteractive = interactive;
57 	fDecisionProvider.SetInteractive(interactive);
58 }
59 
60 
61 void
62 PackageManager::JobFailed(BSupportKit::BJob* job)
63 {
64 	BString error = job->ErrorString();
65 	if (error.Length() > 0) {
66 		error.ReplaceAll("\n", "\n*** ");
67 		fprintf(stderr, "%s", error.String());
68 	}
69 }
70 
71 
72 void
73 PackageManager::JobAborted(BSupportKit::BJob* job)
74 {
75 	DIE(job->Result(), "aborted");
76 }
77 
78 
79 void
80 PackageManager::HandleProblems()
81 {
82 	printf("Encountered problems:\n");
83 
84 	int32 problemCount = fSolver->CountProblems();
85 	for (int32 i = 0; i < problemCount; i++) {
86 		// print problem and possible solutions
87 		BSolverProblem* problem = fSolver->ProblemAt(i);
88 		printf("problem %" B_PRId32 ": %s\n", i + 1,
89 			problem->ToString().String());
90 
91 		int32 solutionCount = problem->CountSolutions();
92 		for (int32 k = 0; k < solutionCount; k++) {
93 			const BSolverProblemSolution* solution = problem->SolutionAt(k);
94 			printf("  solution %" B_PRId32 ":\n", k + 1);
95 			int32 elementCount = solution->CountElements();
96 			for (int32 l = 0; l < elementCount; l++) {
97 				const BSolverProblemSolutionElement* element
98 					= solution->ElementAt(l);
99 				printf("    - %s\n", element->ToString().String());
100 			}
101 		}
102 
103 		if (!fInteractive)
104 			continue;
105 
106 		// let the user choose a solution
107 		printf("Please select a solution, skip the problem for now or quit.\n");
108 		for (;;) {
109 			if (solutionCount > 1)
110 				printf("select [1...%" B_PRId32 "/s/q]: ", solutionCount);
111 			else
112 				printf("select [1/s/q]: ");
113 
114 			char buffer[32];
115 			if (fgets(buffer, sizeof(buffer), stdin) == NULL
116 				|| strcmp(buffer, "q\n") == 0) {
117 				exit(1);
118 			}
119 
120 			if (strcmp(buffer, "s\n") == 0)
121 				break;
122 
123 			char* end;
124 			long selected = strtol(buffer, &end, 0);
125 			if (end == buffer || *end != '\n' || selected < 1
126 				|| selected > solutionCount) {
127 				printf("*** invalid input\n");
128 				continue;
129 			}
130 
131 			status_t error = fSolver->SelectProblemSolution(problem,
132 				problem->SolutionAt(selected - 1));
133 			if (error != B_OK)
134 				DIE(error, "failed to set solution");
135 			break;
136 		}
137 	}
138 
139 	if (problemCount > 0 && !fInteractive)
140 		exit(1);
141 }
142 
143 
144 void
145 PackageManager::ConfirmChanges(bool fromMostSpecific)
146 {
147 	printf("The following changes will be made:\n");
148 
149 	int32 count = fInstalledRepositories.CountItems();
150 	if (fromMostSpecific) {
151 		for (int32 i = count - 1; i >= 0; i--)
152 			_PrintResult(*fInstalledRepositories.ItemAt(i));
153 	} else {
154 		for (int32 i = 0; i < count; i++)
155 			_PrintResult(*fInstalledRepositories.ItemAt(i));
156 	}
157 
158 	if (!fDecisionProvider.YesNoDecisionNeeded(BString(), "Continue?", "yes",
159 			"no", "yes")) {
160 		exit(1);
161 	}
162 }
163 
164 
165 void
166 PackageManager::Warn(status_t error, const char* format, ...)
167 {
168 	va_list args;
169 	va_start(args, format);
170 	vfprintf(stderr, format, args);
171 	va_end(args);
172 
173 	if (error == B_OK)
174 		printf("\n");
175 	else
176 		printf(": %s\n", strerror(error));
177 }
178 
179 
180 void
181 PackageManager::ProgressPackageDownloadStarted(const char* packageName)
182 {
183 	fLastBytes = 0;
184 	fLastRateCalcTime = system_time();
185 	fDownloadRate = 0;
186 	printf("  0%%");
187 }
188 
189 
190 void
191 PackageManager::ProgressPackageDownloadActive(const char* packageName,
192 	float completionPercentage, off_t bytes, off_t totalBytes)
193 {
194 	if (bytes == totalBytes)
195 		fLastBytes = totalBytes;
196 	if (!fInteractive)
197 		return;
198 
199 	// Do not update if nothing changed in the last 500ms
200 	if (bytes <= fLastBytes || (system_time() - fLastRateCalcTime) < 500000)
201 		return;
202 
203 	const bigtime_t time = system_time();
204 	if (time != fLastRateCalcTime) {
205 		fDownloadRate = (bytes - fLastBytes) * 1000000
206 			/ (time - fLastRateCalcTime);
207 	}
208 	fLastRateCalcTime = time;
209 	fLastBytes = bytes;
210 
211 	// Build the current file progress percentage and size string
212 	BString leftStr;
213 	BString rightStr;
214 
215 	int width = 70;
216 	struct winsize winSize;
217 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winSize) == 0)
218 		width = std::min(winSize.ws_col - 2, 78);
219 
220 	if (width < 30) {
221 		// Not much space for anything, just draw a percentage
222 		leftStr.SetToFormat("%3d%%", (int)(completionPercentage * 100));
223 	} else {
224 		leftStr.SetToFormat("%3d%% %s", (int)(completionPercentage * 100),
225 				packageName);
226 
227 		char byteBuffer[32];
228 		char totalBuffer[32];
229 		char rateBuffer[32];
230 		rightStr.SetToFormat("%s/%s  %s ",
231 				string_for_size(bytes, byteBuffer, sizeof(byteBuffer)),
232 				string_for_size(totalBytes, totalBuffer, sizeof(totalBuffer)),
233 				fDownloadRate == 0 ? "--.-" :
234 				string_for_rate(fDownloadRate, rateBuffer, sizeof(rateBuffer)));
235 
236 		if (leftStr.CountChars() + rightStr.CountChars() >= width)
237 		{
238 			// The full string does not fit! Try to make a shorter one.
239 			leftStr.ReplaceLast(".hpkg", "");
240 			leftStr.TruncateChars(width - rightStr.CountChars() - 2);
241 			leftStr.Append(B_UTF8_ELLIPSIS " ");
242 		}
243 
244 		int extraSpace = width - leftStr.CountChars() - rightStr.CountChars();
245 
246 		leftStr.Append(' ', extraSpace);
247 		leftStr.Append(rightStr);
248 	}
249 
250 	const int progChars = leftStr.CountBytes(0,
251 		(int)(width * completionPercentage));
252 
253 	// Set bg to green, fg to white, and print progress bar.
254 	// Then reset colors and print rest of text
255 	// And finally remove any stray chars at the end of the line
256 	printf("\r\x1B[42;37m%.*s\x1B[0m%s\x1B[K", progChars, leftStr.String(),
257 		leftStr.String() + progChars);
258 
259 	// Force terminal to update when the line is complete, to avoid flickering
260 	// because of updates at random times
261 	fflush(stdout);
262 }
263 
264 
265 void
266 PackageManager::ProgressPackageDownloadComplete(const char* packageName)
267 {
268 	if (fInteractive) {
269 		// Erase the line, return to the start, and reset colors
270 		printf("\r\33[2K\r\x1B[0m");
271 	}
272 
273 	char byteBuffer[32];
274 	printf("100%% %s [%s]\n", packageName,
275 		string_for_size(fLastBytes, byteBuffer, sizeof(byteBuffer)));
276 	fflush(stdout);
277 }
278 
279 
280 void
281 PackageManager::ProgressPackageChecksumStarted(const char* title)
282 {
283 	printf("%s...", title);
284 }
285 
286 
287 void
288 PackageManager::ProgressPackageChecksumComplete(const char* title)
289 {
290 	printf("done.\n");
291 }
292 
293 
294 void
295 PackageManager::ProgressStartApplyingChanges(InstalledRepository& repository)
296 {
297 	printf("[%s] Applying changes ...\n", repository.Name().String());
298 }
299 
300 
301 void
302 PackageManager::ProgressTransactionCommitted(InstalledRepository& repository,
303 	const BCommitTransactionResult& result)
304 {
305 	const char* repositoryName = repository.Name().String();
306 
307 	int32 issueCount = result.CountIssues();
308 	for (int32 i = 0; i < issueCount; i++) {
309 		const BTransactionIssue* issue = result.IssueAt(i);
310 		if (issue->PackageName().IsEmpty()) {
311 			printf("[%s] warning: %s\n", repositoryName,
312 				issue->ToString().String());
313 		} else {
314 			printf("[%s] warning: package %s: %s\n", repositoryName,
315 				issue->PackageName().String(), issue->ToString().String());
316 		}
317 	}
318 
319 	printf("[%s] Changes applied. Old activation state backed up in \"%s\"\n",
320 		repositoryName, result.OldStateDirectory().String());
321 	printf("[%s] Cleaning up ...\n", repositoryName);
322 }
323 
324 
325 void
326 PackageManager::ProgressApplyingChangesDone(InstalledRepository& repository)
327 {
328 	printf("[%s] Done.\n", repository.Name().String());
329 }
330 
331 
332 void
333 PackageManager::_PrintResult(InstalledRepository& installationRepository)
334 {
335 	if (!installationRepository.HasChanges())
336 		return;
337 
338 	printf("  in %s:\n", installationRepository.Name().String());
339 
340 	PackageList& packagesToActivate
341 		= installationRepository.PackagesToActivate();
342 	PackageList& packagesToDeactivate
343 		= installationRepository.PackagesToDeactivate();
344 
345 	BStringList upgradedPackages;
346 	BStringList upgradedPackageVersions;
347 	for (int32 i = 0;
348 		BSolverPackage* installPackage = packagesToActivate.ItemAt(i);
349 		i++) {
350 		for (int32 j = 0;
351 			BSolverPackage* uninstallPackage = packagesToDeactivate.ItemAt(j);
352 			j++) {
353 			if (installPackage->Info().Name() == uninstallPackage->Info().Name()) {
354 				upgradedPackages.Add(installPackage->Info().Name());
355 				upgradedPackageVersions.Add(uninstallPackage->Info().Version().ToString());
356 				break;
357 			}
358 		}
359 	}
360 
361 	for (int32 i = 0; BSolverPackage* package = packagesToActivate.ItemAt(i);
362 		i++) {
363 		BString repository;
364 		if (dynamic_cast<MiscLocalRepository*>(package->Repository()) != NULL)
365 			repository = "local file";
366 		else
367 			repository.SetToFormat("repository %s", package->Repository()->Name().String());
368 
369 		int position = upgradedPackages.IndexOf(package->Info().Name());
370 		if (position >= 0) {
371 			printf("    upgrade package %s-%s to %s from %s\n",
372 				package->Info().Name().String(),
373 				upgradedPackageVersions.StringAt(position).String(),
374 				package->Info().Version().ToString().String(),
375 				repository.String());
376 		} else {
377 			printf("    install package %s-%s from %s\n",
378 				package->Info().Name().String(),
379 				package->Info().Version().ToString().String(),
380 				repository.String());
381 		}
382 	}
383 
384 	for (int32 i = 0; BSolverPackage* package = packagesToDeactivate.ItemAt(i);
385 		i++) {
386 		if (upgradedPackages.HasString(package->Info().Name()))
387 			continue;
388 		printf("    uninstall package %s\n", package->VersionedName().String());
389 	}
390 // TODO: Print file/download sizes. Unfortunately our package infos don't
391 // contain the file size. Which is probably correct. The file size (and possibly
392 // other information) should, however, be provided by the repository cache in
393 // some way. Extend BPackageInfo? Create a BPackageFileInfo?
394 }
395