private-sources.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <config.h>
  2. #include <apt-pkg/hashes.h>
  3. #include <apt-pkg/strutl.h>
  4. #include <apt-pkg/configuration.h>
  5. #include <apt-pkg/sourcelist.h>
  6. #include <apt-pkg/cmndline.h>
  7. #include <apt-pkg/error.h>
  8. #include <apt-pkg/fileutl.h>
  9. #include <apt-pkg/cachefile.h>
  10. #include <apt-private/private-output.h>
  11. #include <apt-private/private-sources.h>
  12. #include <apt-private/private-utils.h>
  13. #include <stddef.h>
  14. #include <unistd.h>
  15. #include <iostream>
  16. #include <string>
  17. #include <apti18n.h>
  18. /* Interface discussion with donkult (for the future):
  19. apt [add-{archive,release,component}|edit|change-release|disable]-sources
  20. and be clever and work out stuff from the Release file
  21. */
  22. // EditSource - EditSourcesList /*{{{*/
  23. class APT_HIDDEN ScopedGetLock {
  24. public:
  25. int fd;
  26. ScopedGetLock(std::string const &filename) : fd(GetLock(filename)) {}
  27. ~ScopedGetLock() { close(fd); }
  28. };
  29. bool EditSources(CommandLine &CmdL)
  30. {
  31. std::string sourceslist;
  32. if (CmdL.FileList[1] != NULL)
  33. {
  34. sourceslist = _config->FindDir("Dir::Etc::sourceparts") + CmdL.FileList[1];
  35. if (!APT::String::Endswith(sourceslist, ".list"))
  36. sourceslist += ".list";
  37. } else {
  38. sourceslist = _config->FindFile("Dir::Etc::sourcelist");
  39. }
  40. HashString before;
  41. if (FileExists(sourceslist))
  42. before.FromFile(sourceslist);
  43. ScopedGetLock lock(sourceslist);
  44. if (lock.fd < 0)
  45. return false;
  46. bool res;
  47. bool file_changed = false;
  48. do {
  49. if (EditFileInSensibleEditor(sourceslist) == false)
  50. return false;
  51. if (FileExists(sourceslist) && !before.VerifyFile(sourceslist))
  52. {
  53. file_changed = true;
  54. pkgCacheFile::RemoveCaches();
  55. }
  56. pkgCacheFile CacheFile;
  57. res = CacheFile.BuildCaches(nullptr);
  58. if (res == false || _error->empty(GlobalError::WARNING) == false) {
  59. std::string outs;
  60. strprintf(outs, _("Failed to parse %s. Edit again? "), sourceslist.c_str());
  61. // FIXME: should we add a "restore previous" option here?
  62. if (YnPrompt(outs.c_str(), true) == false)
  63. {
  64. if (res == false && _error->PendingError() == false)
  65. {
  66. CacheFile.Close();
  67. pkgCacheFile::RemoveCaches();
  68. res = CacheFile.BuildCaches(nullptr);
  69. }
  70. break;
  71. }
  72. }
  73. } while (res == false);
  74. if (res == true && file_changed == true)
  75. {
  76. ioprintf(
  77. std::cout, _("Your '%s' file changed, please run 'apt-get update'."),
  78. sourceslist.c_str());
  79. }
  80. return res;
  81. }
  82. /*}}}*/