debsystem.cc 6.5 KB

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