debsystem.cc 7.1 KB

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