debsystem.cc 7.0 KB

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