debsystem.cc 6.5 KB

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