]>
git.saurik.com Git - apt.git/blob - apt-pkg/deb/debsystem.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: debsystem.cc,v 1.4 2004/01/26 17:01:53 mdz Exp $
4 /* ######################################################################
6 System - Abstraction for running on different systems.
8 Basic general structure..
10 ##################################################################### */
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>
21 #include <sys/types.h>
29 class debSystemPrivate
{
31 debSystemPrivate() : LockFD(-1), LockCount(0), StatusFile(0)
34 // For locking support
38 debStatusIndex
*StatusFile
;
41 // System::debSystem - Constructor /*{{{*/
42 // ---------------------------------------------------------------------
44 debSystem::debSystem()
46 d
= new debSystemPrivate();
47 Label
= "Debian dpkg interface";
51 // System::~debSystem - Destructor /*{{{*/
52 // ---------------------------------------------------------------------
54 debSystem::~debSystem()
60 // System::Lock - Get the lock /*{{{*/
61 // ---------------------------------------------------------------------
62 /* This mirrors the operations dpkg does when it starts up. Note the
63 checking of the updates directory. */
64 bool debSystem::Lock()
66 // Disable file locking
67 if (_config
->FindB("Debug::NoLocking",false) == true || d
->LockCount
> 1)
73 // Create the lockfile
74 string AdminDir
= flNotFile(_config
->Find("Dir::State::status"));
75 d
->LockFD
= GetLock(AdminDir
+ "lock");
78 if (errno
== EACCES
|| errno
== EAGAIN
)
79 return _error
->Error(_("Unable to lock the administration directory (%s), "
80 "is another process using it?"),AdminDir
.c_str());
82 return _error
->Error(_("Unable to lock the administration directory (%s), "
83 "are you root?"),AdminDir
.c_str());
86 // See if we need to abort with a dirty journal
87 if (CheckUpdates() == true)
92 if (getenv("SUDO_USER") != NULL
)
93 cmd
= "sudo dpkg --configure -a";
95 cmd
= "dpkg --configure -a";
96 // TRANSLATORS: the %s contains the recovery command, usually
97 // dpkg --configure -a
98 return _error
->Error(_("dpkg was interrupted, you must manually "
99 "run '%s' to correct the problem. "), cmd
);
107 // System::UnLock - Drop a lock /*{{{*/
108 // ---------------------------------------------------------------------
110 bool debSystem::UnLock(bool NoErrors
)
112 if (d
->LockCount
== 0 && NoErrors
== true)
115 if (d
->LockCount
< 1)
116 return _error
->Error(_("Not locked"));
117 if (--d
->LockCount
== 0)
126 // System::CheckUpdates - Check if the updates dir is dirty /*{{{*/
127 // ---------------------------------------------------------------------
128 /* This does a check of the updates directory (dpkg journal) to see if it has
129 any entries in it. */
130 bool debSystem::CheckUpdates()
132 // Check for updates.. (dirty)
133 string File
= flNotFile(_config
->Find("Dir::State::status")) + "updates/";
134 DIR *DirP
= opendir(File
.c_str());
138 /* We ignore any files that are not all digits, this skips .,.. and
139 some tmp files dpkg will leave behind.. */
140 bool Damaged
= false;
141 for (struct dirent
*Ent
= readdir(DirP
); Ent
!= 0; Ent
= readdir(DirP
))
144 for (unsigned int I
= 0; Ent
->d_name
[I
] != 0; I
++)
146 // Check if its not a digit..
147 if (isdigit(Ent
->d_name
[I
]) == 0)
161 // System::CreatePM - Create the underlying package manager /*{{{*/
162 // ---------------------------------------------------------------------
164 pkgPackageManager
*debSystem::CreatePM(pkgDepCache
*Cache
) const
166 return new pkgDPkgPM(Cache
);
169 // System::Initialize - Setup the configuration space.. /*{{{*/
170 // ---------------------------------------------------------------------
171 /* These are the Debian specific configuration variables.. */
172 bool debSystem::Initialize(Configuration
&Cnf
)
174 /* These really should be jammed into a generic 'Local Database' engine
175 which is yet to be determined. The functions in pkgcachegen should
176 be the only users of these */
177 Cnf
.CndSet("Dir::State::extended_states", "extended_states");
178 Cnf
.CndSet("Dir::State::status","/var/lib/dpkg/status");
179 Cnf
.CndSet("Dir::Bin::dpkg","/usr/bin/dpkg");
182 delete d
->StatusFile
;
189 // System::ArchiveSupported - Is a file format supported /*{{{*/
190 // ---------------------------------------------------------------------
191 /* The standard name for a deb is 'deb'.. There are no seperate versions
192 of .deb to worry about.. */
193 bool debSystem::ArchiveSupported(const char *Type
)
195 if (strcmp(Type
,"deb") == 0)
200 // System::Score - Determine how 'Debiany' this sys is.. /*{{{*/
201 // ---------------------------------------------------------------------
202 /* We check some files that are sure tell signs of this being a Debian
204 signed debSystem::Score(Configuration
const &Cnf
)
207 if (FileExists(Cnf
.FindFile("Dir::State::status","/var/lib/dpkg/status")) == true)
209 if (FileExists(Cnf
.FindFile("Dir::Bin::dpkg","/usr/bin/dpkg")) == true)
211 if (FileExists("/etc/debian_version") == true)
216 // System::AddStatusFiles - Register the status files /*{{{*/
217 // ---------------------------------------------------------------------
219 bool debSystem::AddStatusFiles(vector
<pkgIndexFile
*> &List
)
221 if (d
->StatusFile
== 0)
222 d
->StatusFile
= new debStatusIndex(_config
->FindFile("Dir::State::status"));
223 List
.push_back(d
->StatusFile
);
227 // System::FindIndex - Get an index file for status files /*{{{*/
228 // ---------------------------------------------------------------------
230 bool debSystem::FindIndex(pkgCache::PkgFileIterator File
,
231 pkgIndexFile
*&Found
) const
233 if (d
->StatusFile
== 0)
235 if (d
->StatusFile
->FindInCache(*File
.Cache()) == File
)
237 Found
= d
->StatusFile
;