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