debsystem.cc 7.2 KB

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