debsystem.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debsystem.cc,v 1.2 2001/02/20 07:03:17 jgg Exp $
  4. /* ######################################################################
  5. System - Abstraction for running on different systems.
  6. Basic general structure..
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #ifdef __GNUG__
  11. #pragma implementation "apt-pkg/debsystem.h"
  12. #endif
  13. #include <apt-pkg/debsystem.h>
  14. #include <apt-pkg/debversion.h>
  15. #include <apt-pkg/debindexfile.h>
  16. #include <apt-pkg/dpkgpm.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <apt-pkg/error.h>
  19. #include <apt-pkg/fileutl.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <dirent.h>
  23. #include <errno.h>
  24. /*}}}*/
  25. debSystem debSys;
  26. // System::debSystem - Constructor /*{{{*/
  27. // ---------------------------------------------------------------------
  28. /* */
  29. debSystem::debSystem()
  30. {
  31. LockFD = -1;
  32. LockCount = 0;
  33. Label = "Debian dpkg interface";
  34. VS = &debVS;
  35. }
  36. /*}}}*/
  37. // System::Lock - 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 debSystem::Lock()
  42. {
  43. // Disable file locking
  44. if (_config->FindB("Debug::NoLocking",false) == true || LockCount > 1)
  45. {
  46. LockCount++;
  47. return true;
  48. }
  49. // Create the lockfile
  50. string AdminDir = flNotFile(_config->Find("Dir::State::status"));
  51. LockFD = GetLock(AdminDir + "lock");
  52. if (LockFD == -1)
  53. {
  54. if (errno == EACCES || errno == EAGAIN)
  55. return _error->Error("Unable to lock the administration directory (%s), "
  56. "is another process using it?",AdminDir.c_str());
  57. else
  58. return _error->Error("Unable to lock the administration directory (%s), "
  59. "are you root?",AdminDir.c_str());
  60. }
  61. // See if we need to abort with a dirty journal
  62. if (CheckUpdates() == true)
  63. {
  64. close(LockFD);
  65. LockFD = -1;
  66. return _error->Error("dpkg was interrupted, you must manually "
  67. "run 'dpkg --configure -a' to correct the problem. ");
  68. }
  69. LockCount++;
  70. return true;
  71. }
  72. /*}}}*/
  73. // System::UnLock - Drop a lock /*{{{*/
  74. // ---------------------------------------------------------------------
  75. /* */
  76. bool debSystem::UnLock(bool NoErrors)
  77. {
  78. if (LockCount == 0 && NoErrors == true)
  79. return false;
  80. if (LockCount < 1)
  81. return _error->Error("Not locked");
  82. if (--LockCount == 0)
  83. {
  84. close(LockFD);
  85. LockCount = 0;
  86. }
  87. return true;
  88. }
  89. /*}}}*/
  90. // System::CheckUpdates - Check if the updates dir is dirty /*{{{*/
  91. // ---------------------------------------------------------------------
  92. /* This does a check of the updates directory (dpkg journal) to see if it has
  93. any entries in it. */
  94. bool debSystem::CheckUpdates()
  95. {
  96. // Check for updates.. (dirty)
  97. string File = flNotFile(_config->Find("Dir::State::status")) + "updates/";
  98. DIR *DirP = opendir(File.c_str());
  99. if (DirP == 0)
  100. return false;
  101. /* We ignore any files that are not all digits, this skips .,.. and
  102. some tmp files dpkg will leave behind.. */
  103. bool Damaged = false;
  104. for (struct dirent *Ent = readdir(DirP); Ent != 0; Ent = readdir(DirP))
  105. {
  106. Damaged = true;
  107. for (unsigned int I = 0; Ent->d_name[I] != 0; I++)
  108. {
  109. // Check if its not a digit..
  110. if (isdigit(Ent->d_name[I]) == 0)
  111. {
  112. Damaged = false;
  113. break;
  114. }
  115. }
  116. if (Damaged == true)
  117. break;
  118. }
  119. closedir(DirP);
  120. return Damaged;
  121. }
  122. /*}}}*/
  123. // System::CreatePM - Create the underlying package manager /*{{{*/
  124. // ---------------------------------------------------------------------
  125. /* */
  126. pkgPackageManager *debSystem::CreatePM(pkgDepCache *Cache) const
  127. {
  128. return new pkgDPkgPM(Cache);
  129. }
  130. /*}}}*/
  131. // System::Initialize - Setup the configuration space.. /*{{{*/
  132. // ---------------------------------------------------------------------
  133. /* These are the Debian specific configuration variables.. */
  134. bool debSystem::Initialize(Configuration &Cnf)
  135. {
  136. /* These really should be jammed into a generic 'Local Database' engine
  137. which is yet to be determined. The functions in pkgcachegen should
  138. be the only users of these */
  139. Cnf.CndSet("Dir::State::userstatus","status.user"); // Defunct
  140. Cnf.CndSet("Dir::State::status","/var/lib/dpkg/status");
  141. Cnf.CndSet("Dir::Bin::dpkg","/usr/bin/dpkg");
  142. return true;
  143. }
  144. /*}}}*/
  145. // System::ArchiveSupported - Is a file format supported /*{{{*/
  146. // ---------------------------------------------------------------------
  147. /* The standard name for a deb is 'deb'.. There are no seperate versions
  148. of .deb to worry about.. */
  149. bool debSystem::ArchiveSupported(const char *Type)
  150. {
  151. if (strcmp(Type,"deb") == 0)
  152. return true;
  153. return false;
  154. }
  155. /*}}}*/
  156. // System::Score - Determine how 'Debiany' this sys is.. /*{{{*/
  157. // ---------------------------------------------------------------------
  158. /* We check some files that are sure tell signs of this being a Debian
  159. System.. */
  160. signed debSystem::Score(Configuration const &Cnf)
  161. {
  162. signed Score = 0;
  163. if (FileExists(Cnf.FindFile("Dir::State::status","/var/lib/dpkg/status")) == true)
  164. Score += 10;
  165. if (FileExists(Cnf.FindFile("Dir::Bin::dpkg","/usr/bin/dpkg")) == true)
  166. Score += 10;
  167. if (FileExists("/etc/debian_version") == true)
  168. Score += 10;
  169. return Score;
  170. }
  171. /*}}}*/
  172. // System::AddStatusFiles - Register the status files /*{{{*/
  173. // ---------------------------------------------------------------------
  174. /* */
  175. bool debSystem::AddStatusFiles(vector<pkgIndexFile *> &List)
  176. {
  177. List.push_back(new debStatusIndex(_config->FindFile("Dir::State::status")));
  178. return true;
  179. }
  180. /*}}}*/