]>
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 /*{{{*/
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>
22 #include <sys/types.h>
34 class debSystemPrivate
{
36 debSystemPrivate() : LockFD(-1), LockCount(0), StatusFile(0)
39 // For locking support
43 debStatusIndex
*StatusFile
;
46 // System::debSystem - Constructor /*{{{*/
47 // ---------------------------------------------------------------------
49 debSystem::debSystem()
51 d
= new debSystemPrivate();
52 Label
= "Debian dpkg interface";
56 // System::~debSystem - Destructor /*{{{*/
57 // ---------------------------------------------------------------------
59 debSystem::~debSystem()
65 // System::Lock - Get the lock /*{{{*/
66 // ---------------------------------------------------------------------
67 /* This mirrors the operations dpkg does when it starts up. Note the
68 checking of the updates directory. */
69 bool debSystem::Lock()
71 // Disable file locking
72 if (_config
->FindB("Debug::NoLocking",false) == true || d
->LockCount
> 1)
78 // Create the lockfile
79 string AdminDir
= flNotFile(_config
->Find("Dir::State::status"));
80 d
->LockFD
= GetLock(AdminDir
+ "lock");
83 if (errno
== EACCES
|| errno
== EAGAIN
)
84 return _error
->Error(_("Unable to lock the administration directory (%s), "
85 "is another process using it?"),AdminDir
.c_str());
87 return _error
->Error(_("Unable to lock the administration directory (%s), "
88 "are you root?"),AdminDir
.c_str());
91 // See if we need to abort with a dirty journal
92 if (CheckUpdates() == true)
97 if (getenv("SUDO_USER") != NULL
)
98 cmd
= "sudo dpkg --configure -a";
100 cmd
= "dpkg --configure -a";
101 // TRANSLATORS: the %s contains the recovery command, usually
102 // dpkg --configure -a
103 return _error
->Error(_("dpkg was interrupted, you must manually "
104 "run '%s' to correct the problem. "), cmd
);
112 // System::UnLock - Drop a lock /*{{{*/
113 // ---------------------------------------------------------------------
115 bool debSystem::UnLock(bool NoErrors
)
117 if (d
->LockCount
== 0 && NoErrors
== true)
120 if (d
->LockCount
< 1)
121 return _error
->Error(_("Not locked"));
122 if (--d
->LockCount
== 0)
131 // System::CheckUpdates - Check if the updates dir is dirty /*{{{*/
132 // ---------------------------------------------------------------------
133 /* This does a check of the updates directory (dpkg journal) to see if it has
134 any entries in it. */
135 bool debSystem::CheckUpdates()
137 // Check for updates.. (dirty)
138 string File
= flNotFile(_config
->Find("Dir::State::status")) + "updates/";
139 DIR *DirP
= opendir(File
.c_str());
143 /* We ignore any files that are not all digits, this skips .,.. and
144 some tmp files dpkg will leave behind.. */
145 bool Damaged
= false;
146 for (struct dirent
*Ent
= readdir(DirP
); Ent
!= 0; Ent
= readdir(DirP
))
149 for (unsigned int I
= 0; Ent
->d_name
[I
] != 0; I
++)
151 // Check if its not a digit..
152 if (isdigit(Ent
->d_name
[I
]) == 0)
166 // System::CreatePM - Create the underlying package manager /*{{{*/
167 // ---------------------------------------------------------------------
169 pkgPackageManager
*debSystem::CreatePM(pkgDepCache
*Cache
) const
171 return new pkgDPkgPM(Cache
);
174 // System::Initialize - Setup the configuration space.. /*{{{*/
175 // ---------------------------------------------------------------------
176 /* These are the Debian specific configuration variables.. */
177 bool debSystem::Initialize(Configuration
&Cnf
)
179 /* These really should be jammed into a generic 'Local Database' engine
180 which is yet to be determined. The functions in pkgcachegen should
181 be the only users of these */
182 Cnf
.CndSet("Dir::State::extended_states", "extended_states");
183 Cnf
.CndSet("Dir::State::status","/var/lib/dpkg/status");
184 Cnf
.CndSet("Dir::Bin::dpkg","/usr/bin/dpkg");
187 delete d
->StatusFile
;
194 // System::ArchiveSupported - Is a file format supported /*{{{*/
195 // ---------------------------------------------------------------------
196 /* The standard name for a deb is 'deb'.. There are no separate versions
197 of .deb to worry about.. */
198 bool debSystem::ArchiveSupported(const char *Type
)
200 if (strcmp(Type
,"deb") == 0)
205 // System::Score - Determine how 'Debiany' this sys is.. /*{{{*/
206 // ---------------------------------------------------------------------
207 /* We check some files that are sure tell signs of this being a Debian
209 signed debSystem::Score(Configuration
const &Cnf
)
212 if (FileExists(Cnf
.FindFile("Dir::State::status","/var/lib/dpkg/status")) == true)
214 if (FileExists(Cnf
.FindFile("Dir::Bin::dpkg","/usr/bin/dpkg")) == true)
216 if (FileExists("/etc/debian_version") == true)
221 // System::AddStatusFiles - Register the status files /*{{{*/
222 // ---------------------------------------------------------------------
224 bool debSystem::AddStatusFiles(std::vector
<pkgIndexFile
*> &List
)
226 if (d
->StatusFile
== 0)
227 d
->StatusFile
= new debStatusIndex(_config
->FindFile("Dir::State::status"));
228 List
.push_back(d
->StatusFile
);
232 // System::FindIndex - Get an index file for status files /*{{{*/
233 // ---------------------------------------------------------------------
235 bool debSystem::FindIndex(pkgCache::PkgFileIterator File
,
236 pkgIndexFile
*&Found
) const
238 if (d
->StatusFile
== 0)
240 if (d
->StatusFile
->FindInCache(*File
.Cache()) == File
)
242 Found
= d
->StatusFile
;