dpkginit.cc 2.8 KB

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