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