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 fLastBytes = 0; 177 fLastRateCalcTime = system_time(); 178 fDownloadRate = 0; 179 printf(" 0%%"); 180 } 181 182 183 void 184 PackageManager::ProgressPackageDownloadActive(const char* packageName, 185 float completionPercentage, off_t bytes, off_t totalBytes) 186 { 187 if (bytes == totalBytes) 188 fLastBytes = totalBytes; 189 if (!fInteractive) 190 return; 191 192 // Do not update if nothing changed in the last 500ms 193 if (bytes <= fLastBytes || (system_time() - fLastRateCalcTime) < 500000) 194 return; 195 196 const bigtime_t time = system_time(); 197 if (time != fLastRateCalcTime) { 198 fDownloadRate = (bytes - fLastBytes) * 1000000 199 / (time - fLastRateCalcTime); 200 } 201 fLastRateCalcTime = time; 202 fLastBytes = bytes; 203 204 // Build the current file progress percentage and size string 205 BString leftStr; 206 BString rightStr; 207 208 int width = 70; 209 struct winsize winSize; 210 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winSize) == 0) 211 width = std::min(winSize.ws_col - 2, 78); 212 213 if (width < 30) { 214 // Not much space for anything, just draw a percentage 215 leftStr.SetToFormat("%3d%%", (int)(completionPercentage * 100)); 216 } else { 217 leftStr.SetToFormat("%3d%% %s", (int)(completionPercentage * 100), 218 packageName); 219 220 char byteBuffer[32]; 221 char totalBuffer[32]; 222 char rateBuffer[32]; 223 rightStr.SetToFormat("%s/%s %s ", 224 string_for_size(bytes, byteBuffer, sizeof(byteBuffer)), 225 string_for_size(totalBytes, totalBuffer, sizeof(totalBuffer)), 226 fDownloadRate == 0 ? "--.-" : 227 string_for_rate(fDownloadRate, rateBuffer, sizeof(rateBuffer))); 228 229 if (leftStr.CountChars() + rightStr.CountChars() >= width) 230 { 231 // The full string does not fit! Try to make a shorter one. 232 leftStr.ReplaceLast(".hpkg", ""); 233 leftStr.TruncateChars(width - rightStr.CountChars() - 2); 234 leftStr.Append(B_UTF8_ELLIPSIS " "); 235 } 236 237 int extraSpace = width - leftStr.CountChars() - rightStr.CountChars(); 238 239 leftStr.Append(' ', extraSpace); 240 leftStr.Append(rightStr); 241 } 242 243 const int progChars = leftStr.CountBytes(0, 244 (int)(width * completionPercentage)); 245 246 // Set bg to green, fg to white, and print progress bar. 247 // Then reset colors and print rest of text 248 // And finally remove any stray chars at the end of the line 249 printf("\r\x1B[42;37m%.*s\x1B[0m%s\x1B[K", progChars, leftStr.String(), 250 leftStr.String() + progChars); 251 252 // Force terminal to update when the line is complete, to avoid flickering 253 // because of updates at random times 254 fflush(stdout); 255 } 256 257 258 void 259 PackageManager::ProgressPackageDownloadComplete(const char* packageName) 260 { 261 if (fInteractive) { 262 // Erase the line, return to the start, and reset colors 263 printf("\r\33[2K\r\x1B[0m"); 264 } 265 266 char byteBuffer[32]; 267 printf("100%% %s [%s]\n", packageName, 268 string_for_size(fLastBytes, byteBuffer, sizeof(byteBuffer))); 269 fflush(stdout); 270 } 271 272 273 void 274 PackageManager::ProgressPackageChecksumStarted(const char* title) 275 { 276 printf("%s...", title); 277 } 278 279 280 void 281 PackageManager::ProgressPackageChecksumComplete(const char* title) 282 { 283 printf("done.\n"); 284 } 285 286 287 void 288 PackageManager::ProgressStartApplyingChanges(InstalledRepository& repository) 289 { 290 printf("[%s] Applying changes ...\n", repository.Name().String()); 291 } 292 293 294 void 295 PackageManager::ProgressTransactionCommitted(InstalledRepository& repository, 296 const BCommitTransactionResult& result) 297 { 298 const char* repositoryName = repository.Name().String(); 299 300 int32 issueCount = result.CountIssues(); 301 for (int32 i = 0; i < issueCount; i++) { 302 const BTransactionIssue* issue = result.IssueAt(i); 303 if (issue->PackageName().IsEmpty()) { 304 printf("[%s] warning: %s\n", repositoryName, 305 issue->ToString().String()); 306 } else { 307 printf("[%s] warning: package %s: %s\n", repositoryName, 308 issue->PackageName().String(), issue->ToString().String()); 309 } 310 } 311 312 printf("[%s] Changes applied. Old activation state backed up in \"%s\"\n", 313 repositoryName, result.OldStateDirectory().String()); 314 printf("[%s] Cleaning up ...\n", repositoryName); 315 } 316 317 318 void 319 PackageManager::ProgressApplyingChangesDone(InstalledRepository& repository) 320 { 321 printf("[%s] Done.\n", repository.Name().String()); 322 } 323 324 325 void 326 PackageManager::_PrintResult(InstalledRepository& installationRepository) 327 { 328 if (!installationRepository.HasChanges()) 329 return; 330 331 printf(" in %s:\n", installationRepository.Name().String()); 332 333 PackageList& packagesToActivate 334 = installationRepository.PackagesToActivate(); 335 PackageList& packagesToDeactivate 336 = installationRepository.PackagesToDeactivate(); 337 338 BStringList upgradedPackages; 339 BStringList upgradedPackageVersions; 340 for (int32 i = 0; 341 BSolverPackage* installPackage = packagesToActivate.ItemAt(i); 342 i++) { 343 for (int32 j = 0; 344 BSolverPackage* uninstallPackage = packagesToDeactivate.ItemAt(j); 345 j++) { 346 if (installPackage->Info().Name() == uninstallPackage->Info().Name()) { 347 upgradedPackages.Add(installPackage->Info().Name()); 348 upgradedPackageVersions.Add(uninstallPackage->Info().Version().ToString()); 349 break; 350 } 351 } 352 } 353 354 for (int32 i = 0; BSolverPackage* package = packagesToActivate.ItemAt(i); 355 i++) { 356 BString repository; 357 if (dynamic_cast<MiscLocalRepository*>(package->Repository()) != NULL) 358 repository = "local file"; 359 else 360 repository.SetToFormat("repository %s", package->Repository()->Name().String()); 361 362 int position = upgradedPackages.IndexOf(package->Info().Name()); 363 if (position >= 0) { 364 printf(" upgrade package %s-%s to %s from %s\n", 365 package->Info().Name().String(), 366 upgradedPackageVersions.StringAt(position).String(), 367 package->Info().Version().ToString().String(), 368 repository.String()); 369 } else { 370 printf(" install package %s-%s from %s\n", 371 package->Info().Name().String(), 372 package->Info().Version().ToString().String(), 373 repository.String()); 374 } 375 } 376 377 for (int32 i = 0; BSolverPackage* package = packagesToDeactivate.ItemAt(i); 378 i++) { 379 if (upgradedPackages.HasString(package->Info().Name())) 380 continue; 381 printf(" uninstall package %s\n", package->VersionedName().String()); 382 } 383 // TODO: Print file/download sizes. Unfortunately our package infos don't 384 // contain the file size. Which is probably correct. The file size (and possibly 385 // other information) should, however, be provided by the repository cache in 386 // some way. Extend BPackageInfo? Create a BPackageFileInfo? 387 } 388