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