dpkginit.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: dpkginit.cc,v 1.5 1999/08/03 05:21:19 jgg Exp $
  4. /* ######################################################################
  5. DPKG init - Initialize the dpkg stuff
  6. This class provides the locking mechanism used by dpkg for its
  7. database area. It does the proper consistency checks and acquires the
  8. correct kind of lock.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Includes /*{{{*/
  12. #ifdef __GNUG__
  13. #pragma implementation "apt-pkg/dpkginit.h"
  14. #endif
  15. #include <apt-pkg/dpkginit.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <apt-pkg/fileutl.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <dirent.h>
  22. /*}}}*/
  23. // DpkgLock::pkgDpkgLock - Constructor /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* */
  26. pkgDpkgLock::pkgDpkgLock(bool WithUpdates)
  27. {
  28. LockFD = -1;
  29. GetLock(WithUpdates);
  30. }
  31. /*}}}*/
  32. // DpkgLock::~pkgDpkgLock - Destructor /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* */
  35. pkgDpkgLock::~pkgDpkgLock()
  36. {
  37. Close();
  38. }
  39. /*}}}*/
  40. // DpkgLock::GetLock - Get the lock /*{{{*/
  41. // ---------------------------------------------------------------------
  42. /* This mirrors the operations dpkg does when it starts up. Note the
  43. checking of the updates directory. */
  44. bool pkgDpkgLock::GetLock(bool WithUpdates)
  45. {
  46. // Disable file locking
  47. if (_config->FindB("Debug::NoLocking",false) == true)
  48. return true;
  49. Close();
  50. // Create the lockfile
  51. string AdminDir = flNotFile(_config->Find("Dir::State::status"));
  52. LockFD = ::GetLock(AdminDir + "lock");
  53. if (LockFD == -1)
  54. return _error->Error("Unable to lock the administration directory, "
  55. "are you root?");
  56. // See if we need to abort with a dirty journal
  57. if (WithUpdates == true && CheckUpdates() == true)
  58. {
  59. Close();
  60. return _error->Error("dpkg was interrupted, you must manually "
  61. "run 'dpkg --configure -a' to correct the problem. ");
  62. }
  63. return true;
  64. }
  65. /*}}}*/
  66. // DpkgLock::Close - Close the lock /*{{{*/
  67. // ---------------------------------------------------------------------
  68. /* */
  69. void pkgDpkgLock::Close()
  70. {
  71. close(LockFD);
  72. LockFD = -1;
  73. }
  74. /*}}}*/
  75. // DpkgLock::CheckUpdates - Check if the updates dir is dirty /*{{{*/
  76. // ---------------------------------------------------------------------
  77. /* This does a check of the updates directory to see if it has any entries
  78. in it. */
  79. bool pkgDpkgLock::CheckUpdates()
  80. {
  81. // Check for updates.. (dirty)
  82. string File = flNotFile(_config->Find("Dir::State::status")) + "updates/";
  83. DIR *DirP = opendir(File.c_str());
  84. if (DirP == 0)
  85. return false;
  86. /* We ignore any files that are not all digits, this skips .,.. and
  87. some tmp files dpkg will leave behind.. */
  88. bool Damaged = false;
  89. for (struct dirent *Ent = readdir(DirP); Ent != 0; Ent = readdir(DirP))
  90. {
  91. Damaged = true;
  92. for (unsigned int I = 0; Ent->d_name[I] != 0; I++)
  93. {
  94. // Check if its not a digit..
  95. if (isdigit(Ent->d_name[I]) == 0)
  96. {
  97. Damaged = false;
  98. break;
  99. }
  100. }
  101. if (Damaged == true)
  102. break;
  103. }
  104. closedir(DirP);
  105. return Damaged;
  106. }
  107. /*}}}*/