comparison src/hgrunner.cpp @ 455:856da063d76e

Remove stale auth cache files
author Chris Cannam
date Wed, 29 Jun 2011 16:18:47 +0100
parents 66ec8b2ee946
children 319f920a51ee
comparison
equal deleted inserted replaced
454:6f5acaf27d60 455:856da063d76e
389 m_queue.pop_front(); 389 m_queue.pop_front();
390 DEBUG << "checkQueue: have action: running " << toRun.action << endl; 390 DEBUG << "checkQueue: have action: running " << toRun.action << endl;
391 startCommand(toRun); 391 startCommand(toRun);
392 } 392 }
393 393
394 QStringList HgRunner::addExtensionOptions(QStringList params) 394 void HgRunner::pruneOldAuthFiles()
395 { 395 {
396 QString extpath = getExtensionLocation(); 396 QString path = QDesktopServices::storageLocation
397 if (extpath == "") { 397 (QDesktopServices::CacheLocation);
398 DEBUG << "HgRunner::addExtensionOptions: Failed to get extension location" << endl; 398 QDir d(path);
399 return params; 399 if (!d.exists()) return;
400 } 400 QStringList filters;
401 401 filters << "easyhg.*.dat";
402 QStringList fl = d.entryList(filters);
403 foreach (QString f, fl) {
404 QStringList parts = f.split('.');
405 if (parts.size() > 1) {
406 int pid = parts[1].toInt();
407 DEBUG << "Checking pid " << pid << " for cache file " << f << endl;
408
409 ProcessStatus ps = GetProcessStatus(pid);
410 if (ps == ProcessNotRunning) {
411 DEBUG << "Removing stale cache file " << f << endl;
412 QDir(d).remove(f);
413 }
414 }
415 }
416 }
417
418 QString HgRunner::getAuthFilePath()
419 {
402 if (m_authFilePath == "") { 420 if (m_authFilePath == "") {
403 421
404 QByteArray key = randomKey(); 422 pruneOldAuthFiles();
405 if (key == QByteArray()) {
406 DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth key" << endl;
407 return params;
408 }
409 QString key16 = QString::fromLocal8Bit(key.toBase64()).left(16);
410 423
411 QByteArray fileExt = randomKey(); 424 QByteArray fileExt = randomKey();
412 if (fileExt == QByteArray()) { 425 if (fileExt == QByteArray()) {
413 DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth file ext" << endl; 426 DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth file ext" << endl;
414 return params; 427 return "";
415 } 428 }
416 QString fileExt16 = QString::fromLocal8Bit(fileExt.toBase64()).left(16) 429 QString fileExt16 = QString::fromLocal8Bit(fileExt.toBase64()).left(16)
417 .replace('+', '-').replace('/', '.'); 430 .replace('+', '-').replace('/', '_');
418 QString path = QDesktopServices::storageLocation 431 QString path = QDesktopServices::storageLocation
419 (QDesktopServices::CacheLocation); 432 (QDesktopServices::CacheLocation);
420 QDir().mkpath(path); 433 QDir().mkpath(path);
421 if (path == "") { 434 if (path == "") {
422 DEBUG << "HgRunner::addExtensionOptions: Failed to get cache location" << endl; 435 DEBUG << "HgRunner::addExtensionOptions: Failed to get cache location" << endl;
423 return params; 436 return "";
424 } 437 }
425 438
439 m_authFilePath = QString("%1/easyhg.%2.%3.dat").arg(path)
440 .arg(getpid()).arg(fileExt16);
441 }
442
443 return m_authFilePath;
444 }
445
446 QString HgRunner::getAuthKey()
447 {
448 if (m_authKey == "") {
449 QByteArray key = randomKey();
450 if (key == QByteArray()) {
451 DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth key" << endl;
452 return "";
453 }
454 QString key16 = QString::fromLocal8Bit(key.toBase64()).left(16);
426 m_authKey = key16; 455 m_authKey = key16;
427 m_authFilePath = QString("%1/easyhg_%2.dat").arg(path).arg(fileExt16); 456 }
428 } 457
429 458 return m_authKey;
430 params.push_front(QString("easyhg.authkey=%1").arg(m_authKey)); 459 }
431 params.push_front("--config"); 460
432 461 QStringList HgRunner::addExtensionOptions(QStringList params)
433 params.push_front(QString("easyhg.authfile=%1").arg(m_authFilePath)); 462 {
434 params.push_front("--config"); 463 QString extpath = getExtensionLocation();
464 if (extpath == "") {
465 DEBUG << "HgRunner::addExtensionOptions: Failed to get extension location" << endl;
466 return params;
467 }
468
469 QString afp = getAuthFilePath();
470 QString afk = getAuthKey();
471
472 if (afp != "" && afk != "") {
473 params.push_front(QString("easyhg.authkey=%1").arg(m_authKey));
474 params.push_front("--config");
475 params.push_front(QString("easyhg.authfile=%1").arg(m_authFilePath));
476 params.push_front("--config");
477 }
435 478
436 // Looks like this one must be without quotes, even though the SSH 479 // Looks like this one must be without quotes, even though the SSH
437 // one above only works on Windows if it has quotes (at least where 480 // one above only works on Windows if it has quotes (at least where
438 // there is a space in the path). Odd 481 // there is a space in the path). Odd
439 params.push_front(QString("extensions.easyhg=%1").arg(extpath)); 482 params.push_front(QString("extensions.easyhg=%1").arg(extpath));