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