]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debsystem.cc
merge with debian-sid
[apt.git] / apt-pkg / deb / debsystem.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: debsystem.cc,v 1.4 2004/01/26 17:01:53 mdz Exp $
4 /* ######################################################################
5
6 System - Abstraction for running on different systems.
7
8 Basic general structure..
9
10 ##################################################################### */
11 /*}}}*/
12 // Include Files /*{{{*/
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>
20 #include <apti18n.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <dirent.h>
24 #include <errno.h>
25 /*}}}*/
26
27 debSystem debSys;
28
29 // System::debSystem - Constructor /*{{{*/
30 // ---------------------------------------------------------------------
31 /* */
32 debSystem::debSystem()
33 {
34 LockFD = -1;
35 LockCount = 0;
36 StatusFile = 0;
37
38 Label = "Debian dpkg interface";
39 VS = &debVS;
40 }
41 /*}}}*/
42 // System::~debSystem - Destructor /*{{{*/
43 // ---------------------------------------------------------------------
44 /* */
45 debSystem::~debSystem()
46 {
47 delete StatusFile;
48 }
49 /*}}}*/
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. */
54 bool 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)
69 return _error->Error(_("Unable to lock the administration directory (%s), "
70 "is another process using it?"),AdminDir.c_str());
71 else
72 return _error->Error(_("Unable to lock the administration directory (%s), "
73 "are you root?"),AdminDir.c_str());
74 }
75
76 // See if we need to abort with a dirty journal
77 if (CheckUpdates() == true)
78 {
79 close(LockFD);
80 LockFD = -1;
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
88 return _error->Error(_("dpkg was interrupted, you must manually "
89 "run '%s' to correct the problem. "), cmd);
90 }
91
92 LockCount++;
93
94 return true;
95 }
96 /*}}}*/
97 // System::UnLock - Drop a lock /*{{{*/
98 // ---------------------------------------------------------------------
99 /* */
100 bool debSystem::UnLock(bool NoErrors)
101 {
102 if (LockCount == 0 && NoErrors == true)
103 return false;
104
105 if (LockCount < 1)
106 return _error->Error(_("Not locked"));
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. */
120 bool 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 /* */
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
171 if (StatusFile) {
172 delete StatusFile;
173 StatusFile = 0;
174 }
175
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.. */
183 bool 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.. */
194 signed 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 /* */
209 bool debSystem::AddStatusFiles(vector<pkgIndexFile *> &List)
210 {
211 if (StatusFile == 0)
212 StatusFile = new debStatusIndex(_config->FindFile("Dir::State::status"));
213 List.push_back(StatusFile);
214 return true;
215 }
216 /*}}}*/
217 // System::FindIndex - Get an index file for status files /*{{{*/
218 // ---------------------------------------------------------------------
219 /* */
220 bool 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 /*}}}*/